Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Jonathan
@greenbigfrog
@bajro17 what exactly is your issue?
Azzuwan Aziz
@azzuwan
Hi guys, just wondering is there anything like attribute or annotation in crystal that we can use in kemal routes. I am trying to mimic ASP.NET Core Role based authorization
Azzuwan Aziz
@azzuwan
Alternatively, how do I make role based authorization work with JWT middleware. I know I can create a middleware that checks for routes against the role in the JWT payload but what is the recommended way for Kemal?
Julien Reichardt
@j8r
@dscottboggs_gitlab has knowledge about this, he may help you
Jonathan Silverman
@mixflame
Hi
How do I send from a websocket inside a controller action?
Jonathan Silverman
@mixflame
Kemal is the only leader I'll ever need
Daniel Worrall
@Daniel-Worrall
How can I await Kemal being ready
Yoghaswara HN
@nagiwitch_gitlab
Hi, i wanna ask. Can Kemal use GraphQL? Thank you
Azzuwan Aziz
@azzuwan
@nagiwitch_gitlab Short answer no. Long answer yes but you need to use a graphql library and pass the graphql query to that library.
shortly-portly
@shortly-portly
Hi All. I'm new to Crystal and to Kemal but really enjoying the experience so far. I'm trying to learn Crystal by looking at the source code of Kemal and I came across this line in config.cr
 {{ `shards version #{__DIR__}`.chomp.stringify }}
I know this gets the version number of your shard.yml file but not sure why it needs the {{ macro braces. I've tried
`shards version #{__DIR__}`.chomp
and this works just as well. Am I missing something as to why we need the macro {{ }} braces?
thanks
D. Scott Boggs
@dscottboggs_gitlab
Not sure about the specific instance you're talking about but the difference between the macro \`` method and the regular`` method is that the macro one is executed at compile time and evaluated to an AST Node (in this case a string literal) which remains constant after compile time. The one that's not in macro-land will run at runtime. For an example explaining what I mean, run the following commands and observe the results:
echo test text > test-file
echo 'puts `cat test-file`
puts {{`cat test-file`.stringify}}' > test-backtick.cr
crystal build -o test-backtick test-backtick.cr
./test-backtick
echo some different text > test-file
./test-backtick
shortly-portly
@shortly-portly
thanks @dscottboggs_gitlab. It is amazing what a nights sleep will do as I now understand whats going on. Basically we are getting the version number in shard.yml. As you say the {{}} version gets it at compile time vs my version that gets it at run time. If we get the information at compile time then we guarentee the build and version number are in sync. If we get it at run time then we don't guarentee that the shard.yml file hasn't been updated and then no longer matches the build that we are running. For me this is another example of how elegant Crystal is.
D. Scott Boggs
@dscottboggs_gitlab
indeed macros are an incredibly powerful tool
Julien Reichardt
@j8r
In the macro land, read_file can also be used
Edward J. Stembler
@ejstembler_gitlab

Hi there, I'm new to Crystal though I'm a long-time Ruby enthusiast. I'm trying to re-write my blog using Crystal + Kemal and I ran into a possible routing issues. I'm trying to render (or redirect) to a local html file which is not in public. Here's the code:

get "/posts/:year-:month-:day/:slug" do |env|
   year = env.params.url["year"]
   month = env.params.url["month"]
   day = env.params.url["day"]
   slug = env.params.url["slug"]

   path = "./posts/#{year}-#{month}-#{day}/#{slug}.html"

   env.redirect path
end

An example url would be something like: /posts/2017-09-21/some-slug. Though I'm getting a 500 error: Missing hash key: "year". I'm wondering if this is because the dash is messing up the route parser?

Edward J. Stembler
@ejstembler_gitlab
@ejstembler_gitlab Never mind. I got it working by changing the route to only use one keyword for the date. I didn't really need the date broken out anyway. Also, I switched to send_file instead of env.redirect:
get "/posts/:date/:slug" do |env|
  date = env.params.url["date"]
  slug = env.params.url["slug"]

  path = "./posts/#{date}/#{slug}.html"

  send_file env, path
end
ochran
@ochran
@sdogruyol is it possible to remove a key from a kemal session once it has been added?
something like env.session.object.delete("save") for example
Manuel Jacob
@manueljacob
I’m writing a simple webserver that returns some data from a text file database. For tests, a different database should be used than for running the server for production. How can I configure this in a nice way? Ideally the test database should not be mentioned in the application code.
Daniel Worrall
@Daniel-Worrall
Environment variable for database url
Kingsley Hendrickse
@kingsleyh
hello I have an svg file in the public folder - but when I render the html page using the file location in the img tag - it doesn't render the svg - any ideas what I need to do ?
works fine if I use a png though
Kingsley Hendrickse
@kingsleyh
nevermind - I misspelled the name ooops
working now
Benjamin
@kimvex

Hi, I have a question. How do I get array parameters in a get request
for example:
id[]=1&id[]=2

the way I wanted to get the array
env.params.query["id[]"].as(Array)

I tried to get it this way but I take it as a unique string value and not as the array I expected to get. I was guided by what the guide says: "Be sure to access the param name exactly how it was passed. (i.e. env.params.body["likes[]"])" but I don't know why I was wrong because it didn't work

Serdar Dogruyol - Sedo セド
@sdogruyol
@kimvex how may I help? Have you found the solution?
(been a while since I logged in)
Benjamin
@kimvex
@sdogruyol I still haven't managed to solve the problem. I still can't get the array parameters in a get method, even though I use what the kemal guide recommends. I don't know what I'm doing wrong.
What would be the correct way to get the array parameter of the get method?
ochran
@ochran
Hello, how do I create optional url params in Kemal? In Sinatra you would do something like get "/resource/?:optional_param?"
Jonathan
@greenbigfrog
Not aware of kemal having this option. Simply create both routes
ochran
@ochran
Do I have to use two blocks, or can you link one block to two routes?
Jonathan
@greenbigfrog

Kemal only allows one route per block AFAIK
I'm not really that much of an expert ;)

As a temporary solution, why not have 2 blocks, the one for the route without the param simply redirects to the other route with a default param

May I ask what you are trying to implement?
ochran
@ochran
a route like /blog/:category/entry/:id that if you didn't supply the :id would load a blank form, else if you supply the :id would load the same form but with the inputs populated with the current entry's data
2 routes will be better though I reckon, the one without the ID setting defaults. What you reckon?
get "/blog/:category/entry/" do |env|
  category = env.params.url["category"]
  id = nil
  title = body = ""
  # render
end

get "/blog/:category/entry/:id" do |env|
  category = env.params.url["category"]
  id = env.params.url["id"]
  title, body = # get from DB
  # render same page
end
Jonathan
@greenbigfrog
Yeah.I'd just go with 2 routes
Jordan Lumley
@jordanl010
Hey all, extremely new to crystal, looking at it to potentialyl use to create a rest api. So far, I love the language, just reading through the docs on the kemal shard and wanted to ask a question to anyone using Kemal. What exactly is the difference between a filter and a middleware in the context of kemal? And what are the use cases between the two. I wanted to create a token authentication based API but im confused on if I should be validating the token on the filter for "before_all" or some other filter or create a middleware for the call? Or would they both achieve the same end goal?
ochran
@ochran

Hello, if I create a custom error handler

error 404 do
  render "src/404.ecr"
end

when I halt(env, 404) the error handler isn't triggered like it is in Sinatra

Is this an issue or is it just how kemal is designed?
Daniel Worrall
@Daniel-Worrall
I remember there being a documented issue on how halt is handled if you can find that one
Jordan Lumley
@jordanl010
anybody?
phangs
@phangs
is there a kemal tutorial for newbie programmer?