]> source.dussan.org Git - gitea.git/commitdiff
Use mount but not register for chi routes (#13555)
authorLunny Xiao <xiaolunwen@gmail.com>
Mon, 16 Nov 2020 07:33:41 +0000 (15:33 +0800)
committerGitHub <noreply@github.com>
Mon, 16 Nov 2020 07:33:41 +0000 (15:33 +0800)
* 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.

cmd/web.go
contrib/pr/checkout.go
integrations/create_no_session_test.go
integrations/integration_test.go
routers/install.go
routers/routes/chi.go

index 7dcaee306c0731bc26a9402d0381185fd37d0d95..47dbc2675e24db33ab05b05a7f1dfc365290c37f 100644 (file)
@@ -165,9 +165,10 @@ func runWeb(ctx *cli.Context) error {
                        return err
                }
        }
-       // Set up Macaron
+       // Set up Chi routes
        c := routes.NewChi()
-       routes.RegisterRoutes(c)
+       c.Mount("/", routes.NormalRoutes())
+       routes.DelegateToMacaron(c)
 
        err := listen(c, true)
        <-graceful.GetManager().Done()
index 7a56b61fe39dcc36bdc8ec5df023a0a46918d243..528838082560d253c42a2397fea7549b83fb9f9a 100644 (file)
@@ -118,7 +118,8 @@ func runPR() {
        external.RegisterParsers()
        markup.Init()
        c := routes.NewChi()
-       routes.RegisterRoutes(c)
+       c.Mount("/", routes.NormalRoutes())
+       routes.DelegateToMacaron(c)
 
        log.Printf("[PR] Ready for testing !\n")
        log.Printf("[PR] Login with user1, user2, user3, ... with pass: password\n")
index 671c6cd51726045e5ef4a94f33052a2012ddf59e..ae0d9f8120084865648f3f1a67b3bcf2706b35d9 100644 (file)
@@ -59,7 +59,8 @@ func TestSessionFileCreation(t *testing.T) {
        defer func() {
                setting.SessionConfig.ProviderConfig = oldSessionConfig
                c = routes.NewChi()
-               routes.RegisterRoutes(c)
+               c.Mount("/", routes.NormalRoutes())
+               routes.DelegateToMacaron(c)
        }()
 
        var config session.Options
@@ -84,7 +85,8 @@ func TestSessionFileCreation(t *testing.T) {
        setting.SessionConfig.ProviderConfig = string(newConfigBytes)
 
        c = routes.NewChi()
-       routes.RegisterRoutes(c)
+       c.Mount("/", routes.NormalRoutes())
+       routes.DelegateToMacaron(c)
 
        t.Run("NoSessionOnViewIssue", func(t *testing.T) {
                defer PrintCurrentTest(t)()
index 1e42fb53335fe846d504cd6f98bf3a8fc33a5ccd..00aba15603b0bd6aa6cfc34cb6ddc75b8735e8c1 100644 (file)
@@ -68,7 +68,8 @@ func TestMain(m *testing.M) {
 
        initIntegrationTest()
        c = routes.NewChi()
-       routes.RegisterRoutes(c)
+       c.Mount("/", routes.NormalRoutes())
+       routes.DelegateToMacaron(c)
 
        // integration test settings...
        if setting.Cfg != nil {
index 5d0d089dc08b8f1701fef05664038336cbcd3293..4dd934aa04f9f3b92181919ab21dd2159370becd 100644 (file)
@@ -58,18 +58,20 @@ func Install(ctx *context.Context) {
        form.DbSchema = setting.Database.Schema
        form.Charset = setting.Database.Charset
 
-       ctx.Data["CurDbOption"] = "MySQL"
+       var curDBOption = "MySQL"
        switch setting.Database.Type {
        case "postgres":
-               ctx.Data["CurDbOption"] = "PostgreSQL"
+               curDBOption = "PostgreSQL"
        case "mssql":
-               ctx.Data["CurDbOption"] = "MSSQL"
+               curDBOption = "MSSQL"
        case "sqlite3":
                if setting.EnableSQLite3 {
-                       ctx.Data["CurDbOption"] = "SQLite3"
+                       curDBOption = "SQLite3"
                }
        }
 
+       ctx.Data["CurDbOption"] = curDBOption
+
        // Application general settings
        form.AppName = setting.AppName
        form.RepoRootPath = setting.RepoRootPath
index c78fe96ae44ac019d831c9779f7a520503dc0933..1165040778d02d03ecba0ea869a39dbfe33d6718 100644 (file)
@@ -186,6 +186,7 @@ func storageHandler(storageSetting setting.Storage, prefix string, objStore stor
 // NewChi creates a chi Router
 func NewChi() chi.Router {
        c := chi.NewRouter()
+       c.Use(middleware.RealIP)
        if !setting.DisableRouterLog && setting.RouterLogLevel != log.NONE {
                if log.GetLogger("router").GetLevel() <= setting.RouterLogLevel {
                        c.Use(LoggerHandler(setting.RouterLogLevel))
@@ -195,6 +196,7 @@ func NewChi() chi.Router {
        if setting.EnableAccessLog {
                setupAccessLogger(c)
        }
+
        if setting.ProdMode {
                log.Warn("ProdMode ignored")
        }
@@ -233,28 +235,35 @@ func RegisterInstallRoute(c chi.Router) {
        })
 }
 
-// RegisterRoutes registers gin routes
-func RegisterRoutes(c chi.Router) {
+// NormalRoutes represents non install routes
+func NormalRoutes() http.Handler {
+       r := chi.NewRouter()
+
        // for health check
-       c.Head("/", func(w http.ResponseWriter, req *http.Request) {
+       r.Head("/", func(w http.ResponseWriter, req *http.Request) {
                w.WriteHeader(http.StatusOK)
        })
 
        // robots.txt
        if setting.HasRobotsTxt {
-               c.Get("/robots.txt", func(w http.ResponseWriter, req *http.Request) {
+               r.Get("/robots.txt", func(w http.ResponseWriter, req *http.Request) {
                        http.ServeFile(w, req, path.Join(setting.CustomPath, "robots.txt"))
                })
        }
 
+       return r
+}
+
+// DelegateToMacaron delegates other routes to macaron
+func DelegateToMacaron(r chi.Router) {
        m := NewMacaron()
        RegisterMacaronRoutes(m)
 
-       c.NotFound(func(w http.ResponseWriter, req *http.Request) {
+       r.NotFound(func(w http.ResponseWriter, req *http.Request) {
                m.ServeHTTP(w, req)
        })
 
-       c.MethodNotAllowed(func(w http.ResponseWriter, req *http.Request) {
+       r.MethodNotAllowed(func(w http.ResponseWriter, req *http.Request) {
                m.ServeHTTP(w, req)
        })
 }