Skip to content

Commit 8272730

Browse files
authored
Merge pull request #1 from chris-ramon/promotes-go1.22
Promotes go1.22 & new built-in routing.
2 parents e2bd4cb + 57046ab commit 8272730

File tree

14 files changed

+36
-60
lines changed

14 files changed

+36
-60
lines changed

.github/workflows/tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
runs-on: ubuntu-latest
1313
strategy:
1414
matrix:
15-
go: ["1.20"]
15+
go: ["1.22.3"]
1616
name: Go ${{ matrix.go }}
1717
steps:
1818
- uses: actions/checkout@v2

docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ version: "3.9"
33
services:
44
app:
55
working_dir: /app
6-
image: golang:1.20
6+
image: golang:1.22.3
77
command: sh -c "PORT=8080 DB_USER=admin DB_PWD=admin DB_HOST=db DB_NAME=local DB_SSL_MODE=disable ./bin/_dev.sh"
88
volumes:
99
- ./:/app

domain/admin/route.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package admin
33
import (
44
"net/http"
55

6-
"github.com/julienschmidt/httprouter"
7-
86
"github.com/chris-ramon/golang-scaffolding/pkg/ctxutil"
97
"github.com/chris-ramon/golang-scaffolding/pkg/route"
108
)
@@ -22,7 +20,7 @@ func (ro *routes) All() []route.Route {
2220
route.Route{
2321
HTTPMethod: "GET",
2422
Path: "/admin",
25-
Handler: func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
23+
Handler: func(w http.ResponseWriter, r *http.Request) {
2624
r = r.WithContext(ctxutil.WithAuthHeader(r.Context(), r.Header))
2725
ro.handlers.GetAdmin().ServeHTTP(w, r)
2826
},

domain/auth/handler.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"log"
88
"net/http"
99

10-
"github.com/julienschmidt/httprouter"
11-
1210
"github.com/chris-ramon/golang-scaffolding/domain/auth/types"
1311
"github.com/chris-ramon/golang-scaffolding/pkg/ctxutil"
1412
)
@@ -22,14 +20,14 @@ type handlers struct {
2220
service Service
2321
}
2422

25-
func (h *handlers) GetPing() httprouter.Handle {
26-
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
23+
func (h *handlers) GetPing() http.HandlerFunc {
24+
return func(w http.ResponseWriter, r *http.Request) {
2725
w.Write([]byte("ok"))
2826
}
2927
}
3028

31-
func (h *handlers) GetCurrentUser() httprouter.Handle {
32-
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
29+
func (h *handlers) GetCurrentUser() http.HandlerFunc {
30+
return func(w http.ResponseWriter, r *http.Request) {
3331
r = r.WithContext(ctxutil.WithAuthHeader(r.Context(), r.Header))
3432
jwtToken, err := ctxutil.AuthHeaderValueFromCtx(r.Context())
3533
if err != nil {
@@ -49,8 +47,8 @@ func (h *handlers) GetCurrentUser() httprouter.Handle {
4947
}
5048
}
5149

52-
func (h *handlers) PostSignIn() httprouter.Handle {
53-
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
50+
func (h *handlers) PostSignIn() http.HandlerFunc {
51+
return func(w http.ResponseWriter, r *http.Request) {
5452
b, err := ioutil.ReadAll(r.Body)
5553
if err != nil {
5654
log.Printf("failed to read request body: %v", err)

domain/auth/handler_test.go

+3-14
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"testing"
1212

1313
"github.com/chris-ramon/golang-scaffolding/domain/auth/types"
14-
"github.com/julienschmidt/httprouter"
1514
)
1615

1716
type testReaderError int
@@ -37,9 +36,8 @@ func TestGetPing(t *testing.T) {
3736
h := NewHandlers(&serviceMock{})
3837
req := httptest.NewRequest("GET", "/ping", nil)
3938
w := httptest.NewRecorder()
40-
params := httprouter.Params{}
4139

42-
h.GetPing()(w, req, params)
40+
h.GetPing()(w, req)
4341

4442
body, err := io.ReadAll(w.Result().Body)
4543
if err != nil {
@@ -56,7 +54,6 @@ func TestGetCurrentUser(t *testing.T) {
5654
srvMock *serviceMock
5755
request *http.Request
5856
responseWriter *httptest.ResponseRecorder
59-
params httprouter.Params
6057
header http.Header
6158
expectedBody string
6259
expectedStatusCode uint
@@ -74,7 +71,6 @@ func TestGetCurrentUser(t *testing.T) {
7471
},
7572
request: httptest.NewRequest("GET", "/auth/current-user", nil),
7673
responseWriter: httptest.NewRecorder(),
77-
params: httprouter.Params{},
7874
header: map[string][]string{
7975
"Authorization": []string{"Bearer Test-JWT-Token"},
8076
},
@@ -86,7 +82,6 @@ func TestGetCurrentUser(t *testing.T) {
8682
srvMock: &serviceMock{},
8783
request: httptest.NewRequest("GET", "/auth/current-user", nil),
8884
responseWriter: httptest.NewRecorder(),
89-
params: httprouter.Params{},
9085
header: map[string][]string{},
9186
expectedBody: "failed to get authorization header",
9287
expectedStatusCode: http.StatusInternalServerError,
@@ -100,7 +95,6 @@ func TestGetCurrentUser(t *testing.T) {
10095
},
10196
request: httptest.NewRequest("GET", "/auth/current-user", nil),
10297
responseWriter: httptest.NewRecorder(),
103-
params: httprouter.Params{},
10498
header: map[string][]string{
10599
"Authorization": []string{"Bearer Test-JWT-Token"},
106100
},
@@ -119,7 +113,7 @@ func TestGetCurrentUser(t *testing.T) {
119113
}
120114
}
121115

122-
h.GetCurrentUser()(testCase.responseWriter, testCase.request, testCase.params)
116+
h.GetCurrentUser()(testCase.responseWriter, testCase.request)
123117

124118
if !strings.Contains(testCase.responseWriter.Body.String(), testCase.expectedBody) {
125119
t.Fatalf("expected: %v, got: %v", testCase.expectedBody, testCase.responseWriter.Body.String())
@@ -134,7 +128,6 @@ func TestPostSignIn(t *testing.T) {
134128
srvMock *serviceMock
135129
request *http.Request
136130
responseWriter *httptest.ResponseRecorder
137-
params httprouter.Params
138131
expectedBody string
139132
}
140133

@@ -154,7 +147,6 @@ func TestPostSignIn(t *testing.T) {
154147
bytes.NewBuffer([]byte(`{"email":"[email protected]","password":"test-pwd"}`)),
155148
),
156149
responseWriter: httptest.NewRecorder(),
157-
params: httprouter.Params{},
158150
expectedBody: "test user",
159151
},
160152
{
@@ -172,7 +164,6 @@ func TestPostSignIn(t *testing.T) {
172164
testReaderError(0),
173165
),
174166
responseWriter: httptest.NewRecorder(),
175-
params: httprouter.Params{},
176167
expectedBody: "failed to read request body",
177168
},
178169
{
@@ -190,7 +181,6 @@ func TestPostSignIn(t *testing.T) {
190181
bytes.NewBuffer([]byte(`{invalid}`)),
191182
),
192183
responseWriter: httptest.NewRecorder(),
193-
params: httprouter.Params{},
194184
expectedBody: "failed to json unmarshal request body",
195185
},
196186
{
@@ -206,15 +196,14 @@ func TestPostSignIn(t *testing.T) {
206196
bytes.NewBuffer([]byte(`{"email":"[email protected]","password":"test-pwd"}`)),
207197
),
208198
responseWriter: httptest.NewRecorder(),
209-
params: httprouter.Params{},
210199
expectedBody: "failed to find current user",
211200
},
212201
}
213202

214203
for _, testCase := range testCases {
215204
t.Run(testCase.name, func(t *testing.T) {
216205
h := NewHandlers(testCase.srvMock)
217-
h.PostSignIn()(testCase.responseWriter, testCase.request, testCase.params)
206+
h.PostSignIn()(testCase.responseWriter, testCase.request)
218207
if !strings.Contains(testCase.responseWriter.Body.String(), testCase.expectedBody) {
219208
t.Fatalf("expected: %v, got: %v", testCase.expectedBody, testCase.responseWriter.Body.String())
220209
}

domain/auth/route.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package auth
22

33
import (
4-
"github.com/julienschmidt/httprouter"
4+
"net/http"
55

66
"github.com/chris-ramon/golang-scaffolding/pkg/route"
77
)
88

99
type Handlers interface {
10-
GetPing() httprouter.Handle
11-
GetCurrentUser() httprouter.Handle
12-
PostSignIn() httprouter.Handle
10+
GetPing() http.HandlerFunc
11+
GetCurrentUser() http.HandlerFunc
12+
PostSignIn() http.HandlerFunc
1313
}
1414

1515
type routes struct {

domain/auth/route_test.go

+9-11
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,26 @@ import (
44
"net/http"
55
"testing"
66

7-
"github.com/julienschmidt/httprouter"
8-
97
"github.com/chris-ramon/golang-scaffolding/pkg/route"
108
)
119

1210
type handlersMock struct {
13-
getPing httprouter.Handle
14-
getCurrentUser httprouter.Handle
15-
postSignIn httprouter.Handle
11+
getPing http.HandlerFunc
12+
getCurrentUser http.HandlerFunc
13+
postSignIn http.HandlerFunc
1614
}
1715

18-
func (h *handlersMock) GetPing() httprouter.Handle { return h.getPing }
19-
func (h *handlersMock) GetCurrentUser() httprouter.Handle { return h.getCurrentUser }
20-
func (h *handlersMock) PostSignIn() httprouter.Handle { return h.postSignIn }
16+
func (h *handlersMock) GetPing() http.HandlerFunc { return h.getPing }
17+
func (h *handlersMock) GetCurrentUser() http.HandlerFunc { return h.getCurrentUser }
18+
func (h *handlersMock) PostSignIn() http.HandlerFunc { return h.postSignIn }
2119

2220
func TestRoutesAll(t *testing.T) {
2321
h := &handlersMock{
24-
getPing: func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
22+
getPing: func(w http.ResponseWriter, r *http.Request) {
2523
},
26-
getCurrentUser: func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
24+
getCurrentUser: func(w http.ResponseWriter, r *http.Request) {
2725
},
28-
postSignIn: func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
26+
postSignIn: func(w http.ResponseWriter, r *http.Request) {
2927
},
3028
}
3129

domain/gql/route.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"net/http"
55

66
"github.com/graphql-go/handler"
7-
"github.com/julienschmidt/httprouter"
87

98
"github.com/chris-ramon/golang-scaffolding/pkg/ctxutil"
109
"github.com/chris-ramon/golang-scaffolding/pkg/route"
@@ -24,15 +23,15 @@ func (ro *routes) All() []route.Route {
2423
route.Route{
2524
HTTPMethod: "GET",
2625
Path: "/graphql",
27-
Handler: func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
26+
Handler: func(w http.ResponseWriter, r *http.Request) {
2827
r = r.WithContext(ctxutil.WithAuthHeader(r.Context(), r.Header))
2928
ro.handlers.GetGraphQL().ServeHTTP(w, r)
3029
},
3130
},
3231
route.Route{
3332
HTTPMethod: "POST",
3433
Path: "/graphql",
35-
Handler: func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
34+
Handler: func(w http.ResponseWriter, r *http.Request) {
3635
r = r.WithContext(ctxutil.WithAuthHeader(r.Context(), r.Header))
3736
ro.handlers.PostGraphQL().ServeHTTP(w, r)
3837
},

domain/users/handler.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"encoding/json"
66
"net/http"
77

8-
"github.com/julienschmidt/httprouter"
9-
108
"github.com/admin-golang/admin/dataloader"
119
"github.com/chris-ramon/golang-scaffolding/domain/users/mappers"
1210
userTypes "github.com/chris-ramon/golang-scaffolding/domain/users/types"
@@ -16,8 +14,8 @@ type handlers struct {
1614
srv Service
1715
}
1816

19-
func (h *handlers) GetUsers() httprouter.Handle {
20-
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
17+
func (h *handlers) GetUsers() http.HandlerFunc {
18+
return func(w http.ResponseWriter, r *http.Request) {
2119
users, err := h.srv.FindUsers(r.Context())
2220

2321
if err != nil {

domain/users/route.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package users
22

33
import (
4-
"github.com/julienschmidt/httprouter"
4+
"net/http"
55

66
"github.com/chris-ramon/golang-scaffolding/pkg/route"
77
)
88

99
type Handlers interface {
10-
GetUsers() httprouter.Handle
10+
GetUsers() http.HandlerFunc
1111
}
1212

1313
type routes struct {

go.mod

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/chris-ramon/golang-scaffolding
22

3-
go 1.20
3+
go 1.22.3
44

55
require (
66
github.com/graphql-go/graphql v0.8.1
@@ -11,7 +11,6 @@ require (
1111
github.com/admin-golang/admin v0.0.0-20220720064250-fa4d88cf3085
1212
github.com/golang-jwt/jwt/v5 v5.0.0-rc.2
1313
github.com/golang-migrate/migrate/v4 v4.15.2
14-
github.com/julienschmidt/httprouter v1.3.0
1514
github.com/lib/pq v1.10.8
1615
)
1716

go.sum

-1
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,6 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
724724
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
725725
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
726726
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
727-
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
728727
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
729728
github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
730729
github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=

main.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"log"
66
"net/http"
77

8-
"github.com/julienschmidt/httprouter"
9-
108
"github.com/chris-ramon/golang-scaffolding/config"
119
"github.com/chris-ramon/golang-scaffolding/db"
1210
"github.com/chris-ramon/golang-scaffolding/domain/admin"
@@ -36,7 +34,7 @@ func main() {
3634
log.Println("successfully run migrations")
3735
}
3836

39-
router := httprouter.New()
37+
router := http.NewServeMux()
4038

4139
usersRepo := users.NewRepo(db)
4240
usersService := users.NewService(usersRepo)
@@ -78,7 +76,7 @@ func main() {
7876
routes = append(routes, usersRoutes.All()...)
7977

8078
for _, r := range routes {
81-
router.Handle(r.HTTPMethod, r.Path, r.Handler)
79+
router.HandleFunc(fmt.Sprintf("%s %s", r.HTTPMethod, r.Path), r.Handler)
8280
}
8381

8482
log.Printf("server running on port :%s", conf.Port)

pkg/route/route.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package route
22

33
import (
4-
"github.com/julienschmidt/httprouter"
4+
"net/http"
55
)
66

77
type Route struct {
88
HTTPMethod string
99
Path string
10-
Handler httprouter.Handle
10+
Handler http.HandlerFunc
1111
}

0 commit comments

Comments
 (0)