Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove go-chi and upgrade dependencies #11

Merged
merged 1 commit into from
Apr 6, 2024
Merged
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: up down pull update clean migrate test generate-sqlboiler generate-mocks psql run

sqlboiler-version = v4.15.0
sqlboiler-version = v4.16.2

up:
docker compose up --detach postgres
Expand Down
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.4'

services:
postgres:
image: postgres:15-alpine
Expand Down
27 changes: 13 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
module github.com/abeltay/go-template

go 1.21
go 1.22

require (
github.com/abeltay/go-kit v0.0.0-20230604060156-1a2dda4701f5
github.com/caarlos0/env/v9 v9.0.0
github.com/friendsofgo/errors v0.9.2
github.com/go-chi/chi/v5 v5.0.10
github.com/google/uuid v1.5.0
github.com/jackc/pgx/v4 v4.18.1
github.com/rubenv/sql-migrate v1.5.2
github.com/stretchr/testify v1.8.4
github.com/volatiletech/sqlboiler/v4 v4.15.0
github.com/volatiletech/strmangle v0.0.5
go.uber.org/zap v1.26.0
github.com/google/uuid v1.6.0
github.com/jackc/pgx/v4 v4.18.3
github.com/rubenv/sql-migrate v1.6.1
github.com/stretchr/testify v1.9.0
github.com/volatiletech/sqlboiler/v4 v4.16.2
github.com/volatiletech/strmangle v0.0.6
go.uber.org/zap v1.27.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-gorp/gorp/v3 v3.1.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.14.1 // indirect
github.com/jackc/pgconn v1.14.3 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.2 // indirect
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/jackc/pgtype v1.14.3 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/stretchr/objx v0.5.1 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/volatiletech/inflect v0.0.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
94 changes: 42 additions & 52 deletions go.sum

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions rest/logger/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ type ZapLogMiddleware struct {
Logger *zap.Logger
}

func (f ZapLogMiddleware) Chain(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
func (f ZapLogMiddleware) Chain(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
uuid, err := uuid.NewUUID()
requestID := uuid.String()
if err != nil {
Expand All @@ -67,6 +67,6 @@ func (f ZapLogMiddleware) Chain(next http.Handler) http.Handler {
zap.Float64("response_time_ms", time.Since(start).Seconds()*1000),
)
}()
next.ServeHTTP(w, r)
})
next(w, r)
}
}
23 changes: 7 additions & 16 deletions rest/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/abeltay/go-template/postgres"
"github.com/abeltay/go-template/rest/logger"
"github.com/go-chi/chi/v5"
"go.uber.org/zap"
)

Expand All @@ -18,27 +17,19 @@ type Router struct {

// Handler returns a http.Handler to be run by a http.Server
func (f Router) Handler() http.Handler {
r := chi.NewRouter()
mux := http.NewServeMux()
logger := logger.ZapLogMiddleware{
Logger: f.ZapLogger,
}
r.Use(logger.Chain)
r.Get("/livez", liveliness)
r.Get("/readyz", f.Ping)
mux.HandleFunc("GET /livez", logger.Chain(liveliness))
mux.HandleFunc("GET /readyz", logger.Chain(f.Ping))

r.Group(f.group)
return r
mux.HandleFunc("POST /user/", logger.Chain(f.AddUser))
mux.HandleFunc("GET /user/{id}", logger.Chain(f.UserFullName))

return mux
}

func liveliness(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "ok")
}

func (f Router) group(r chi.Router) {
r.Route("/user", f.users)
}

func (f Router) users(r chi.Router) {
r.Post("/", f.AddUser)
r.Get("/{id}", f.UserFullName)
}
13 changes: 10 additions & 3 deletions rest/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"

"github.com/abeltay/go-template/rest/logger"
"github.com/go-chi/chi/v5"
"go.uber.org/zap"
)

Expand All @@ -34,14 +34,21 @@ func (f Router) AddUser(w http.ResponseWriter, r *http.Request) {

func (f Router) UserFullName(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
idParam := chi.URLParam(r, "id")
id, err := strconv.Atoi(idParam)
params, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
log := logger.ZapLogger(ctx)
log.Info("param error", zap.Error(err))
http.Error(w, "error", http.StatusBadRequest)
return
}
idStr := params.Get("id")
id, err := strconv.Atoi(idStr)
if err != nil {
log := logger.ZapLogger(ctx)
log.Info("param error, cannot convert to int", zap.Error(err))
http.Error(w, "error", http.StatusBadRequest)
return
}
name, err := f.Service.UserFullName(ctx, id)
if err != nil {
log := logger.ZapLogger(ctx)
Expand Down
Loading