From b59eea8657c07f3e4aaa58429f498e3813358ed5 Mon Sep 17 00:00:00 2001 From: Naman Sood Date: Fri, 19 Mar 2021 01:23:40 -0400 Subject: [PATCH] posts/hello-world: correct myself a bit re: routing Signed-off-by: Naman Sood --- posts/hello-world.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/posts/hello-world.md b/posts/hello-world.md index 6ad1329..26ed967 100644 --- a/posts/hello-world.md +++ b/posts/hello-world.md @@ -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. -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 func (s *server) router(res http.ResponseWriter, req *http.Request) {