@@ -14,13 +14,12 @@ import (
1414 "time"
1515
1616 "code.gitea.io/gitea/modules/setting"
17- "gopkg.in/macaron.v1"
1817)
1918
2019//go:generate go run -mod=vendor main.go
2120//go:generate go fmt bindata.go
2221
23- // Options represents the available options to configure the macaron handler.
22+ // Options represents the available options to configure the http handler.
2423type Options struct {
2524 Directory string
2625 IndexFile string
@@ -32,9 +31,11 @@ type Options struct {
3231 Prefix string
3332}
3433
35- // Custom implements the macaron static handler for serving custom assets.
36- func Custom (opts * Options ) macaron.Handler {
37- return opts .staticHandler (path .Join (setting .CustomPath , "public" ))
34+ // Custom implements the http static handler for serving custom assets.
35+ func Custom (opts * Options ) func (next http.Handler ) http.Handler {
36+ return func (next http.Handler ) http.Handler {
37+ return opts .staticHandler (path .Join (setting .CustomPath , "public" ))
38+ }
3839}
3940
4041// staticFileSystem implements http.FileSystem interface.
@@ -44,7 +45,7 @@ type staticFileSystem struct {
4445
4546func newStaticFileSystem (directory string ) staticFileSystem {
4647 if ! filepath .IsAbs (directory ) {
47- directory = filepath .Join (macaron . Root , directory )
48+ directory = filepath .Join (setting . AppWorkPath , directory )
4849 }
4950 dir := http .Dir (directory )
5051 return staticFileSystem {& dir }
@@ -55,11 +56,13 @@ func (fs staticFileSystem) Open(name string) (http.File, error) {
5556}
5657
5758// StaticHandler sets up a new middleware for serving static files in the
58- func StaticHandler (dir string , opts * Options ) macaron.Handler {
59- return opts .staticHandler (dir )
59+ func StaticHandler (dir string , opts * Options ) func (next http.Handler ) http.Handler {
60+ return func (next http.Handler ) http.Handler {
61+ return opts .staticHandler (dir )
62+ }
6063}
6164
62- func (opts * Options ) staticHandler (dir string ) macaron. Handler {
65+ func (opts * Options ) staticHandler (dir string ) http. HandlerFunc {
6366 // Defaults
6467 if len (opts .IndexFile ) == 0 {
6568 opts .IndexFile = "index.html"
@@ -77,17 +80,17 @@ func (opts *Options) staticHandler(dir string) macaron.Handler {
7780 opts .FileSystem = newStaticFileSystem (dir )
7881 }
7982
80- return func (ctx * macaron. Context , log * log. Logger ) {
81- opts .handle (ctx , log , opts )
83+ return func (w http. ResponseWriter , req * http. Request ) {
84+ opts .handle (w , req , opts )
8285 }
8386}
8487
85- func (opts * Options ) handle (ctx * macaron. Context , log * log. Logger , opt * Options ) bool {
86- if ctx . Req . Method != "GET" && ctx . Req .Method != "HEAD" {
88+ func (opts * Options ) handle (w http. ResponseWriter , req * http. Request , opt * Options ) bool {
89+ if req . Method != "GET" && req .Method != "HEAD" {
8790 return false
8891 }
8992
90- file := ctx . Req .URL .Path
93+ file := req .URL .Path
9194 // if we have a prefix, filter requests by stripping the prefix
9295 if opt .Prefix != "" {
9396 if ! strings .HasPrefix (file , opt .Prefix ) {
@@ -114,8 +117,8 @@ func (opts *Options) handle(ctx *macaron.Context, log *log.Logger, opt *Options)
114117 // Try to serve index file
115118 if fi .IsDir () {
116119 // Redirect if missing trailing slash.
117- if ! strings .HasSuffix (ctx . Req .URL .Path , "/" ) {
118- http .Redirect (ctx . Resp , ctx . Req . Request , path .Clean (ctx . Req .URL .Path + "/" ), http .StatusFound )
120+ if ! strings .HasSuffix (req .URL .Path , "/" ) {
121+ http .Redirect (w , req , path .Clean (req .URL .Path + "/" ), http .StatusFound )
119122 return true
120123 }
121124
@@ -137,16 +140,16 @@ func (opts *Options) handle(ctx *macaron.Context, log *log.Logger, opt *Options)
137140
138141 // Add an Expires header to the static content
139142 if opt .ExpiresAfter > 0 {
140- ctx . Resp .Header ().Set ("Expires" , time .Now ().Add (opt .ExpiresAfter ).UTC ().Format (http .TimeFormat ))
143+ w .Header ().Set ("Expires" , time .Now ().Add (opt .ExpiresAfter ).UTC ().Format (http .TimeFormat ))
141144 tag := GenerateETag (string (fi .Size ()), fi .Name (), fi .ModTime ().UTC ().Format (http .TimeFormat ))
142- ctx . Resp .Header ().Set ("ETag" , tag )
143- if ctx . Req .Header .Get ("If-None-Match" ) == tag {
144- ctx . Resp .WriteHeader (304 )
145+ w .Header ().Set ("ETag" , tag )
146+ if req .Header .Get ("If-None-Match" ) == tag {
147+ w .WriteHeader (304 )
145148 return false
146149 }
147150 }
148151
149- http .ServeContent (ctx . Resp , ctx . Req . Request , file , fi .ModTime (), f )
152+ http .ServeContent (w , req , file , fi .ModTime (), f )
150153 return true
151154}
152155
0 commit comments