Skip to content

Commit

Permalink
fix: fixed All method and requires go1.22
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenQuim committed Feb 8, 2024
1 parent 13731e5 commit 8d3e39e
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 32 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-fuego/fuego

go 1.21.3
go 1.22.0

require (
github.com/getkin/kin-openapi v0.122.0
Expand Down
4 changes: 2 additions & 2 deletions go.work
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
go 1.21.5
go 1.22.0

use (
.
./examples/full-app-gourmet
./examples/basic
./examples/full-app-gourmet
)
24 changes: 9 additions & 15 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ type Route[ResponseBody any, RequestBody any] struct {
operation *openapi3.Operation
}

const MethodAll = "ALL"

// Capture all methods (GET, POST, PUT, PATCH, DELETE) and register a controller.
func All[T any, B any, Contexted ctx[B]](s *Server, path string, controller func(Contexted) (T, error), middlewares ...func(http.Handler) http.Handler) Route[T, B] {
return Register[T](s, MethodAll, path, controller, middlewares...)
for _, method := range []string{http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete} {
Register[T](s, method, path, controller, middlewares...)
}
return Register[T](s, http.MethodGet, path, controller, middlewares...)
}

func Get[T any, B any, Contexted ctx[B]](s *Server, path string, controller func(Contexted) (T, error), middlewares ...func(http.Handler) http.Handler) Route[T, B] {
Expand All @@ -72,10 +73,8 @@ func Patch[T any, B any, Contexted ctx[B]](s *Server, path string, controller fu

// Registers route into the default mux.
func Register[T any, B any, Contexted ctx[B]](s *Server, method string, path string, controller func(Contexted) (T, error), middlewares ...func(http.Handler) http.Handler) Route[T, B] {
fullPath := s.basePath + path
if isGo1_22 {
fullPath = method + " " + path
}
fullPath := method + " " + s.basePath + path

slog.Debug("registering openapi controller " + fullPath)

route := register[T, B](s, method, path, httpHandler[T, B](s, controller), middlewares...)
Expand All @@ -88,10 +87,7 @@ func Register[T any, B any, Contexted ctx[B]](s *Server, method string, path str
}

func register[T any, B any](s *Server, method string, path string, controller http.Handler, middlewares ...func(http.Handler) http.Handler) Route[T, B] {
fullPath := s.basePath + path
if isGo1_22 && method != MethodAll {
fullPath = method + " " + fullPath
}
fullPath := method + " " + s.basePath + path

allMiddlewares := append(middlewares, s.middlewares...)
s.Mux.Handle(fullPath, withMiddlewares(controller, allMiddlewares...))
Expand Down Expand Up @@ -187,10 +183,8 @@ func PatchStd(s *Server, path string, controller func(http.ResponseWriter, *http

// RegisterStd registers a standard http handler into the default mux.
func RegisterStd(s *Server, method string, path string, controller func(http.ResponseWriter, *http.Request), middlewares ...func(http.Handler) http.Handler) Route[any, any] {
fullPath := s.basePath + path
if isGo1_22 {
fullPath = method + " " + path
}
fullPath := method + " " + s.basePath + path

slog.Debug("registering standard controller " + fullPath)
route := register[any, any](s, method, path, http.HandlerFunc(controller), middlewares...)

Expand Down
3 changes: 0 additions & 3 deletions openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,6 @@ func RegisterOpenAPIOperation[T any, B any](s *Server, method, path string) (*op
operation.AddParameter(parameter)
}

if method == MethodAll {
method = http.MethodGet
}
s.OpenApiSpec.AddOperation(path, method, operation)

return operation, nil
Expand Down
11 changes: 0 additions & 11 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@ import (
"log/slog"
"net/http"
"os"
"runtime"
"strings"
"time"

"github.com/getkin/kin-openapi/openapi3"
"github.com/golang-jwt/jwt/v5"
)

var isGo1_22 = strings.TrimPrefix(runtime.Version(), "devel ") >= "go1.22"

type OpenapiConfig struct {
DisableSwagger bool
DisableLocalSave bool
Expand Down Expand Up @@ -100,13 +96,6 @@ func NewServer(options ...func(*Server)) *Server {
option(s)
}

if !isGo1_22 {
slog.Warn(
"Please upgrade to Go >= 1.22. " +
"You are running " + runtime.Version() + ": " +
"you cannot use path params nor register routes with the same path but different methods. ")
}

s.startTime = time.Now()

if s.autoAuth.Enabled {
Expand Down

0 comments on commit 8d3e39e

Please sign in to comment.