Skip to content

Commit c296f4f

Browse files
authored
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
1 parent 0ae35c6 commit c296f4f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+4796
-249
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ issues:
7070
- path: modules/log/
7171
linters:
7272
- errcheck
73-
- path: routers/routes/routes.go
73+
- path: routers/routes/macaron.go
7474
linters:
7575
- dupl
7676
- path: routers/api/v1/repo/issue_subscription.go

cmd/web.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import (
1919
"code.gitea.io/gitea/routers"
2020
"code.gitea.io/gitea/routers/routes"
2121

22-
"gitea.com/macaron/macaron"
23-
2422
context2 "github.com/gorilla/context"
2523
"github.com/unknwon/com"
2624
"github.com/urfave/cli"
@@ -135,9 +133,9 @@ func runWeb(ctx *cli.Context) error {
135133
return err
136134
}
137135
}
138-
m := routes.NewMacaron()
139-
routes.RegisterInstallRoute(m)
140-
err := listen(m, false)
136+
c := routes.NewChi()
137+
routes.RegisterInstallRoute(c)
138+
err := listen(c, false)
141139
select {
142140
case <-graceful.GetManager().IsShutdown():
143141
<-graceful.GetManager().Done()
@@ -168,10 +166,10 @@ func runWeb(ctx *cli.Context) error {
168166
}
169167
}
170168
// Set up Macaron
171-
m := routes.NewMacaron()
172-
routes.RegisterRoutes(m)
169+
c := routes.NewChi()
170+
routes.RegisterRoutes(c)
173171

174-
err := listen(m, true)
172+
err := listen(c, true)
175173
<-graceful.GetManager().Done()
176174
log.Info("PID: %d Gitea Web Finished", os.Getpid())
177175
log.Close()
@@ -212,7 +210,7 @@ func setPort(port string) error {
212210
return nil
213211
}
214212

215-
func listen(m *macaron.Macaron, handleRedirector bool) error {
213+
func listen(m http.Handler, handleRedirector bool) error {
216214
listenAddr := setting.HTTPAddr
217215
if setting.Protocol != setting.UnixSocket && setting.Protocol != setting.FCGIUnix {
218216
listenAddr = net.JoinHostPort(listenAddr, setting.HTTPPort)

contrib/pr/checkout.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ func runPR() {
117117
//routers.GlobalInit()
118118
external.RegisterParsers()
119119
markup.Init()
120-
m := routes.NewMacaron()
121-
routes.RegisterRoutes(m)
120+
c := routes.NewChi()
121+
routes.RegisterRoutes(c)
122122

123123
log.Printf("[PR] Ready for testing !\n")
124124
log.Printf("[PR] Login with user1, user2, user3, ... with pass: password\n")
@@ -138,7 +138,7 @@ func runPR() {
138138
*/
139139

140140
//Start the server
141-
http.ListenAndServe(":8080", context2.ClearHandler(m))
141+
http.ListenAndServe(":8080", context2.ClearHandler(c))
142142

143143
log.Printf("[PR] Cleaning up ...\n")
144144
/*

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ require (
3939
github.com/facebookgo/subset v0.0.0-20150612182917-8dac2c3c4870 // indirect
4040
github.com/gliderlabs/ssh v0.3.1
4141
github.com/glycerine/go-unsnap-stream v0.0.0-20190901134440-81cf024a9e0a // indirect
42+
github.com/go-chi/chi v1.5.0
4243
github.com/go-enry/go-enry/v2 v2.5.2
4344
github.com/go-git/go-billy/v5 v5.0.0
4445
github.com/go-git/go-git/v5 v5.2.0

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF
288288
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
289289
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
290290
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
291+
github.com/ethantkoenig/rupture v0.0.0-20181029165146-c3b3b810dc77 h1:ZLWiTTzTUBb0WEXUxobYI/RxULIzOoIP7pgfDd4p1cw=
291292
github.com/ethantkoenig/rupture v0.0.0-20181029165146-c3b3b810dc77/go.mod h1:MkKY/CB98aVE4VxO63X5vTQKUgcn+3XP15LMASe3lYs=
292293
github.com/facebookgo/ensure v0.0.0-20160127193407-b4ab57deab51 h1:0JZ+dUmQeA8IIVUMzysrX4/AKuQwWhV2dYQuPZdvdSQ=
293294
github.com/facebookgo/ensure v0.0.0-20160127193407-b4ab57deab51/go.mod h1:Yg+htXGokKKdzcwhuNDwVvN+uBxDGXJ7G/VN1d8fa64=
@@ -323,6 +324,8 @@ github.com/glycerine/goconvey v0.0.0-20190410193231-58a59202ab31 h1:gclg6gY70GLy
323324
github.com/glycerine/goconvey v0.0.0-20190410193231-58a59202ab31/go.mod h1:Ogl1Tioa0aV7gstGFO7KhffUsb9M4ydbEbbxpcEDc24=
324325
github.com/go-asn1-ber/asn1-ber v1.5.1 h1:pDbRAunXzIUXfx4CB2QJFv5IuPiuoW+sWvr/Us009o8=
325326
github.com/go-asn1-ber/asn1-ber v1.5.1/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0=
327+
github.com/go-chi/chi v1.5.0 h1:2ZcJZozJ+rj6BA0c19ffBUGXEKAT/aOLOtQjD46vBRA=
328+
github.com/go-chi/chi v1.5.0/go.mod h1:REp24E+25iKvxgeTfHmdUoL5x15kBiDBlnIl5bCwe2k=
326329
github.com/go-enry/go-enry/v2 v2.5.2 h1:3f3PFAO6JitWkPi1GQ5/m6Xu4gNL1U5soJ8QaYqJ0YQ=
327330
github.com/go-enry/go-enry/v2 v2.5.2/go.mod h1:GVzIiAytiS5uT/QiuakK7TF1u4xDab87Y8V5EJRpsIQ=
328331
github.com/go-enry/go-oniguruma v1.2.1 h1:k8aAMuJfMrqm/56SG2lV9Cfti6tC4x8673aHCcBk+eo=
@@ -655,6 +658,7 @@ github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0f
655658
github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
656659
github.com/jackc/puddle v1.1.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
657660
github.com/jarcoal/httpmock v0.0.0-20180424175123-9c70cfe4a1da/go.mod h1:ks+b9deReOc7jgqp+e7LuFiCBH6Rm5hL32cLcEAArb4=
661+
github.com/jaytaylor/html2text v0.0.0-20200412013138-3577fbdbcff7 h1:g0fAGBisHaEQ0TRq1iBvemFRf+8AEWEmBESSiWB3Vsc=
658662
github.com/jaytaylor/html2text v0.0.0-20200412013138-3577fbdbcff7/go.mod h1:CVKlgaMiht+LXvHG173ujK6JUhZXKb2u/BQtjPDIvyk=
659663
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
660664
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
@@ -684,6 +688,7 @@ github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7V
684688
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
685689
github.com/karrick/godirwalk v1.8.0/go.mod h1:H5KPZjojv4lE+QYImBI8xVtrBRgYrIVsaRPx4tDPEn4=
686690
github.com/karrick/godirwalk v1.10.3/go.mod h1:RoGL9dQei4vP9ilrpETWE8CLOZ1kiN0LhBygSwrAsHA=
691+
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
687692
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
688693
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd h1:Coekwdh0v2wtGp9Gmz1Ze3eVRAWJMLokvN3QjdzCHLY=
689694
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
@@ -1025,6 +1030,7 @@ github.com/spf13/viper v1.7.0 h1:xVKxvI7ouOI5I+U9s2eeiUfMaWBVoXA3AWskkrqK0VM=
10251030
github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
10261031
github.com/spf13/viper v1.7.1 h1:pM5oEahlgWv/WnHXpgbKz7iLIxRf65tye2Ci+XFK5sk=
10271032
github.com/spf13/viper v1.7.1/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
1033+
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf h1:pvbZ0lM0XWPBqUKqFU8cmavspvIl9nulOYwdy6IFRRo=
10281034
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf/go.mod h1:RJID2RhlZKId02nZ62WenDCkgHFerpIOmW0iT7GKmXM=
10291035
github.com/steveyen/gtreap v0.1.0 h1:CjhzTa274PyJLJuMZwIzCO1PfC00oRa8d1Kc78bFXJM=
10301036
github.com/steveyen/gtreap v0.1.0/go.mod h1:kl/5J7XbrOmlIbYIXdRHDDE5QxHqpk0cmkT7Z4dM9/Y=
@@ -1069,6 +1075,7 @@ github.com/unknwon/com v1.0.1/go.mod h1:tOOxU81rwgoCLoOVVPHb6T/wt8HZygqH5id+GNnl
10691075
github.com/unknwon/i18n v0.0.0-20190805065654-5c6446a380b6/go.mod h1:+5rDk6sDGpl3azws3O+f+GpFSyN9GVr0K8cvQLQM2ZQ=
10701076
github.com/unknwon/i18n v0.0.0-20200823051745-09abd91c7f2c h1:679/gJXwrsHC3RATr0YYjZvDMJPYN7W9FGSGNoLmKxM=
10711077
github.com/unknwon/i18n v0.0.0-20200823051745-09abd91c7f2c/go.mod h1:+5rDk6sDGpl3azws3O+f+GpFSyN9GVr0K8cvQLQM2ZQ=
1078+
github.com/unknwon/paginater v0.0.0-20200328080006-042474bd0eae h1:ihaXiJkaca54IaCSnEXtE/uSZOmPxKZhDfVLrzZLFDs=
10721079
github.com/unknwon/paginater v0.0.0-20200328080006-042474bd0eae/go.mod h1:1fdkY6xxl6ExVs2QFv7R0F5IRZHKA8RahhB9fMC9RvM=
10731080
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
10741081
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=

integrations/create_no_session_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ func TestSessionFileCreation(t *testing.T) {
5858
oldSessionConfig := setting.SessionConfig.ProviderConfig
5959
defer func() {
6060
setting.SessionConfig.ProviderConfig = oldSessionConfig
61-
mac = routes.NewMacaron()
62-
routes.RegisterRoutes(mac)
61+
c = routes.NewChi()
62+
routes.RegisterRoutes(c)
6363
}()
6464

6565
var config session.Options
@@ -83,8 +83,8 @@ func TestSessionFileCreation(t *testing.T) {
8383

8484
setting.SessionConfig.ProviderConfig = string(newConfigBytes)
8585

86-
mac = routes.NewMacaron()
87-
routes.RegisterRoutes(mac)
86+
c = routes.NewChi()
87+
routes.RegisterRoutes(c)
8888

8989
t.Run("NoSessionOnViewIssue", func(t *testing.T) {
9090
defer PrintCurrentTest(t)()

integrations/git_helper_for_declarative_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL), prepare ...bo
8282
defer prepareTestEnv(t, 1)()
8383
}
8484
s := http.Server{
85-
Handler: mac,
85+
Handler: c,
8686
}
8787

8888
u, err := url.Parse(setting.AppURL)

integrations/integration_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ import (
3434
"code.gitea.io/gitea/routers"
3535
"code.gitea.io/gitea/routers/routes"
3636

37-
"gitea.com/macaron/macaron"
3837
"github.com/PuerkitoBio/goquery"
38+
"github.com/go-chi/chi"
3939
"github.com/stretchr/testify/assert"
4040
"github.com/unknwon/com"
4141
)
4242

43-
var mac *macaron.Macaron
43+
var c chi.Router
4444

4545
type NilResponseRecorder struct {
4646
httptest.ResponseRecorder
@@ -67,8 +67,8 @@ func TestMain(m *testing.M) {
6767
defer cancel()
6868

6969
initIntegrationTest()
70-
mac = routes.NewMacaron()
71-
routes.RegisterRoutes(mac)
70+
c = routes.NewChi()
71+
routes.RegisterRoutes(c)
7272

7373
// integration test settings...
7474
if setting.Cfg != nil {
@@ -404,7 +404,7 @@ const NoExpectedStatus = -1
404404
func MakeRequest(t testing.TB, req *http.Request, expectedStatus int) *httptest.ResponseRecorder {
405405
t.Helper()
406406
recorder := httptest.NewRecorder()
407-
mac.ServeHTTP(recorder, req)
407+
c.ServeHTTP(recorder, req)
408408
if expectedStatus != NoExpectedStatus {
409409
if !assert.EqualValues(t, expectedStatus, recorder.Code,
410410
"Request: %s %s", req.Method, req.URL.String()) {
@@ -417,7 +417,7 @@ func MakeRequest(t testing.TB, req *http.Request, expectedStatus int) *httptest.
417417
func MakeRequestNilResponseRecorder(t testing.TB, req *http.Request, expectedStatus int) *NilResponseRecorder {
418418
t.Helper()
419419
recorder := NewNilResponseRecorder()
420-
mac.ServeHTTP(recorder, req)
420+
c.ServeHTTP(recorder, req)
421421
if expectedStatus != NoExpectedStatus {
422422
if !assert.EqualValues(t, expectedStatus, recorder.Code,
423423
"Request: %s %s", req.Method, req.URL.String()) {

integrations/oauth_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
package integrations
66

77
import (
8+
"bytes"
89
"encoding/json"
10+
"io/ioutil"
911
"testing"
1012

1113
"code.gitea.io/gitea/modules/setting"
@@ -233,11 +235,21 @@ func TestRefreshTokenInvalidation(t *testing.T) {
233235
"redirect_uri": "a",
234236
"refresh_token": parsed.RefreshToken,
235237
})
238+
// tip: Why this changed, because macaron will set req.Body back when consume the req but chi will not.
239+
bs, err := ioutil.ReadAll(refreshReq.Body)
240+
assert.NoError(t, err)
241+
242+
refreshReq.Body = ioutil.NopCloser(bytes.NewReader(bs))
236243
MakeRequest(t, refreshReq, 200)
244+
245+
refreshReq.Body = ioutil.NopCloser(bytes.NewReader(bs))
237246
MakeRequest(t, refreshReq, 200)
238247

239248
// test with invalidation
240249
setting.OAuth2.InvalidateRefreshTokens = true
250+
refreshReq.Body = ioutil.NopCloser(bytes.NewReader(bs))
241251
MakeRequest(t, refreshReq, 200)
252+
253+
refreshReq.Body = ioutil.NopCloser(bytes.NewReader(bs))
242254
MakeRequest(t, refreshReq, 400)
243255
}

modules/public/dynamic.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66

77
package public
88

9-
import (
10-
"gitea.com/macaron/macaron"
11-
)
9+
import "net/http"
1210

1311
// Static implements the macaron static handler for serving assets.
14-
func Static(opts *Options) macaron.Handler {
12+
func Static(opts *Options) func(next http.Handler) http.Handler {
1513
return opts.staticHandler(opts.Directory)
1614
}

modules/public/public.go

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import (
1515
"time"
1616

1717
"code.gitea.io/gitea/modules/setting"
18-
19-
"gitea.com/macaron/macaron"
2018
)
2119

2220
// Options represents the available options to configure the macaron handler.
@@ -41,7 +39,7 @@ var KnownPublicEntries = []string{
4139
}
4240

4341
// Custom implements the macaron static handler for serving custom assets.
44-
func Custom(opts *Options) macaron.Handler {
42+
func Custom(opts *Options) func(next http.Handler) http.Handler {
4543
return opts.staticHandler(path.Join(setting.CustomPath, "public"))
4644
}
4745

@@ -52,7 +50,7 @@ type staticFileSystem struct {
5250

5351
func newStaticFileSystem(directory string) staticFileSystem {
5452
if !filepath.IsAbs(directory) {
55-
directory = filepath.Join(macaron.Root, directory)
53+
directory = filepath.Join(setting.AppWorkPath, directory)
5654
}
5755
dir := http.Dir(directory)
5856
return staticFileSystem{&dir}
@@ -63,39 +61,43 @@ func (fs staticFileSystem) Open(name string) (http.File, error) {
6361
}
6462

6563
// StaticHandler sets up a new middleware for serving static files in the
66-
func StaticHandler(dir string, opts *Options) macaron.Handler {
64+
func StaticHandler(dir string, opts *Options) func(next http.Handler) http.Handler {
6765
return opts.staticHandler(dir)
6866
}
6967

70-
func (opts *Options) staticHandler(dir string) macaron.Handler {
71-
// Defaults
72-
if len(opts.IndexFile) == 0 {
73-
opts.IndexFile = "index.html"
74-
}
75-
// Normalize the prefix if provided
76-
if opts.Prefix != "" {
77-
// Ensure we have a leading '/'
78-
if opts.Prefix[0] != '/' {
79-
opts.Prefix = "/" + opts.Prefix
68+
func (opts *Options) staticHandler(dir string) func(next http.Handler) http.Handler {
69+
return func(next http.Handler) http.Handler {
70+
// Defaults
71+
if len(opts.IndexFile) == 0 {
72+
opts.IndexFile = "index.html"
73+
}
74+
// Normalize the prefix if provided
75+
if opts.Prefix != "" {
76+
// Ensure we have a leading '/'
77+
if opts.Prefix[0] != '/' {
78+
opts.Prefix = "/" + opts.Prefix
79+
}
80+
// Remove any trailing '/'
81+
opts.Prefix = strings.TrimRight(opts.Prefix, "/")
82+
}
83+
if opts.FileSystem == nil {
84+
opts.FileSystem = newStaticFileSystem(dir)
8085
}
81-
// Remove any trailing '/'
82-
opts.Prefix = strings.TrimRight(opts.Prefix, "/")
83-
}
84-
if opts.FileSystem == nil {
85-
opts.FileSystem = newStaticFileSystem(dir)
86-
}
8786

88-
return func(ctx *macaron.Context, log *log.Logger) {
89-
opts.handle(ctx, log, opts)
87+
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
88+
if !opts.handle(w, req, opts) {
89+
next.ServeHTTP(w, req)
90+
}
91+
})
9092
}
9193
}
9294

93-
func (opts *Options) handle(ctx *macaron.Context, log *log.Logger, opt *Options) bool {
94-
if ctx.Req.Method != "GET" && ctx.Req.Method != "HEAD" {
95+
func (opts *Options) handle(w http.ResponseWriter, req *http.Request, opt *Options) bool {
96+
if req.Method != "GET" && req.Method != "HEAD" {
9597
return false
9698
}
9799

98-
file := ctx.Req.URL.Path
100+
file := req.URL.Path
99101
// if we have a prefix, filter requests by stripping the prefix
100102
if opt.Prefix != "" {
101103
if !strings.HasPrefix(file, opt.Prefix) {
@@ -117,7 +119,7 @@ func (opts *Options) handle(ctx *macaron.Context, log *log.Logger, opt *Options)
117119
}
118120
for _, entry := range KnownPublicEntries {
119121
if entry == parts[1] {
120-
ctx.Resp.WriteHeader(404)
122+
w.WriteHeader(404)
121123
return true
122124
}
123125
}
@@ -135,8 +137,8 @@ func (opts *Options) handle(ctx *macaron.Context, log *log.Logger, opt *Options)
135137
// Try to serve index file
136138
if fi.IsDir() {
137139
// Redirect if missing trailing slash.
138-
if !strings.HasSuffix(ctx.Req.URL.Path, "/") {
139-
http.Redirect(ctx.Resp, ctx.Req.Request, path.Clean(ctx.Req.URL.Path+"/"), http.StatusFound)
140+
if !strings.HasSuffix(req.URL.Path, "/") {
141+
http.Redirect(w, req, path.Clean(req.URL.Path+"/"), http.StatusFound)
140142
return true
141143
}
142144

@@ -148,7 +150,7 @@ func (opts *Options) handle(ctx *macaron.Context, log *log.Logger, opt *Options)
148150

149151
fi, err = f.Stat()
150152
if err != nil || fi.IsDir() {
151-
return true
153+
return false
152154
}
153155
}
154156

@@ -158,16 +160,16 @@ func (opts *Options) handle(ctx *macaron.Context, log *log.Logger, opt *Options)
158160

159161
// Add an Expires header to the static content
160162
if opt.ExpiresAfter > 0 {
161-
ctx.Resp.Header().Set("Expires", time.Now().Add(opt.ExpiresAfter).UTC().Format(http.TimeFormat))
163+
w.Header().Set("Expires", time.Now().Add(opt.ExpiresAfter).UTC().Format(http.TimeFormat))
162164
tag := GenerateETag(fmt.Sprint(fi.Size()), fi.Name(), fi.ModTime().UTC().Format(http.TimeFormat))
163-
ctx.Resp.Header().Set("ETag", tag)
164-
if ctx.Req.Header.Get("If-None-Match") == tag {
165-
ctx.Resp.WriteHeader(304)
166-
return false
165+
w.Header().Set("ETag", tag)
166+
if req.Header.Get("If-None-Match") == tag {
167+
w.WriteHeader(304)
168+
return true
167169
}
168170
}
169171

170-
http.ServeContent(ctx.Resp, ctx.Req.Request, file, fi.ModTime(), f)
172+
http.ServeContent(w, req, file, fi.ModTime(), f)
171173
return true
172174
}
173175

0 commit comments

Comments
 (0)