Skip to content

Commit

Permalink
Moved all non-net/http related code to the new Engine struct
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenQuim committed Dec 20, 2024
1 parent 17ded45 commit bebd5bd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
16 changes: 16 additions & 0 deletions engine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package fuego

func NewEngine() *Engine {
return &Engine{
OpenAPI: NewOpenAPI(),
ErrorHandler: ErrorHandler,
}
}

// The Engine is the main struct of the framework.
type Engine struct {
OpenAPI *OpenAPI
ErrorHandler func(error) error

acceptedContentTypes []string
}
20 changes: 9 additions & 11 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,24 @@ type Server struct {
disableAutoGroupTags bool
basePath string // Base path of the group

// Points to the server OpenAPI struct.
OpenAPI *OpenAPI
*Engine

Security Security

autoAuth AutoAuthConfig
fs fs.FS
template *template.Template // TODO: use preparsed templates

acceptedContentTypes []string

DisallowUnknownFields bool // If true, the server will return an error if the request body contains unknown fields. Useful for quick debugging in development.
DisableOpenapi bool // If true, the routes within the server will not generate an OpenAPI spec.
maxBodySize int64

Serialize Sender // Custom serializer that overrides the default one.
SerializeError ErrorSender // Used to serialize the error response. Defaults to [SendError].
ErrorHandler func(err error) error // Used to transform any error into a unified error type structure with status code. Defaults to [ErrorHandler]
startTime time.Time
// Custom serializer that overrides the default one.
Serialize Sender
// Used to serialize the error response. Defaults to [SendError].
SerializeError ErrorSender

startTime time.Time

OpenAPIConfig OpenAPIConfig

Expand All @@ -99,8 +98,8 @@ func NewServer(options ...func(*Server)) *Server {
WriteTimeout: 30 * time.Second,
IdleTimeout: 30 * time.Second,
},
Mux: http.NewServeMux(),
OpenAPI: NewOpenAPI(),
Mux: http.NewServeMux(),
Engine: NewEngine(),

OpenAPIConfig: defaultOpenAPIConfig,

Expand All @@ -113,7 +112,6 @@ func NewServer(options ...func(*Server)) *Server {
WithDisallowUnknownFields(true),
WithSerializer(Send),
WithErrorSerializer(SendError),
WithErrorHandler(ErrorHandler),
WithRouteOptions(
OptionAddResponse(http.StatusBadRequest, "Bad Request _(validation or deserialization error)_", Response{Type: HTTPError{}}),
OptionAddResponse(http.StatusInternalServerError, "Internal Server Error _(panics)_", Response{Type: HTTPError{}}),
Expand Down

0 comments on commit bebd5bd

Please sign in to comment.