Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support setting routes for serving static files #64

Merged
merged 4 commits into from
May 12, 2024
Merged

Support setting routes for serving static files #64

merged 4 commits into from
May 12, 2024

Conversation

yanecc
Copy link
Contributor

@yanecc yanecc commented May 2, 2024

This is a work in progress, aiming to address #60
There is still much work to do ...

@yanecc
Copy link
Contributor Author

yanecc commented May 5, 2024

It can work as expected now but with unhandled errors.

ERROR - http.server: Unhandled exception on HTTP::Handler
Closed stream (IO::Error)
  from A:\Scoop\apps\crystal\current\src\http\server\response.cr:204 in 'check_headers'
  from A:\Scoop\apps\crystal\current\src\http\server\response.cr:40 in 'status='
  from A:\Scoop\apps\crystal\current\src\http\server\response.cr:89 in 'status_code='
  from C:\Users\Sunrise\Documents\CodeRepo\Crystal\test\static-server\lib\grip\src\grip\handlers\exception.cr:16 in 'call'
  from A:\Scoop\apps\crystal\current\src\http\server\request_processor.cr:51 in 'process'
  from A:\Scoop\apps\crystal\current\src\http\server.cr:521 in 'handle_client'
  from A:\Scoop\apps\crystal\current\src\http\server.cr:451 in '->'
  from A:\Scoop\apps\crystal\current\src\fiber.cr:141 in 'run'
  from A:\Scoop\apps\crystal\current\src\fiber.cr:93 in '->'

@grkek
Copy link
Member

grkek commented May 7, 2024

Closed stream (IO::Error)

Can you investigate why does this happen?

@yanecc
Copy link
Contributor Author

yanecc commented May 7, 2024

Relevant issue: crystal-lang/crystal#14566

@grkek
Copy link
Member

grkek commented May 11, 2024

Relevant issue: crystal-lang/crystal#14566

Should be fixed by #66

@yanecc
Copy link
Contributor Author

yanecc commented May 11, 2024

The code works well now and is reviewable.

@yanecc yanecc marked this pull request as ready for review May 11, 2024 15:19
@grkek
Copy link
Member

grkek commented May 12, 2024

@yanecc Can you provide an example?

@grkek grkek merged commit 51f1ad8 into grip-framework:core May 12, 2024
1 check passed
@grkek
Copy link
Member

grkek commented May 12, 2024

The code works well now and is reviewable.

I am going to modify this code a little bit to be a bit more Grip-esque, I will update the documentation with the news.

@yanecc
Copy link
Contributor Author

yanecc commented May 12, 2024

@yanecc Can you provide an example?

Sure, here is a simple illustration.

class Application < Grip::Application
  def initialize()
    super(environment: "development", serve_static: true)
  end

  getter host : String = "127.0.0.1"
  # getter pubilc_dir : String = "./public"
  getter static_routes : Hash(String, String) = {
    # "/"        => "./public",
    "/foo/bar" => "./dir1",
    "/baz"     => "./path/to/dir2",
  }
end

# By default, when serve_static is set to true, public_dir will be set to "./public".
# You could use pubilc_dir to set the static directory.

# If static_routes is set, pubilc_dir will be overwritten.
# GET http://127.0.0.1          #=> 404 Not Found
# GET http://127.0.0.1/foo/bar  #=> 200 OK -> ./dir1/index.html
# GET http://127.0.0.1/baz      #=> 200 OK -> ./path/to/dir2/index.html

@grkek
Copy link
Member

grkek commented May 12, 2024

@yanecc Can you provide an example?

Sure, here is a simple illustration.

class Application < Grip::Application
  def initialize()
    super(environment: "development", serve_static: true)
  end

  getter host : String = "127.0.0.1"
  # getter pubilc_dir : String = "./public"
  getter static_routes : Hash(String, String) = {
    # "/"        => "./public",
    "/foo/bar" => "./dir1",
    "/baz"     => "./path/to/dir2",
  }
end

# By default, when serve_static is set to true, public_dir will be set to "./public".
# You could use pubilc_dir to set the static directory.

# If static_routes is set, pubilc_dir will be overwritten.
# GET http://127.0.0.1          #=> 404 Not Found
# GET http://127.0.0.1/foo/bar  #=> 200 OK -> ./dir1/index.html
# GET http://127.0.0.1/baz      #=> 200 OK -> ./path/to/dir2/index.html

I updated the version of Grip to 3.0.0 since I implemented breaking changes and will update the documentation to reflect your changes as well, added a new DSL macro.

@grkek
Copy link
Member

grkek commented May 12, 2024

@yanecc Added the documentation, check it out.

@yanecc
Copy link
Contributor Author

yanecc commented May 12, 2024

@yanecc Added the documentation, check it out.

Looks good. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants