diff --git a/config/configs.go b/config/configs.go index b975644..50a745a 100644 --- a/config/configs.go +++ b/config/configs.go @@ -37,14 +37,15 @@ type ( // ServerNode dotweb app's httpserver config ServerNode struct { - EnabledListDir bool `xml:"enabledlistdir,attr"` // enable listing of directories, only valid for Router.ServerFile, default is false - EnabledRequestID bool `xml:"enabledrequestid,attr"` // enable uniq request ID, default is false, 32-bit UUID is used if enabled - EnabledGzip bool `xml:"enabledgzip,attr"` // enable gzip - EnabledAutoHEAD bool `xml:"enabledautohead,attr"` // ehanble HEAD routing, default is false, will add HEAD routing for all routes except for websocket and HEAD - EnabledAutoOPTIONS bool // enable OPTIONS routing, default is false, will add OPTIONS routing for all routes except for websocket and OPTIONS - EnabledIgnoreFavicon bool `xml:"enabledignorefavicon,attr"` // ignore favicon.ico request, return empty reponse if set - EnabledBindUseJsonTag bool `xml:"enabledbindusejsontag,attr"` // allow Bind to use JSON tag, default is false, Bind will use json tag automatically and ignore form tag - EnabledStaticFileMiddleware bool // The flag which enabled or disabled middleware for static-file route + EnabledListDir bool `xml:"enabledlistdir,attr"` // enable listing of directories, only valid for Router.ServerFile, default is false + EnabledRequestID bool `xml:"enabledrequestid,attr"` // enable uniq request ID, default is false, 32-bit UUID is used if enabled + EnabledGzip bool `xml:"enabledgzip,attr"` // enable gzip + EnabledAutoHEAD bool `xml:"enabledautohead,attr"` // ehanble HEAD routing, default is false, will add HEAD routing for all routes except for websocket and HEAD + EnabledAutoOPTIONS bool `xml:"-"` // enable OPTIONS routing, default is false, will add OPTIONS routing for all routes except for websocket and OPTIONS + EnabledRedirectTrailingSlash bool `xml:"enabledredirecttrailingslash,attr"` // enable automatic redirection for URLs with trailing slash, default is false to match net/http behavior + EnabledIgnoreFavicon bool `xml:"enabledignorefavicon,attr"` // ignore favicon.ico request, return empty reponse if set + EnabledBindUseJsonTag bool `xml:"enabledbindusejsontag,attr"` // allow Bind to use JSON tag, default is false, Bind will use json tag automatically and ignore form tag + EnabledStaticFileMiddleware bool `xml:"-"` // The flag which enabled or disabled middleware for static-file route Port int `xml:"port,attr"` // port EnabledTLS bool `xml:"enabledtls,attr"` // enable TLS TLSCertFile string `xml:"tlscertfile,attr"` // certifications file for TLS diff --git a/consts.go b/consts.go index ba5e7b0..e6e4dd4 100644 --- a/consts.go +++ b/consts.go @@ -3,7 +3,7 @@ package dotweb // Global define const ( // Version current version - Version = "1.7.3" + Version = "1.8" ) // Log define diff --git a/dotweb.go b/dotweb.go index abefcd5..9f42176 100644 --- a/dotweb.go +++ b/dotweb.go @@ -705,7 +705,14 @@ func DefaultMethodNotAllowedHandler(ctx Context) { // DefaultAutoOPTIONSHandler default handler for options request // if set HttpServer.EnabledAutoOPTIONS, auto bind this handler +// Sets CORS headers to support cross-origin preflight requests (Issue #250) func DefaultAutoOPTIONSHandler(ctx Context) error { + // Set CORS headers for preflight requests + h := ctx.Response().Header() + h.Set("Access-Control-Allow-Origin", "*") + h.Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") + h.Set("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With") + h.Set("Access-Control-Max-Age", "86400") return ctx.WriteStringC(http.StatusNoContent, "") } diff --git a/router.go b/router.go index aa19137..4ae3b87 100644 --- a/router.go +++ b/router.go @@ -155,8 +155,16 @@ func (ps Params) ByName(name string) string { // New returns a new initialized Router. // Path auto-correction, including trailing slashes, is enabled by default. func NewRouter(server *HttpServer) *router { + // Use ServerConfig.EnabledRedirectTrailingSlash if set, otherwise default to false + // to match net/http behavior (Issue #245) + // Note: During initialization, ServerConfig may be nil, so we check for that + redirectTrailingSlash := false + if server != nil && server.DotApp != nil && server.DotApp.Config != nil && server.DotApp.Config.Server != nil { + redirectTrailingSlash = server.ServerConfig().EnabledRedirectTrailingSlash + } + return &router{ - RedirectTrailingSlash: true, + RedirectTrailingSlash: redirectTrailingSlash, RedirectFixedPath: true, HandleOPTIONS: true, allRouterExpress: make(map[string]struct{}),