aboutsummaryrefslogtreecommitdiffstats
path: root/routers/init.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2021-06-09 07:33:54 +0800
committerGitHub <noreply@github.com>2021-06-09 01:33:54 +0200
commit1bfb0a24d843e10d6d95c4319a84980485e584ed (patch)
treee4a736f9abee3eaad1270bf3b60ee3bb9401a9dc /routers/init.go
parente03a91a48ef7fb716cc7c8bfb411ca8f332dcfe5 (diff)
downloadgitea-1bfb0a24d843e10d6d95c4319a84980485e584ed.tar.gz
gitea-1bfb0a24d843e10d6d95c4319a84980485e584ed.zip
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>
Diffstat (limited to 'routers/init.go')
-rw-r--r--routers/init.go80
1 files changed, 19 insertions, 61 deletions
diff --git a/routers/init.go b/routers/init.go
index 220d87a29d..5e2eca439e 100644
--- a/routers/init.go
+++ b/routers/init.go
@@ -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
+}