-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): Add a wrapper function for handlers to accept context and …
…return errors
- Loading branch information
1 parent
559a12d
commit 647ef35
Showing
21 changed files
with
2,772 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package hackgrp | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package hackgrp | ||
|
||
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
14
app/services/thoughts-api/v1/handlers/handlers/handlers.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package handlers | ||
|
||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package v1 | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package web | ||
|
||
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.