Skip to content

Commit

Permalink
refactor: consolidate result handling and refactor fx module names
Browse files Browse the repository at this point in the history
- Replace `ResponseResult` with `Result` and simplify result handling
- Rename Fx modules for consistency (e.g., `Module` to `FxModule`)
- Implement interface assertions for repository implementations
- Change Result methods to return `ResultImpl`
- Refactor middleware to use new `Result` type
- Add and refactor result definition and related tests
- Rename `Module` to `FxModule` in various packages for clarity
  • Loading branch information
eser committed Aug 23, 2024
1 parent 31bd12c commit 38db54e
Show file tree
Hide file tree
Showing 32 changed files with 353 additions and 280 deletions.
2 changes: 2 additions & 0 deletions pkg/app/data/repositories/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type UserRepository interface {

type UserRepositoryImpl struct{}

var _ UserRepository = (*UserRepositoryImpl)(nil)

func NewUserRepository() *UserRepositoryImpl {
return &UserRepositoryImpl{}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
func Run() {
app := fx.New(
fx.WithLogger(logfx.GetFxLogger),
bliss.Module,
Module,
bliss.FxModule,
FxModule,
)

app.Run()
Expand Down
10 changes: 5 additions & 5 deletions pkg/app/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import (
"go.uber.org/fx"
)

var Module = fx.Module( //nolint:gochecknoglobals
var FxModule = fx.Module( //nolint:gochecknoglobals
"app",
fx.Invoke(
RegisterRoutes,
),
fx.Provide(
bliss.LoadConfig[AppConfig](LoadConfig),
),
healthcheck.Module,
openapi.Module,
healthcheck.FxModule,
openapi.FxModule,
)

func LoadConfig(cl configfx.ConfigLoader) (*AppConfig, error) {
Expand Down Expand Up @@ -50,7 +50,7 @@ func RegisterRoutes(routes httpfx.Router, appConfig *AppConfig) {
routes.Use(middlewares.CorsMiddleware())

routes.
Route("GET /", func(ctx *httpfx.Context) httpfx.ResponseResult {
Route("GET /", func(ctx *httpfx.Context) httpfx.Result {
message := fmt.Sprintf(
"Hello %s (%s) from %s!",
ctx.Request.Context().Value(middlewares.ClientAddr),
Expand All @@ -65,7 +65,7 @@ func RegisterRoutes(routes httpfx.Router, appConfig *AppConfig) {
HasResponse(http.StatusOK)

routes.
Route("GET /protected", middlewares.AuthMiddleware(), func(ctx *httpfx.Context) httpfx.ResponseResult {
Route("GET /protected", middlewares.AuthMiddleware(), func(ctx *httpfx.Context) httpfx.Result {
message := fmt.Sprintf("Hello from %s! this endpoint is protected!", appConfig.AppName)

return ctx.Results.PlainText(message)
Expand Down
2 changes: 2 additions & 0 deletions pkg/bliss/configfx/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type ConfigLoader interface {

type ConfigLoaderImpl struct{}

var _ ConfigLoader = (*ConfigLoaderImpl)(nil)

func NewConfigLoader() *ConfigLoaderImpl {
return &ConfigLoaderImpl{}
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/bliss/configfx/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ import (

var ErrConfigDecoding = errors.New("config decoding error")

var Module = fx.Module( //nolint:gochecknoglobals
var FxModule = fx.Module( //nolint:gochecknoglobals
"config",
fx.Provide(
New,
),
)

type Result struct {
type FxResult struct {
fx.Out

ConfigLoader ConfigLoader
}

func New() (Result, error) {
return Result{ //nolint:exhaustruct
func New() (FxResult, error) {
return FxResult{ //nolint:exhaustruct
ConfigLoader: NewConfigLoader(),
}, nil
}
8 changes: 4 additions & 4 deletions pkg/bliss/datafx/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ import (
"go.uber.org/fx"
)

var Module = fx.Module( //nolint:gochecknoglobals
var FxModule = fx.Module( //nolint:gochecknoglobals
"data",
fx.Provide(
New,
),
)

type Result struct {
type FxResult struct {
fx.Out

DataProvider DataProvider
}

func New() (Result, error) {
return Result{ //nolint:exhaustruct
func New() (FxResult, error) {
return FxResult{ //nolint:exhaustruct
DataProvider: NewDataProvider(),
}, nil
}
2 changes: 2 additions & 0 deletions pkg/bliss/datafx/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ type DataProvider interface {

type DataProviderImpl struct{}

var _ DataProvider = (*DataProviderImpl)(nil)

func NewDataProvider() *DataProviderImpl {
return &DataProviderImpl{}
}
2 changes: 1 addition & 1 deletion pkg/bliss/httpfx/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Context struct {
// isAborted bool
}

func (c *Context) Next() ResponseResult {
func (c *Context) Next() Result {
c.index++

for c.index < len(c.handlers) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/bliss/httpfx/middlewares/auth-middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
var ErrInvalidSigningMethod = errors.New("Invalid signing method")

func AuthMiddleware() httpfx.Handler {
return func(ctx *httpfx.Context) httpfx.ResponseResult {
return func(ctx *httpfx.Context) httpfx.Result {
tokenString, hasToken := getBearerToken(ctx)

if !hasToken {
Expand Down
4 changes: 2 additions & 2 deletions pkg/bliss/httpfx/middlewares/auth-middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ func TestAuthMiddleware(t *testing.T) {
middleware := middlewares.AuthMiddleware()
result := middleware(&httpCtx)

if result.StatusCode != tt.expectedStatusCode {
assert.Equal(t, tt.expectedStatusCode, result.StatusCode)
if result.StatusCode() != tt.expectedStatusCode {
assert.Equal(t, tt.expectedStatusCode, result.StatusCode())
}

if tt.expectedStatusCode == http.StatusOK || tt.expectedStatusCode == http.StatusNoContent {
Expand Down
2 changes: 1 addition & 1 deletion pkg/bliss/httpfx/middlewares/correlation-id-middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
const CorrelationIdHeader = "X-Correlation-Id"

func CorrelationIdMiddleware() httpfx.Handler {
return func(ctx *httpfx.Context) httpfx.ResponseResult {
return func(ctx *httpfx.Context) httpfx.Result {
// FIXME(@eser): no need to check if the header is specified
correlationId := ctx.Request.Header.Get(CorrelationIdHeader)
if correlationId == "" {
Expand Down
2 changes: 1 addition & 1 deletion pkg/bliss/httpfx/middlewares/cors-middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
const AccessControlAllowOriginHeader = "Access-Control-Allow-Origin"

func CorsMiddleware() httpfx.Handler {
return func(ctx *httpfx.Context) httpfx.ResponseResult {
return func(ctx *httpfx.Context) httpfx.Result {
result := ctx.Next()

ctx.ResponseWriter.Header().Set(AccessControlAllowOriginHeader, "*")
Expand Down
2 changes: 1 addition & 1 deletion pkg/bliss/httpfx/middlewares/error-handler-middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package middlewares
import "github.com/eser/go-service/pkg/bliss/httpfx"

func ErrorHandlerMiddleware() httpfx.Handler {
return func(ctx *httpfx.Context) httpfx.ResponseResult {
return func(ctx *httpfx.Context) httpfx.Result {
result := ctx.Next()

return result
Expand Down
2 changes: 1 addition & 1 deletion pkg/bliss/httpfx/middlewares/resolve-address-middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
)

func ResolveAddressMiddleware() httpfx.Handler {
return func(ctx *httpfx.Context) httpfx.ResponseResult {
return func(ctx *httpfx.Context) httpfx.Result {
addr := GetClientAddrs(ctx.Request)

newContext := context.WithValue(
Expand Down
2 changes: 1 addition & 1 deletion pkg/bliss/httpfx/middlewares/response-time-middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
const ResponseTimeHeader = "X-Request-Time"

func ResponseTimeMiddleware() httpfx.Handler {
return func(ctx *httpfx.Context) httpfx.ResponseResult {
return func(ctx *httpfx.Context) httpfx.Result {
startTime := time.Now()

result := ctx.Next()
Expand Down
8 changes: 4 additions & 4 deletions pkg/bliss/httpfx/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"go.uber.org/fx"
)

var Module = fx.Module( //nolint:gochecknoglobals
var FxModule = fx.Module( //nolint:gochecknoglobals
"httpservice",
fx.Provide(
New,
Expand All @@ -22,7 +22,7 @@ var Module = fx.Module( //nolint:gochecknoglobals
),
)

type Result struct {
type FxResult struct {
fx.Out

HttpService *HttpService
Expand All @@ -37,7 +37,7 @@ type HttpService struct {
Routes Router
}

func New(config *Config) (Result, error) {
func New(config *Config) (FxResult, error) {
routes := NewRouter("/")

server := &http.Server{ //nolint:exhaustruct
Expand All @@ -51,7 +51,7 @@ func New(config *Config) (Result, error) {
Handler: routes.GetMux(),
}

return Result{ //nolint:exhaustruct
return FxResult{ //nolint:exhaustruct
HttpService: &HttpService{Server: server, Config: config, Routes: routes},
Routes: routes,
}, nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/bliss/httpfx/modules/healthcheck/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"go.uber.org/fx"
)

var Module = fx.Module( //nolint:gochecknoglobals
var FxModule = fx.Module( //nolint:gochecknoglobals
"healthcheck",
fx.Invoke(
RegisterRoutes,
Expand All @@ -16,7 +16,7 @@ var Module = fx.Module( //nolint:gochecknoglobals

func RegisterRoutes(routes httpfx.Router) {
routes.
Route("GET /health-check", func(ctx *httpfx.Context) httpfx.ResponseResult {
Route("GET /health-check", func(ctx *httpfx.Context) httpfx.Result {
return ctx.Results.Ok()
}).
HasSummary("Health Check").
Expand Down
4 changes: 2 additions & 2 deletions pkg/bliss/httpfx/modules/openapi/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type ApiIdentity struct {
version string
}

var Module = fx.Module( //nolint:gochecknoglobals
var FxModule = fx.Module( //nolint:gochecknoglobals
"openapi",
fx.Invoke(
RegisterRoutes,
Expand All @@ -19,7 +19,7 @@ var Module = fx.Module( //nolint:gochecknoglobals

func RegisterRoutes(routes httpfx.Router) {
routes.
Route("GET /openapi.json", func(ctx *httpfx.Context) httpfx.ResponseResult {
Route("GET /openapi.json", func(ctx *httpfx.Context) httpfx.Result {
spec := &ApiIdentity{
name: "golang-service",
version: "0.0.0",
Expand Down
2 changes: 1 addition & 1 deletion pkg/bliss/httpfx/primitives.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package httpfx

type (
Handler func(*Context) ResponseResult
Handler func(*Context) Result
HandlerChain []Handler
Middleware func() Handler
)
49 changes: 49 additions & 0 deletions pkg/bliss/httpfx/result.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package httpfx

import (
"github.com/eser/go-service/pkg/bliss/results"
)

type Result interface {
results.Result

StatusCode() int
Body() []byte

RedirectToUri() string
}

type ResultImpl struct {
results.ResultImpl

InnerStatusCode int
InnerBody []byte

InnerRedirectToUri string
}

var _ Result = (*ResultImpl)(nil)

func (r ResultImpl) StatusCode() int {
return r.InnerStatusCode
}

func (r ResultImpl) Body() []byte {
return r.InnerBody
}

func (r ResultImpl) RedirectToUri() string {
return r.InnerRedirectToUri
}

func (r ResultImpl) WithStatusCode(statusCode int) ResultImpl {
r.InnerStatusCode = statusCode

return r
}

func (r ResultImpl) WithBody(body string) ResultImpl {
r.InnerBody = []byte(body)

return r
}
Loading

0 comments on commit 38db54e

Please sign in to comment.