Skip to content

Commit 586bfb9

Browse files
authored
Use mount but not register for chi routes (#13555)
* Use mount but not register for chi routes * Fix test * Fix test * Fix test * Fix comment * turn back unnecessary change * Remove the timout middleware since some operations may spend much time.
1 parent 8c2b5fe commit 586bfb9

File tree

6 files changed

+32
-16
lines changed

6 files changed

+32
-16
lines changed

cmd/web.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,10 @@ func runWeb(ctx *cli.Context) error {
165165
return err
166166
}
167167
}
168-
// Set up Macaron
168+
// Set up Chi routes
169169
c := routes.NewChi()
170-
routes.RegisterRoutes(c)
170+
c.Mount("/", routes.NormalRoutes())
171+
routes.DelegateToMacaron(c)
171172

172173
err := listen(c, true)
173174
<-graceful.GetManager().Done()

contrib/pr/checkout.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ func runPR() {
118118
external.RegisterParsers()
119119
markup.Init()
120120
c := routes.NewChi()
121-
routes.RegisterRoutes(c)
121+
c.Mount("/", routes.NormalRoutes())
122+
routes.DelegateToMacaron(c)
122123

123124
log.Printf("[PR] Ready for testing !\n")
124125
log.Printf("[PR] Login with user1, user2, user3, ... with pass: password\n")

integrations/create_no_session_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ func TestSessionFileCreation(t *testing.T) {
5959
defer func() {
6060
setting.SessionConfig.ProviderConfig = oldSessionConfig
6161
c = routes.NewChi()
62-
routes.RegisterRoutes(c)
62+
c.Mount("/", routes.NormalRoutes())
63+
routes.DelegateToMacaron(c)
6364
}()
6465

6566
var config session.Options
@@ -84,7 +85,8 @@ func TestSessionFileCreation(t *testing.T) {
8485
setting.SessionConfig.ProviderConfig = string(newConfigBytes)
8586

8687
c = routes.NewChi()
87-
routes.RegisterRoutes(c)
88+
c.Mount("/", routes.NormalRoutes())
89+
routes.DelegateToMacaron(c)
8890

8991
t.Run("NoSessionOnViewIssue", func(t *testing.T) {
9092
defer PrintCurrentTest(t)()

integrations/integration_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ func TestMain(m *testing.M) {
6868

6969
initIntegrationTest()
7070
c = routes.NewChi()
71-
routes.RegisterRoutes(c)
71+
c.Mount("/", routes.NormalRoutes())
72+
routes.DelegateToMacaron(c)
7273

7374
// integration test settings...
7475
if setting.Cfg != nil {

routers/install.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,20 @@ func Install(ctx *context.Context) {
5858
form.DbSchema = setting.Database.Schema
5959
form.Charset = setting.Database.Charset
6060

61-
ctx.Data["CurDbOption"] = "MySQL"
61+
var curDBOption = "MySQL"
6262
switch setting.Database.Type {
6363
case "postgres":
64-
ctx.Data["CurDbOption"] = "PostgreSQL"
64+
curDBOption = "PostgreSQL"
6565
case "mssql":
66-
ctx.Data["CurDbOption"] = "MSSQL"
66+
curDBOption = "MSSQL"
6767
case "sqlite3":
6868
if setting.EnableSQLite3 {
69-
ctx.Data["CurDbOption"] = "SQLite3"
69+
curDBOption = "SQLite3"
7070
}
7171
}
7272

73+
ctx.Data["CurDbOption"] = curDBOption
74+
7375
// Application general settings
7476
form.AppName = setting.AppName
7577
form.RepoRootPath = setting.RepoRootPath

routers/routes/chi.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ func storageHandler(storageSetting setting.Storage, prefix string, objStore stor
186186
// NewChi creates a chi Router
187187
func NewChi() chi.Router {
188188
c := chi.NewRouter()
189+
c.Use(middleware.RealIP)
189190
if !setting.DisableRouterLog && setting.RouterLogLevel != log.NONE {
190191
if log.GetLogger("router").GetLevel() <= setting.RouterLogLevel {
191192
c.Use(LoggerHandler(setting.RouterLogLevel))
@@ -195,6 +196,7 @@ func NewChi() chi.Router {
195196
if setting.EnableAccessLog {
196197
setupAccessLogger(c)
197198
}
199+
198200
if setting.ProdMode {
199201
log.Warn("ProdMode ignored")
200202
}
@@ -233,28 +235,35 @@ func RegisterInstallRoute(c chi.Router) {
233235
})
234236
}
235237

236-
// RegisterRoutes registers gin routes
237-
func RegisterRoutes(c chi.Router) {
238+
// NormalRoutes represents non install routes
239+
func NormalRoutes() http.Handler {
240+
r := chi.NewRouter()
241+
238242
// for health check
239-
c.Head("/", func(w http.ResponseWriter, req *http.Request) {
243+
r.Head("/", func(w http.ResponseWriter, req *http.Request) {
240244
w.WriteHeader(http.StatusOK)
241245
})
242246

243247
// robots.txt
244248
if setting.HasRobotsTxt {
245-
c.Get("/robots.txt", func(w http.ResponseWriter, req *http.Request) {
249+
r.Get("/robots.txt", func(w http.ResponseWriter, req *http.Request) {
246250
http.ServeFile(w, req, path.Join(setting.CustomPath, "robots.txt"))
247251
})
248252
}
249253

254+
return r
255+
}
256+
257+
// DelegateToMacaron delegates other routes to macaron
258+
func DelegateToMacaron(r chi.Router) {
250259
m := NewMacaron()
251260
RegisterMacaronRoutes(m)
252261

253-
c.NotFound(func(w http.ResponseWriter, req *http.Request) {
262+
r.NotFound(func(w http.ResponseWriter, req *http.Request) {
254263
m.ServeHTTP(w, req)
255264
})
256265

257-
c.MethodNotAllowed(func(w http.ResponseWriter, req *http.Request) {
266+
r.MethodNotAllowed(func(w http.ResponseWriter, req *http.Request) {
258267
m.ServeHTTP(w, req)
259268
})
260269
}

0 commit comments

Comments
 (0)