Skip to content
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 .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go: ["1.20"]
go: ["1.22.3"]
name: Go ${{ matrix.go }}
steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: "3.9"
services:
app:
working_dir: /app
image: golang:1.20
image: golang:1.22.3
command: sh -c "PORT=8080 DB_USER=admin DB_PWD=admin DB_HOST=db DB_NAME=local DB_SSL_MODE=disable ./bin/_dev.sh"
volumes:
- ./:/app
Expand Down
4 changes: 1 addition & 3 deletions domain/admin/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package admin
import (
"net/http"

"github.com/julienschmidt/httprouter"

"github.com/chris-ramon/golang-scaffolding/pkg/ctxutil"
"github.com/chris-ramon/golang-scaffolding/pkg/route"
)
Expand All @@ -22,7 +20,7 @@ func (ro *routes) All() []route.Route {
route.Route{
HTTPMethod: "GET",
Path: "/admin",
Handler: func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
Handler: func(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(ctxutil.WithAuthHeader(r.Context(), r.Header))
ro.handlers.GetAdmin().ServeHTTP(w, r)
},
Expand Down
14 changes: 6 additions & 8 deletions domain/auth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"log"
"net/http"

"github.com/julienschmidt/httprouter"

"github.com/chris-ramon/golang-scaffolding/domain/auth/types"
"github.com/chris-ramon/golang-scaffolding/pkg/ctxutil"
)
Expand All @@ -22,14 +20,14 @@ type handlers struct {
service Service
}

func (h *handlers) GetPing() httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
func (h *handlers) GetPing() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
}
}

func (h *handlers) GetCurrentUser() httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
func (h *handlers) GetCurrentUser() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(ctxutil.WithAuthHeader(r.Context(), r.Header))
jwtToken, err := ctxutil.AuthHeaderValueFromCtx(r.Context())
if err != nil {
Expand All @@ -49,8 +47,8 @@ func (h *handlers) GetCurrentUser() httprouter.Handle {
}
}

func (h *handlers) PostSignIn() httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
func (h *handlers) PostSignIn() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("failed to read request body: %v", err)
Expand Down
17 changes: 3 additions & 14 deletions domain/auth/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"testing"

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

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

h.GetPing()(w, req, params)
h.GetPing()(w, req)

body, err := io.ReadAll(w.Result().Body)
if err != nil {
Expand All @@ -56,7 +54,6 @@ func TestGetCurrentUser(t *testing.T) {
srvMock *serviceMock
request *http.Request
responseWriter *httptest.ResponseRecorder
params httprouter.Params
header http.Header
expectedBody string
expectedStatusCode uint
Expand All @@ -74,7 +71,6 @@ func TestGetCurrentUser(t *testing.T) {
},
request: httptest.NewRequest("GET", "/auth/current-user", nil),
responseWriter: httptest.NewRecorder(),
params: httprouter.Params{},
header: map[string][]string{
"Authorization": []string{"Bearer Test-JWT-Token"},
},
Expand All @@ -86,7 +82,6 @@ func TestGetCurrentUser(t *testing.T) {
srvMock: &serviceMock{},
request: httptest.NewRequest("GET", "/auth/current-user", nil),
responseWriter: httptest.NewRecorder(),
params: httprouter.Params{},
header: map[string][]string{},
expectedBody: "failed to get authorization header",
expectedStatusCode: http.StatusInternalServerError,
Expand All @@ -100,7 +95,6 @@ func TestGetCurrentUser(t *testing.T) {
},
request: httptest.NewRequest("GET", "/auth/current-user", nil),
responseWriter: httptest.NewRecorder(),
params: httprouter.Params{},
header: map[string][]string{
"Authorization": []string{"Bearer Test-JWT-Token"},
},
Expand All @@ -119,7 +113,7 @@ func TestGetCurrentUser(t *testing.T) {
}
}

h.GetCurrentUser()(testCase.responseWriter, testCase.request, testCase.params)
h.GetCurrentUser()(testCase.responseWriter, testCase.request)

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

Expand All @@ -154,7 +147,6 @@ func TestPostSignIn(t *testing.T) {
bytes.NewBuffer([]byte(`{"email":"[email protected]","password":"test-pwd"}`)),
),
responseWriter: httptest.NewRecorder(),
params: httprouter.Params{},
expectedBody: "test user",
},
{
Expand All @@ -172,7 +164,6 @@ func TestPostSignIn(t *testing.T) {
testReaderError(0),
),
responseWriter: httptest.NewRecorder(),
params: httprouter.Params{},
expectedBody: "failed to read request body",
},
{
Expand All @@ -190,7 +181,6 @@ func TestPostSignIn(t *testing.T) {
bytes.NewBuffer([]byte(`{invalid}`)),
),
responseWriter: httptest.NewRecorder(),
params: httprouter.Params{},
expectedBody: "failed to json unmarshal request body",
},
{
Expand All @@ -206,15 +196,14 @@ func TestPostSignIn(t *testing.T) {
bytes.NewBuffer([]byte(`{"email":"[email protected]","password":"test-pwd"}`)),
),
responseWriter: httptest.NewRecorder(),
params: httprouter.Params{},
expectedBody: "failed to find current user",
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
h := NewHandlers(testCase.srvMock)
h.PostSignIn()(testCase.responseWriter, testCase.request, testCase.params)
h.PostSignIn()(testCase.responseWriter, testCase.request)
if !strings.Contains(testCase.responseWriter.Body.String(), testCase.expectedBody) {
t.Fatalf("expected: %v, got: %v", testCase.expectedBody, testCase.responseWriter.Body.String())
}
Expand Down
8 changes: 4 additions & 4 deletions domain/auth/route.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package auth

import (
"github.com/julienschmidt/httprouter"
"net/http"

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

type Handlers interface {
GetPing() httprouter.Handle
GetCurrentUser() httprouter.Handle
PostSignIn() httprouter.Handle
GetPing() http.HandlerFunc
GetCurrentUser() http.HandlerFunc
PostSignIn() http.HandlerFunc
}

type routes struct {
Expand Down
20 changes: 9 additions & 11 deletions domain/auth/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,26 @@ import (
"net/http"
"testing"

"github.com/julienschmidt/httprouter"

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

type handlersMock struct {
getPing httprouter.Handle
getCurrentUser httprouter.Handle
postSignIn httprouter.Handle
getPing http.HandlerFunc
getCurrentUser http.HandlerFunc
postSignIn http.HandlerFunc
}

func (h *handlersMock) GetPing() httprouter.Handle { return h.getPing }
func (h *handlersMock) GetCurrentUser() httprouter.Handle { return h.getCurrentUser }
func (h *handlersMock) PostSignIn() httprouter.Handle { return h.postSignIn }
func (h *handlersMock) GetPing() http.HandlerFunc { return h.getPing }
func (h *handlersMock) GetCurrentUser() http.HandlerFunc { return h.getCurrentUser }
func (h *handlersMock) PostSignIn() http.HandlerFunc { return h.postSignIn }

func TestRoutesAll(t *testing.T) {
h := &handlersMock{
getPing: func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
getPing: func(w http.ResponseWriter, r *http.Request) {
},
getCurrentUser: func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
getCurrentUser: func(w http.ResponseWriter, r *http.Request) {
},
postSignIn: func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
postSignIn: func(w http.ResponseWriter, r *http.Request) {
},
}

Expand Down
5 changes: 2 additions & 3 deletions domain/gql/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net/http"

"github.com/graphql-go/handler"
"github.com/julienschmidt/httprouter"

"github.com/chris-ramon/golang-scaffolding/pkg/ctxutil"
"github.com/chris-ramon/golang-scaffolding/pkg/route"
Expand All @@ -24,15 +23,15 @@ func (ro *routes) All() []route.Route {
route.Route{
HTTPMethod: "GET",
Path: "/graphql",
Handler: func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
Handler: func(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(ctxutil.WithAuthHeader(r.Context(), r.Header))
ro.handlers.GetGraphQL().ServeHTTP(w, r)
},
},
route.Route{
HTTPMethod: "POST",
Path: "/graphql",
Handler: func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
Handler: func(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(ctxutil.WithAuthHeader(r.Context(), r.Header))
ro.handlers.PostGraphQL().ServeHTTP(w, r)
},
Expand Down
6 changes: 2 additions & 4 deletions domain/users/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"encoding/json"
"net/http"

"github.com/julienschmidt/httprouter"

"github.com/admin-golang/admin/dataloader"
"github.com/chris-ramon/golang-scaffolding/domain/users/mappers"
userTypes "github.com/chris-ramon/golang-scaffolding/domain/users/types"
Expand All @@ -16,8 +14,8 @@ type handlers struct {
srv Service
}

func (h *handlers) GetUsers() httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
func (h *handlers) GetUsers() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
users, err := h.srv.FindUsers(r.Context())

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions domain/users/route.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package users

import (
"github.com/julienschmidt/httprouter"
"net/http"

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

type Handlers interface {
GetUsers() httprouter.Handle
GetUsers() http.HandlerFunc
}

type routes struct {
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/chris-ramon/golang-scaffolding

go 1.20
go 1.22.3

require (
github.com/graphql-go/graphql v0.8.1
Expand All @@ -11,7 +11,6 @@ require (
github.com/admin-golang/admin v0.0.0-20220720064250-fa4d88cf3085
github.com/golang-jwt/jwt/v5 v5.0.0-rc.2
github.com/golang-migrate/migrate/v4 v4.15.2
github.com/julienschmidt/httprouter v1.3.0
github.com/lib/pq v1.10.8
)

Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,6 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
Expand Down
6 changes: 2 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"log"
"net/http"

"github.com/julienschmidt/httprouter"

"github.com/chris-ramon/golang-scaffolding/config"
"github.com/chris-ramon/golang-scaffolding/db"
"github.com/chris-ramon/golang-scaffolding/domain/admin"
Expand Down Expand Up @@ -36,7 +34,7 @@ func main() {
log.Println("successfully run migrations")
}

router := httprouter.New()
router := http.NewServeMux()

usersRepo := users.NewRepo(db)
usersService := users.NewService(usersRepo)
Expand Down Expand Up @@ -78,7 +76,7 @@ func main() {
routes = append(routes, usersRoutes.All()...)

for _, r := range routes {
router.Handle(r.HTTPMethod, r.Path, r.Handler)
router.HandleFunc(fmt.Sprintf("%s %s", r.HTTPMethod, r.Path), r.Handler)
}

log.Printf("server running on port :%s", conf.Port)
Expand Down
4 changes: 2 additions & 2 deletions pkg/route/route.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package route

import (
"github.com/julienschmidt/httprouter"
"net/http"
)

type Route struct {
HTTPMethod string
Path string
Handler httprouter.Handle
Handler http.HandlerFunc
}