diff options
Diffstat (limited to 'content')
| -rw-r--r-- | content/posts/002.md | 7 | ||||
| -rw-r--r-- | content/posts/004.md | 14 |
2 files changed, 10 insertions, 11 deletions
diff --git a/content/posts/002.md b/content/posts/002.md index 8e22144..93ee423 100644 --- a/content/posts/002.md +++ b/content/posts/002.md @@ -56,7 +56,6 @@ I think the project should grow organically to some degree. What we want to do i Start with a `main.go` and make a `Run` function or some equivalent which it calls. ```go - func Run() error { // actual important stuff here } @@ -71,7 +70,7 @@ This allows you to test your run function in a unit test, and keeps your `main` As your project grows, you can keep it flat inside the root directory -``` +```shell ├── api.go ├── go.mod ├── go.sum @@ -82,7 +81,7 @@ As your project grows, you can keep it flat inside the root directory Even just glancing at that, you can guess that this might be an RSS server, that uses sqlite to back it. Who knows what -``` +```shell ├── drivers ├── entities ├── interfaces @@ -95,7 +94,7 @@ As things evolve you might want to put them in `internal` to hide them from bein I can't be bothered rewriting my example, so here's a random one I found online; it's probably all right. [Server Project](https://go.dev/doc/modules/layout#server-project) -``` +```shell project-root-directory/ go.mod internal/ diff --git a/content/posts/004.md b/content/posts/004.md index ebfaad2..e5d28b4 100644 --- a/content/posts/004.md +++ b/content/posts/004.md @@ -10,7 +10,7 @@ I write them like this for reasons that are probably fairly contextual. I've wri I've used `ints` instead of the `http.StatusXXXX` and omitted `JSON` tags in an attempt to try save up screen space. To begin with, you might have something like this: -``` +```go package main import ( @@ -33,7 +33,7 @@ func main() { Then you might get told off because you've just registered routes with the default mux, which isn't very testable. So you tweak it a little bit. -``` +```go package main import ( @@ -71,7 +71,7 @@ func main() { But now you need to do something real, you want to store and fetch data. -``` +```go package main import ( @@ -159,7 +159,7 @@ func main() { } ``` -``` +```shell ❯ curl -X POST localhost:8080 --header "Content-Type: application/json" -d '{"foo":"bar"}' {"ID":0,"Foo":"bar"} ❯ curl -X GET localhost:8080/0 @@ -173,7 +173,7 @@ This API is inconsistent. It sometimes returns `JSON`, and the others return str So let's try to standardise things. First, let's design some form of REST spec. -``` +```go type JSONResp[T any] struct { Resources []T Errs []ErrorResp @@ -187,7 +187,7 @@ type ErrorResp struct { We want to be able to support fetching multiple resources at once, if we can only fetch some resources, let's return them under `resources` and show the errors under `errs` Now, add some helpful functions to handle things. -``` +```go func Post[In any, Out any](successCode int, fn func(context.Context, In) ([]Out, []ErrorResp)) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { var v In @@ -229,7 +229,7 @@ And we've standardised all `POST` requests! This function can be used by all `POST` requests, ensuring they adhere to the spec. It also removes the repetitive code around marshalling and unmarshalling to `JSON` and handles errors in a consistent manner. The handler functions accept a `context` param and their expected struct input. -``` +```go func (s *Server) Register(mux *http.ServeMux) { ... mux.HandleFunc("POST /", Post(201, s.Post)) |
