posts/hello-world: correct myself a bit re: routing

Signed-off-by: Naman Sood <mail@nsood.in>
This commit is contained in:
Naman Sood 2021-03-19 01:23:40 -04:00
parent d931421929
commit b59eea8657

View file

@ -68,7 +68,7 @@ router.get('/:slug', function(req, res) {
The second route is the problem -- it seems that Go's built-in router doesn't support parameters in the URL by default. The internet seems to be torn on this issue -- half the people suggest that I use something like the [gorilla/mux](https://github.com/gorilla/mux) package, while the other half suggest [creating my own router](https://benhoyt.com/writings/go-routing/). I don't really like either solution. The first goes against the spirit of what I wanted to do when I started -- use the standard library tools to create the server, while the second seems a bit overkill -- I only need to serve a home page, a page for individual blogs, some static content, and maybe a custom error page. The second route is the problem -- it seems that Go's built-in router doesn't support parameters in the URL by default. The internet seems to be torn on this issue -- half the people suggest that I use something like the [gorilla/mux](https://github.com/gorilla/mux) package, while the other half suggest [creating my own router](https://benhoyt.com/writings/go-routing/). I don't really like either solution. The first goes against the spirit of what I wanted to do when I started -- use the standard library tools to create the server, while the second seems a bit overkill -- I only need to serve a home page, a page for individual blogs, some static content, and maybe a custom error page.
I ended up going with something similar to [Alex Wagner's approach](https://blog.merovius.de/2017/06/18/how-not-to-use-an-http-router.html). He calls it "not using an HTTP router", but I don't think that's quite correct. After all, there is a singular point of entry which then sends the request down different code paths depending on the URL and request type. That's... a router. That said, a router for a simple website can be as simple as the website itself. Here's what my router currently looks like (with some implementation details elided): I ended up going with something similar to [Alex Wagner's approach](https://blog.merovius.de/2017/06/18/how-not-to-use-an-http-router.html). He calls it "not using an HTTP router", but my solution is slightly different, to where I don't think that's quite correct for me. He first looks at the path prefix, then sends it to another handler which handles that paths of that prefix, including handling different HTTP verbs. I have a similar solution, but each of my downstream handlers are so small, that the thing I ended up with essentially a router anyway. That said, a router for a simple website can be as simple as the website itself. Here's what my router currently looks like (with some implementation details elided):
```go ```go
func (s *server) router(res http.ResponseWriter, req *http.Request) { func (s *server) router(res http.ResponseWriter, req *http.Request) {