Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
module github.com/BSick7/go-api

go 1.13
go 1.18

require (
github.com/cristalhq/jwt/v3 v3.0.4
github.com/google/uuid v1.3.0
github.com/gorilla/handlers v1.4.2
github.com/gorilla/mux v1.7.4
github.com/klauspost/compress v1.13.4 // indirect
github.com/stretchr/testify v1.7.0
github.com/svanharmelen/jsonapi v0.0.0-20180618144545-0c0828c3f16d
github.com/xi2/httpgzip v0.0.0-20190509075255-932ab5e254ae
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/klauspost/compress v1.13.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
21 changes: 21 additions & 0 deletions json/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package json

import (
"net/http"
"time"
)

type ControllerFunc[T any] func(req *Request) (T, error)

func Controller[T any](controller ControllerFunc[T]) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
res := &ResponseWriter{ResponseWriter: w, start: time.Now()}
req := &Request{Request: r}
if out, err := controller(req); err != nil {
res.SendError(err)
} else {
res.Send(out)
}
})
}