forked from forgejo/forgejo
Refactor routers directory (#15800)
* refactor routers directory * move func used for web and api to common * make corsHandler a function to prohibit side efects * rm unused func Co-authored-by: 6543 <6543@obermui.de>
This commit is contained in:
parent
e03a91a48e
commit
1bfb0a24d8
107 changed files with 940 additions and 800 deletions
|
@ -6,12 +6,9 @@ package routers
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/models/migrations"
|
||||
"code.gitea.io/gitea/modules/auth/sso"
|
||||
"code.gitea.io/gitea/modules/cache"
|
||||
"code.gitea.io/gitea/modules/cron"
|
||||
|
@ -32,6 +29,11 @@ import (
|
|||
"code.gitea.io/gitea/modules/svg"
|
||||
"code.gitea.io/gitea/modules/task"
|
||||
"code.gitea.io/gitea/modules/translation"
|
||||
"code.gitea.io/gitea/modules/web"
|
||||
apiv1 "code.gitea.io/gitea/routers/api/v1"
|
||||
"code.gitea.io/gitea/routers/common"
|
||||
"code.gitea.io/gitea/routers/private"
|
||||
web_routers "code.gitea.io/gitea/routers/web"
|
||||
"code.gitea.io/gitea/services/mailer"
|
||||
mirror_service "code.gitea.io/gitea/services/mirror"
|
||||
pull_service "code.gitea.io/gitea/services/pull"
|
||||
|
@ -63,63 +65,6 @@ func NewServices() {
|
|||
notification.NewContext()
|
||||
}
|
||||
|
||||
// In case of problems connecting to DB, retry connection. Eg, PGSQL in Docker Container on Synology
|
||||
func initDBEngine(ctx context.Context) (err error) {
|
||||
log.Info("Beginning ORM engine initialization.")
|
||||
for i := 0; i < setting.Database.DBConnectRetries; i++ {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return fmt.Errorf("Aborted due to shutdown:\nin retry ORM engine initialization")
|
||||
default:
|
||||
}
|
||||
log.Info("ORM engine initialization attempt #%d/%d...", i+1, setting.Database.DBConnectRetries)
|
||||
if err = models.NewEngine(ctx, migrations.Migrate); err == nil {
|
||||
break
|
||||
} else if i == setting.Database.DBConnectRetries-1 {
|
||||
return err
|
||||
}
|
||||
log.Error("ORM engine initialization attempt #%d/%d failed. Error: %v", i+1, setting.Database.DBConnectRetries, err)
|
||||
log.Info("Backing off for %d seconds", int64(setting.Database.DBConnectBackoff/time.Second))
|
||||
time.Sleep(setting.Database.DBConnectBackoff)
|
||||
}
|
||||
models.HasEngine = true
|
||||
return nil
|
||||
}
|
||||
|
||||
// PreInstallInit preloads the configuration to check if we need to run install
|
||||
func PreInstallInit(ctx context.Context) bool {
|
||||
setting.NewContext()
|
||||
if !setting.InstallLock {
|
||||
log.Trace("AppPath: %s", setting.AppPath)
|
||||
log.Trace("AppWorkPath: %s", setting.AppWorkPath)
|
||||
log.Trace("Custom path: %s", setting.CustomPath)
|
||||
log.Trace("Log path: %s", setting.LogRootPath)
|
||||
log.Trace("Preparing to run install page")
|
||||
translation.InitLocales()
|
||||
if setting.EnableSQLite3 {
|
||||
log.Info("SQLite3 Supported")
|
||||
}
|
||||
setting.InitDBConfig()
|
||||
svg.Init()
|
||||
}
|
||||
|
||||
return !setting.InstallLock
|
||||
}
|
||||
|
||||
// PostInstallInit rereads the settings and starts up the database
|
||||
func PostInstallInit(ctx context.Context) {
|
||||
setting.NewContext()
|
||||
setting.InitDBConfig()
|
||||
if setting.InstallLock {
|
||||
if err := initDBEngine(ctx); err == nil {
|
||||
log.Info("ORM engine initialization successful!")
|
||||
} else {
|
||||
log.Fatal("ORM engine initialization failed: %v", err)
|
||||
}
|
||||
svg.Init()
|
||||
}
|
||||
}
|
||||
|
||||
// GlobalInit is for global configuration reload-able.
|
||||
func GlobalInit(ctx context.Context) {
|
||||
setting.NewContext()
|
||||
|
@ -151,7 +96,7 @@ func GlobalInit(ctx context.Context) {
|
|||
} else if setting.Database.UseSQLite3 {
|
||||
log.Fatal("SQLite3 is set in settings but NOT Supported")
|
||||
}
|
||||
if err := initDBEngine(ctx); err == nil {
|
||||
if err := common.InitDBEngine(ctx); err == nil {
|
||||
log.Info("ORM engine initialization successful!")
|
||||
} else {
|
||||
log.Fatal("ORM engine initialization failed: %v", err)
|
||||
|
@ -193,3 +138,16 @@ func GlobalInit(ctx context.Context) {
|
|||
|
||||
svg.Init()
|
||||
}
|
||||
|
||||
// NormalRoutes represents non install routes
|
||||
func NormalRoutes() *web.Route {
|
||||
r := web.NewRoute()
|
||||
for _, middle := range common.Middlewares() {
|
||||
r.Use(middle)
|
||||
}
|
||||
|
||||
r.Mount("/", web_routers.Routes())
|
||||
r.Mount("/api/v1", apiv1.Routes())
|
||||
r.Mount("/api/internal", private.Routes())
|
||||
return r
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue