diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2020-11-13 20:51:07 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-13 14:51:07 +0200 |
commit | c296f4fed66288431fa7ec3a64f990beccd29eb1 (patch) | |
tree | 6b2f1971303967671bd5da1d1149407e410d62bd /modules | |
parent | 0ae35c66f2efe608e3176f796866c18461f0780f (diff) | |
download | gitea-c296f4fed66288431fa7ec3a64f990beccd29eb1.tar.gz gitea-c296f4fed66288431fa7ec3a64f990beccd29eb1.zip |
Introduce go chi web framework as frontend of macaron, so that we can move routes from macaron to chi step by step (#7420)
* When route cannot be found on chi, go to macaron
* Stick chi version to 1.5.0
* Follow router log setting
Diffstat (limited to 'modules')
-rw-r--r-- | modules/public/dynamic.go | 6 | ||||
-rw-r--r-- | modules/public/public.go | 74 | ||||
-rw-r--r-- | modules/public/static.go | 5 |
3 files changed, 42 insertions, 43 deletions
diff --git a/modules/public/dynamic.go b/modules/public/dynamic.go index 07f83e1f6a..f1a4dbb1a3 100644 --- a/modules/public/dynamic.go +++ b/modules/public/dynamic.go @@ -6,11 +6,9 @@ package public -import ( - "gitea.com/macaron/macaron" -) +import "net/http" // Static implements the macaron static handler for serving assets. -func Static(opts *Options) macaron.Handler { +func Static(opts *Options) func(next http.Handler) http.Handler { return opts.staticHandler(opts.Directory) } diff --git a/modules/public/public.go b/modules/public/public.go index 2dcc530a73..3a2fa4c57c 100644 --- a/modules/public/public.go +++ b/modules/public/public.go @@ -15,8 +15,6 @@ import ( "time" "code.gitea.io/gitea/modules/setting" - - "gitea.com/macaron/macaron" ) // Options represents the available options to configure the macaron handler. @@ -41,7 +39,7 @@ var KnownPublicEntries = []string{ } // Custom implements the macaron static handler for serving custom assets. -func Custom(opts *Options) macaron.Handler { +func Custom(opts *Options) func(next http.Handler) http.Handler { return opts.staticHandler(path.Join(setting.CustomPath, "public")) } @@ -52,7 +50,7 @@ type staticFileSystem struct { func newStaticFileSystem(directory string) staticFileSystem { if !filepath.IsAbs(directory) { - directory = filepath.Join(macaron.Root, directory) + directory = filepath.Join(setting.AppWorkPath, directory) } dir := http.Dir(directory) return staticFileSystem{&dir} @@ -63,39 +61,43 @@ func (fs staticFileSystem) Open(name string) (http.File, error) { } // StaticHandler sets up a new middleware for serving static files in the -func StaticHandler(dir string, opts *Options) macaron.Handler { +func StaticHandler(dir string, opts *Options) func(next http.Handler) http.Handler { return opts.staticHandler(dir) } -func (opts *Options) staticHandler(dir string) macaron.Handler { - // Defaults - if len(opts.IndexFile) == 0 { - opts.IndexFile = "index.html" - } - // Normalize the prefix if provided - if opts.Prefix != "" { - // Ensure we have a leading '/' - if opts.Prefix[0] != '/' { - opts.Prefix = "/" + opts.Prefix +func (opts *Options) staticHandler(dir string) func(next http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + // Defaults + if len(opts.IndexFile) == 0 { + opts.IndexFile = "index.html" + } + // Normalize the prefix if provided + if opts.Prefix != "" { + // Ensure we have a leading '/' + if opts.Prefix[0] != '/' { + opts.Prefix = "/" + opts.Prefix + } + // Remove any trailing '/' + opts.Prefix = strings.TrimRight(opts.Prefix, "/") + } + if opts.FileSystem == nil { + opts.FileSystem = newStaticFileSystem(dir) } - // Remove any trailing '/' - opts.Prefix = strings.TrimRight(opts.Prefix, "/") - } - if opts.FileSystem == nil { - opts.FileSystem = newStaticFileSystem(dir) - } - return func(ctx *macaron.Context, log *log.Logger) { - opts.handle(ctx, log, opts) + return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + if !opts.handle(w, req, opts) { + next.ServeHTTP(w, req) + } + }) } } -func (opts *Options) handle(ctx *macaron.Context, log *log.Logger, opt *Options) bool { - if ctx.Req.Method != "GET" && ctx.Req.Method != "HEAD" { +func (opts *Options) handle(w http.ResponseWriter, req *http.Request, opt *Options) bool { + if req.Method != "GET" && req.Method != "HEAD" { return false } - file := ctx.Req.URL.Path + file := req.URL.Path // if we have a prefix, filter requests by stripping the prefix if opt.Prefix != "" { if !strings.HasPrefix(file, opt.Prefix) { @@ -117,7 +119,7 @@ func (opts *Options) handle(ctx *macaron.Context, log *log.Logger, opt *Options) } for _, entry := range KnownPublicEntries { if entry == parts[1] { - ctx.Resp.WriteHeader(404) + w.WriteHeader(404) return true } } @@ -135,8 +137,8 @@ func (opts *Options) handle(ctx *macaron.Context, log *log.Logger, opt *Options) // Try to serve index file if fi.IsDir() { // Redirect if missing trailing slash. - if !strings.HasSuffix(ctx.Req.URL.Path, "/") { - http.Redirect(ctx.Resp, ctx.Req.Request, path.Clean(ctx.Req.URL.Path+"/"), http.StatusFound) + if !strings.HasSuffix(req.URL.Path, "/") { + http.Redirect(w, req, path.Clean(req.URL.Path+"/"), http.StatusFound) return true } @@ -148,7 +150,7 @@ func (opts *Options) handle(ctx *macaron.Context, log *log.Logger, opt *Options) fi, err = f.Stat() if err != nil || fi.IsDir() { - return true + return false } } @@ -158,16 +160,16 @@ func (opts *Options) handle(ctx *macaron.Context, log *log.Logger, opt *Options) // Add an Expires header to the static content if opt.ExpiresAfter > 0 { - ctx.Resp.Header().Set("Expires", time.Now().Add(opt.ExpiresAfter).UTC().Format(http.TimeFormat)) + w.Header().Set("Expires", time.Now().Add(opt.ExpiresAfter).UTC().Format(http.TimeFormat)) tag := GenerateETag(fmt.Sprint(fi.Size()), fi.Name(), fi.ModTime().UTC().Format(http.TimeFormat)) - ctx.Resp.Header().Set("ETag", tag) - if ctx.Req.Header.Get("If-None-Match") == tag { - ctx.Resp.WriteHeader(304) - return false + w.Header().Set("ETag", tag) + if req.Header.Get("If-None-Match") == tag { + w.WriteHeader(304) + return true } } - http.ServeContent(ctx.Resp, ctx.Req.Request, file, fi.ModTime(), f) + http.ServeContent(w, req, file, fi.ModTime(), f) return true } diff --git a/modules/public/static.go b/modules/public/static.go index 76050632c9..8da10567ea 100644 --- a/modules/public/static.go +++ b/modules/public/static.go @@ -8,12 +8,11 @@ package public import ( "io/ioutil" - - "gitea.com/macaron/macaron" + "net/http" ) // Static implements the macaron static handler for serving assets. -func Static(opts *Options) macaron.Handler { +func Static(opts *Options) func(next http.Handler) http.Handler { opts.FileSystem = Assets // we don't need to pass the directory, because the directory var is only // used when in the options there is no FileSystem. |