stevenanthonyrevo / RocketShipCountDown

Happy Hacktoberfest Contributors! This year a new repository will be open-sourced named "RocketCountDown". A space-inspired countdown tracker built in Go. For new Coders, and fellow Developers to please join this exciting project for a chance to submit PR's.
MIT License
3 stars 7 forks source link

Add to Go/HTTP server a new "/static" directory #1

Closed stevenanthonyrevo closed 3 years ago

stevenanthonyrevo commented 3 years ago

It would be great to see a "/static" directory serving image assets such as logos or photography, even external JS, or CSS.

Feel free to create an implementation that has basic error handling to handle any 404's. This "./static" directory should only serve assets and not give permission access to the root directory of the application.

umairkarel commented 3 years ago

I can't find "Go/HTTP server" folder

stevenanthonyrevo commented 3 years ago

@umairkarel, Thanks for showing interest in this issue. No server folder has been created yet. I'll leave the naming convention up for consideration.

Overall looking for an implementation utilizing Gin or the static function.

A simpler example is calling an "assets" folder within the main package.

router.Static("/assets", "./static")

Let me know if this helps or if you have any further questions, and Happy Hacktoberfest!

0x471 commented 3 years ago

@stevenanthonyrevo, it could be also something like this:

fileServer := http.FileServer(http.Dir("./static/"))
router.Handle("/assets/", http.StripPrefix("/static", fileServer))

Happy hacktoberfest (:

stevenanthonyrevo commented 3 years ago

Thanks for the code snippet, this is close, @0x471, and Happy Hacktoberfest!

I've tried implementing and received a type error in go.

http.StripPrefix("/static", fileServer) cannot use a string value in argument to router.Handle. (value of type http.Handler)

Please add a PR and I'll merge.