Skip to content

Commit

Permalink
feat(api): Add a wrapper function for handlers to accept context and …
Browse files Browse the repository at this point in the history
…return errors
  • Loading branch information
youngjun827 committed Nov 7, 2023
1 parent 559a12d commit 647ef35
Show file tree
Hide file tree
Showing 21 changed files with 2,772 additions and 3 deletions.
12 changes: 11 additions & 1 deletion app/services/thoughts-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"time"

"github.com/ardanlabs/conf/v3"
"github.com/youngjun827/thoughts/app/services/thoughts-api/v1/handlers/handlers"
v1 "github.com/youngjun827/thoughts/business/web/v1"
"github.com/youngjun827/thoughts/business/web/v1/debug"
"github.com/youngjun827/thoughts/foundation/logger"
)
Expand Down Expand Up @@ -113,9 +115,17 @@ func run(ctx context.Context, log *logger.Logger) error {
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)

cfgMux := v1.APIMuxConfig{
Build: build,
Shutdown: shutdown,
Log: log,
}

apiMux := v1.APIMux(cfgMux, handlers.Routes{})

api := http.Server{
Addr: cfg.Web.APIHost,
Handler: nil,
Handler: apiMux,
ReadTimeout: cfg.Web.ReadTimeout,
WriteTimeout: cfg.Web.WriteTimeout,
IdleTimeout: cfg.Web.IdleTimeout,
Expand Down
22 changes: 22 additions & 0 deletions app/services/thoughts-api/v1/handlers/hackgrp/hackgrp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package hackgrp

Check failure on line 1 in app/services/thoughts-api/v1/handlers/hackgrp/hackgrp.go

View workflow job for this annotation

GitHub Actions / build (1.21.3)

at least one file in a package should have a package comment (ST1000)

import (
"context"
"encoding/json"
"net/http"
)

func Hack(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
status := struct {
Status string
}{
Status: "OK",
}

err := json.NewEncoder(w).Encode(status)
if err != nil {
return err
}

return nil
}
12 changes: 12 additions & 0 deletions app/services/thoughts-api/v1/handlers/hackgrp/route.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package hackgrp

Check failure on line 1 in app/services/thoughts-api/v1/handlers/hackgrp/route.go

View workflow job for this annotation

GitHub Actions / build (1.21.3)

at least one file in a package should have a package comment (ST1000)

import (
"github.com/go-chi/chi/v5"
"github.com/youngjun827/thoughts/foundation/web"
)

func Routes(router chi.Router) {
router.Route("/hack", func(c chi.Router) {
c.Get("/", web.HandlerAdapter(Hack))
})
}
14 changes: 14 additions & 0 deletions app/services/thoughts-api/v1/handlers/handlers/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package handlers

Check failure on line 1 in app/services/thoughts-api/v1/handlers/handlers/handlers.go

View workflow job for this annotation

GitHub Actions / build (1.21.3)

at least one file in a package should have a package comment (ST1000)

import (
"github.com/go-chi/chi/v5"
"github.com/youngjun827/thoughts/app/services/thoughts-api/v1/handlers/hackgrp"
v1 "github.com/youngjun827/thoughts/business/web/v1"
)


type Routes struct{}

func (r Routes) Add(router chi.Router, cfg v1.APIMuxConfig) {
hackgrp.Routes(router)
}
27 changes: 27 additions & 0 deletions business/web/v1/v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package v1

Check failure on line 1 in business/web/v1/v1.go

View workflow job for this annotation

GitHub Actions / build (1.21.3)

at least one file in a package should have a package comment (ST1000)

import (
"os"

"github.com/go-chi/chi/v5"
"github.com/youngjun827/thoughts/foundation/logger"
"github.com/youngjun827/thoughts/foundation/web"
)

type APIMuxConfig struct {
Build string
Shutdown chan os.Signal
Log *logger.Logger
}

type RouteAdder interface {
Add(router chi.Router, cfg APIMuxConfig)
}

func APIMux(cfg APIMuxConfig, routeAdder RouteAdder) chi.Router {
app := web.NewApp(cfg.Shutdown)

routeAdder.Add(app.Mux, cfg)

return app.Mux
}
37 changes: 37 additions & 0 deletions foundation/web/web.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package web

Check failure on line 1 in foundation/web/web.go

View workflow job for this annotation

GitHub Actions / build (1.21.3)

at least one file in a package should have a package comment (ST1000)

import (
"context"
"net/http"
"os"

"github.com/go-chi/chi/v5"
)

type Handler func(context.Context, http.ResponseWriter, *http.Request) error

type App struct {
Mux *chi.Mux
shutdown chan os.Signal
}

func NewApp(shutdown chan os.Signal) *App {
r := chi.NewRouter()
return &App{
Mux: r,
shutdown: shutdown,
}
}

func HandlerAdapter(handler Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
err := handler(r.Context(), w, r)
if err != nil {
// Handle the error as needed
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
}
}
func (a *App) Handle(method string, path string, handler Handler) {
a.Mux.MethodFunc(method, path, HandlerAdapter(handler))
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/youngjun827/thoughts

go 1.21.3

require github.com/ardanlabs/conf/v3 v3.1.7
require (
github.com/ardanlabs/conf/v3 v3.1.7
github.com/go-chi/chi/v5 v5.0.10
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/ardanlabs/conf/v3 v3.1.7 h1:p232cF68TafoA5U9ZlbxUIhGJtGNdKHBXF80Fdqb5t0=
github.com/ardanlabs/conf/v3 v3.1.7/go.mod h1:zclexWKe0NVj6LHQ8NgDDZ7bQ1spE0KeKPFficdtAjU=
github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk=
github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
6 changes: 5 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ run:
run-help:
go run app/services/thoughts-api/main.go --help | go run app/tooling/logfmt/main.go

curl:
curl -il http://localhost:3000/hack

# ==============================================================================
# Define dependencies

Expand Down Expand Up @@ -103,4 +106,5 @@ metrics-view-sc:

tidy:
go mod tidy
go mod vendor
go mod vendor

3 changes: 3 additions & 0 deletions vendor/github.com/go-chi/chi/v5/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 647ef35

Please sign in to comment.