Skip to content

Commit 5aead51

Browse files
committed
experiment to convert macaron to chi
1 parent 877df0f commit 5aead51

Some content is hidden

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

44 files changed

+3997
-176
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ issues:
6767
- path: modules/log/
6868
linters:
6969
- errcheck
70-
- path: routers/routes/routes.go
70+
- path: routers/routes/macaron.go
7171
linters:
7272
- dupl
7373
- path: routers/repo/view.go

cmd/web.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ func runWeb(ctx *cli.Context) error {
107107

108108
routers.GlobalInit()
109109

110-
m := routes.NewMacaron()
111-
routes.RegisterRoutes(m)
110+
c := routes.NewChi()
111+
routes.RegisterRoutes(c)
112112

113113
// Flag for port number in case first time run conflict.
114114
if ctx.IsSet("port") {
@@ -164,16 +164,16 @@ func runWeb(ctx *cli.Context) error {
164164
var err error
165165
switch setting.Protocol {
166166
case setting.HTTP:
167-
err = runHTTP(listenAddr, context2.ClearHandler(m))
167+
err = runHTTP(listenAddr, context2.ClearHandler(c))
168168
case setting.HTTPS:
169169
if setting.EnableLetsEncrypt {
170-
err = runLetsEncrypt(listenAddr, setting.Domain, setting.LetsEncryptDirectory, setting.LetsEncryptEmail, context2.ClearHandler(m))
170+
err = runLetsEncrypt(listenAddr, setting.Domain, setting.LetsEncryptDirectory, setting.LetsEncryptEmail, context2.ClearHandler(c))
171171
break
172172
}
173173
if setting.RedirectOtherPort {
174174
go runHTTPRedirector()
175175
}
176-
err = runHTTPS(listenAddr, setting.CertFile, setting.KeyFile, context2.ClearHandler(m))
176+
err = runHTTPS(listenAddr, setting.CertFile, setting.KeyFile, context2.ClearHandler(c))
177177
case setting.FCGI:
178178
var listener net.Listener
179179
listener, err = net.Listen("tcp", listenAddr)
@@ -185,7 +185,7 @@ func runWeb(ctx *cli.Context) error {
185185
log.Fatal("Failed to stop server: %v", err)
186186
}
187187
}()
188-
err = fcgi.Serve(listener, context2.ClearHandler(m))
188+
err = fcgi.Serve(listener, context2.ClearHandler(c))
189189
case setting.UnixSocket:
190190
if err := os.Remove(listenAddr); err != nil && !os.IsNotExist(err) {
191191
log.Fatal("Failed to remove unix socket directory %s: %v", listenAddr, err)
@@ -201,7 +201,7 @@ func runWeb(ctx *cli.Context) error {
201201
if err = os.Chmod(listenAddr, os.FileMode(setting.UnixSocketPermission)); err != nil {
202202
log.Fatal("Failed to set permission of unix socket: %v", err)
203203
}
204-
err = http.Serve(listener, context2.ClearHandler(m))
204+
err = http.Serve(listener, context2.ClearHandler(c))
205205
default:
206206
log.Fatal("Invalid protocol: %s", setting.Protocol)
207207
}

contrib/pr/checkout.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ func runPR() {
114114
//routers.GlobalInit()
115115
external.RegisterParsers()
116116
markup.Init()
117-
m := routes.NewMacaron()
118-
routes.RegisterRoutes(m)
117+
c := routes.NewChi()
118+
routes.RegisterRoutes(c)
119119

120120
log.Printf("[PR] Ready for testing !\n")
121121
log.Printf("[PR] Login with user1, user2, user3, ... with pass: password\n")
@@ -135,7 +135,7 @@ func runPR() {
135135
*/
136136

137137
//Start the server
138-
http.ListenAndServe(":8080", context2.ClearHandler(m))
138+
http.ListenAndServe(":8080", context2.ClearHandler(c))
139139

140140
log.Printf("[PR] Cleaning up ...\n")
141141
/*

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ require (
4343
github.com/gliderlabs/ssh v0.2.2
4444
github.com/glycerine/go-unsnap-stream v0.0.0-20180323001048-9f0cb55181dd // indirect
4545
github.com/glycerine/goconvey v0.0.0-20190315024820-982ee783a72e // indirect
46+
github.com/go-chi/chi v4.0.2+incompatible
4647
github.com/go-macaron/binding v0.0.0-20160711225916-9440f336b443
4748
github.com/go-macaron/cache v0.0.0-20151013081102-561735312776
4849
github.com/go-macaron/captcha v0.0.0-20151123225153-8aa5919789ab

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ github.com/glycerine/go-unsnap-stream v0.0.0-20180323001048-9f0cb55181dd h1:r04M
106106
github.com/glycerine/go-unsnap-stream v0.0.0-20180323001048-9f0cb55181dd/go.mod h1:/20jfyN9Y5QPEAprSgKAUr+glWDY39ZiUEAYOEv5dsE=
107107
github.com/glycerine/goconvey v0.0.0-20190315024820-982ee783a72e h1:SiEs4J3BKVIeaWrH3tKaz3QLZhJ68iJ/A4xrzIoE5+Y=
108108
github.com/glycerine/goconvey v0.0.0-20190315024820-982ee783a72e/go.mod h1:Ogl1Tioa0aV7gstGFO7KhffUsb9M4ydbEbbxpcEDc24=
109+
github.com/go-chi/chi v4.0.2+incompatible h1:maB6vn6FqCxrpz4FqWdh4+lwpyZIQS7YEAUcHlgXVRs=
110+
github.com/go-chi/chi v4.0.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
109111
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
110112
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
111113
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=

integrations/create_no_session_test.go

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

6464
var config session.Options
@@ -82,8 +82,8 @@ func TestSessionFileCreation(t *testing.T) {
8282

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

85-
mac = routes.NewMacaron()
86-
routes.RegisterRoutes(mac)
85+
c = routes.NewChi()
86+
routes.RegisterRoutes(c)
8787

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

integrations/git_helper_for_declarative_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func createSSHUrl(gitPath string, u *url.URL) *url.URL {
5252
func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL)) {
5353
prepareTestEnv(t, 1)
5454
s := http.Server{
55-
Handler: mac,
55+
Handler: c,
5656
}
5757

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

integrations/integration_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434
"gopkg.in/testfixtures.v2"
3535
)
3636

37-
var mac *macaron.Macaron
37+
var c chi.Router
3838

3939
type NilResponseRecorder struct {
4040
httptest.ResponseRecorder
@@ -55,8 +55,8 @@ func NewNilResponseRecorder() *NilResponseRecorder {
5555

5656
func TestMain(m *testing.M) {
5757
initIntegrationTest()
58-
mac = routes.NewMacaron()
59-
routes.RegisterRoutes(mac)
58+
c = routes.NewChi()
59+
routes.RegisterRoutes(c)
6060

6161
var helper testfixtures.Helper
6262
if setting.UseMySQL {
@@ -335,7 +335,7 @@ const NoExpectedStatus = -1
335335

336336
func MakeRequest(t testing.TB, req *http.Request, expectedStatus int) *httptest.ResponseRecorder {
337337
recorder := httptest.NewRecorder()
338-
mac.ServeHTTP(recorder, req)
338+
c.ServeHTTP(recorder, req)
339339
if expectedStatus != NoExpectedStatus {
340340
if !assert.EqualValues(t, expectedStatus, recorder.Code,
341341
"Request: %s %s", req.Method, req.URL.String()) {
@@ -347,7 +347,7 @@ func MakeRequest(t testing.TB, req *http.Request, expectedStatus int) *httptest.
347347

348348
func MakeRequestNilResponseRecorder(t testing.TB, req *http.Request, expectedStatus int) *NilResponseRecorder {
349349
recorder := NewNilResponseRecorder()
350-
mac.ServeHTTP(recorder, req)
350+
c.ServeHTTP(recorder, req)
351351
if expectedStatus != NoExpectedStatus {
352352
if !assert.EqualValues(t, expectedStatus, recorder.Code,
353353
"Request: %s %s", req.Method, req.URL.String()) {

modules/public/dynamic.go

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

77
package public
88

9-
import (
10-
"gopkg.in/macaron.v1"
11-
)
9+
import "net/http"
1210

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

modules/public/public.go

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
2423
type 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

4546
func 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

modules/public/static.go

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

77
package public
88

9-
import (
10-
"gopkg.in/macaron.v1"
11-
)
9+
import "net/http"
1210

13-
// Static implements the macaron static handler for serving assets.
14-
func Static(opts *Options) macaron.Handler {
15-
opts.FileSystem = Assets
16-
// we don't need to pass the directory, because the directory var is only
17-
// used when in the options there is no FileSystem.
18-
return opts.staticHandler("")
11+
// Static implements the http static handler for serving assets.
12+
func Static(opts *Options) func(next http.Handler) http.Handler {
13+
return func(next http.Handler) http.Handler {
14+
opts.FileSystem = Assets
15+
// we don't need to pass the directory, because the directory var is only
16+
// used when in the options there is no FileSystem.
17+
return opts.staticHandler("")
18+
}
1919
}

0 commit comments

Comments
 (0)