1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-11-25 08:59:31 -05:00

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.
This commit is contained in:
Lunny Xiao 2020-11-16 15:33:41 +08:00 committed by GitHub
parent 8c2b5feeae
commit 586bfb9f32
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 32 additions and 16 deletions

View file

@ -165,9 +165,10 @@ func runWeb(ctx *cli.Context) error {
return err return err
} }
} }
// Set up Macaron // Set up Chi routes
c := routes.NewChi() c := routes.NewChi()
routes.RegisterRoutes(c) c.Mount("/", routes.NormalRoutes())
routes.DelegateToMacaron(c)
err := listen(c, true) err := listen(c, true)
<-graceful.GetManager().Done() <-graceful.GetManager().Done()

View file

@ -118,7 +118,8 @@ func runPR() {
external.RegisterParsers() external.RegisterParsers()
markup.Init() markup.Init()
c := routes.NewChi() c := routes.NewChi()
routes.RegisterRoutes(c) c.Mount("/", routes.NormalRoutes())
routes.DelegateToMacaron(c)
log.Printf("[PR] Ready for testing !\n") log.Printf("[PR] Ready for testing !\n")
log.Printf("[PR] Login with user1, user2, user3, ... with pass: password\n") log.Printf("[PR] Login with user1, user2, user3, ... with pass: password\n")

View file

@ -59,7 +59,8 @@ func TestSessionFileCreation(t *testing.T) {
defer func() { defer func() {
setting.SessionConfig.ProviderConfig = oldSessionConfig setting.SessionConfig.ProviderConfig = oldSessionConfig
c = routes.NewChi() c = routes.NewChi()
routes.RegisterRoutes(c) c.Mount("/", routes.NormalRoutes())
routes.DelegateToMacaron(c)
}() }()
var config session.Options var config session.Options
@ -84,7 +85,8 @@ func TestSessionFileCreation(t *testing.T) {
setting.SessionConfig.ProviderConfig = string(newConfigBytes) setting.SessionConfig.ProviderConfig = string(newConfigBytes)
c = routes.NewChi() c = routes.NewChi()
routes.RegisterRoutes(c) c.Mount("/", routes.NormalRoutes())
routes.DelegateToMacaron(c)
t.Run("NoSessionOnViewIssue", func(t *testing.T) { t.Run("NoSessionOnViewIssue", func(t *testing.T) {
defer PrintCurrentTest(t)() defer PrintCurrentTest(t)()

View file

@ -68,7 +68,8 @@ func TestMain(m *testing.M) {
initIntegrationTest() initIntegrationTest()
c = routes.NewChi() c = routes.NewChi()
routes.RegisterRoutes(c) c.Mount("/", routes.NormalRoutes())
routes.DelegateToMacaron(c)
// integration test settings... // integration test settings...
if setting.Cfg != nil { if setting.Cfg != nil {

View file

@ -58,18 +58,20 @@ func Install(ctx *context.Context) {
form.DbSchema = setting.Database.Schema form.DbSchema = setting.Database.Schema
form.Charset = setting.Database.Charset form.Charset = setting.Database.Charset
ctx.Data["CurDbOption"] = "MySQL" var curDBOption = "MySQL"
switch setting.Database.Type { switch setting.Database.Type {
case "postgres": case "postgres":
ctx.Data["CurDbOption"] = "PostgreSQL" curDBOption = "PostgreSQL"
case "mssql": case "mssql":
ctx.Data["CurDbOption"] = "MSSQL" curDBOption = "MSSQL"
case "sqlite3": case "sqlite3":
if setting.EnableSQLite3 { if setting.EnableSQLite3 {
ctx.Data["CurDbOption"] = "SQLite3" curDBOption = "SQLite3"
} }
} }
ctx.Data["CurDbOption"] = curDBOption
// Application general settings // Application general settings
form.AppName = setting.AppName form.AppName = setting.AppName
form.RepoRootPath = setting.RepoRootPath form.RepoRootPath = setting.RepoRootPath

View file

@ -186,6 +186,7 @@ func storageHandler(storageSetting setting.Storage, prefix string, objStore stor
// NewChi creates a chi Router // NewChi creates a chi Router
func NewChi() chi.Router { func NewChi() chi.Router {
c := chi.NewRouter() c := chi.NewRouter()
c.Use(middleware.RealIP)
if !setting.DisableRouterLog && setting.RouterLogLevel != log.NONE { if !setting.DisableRouterLog && setting.RouterLogLevel != log.NONE {
if log.GetLogger("router").GetLevel() <= setting.RouterLogLevel { if log.GetLogger("router").GetLevel() <= setting.RouterLogLevel {
c.Use(LoggerHandler(setting.RouterLogLevel)) c.Use(LoggerHandler(setting.RouterLogLevel))
@ -195,6 +196,7 @@ func NewChi() chi.Router {
if setting.EnableAccessLog { if setting.EnableAccessLog {
setupAccessLogger(c) setupAccessLogger(c)
} }
if setting.ProdMode { if setting.ProdMode {
log.Warn("ProdMode ignored") log.Warn("ProdMode ignored")
} }
@ -233,28 +235,35 @@ func RegisterInstallRoute(c chi.Router) {
}) })
} }
// RegisterRoutes registers gin routes // NormalRoutes represents non install routes
func RegisterRoutes(c chi.Router) { func NormalRoutes() http.Handler {
r := chi.NewRouter()
// for health check // 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) w.WriteHeader(http.StatusOK)
}) })
// robots.txt // robots.txt
if setting.HasRobotsTxt { 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")) 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() m := NewMacaron()
RegisterMacaronRoutes(m) RegisterMacaronRoutes(m)
c.NotFound(func(w http.ResponseWriter, req *http.Request) { r.NotFound(func(w http.ResponseWriter, req *http.Request) {
m.ServeHTTP(w, req) 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) m.ServeHTTP(w, req)
}) })
} }