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
This commit is contained in:
parent
0ae35c66f2
commit
c296f4fed6
49 changed files with 4796 additions and 249 deletions
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue