Skip to content

Commit 60909dd

Browse files
committed
Fix lint
1 parent 6e6c05c commit 60909dd

File tree

16 files changed

+129
-103
lines changed

16 files changed

+129
-103
lines changed

.golangci.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ issues:
7070
- path: modules/log/
7171
linters:
7272
- errcheck
73-
- path: routers/routes/web.go
74-
linters:
75-
- dupl
7673
- path: routers/api/v1/repo/issue_subscription.go
7774
linters:
7875
- dupl
@@ -114,3 +111,4 @@ issues:
114111
linters:
115112
- staticcheck
116113
text: "svc.IsAnInteractiveSession is deprecated: Use IsWindowsService instead."
114+

cmd/web.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"code.gitea.io/gitea/modules/setting"
1919
"code.gitea.io/gitea/routers"
2020
"code.gitea.io/gitea/routers/install"
21-
"code.gitea.io/gitea/routers/web"
2221

2322
context2 "github.com/gorilla/context"
2423
"github.com/urfave/cli"
@@ -89,7 +88,7 @@ func runWeb(ctx *cli.Context) error {
8988
}
9089

9190
// Perform pre-initialization
92-
needsInstall := install.PreInstallInit(graceful.GetManager().HammerContext())
91+
needsInstall := install.PreInit(graceful.GetManager().HammerContext())
9392
if needsInstall {
9493
// Flag for port number in case first time run conflict
9594
if ctx.IsSet("port") {
@@ -102,7 +101,7 @@ func runWeb(ctx *cli.Context) error {
102101
return err
103102
}
104103
}
105-
c := install.InstallRoutes()
104+
c := install.Routes()
106105
err := listen(c, false)
107106
select {
108107
case <-graceful.GetManager().IsShutdown():
@@ -135,7 +134,7 @@ func runWeb(ctx *cli.Context) error {
135134
}
136135

137136
// Set up Chi routes
138-
c := web.NormalRoutes()
137+
c := routers.NormalRoutes()
139138
err := listen(c, true)
140139
<-graceful.GetManager().Done()
141140
log.Info("PID: %d Gitea Web Finished", os.Getpid())

contrib/pr/checkout.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"code.gitea.io/gitea/modules/setting"
3232
"code.gitea.io/gitea/modules/util"
3333
"code.gitea.io/gitea/routers"
34-
"code.gitea.io/gitea/routers/web"
3534

3635
"github.com/go-git/go-git/v5"
3736
"github.com/go-git/go-git/v5/config"
@@ -116,7 +115,7 @@ func runPR() {
116115
//routers.GlobalInit()
117116
external.RegisterRenderers()
118117
markup.Init()
119-
c := web.NormalRoutes()
118+
c := routers.NormalRoutes()
120119

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

integrations/create_no_session_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
"code.gitea.io/gitea/modules/setting"
1616
"code.gitea.io/gitea/modules/util"
17-
"code.gitea.io/gitea/routers/routes"
17+
"code.gitea.io/gitea/routers"
1818

1919
"gitea.com/go-chi/session"
2020
jsoniter "github.com/json-iterator/go"
@@ -58,7 +58,7 @@ func TestSessionFileCreation(t *testing.T) {
5858
oldSessionConfig := setting.SessionConfig.ProviderConfig
5959
defer func() {
6060
setting.SessionConfig.ProviderConfig = oldSessionConfig
61-
c = routes.NormalRoutes()
61+
c = routers.NormalRoutes()
6262
}()
6363

6464
var config session.Options
@@ -84,7 +84,7 @@ func TestSessionFileCreation(t *testing.T) {
8484

8585
setting.SessionConfig.ProviderConfig = string(newConfigBytes)
8686

87-
c = routes.NormalRoutes()
87+
c = routers.NormalRoutes()
8888

8989
t.Run("NoSessionOnViewIssue", func(t *testing.T) {
9090
defer PrintCurrentTest(t)()

integrations/integration_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"code.gitea.io/gitea/modules/util"
3535
"code.gitea.io/gitea/modules/web"
3636
"code.gitea.io/gitea/routers"
37-
"code.gitea.io/gitea/routers/routes"
3837

3938
"github.com/PuerkitoBio/goquery"
4039
jsoniter "github.com/json-iterator/go"
@@ -88,7 +87,7 @@ func TestMain(m *testing.M) {
8887
defer cancel()
8988

9089
initIntegrationTest()
91-
c = routes.NormalRoutes()
90+
c = routers.NormalRoutes()
9291

9392
// integration test settings...
9493
if setting.Cfg != nil {

integrations/lfs_getobject_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"code.gitea.io/gitea/models"
1616
"code.gitea.io/gitea/modules/lfs"
1717
"code.gitea.io/gitea/modules/setting"
18-
"code.gitea.io/gitea/routers/routes"
18+
"code.gitea.io/gitea/routers/web"
1919

2020
jsoniter "github.com/json-iterator/go"
2121
gzipp "github.com/klauspost/compress/gzip"
@@ -99,7 +99,7 @@ func TestGetLFSLarge(t *testing.T) {
9999
t.Skip()
100100
return
101101
}
102-
content := make([]byte, routes.GzipMinSize*10)
102+
content := make([]byte, web.GzipMinSize*10)
103103
for i := range content {
104104
content[i] = byte(i % 256)
105105
}
@@ -115,7 +115,7 @@ func TestGetLFSGzip(t *testing.T) {
115115
t.Skip()
116116
return
117117
}
118-
b := make([]byte, routes.GzipMinSize*10)
118+
b := make([]byte, web.GzipMinSize*10)
119119
for i := range b {
120120
b[i] = byte(i % 256)
121121
}
@@ -136,7 +136,7 @@ func TestGetLFSZip(t *testing.T) {
136136
t.Skip()
137137
return
138138
}
139-
b := make([]byte, routes.GzipMinSize*10)
139+
b := make([]byte, web.GzipMinSize*10)
140140
for i := range b {
141141
b[i] = byte(i % 256)
142142
}

models/models.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ import (
1212
"fmt"
1313
"reflect"
1414
"strings"
15-
"time"
1615

17-
"code.gitea.io/gitea/models/migrations"
18-
"code.gitea.io/gitea/modules/log"
1916
"code.gitea.io/gitea/modules/setting"
2017

2118
// Needed for the MySQL driver
@@ -232,29 +229,6 @@ func NewEngine(ctx context.Context, migrateFunc func(*xorm.Engine) error) (err e
232229
return nil
233230
}
234231

235-
// InitDBEngine In case of problems connecting to DB, retry connection. Eg, PGSQL in Docker Container on Synology
236-
func InitDBEngine(ctx context.Context) (err error) {
237-
log.Info("Beginning ORM engine initialization.")
238-
for i := 0; i < setting.Database.DBConnectRetries; i++ {
239-
select {
240-
case <-ctx.Done():
241-
return fmt.Errorf("Aborted due to shutdown:\nin retry ORM engine initialization")
242-
default:
243-
}
244-
log.Info("ORM engine initialization attempt #%d/%d...", i+1, setting.Database.DBConnectRetries)
245-
if err = NewEngine(ctx, migrations.Migrate); err == nil {
246-
break
247-
} else if i == setting.Database.DBConnectRetries-1 {
248-
return err
249-
}
250-
log.Error("ORM engine initialization attempt #%d/%d failed. Error: %v", i+1, setting.Database.DBConnectRetries, err)
251-
log.Info("Backing off for %d seconds", int64(setting.Database.DBConnectBackoff/time.Second))
252-
time.Sleep(setting.Database.DBConnectBackoff)
253-
}
254-
HasEngine = true
255-
return nil
256-
}
257-
258232
// NamesToBean return a list of beans or an error
259233
func NamesToBean(names ...string) ([]interface{}, error) {
260234
beans := []interface{}{}

routers/common/db.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package common
6+
7+
import (
8+
"context"
9+
"fmt"
10+
"time"
11+
12+
"code.gitea.io/gitea/models"
13+
"code.gitea.io/gitea/models/migrations"
14+
"code.gitea.io/gitea/modules/log"
15+
"code.gitea.io/gitea/modules/setting"
16+
)
17+
18+
// InitDBEngine In case of problems connecting to DB, retry connection. Eg, PGSQL in Docker Container on Synology
19+
func InitDBEngine(ctx context.Context) (err error) {
20+
log.Info("Beginning ORM engine initialization.")
21+
for i := 0; i < setting.Database.DBConnectRetries; i++ {
22+
select {
23+
case <-ctx.Done():
24+
return fmt.Errorf("Aborted due to shutdown:\nin retry ORM engine initialization")
25+
default:
26+
}
27+
log.Info("ORM engine initialization attempt #%d/%d...", i+1, setting.Database.DBConnectRetries)
28+
if err = models.NewEngine(ctx, migrations.Migrate); err == nil {
29+
break
30+
} else if i == setting.Database.DBConnectRetries-1 {
31+
return err
32+
}
33+
log.Error("ORM engine initialization attempt #%d/%d failed. Error: %v", i+1, setting.Database.DBConnectRetries, err)
34+
log.Info("Backing off for %d seconds", int64(setting.Database.DBConnectBackoff/time.Second))
35+
time.Sleep(setting.Database.DBConnectBackoff)
36+
}
37+
models.HasEngine = true
38+
return nil
39+
}

routers/init.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ import (
2929
"code.gitea.io/gitea/modules/svg"
3030
"code.gitea.io/gitea/modules/task"
3131
"code.gitea.io/gitea/modules/translation"
32+
"code.gitea.io/gitea/modules/web"
33+
apiv1 "code.gitea.io/gitea/routers/api/v1"
34+
"code.gitea.io/gitea/routers/common"
35+
"code.gitea.io/gitea/routers/private"
36+
web_routers "code.gitea.io/gitea/routers/web"
3237
"code.gitea.io/gitea/services/mailer"
3338
mirror_service "code.gitea.io/gitea/services/mirror"
3439
pull_service "code.gitea.io/gitea/services/pull"
@@ -91,7 +96,7 @@ func GlobalInit(ctx context.Context) {
9196
} else if setting.Database.UseSQLite3 {
9297
log.Fatal("SQLite3 is set in settings but NOT Supported")
9398
}
94-
if err := models.InitDBEngine(ctx); err == nil {
99+
if err := common.InitDBEngine(ctx); err == nil {
95100
log.Info("ORM engine initialization successful!")
96101
} else {
97102
log.Fatal("ORM engine initialization failed: %v", err)
@@ -133,3 +138,16 @@ func GlobalInit(ctx context.Context) {
133138

134139
svg.Init()
135140
}
141+
142+
// NormalRoutes represents non install routes
143+
func NormalRoutes() *web.Route {
144+
r := web.NewRoute()
145+
for _, middle := range common.Middlewares() {
146+
r.Use(middle)
147+
}
148+
149+
r.Mount("/", web_routers.Routes())
150+
r.Mount("/api/v1", apiv1.Routes())
151+
r.Mount("/api/internal", private.Routes())
152+
return r
153+
}

routers/install/install.go

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"code.gitea.io/gitea/modules/util"
2929
"code.gitea.io/gitea/modules/web"
3030
"code.gitea.io/gitea/modules/web/middleware"
31+
"code.gitea.io/gitea/routers/common"
3132
"code.gitea.io/gitea/services/forms"
3233

3334
"gitea.com/go-chi/session"
@@ -40,8 +41,8 @@ const (
4041
tplPostInstall base.TplName = "post-install"
4142
)
4243

43-
// InstallInit prepare for rendering installation page
44-
func InstallInit(next http.Handler) http.Handler {
44+
// Init prepare for rendering installation page
45+
func Init(next http.Handler) http.Handler {
4546
var rnd = templates.HTMLRenderer()
4647

4748
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
@@ -85,6 +86,40 @@ func InstallInit(next http.Handler) http.Handler {
8586
})
8687
}
8788

89+
// PreInit preloads the configuration to check if we need to run install
90+
func PreInit(ctx std_context.Context) bool {
91+
setting.NewContext()
92+
if !setting.InstallLock {
93+
log.Trace("AppPath: %s", setting.AppPath)
94+
log.Trace("AppWorkPath: %s", setting.AppWorkPath)
95+
log.Trace("Custom path: %s", setting.CustomPath)
96+
log.Trace("Log path: %s", setting.LogRootPath)
97+
log.Trace("Preparing to run install page")
98+
translation.InitLocales()
99+
if setting.EnableSQLite3 {
100+
log.Info("SQLite3 Supported")
101+
}
102+
setting.InitDBConfig()
103+
svg.Init()
104+
}
105+
106+
return !setting.InstallLock
107+
}
108+
109+
// PostInit rereads the settings and starts up the database
110+
func PostInit(ctx std_context.Context) {
111+
setting.NewContext()
112+
setting.InitDBConfig()
113+
if setting.InstallLock {
114+
if err := common.InitDBEngine(ctx); err == nil {
115+
log.Info("ORM engine initialization successful!")
116+
} else {
117+
log.Fatal("ORM engine initialization failed: %v", err)
118+
}
119+
svg.Init()
120+
}
121+
}
122+
88123
// Install render installation page
89124
func Install(ctx *context.Context) {
90125
form := forms.InstallForm{}
@@ -160,42 +195,8 @@ func Install(ctx *context.Context) {
160195
ctx.HTML(http.StatusOK, tplInstall)
161196
}
162197

163-
// PreInstallInit preloads the configuration to check if we need to run install
164-
func PreInstallInit(ctx std_context.Context) bool {
165-
setting.NewContext()
166-
if !setting.InstallLock {
167-
log.Trace("AppPath: %s", setting.AppPath)
168-
log.Trace("AppWorkPath: %s", setting.AppWorkPath)
169-
log.Trace("Custom path: %s", setting.CustomPath)
170-
log.Trace("Log path: %s", setting.LogRootPath)
171-
log.Trace("Preparing to run install page")
172-
translation.InitLocales()
173-
if setting.EnableSQLite3 {
174-
log.Info("SQLite3 Supported")
175-
}
176-
setting.InitDBConfig()
177-
svg.Init()
178-
}
179-
180-
return !setting.InstallLock
181-
}
182-
183-
// PostInstallInit rereads the settings and starts up the database
184-
func PostInstallInit(ctx std_context.Context) {
185-
setting.NewContext()
186-
setting.InitDBConfig()
187-
if setting.InstallLock {
188-
if err := models.InitDBEngine(ctx); err == nil {
189-
log.Info("ORM engine initialization successful!")
190-
} else {
191-
log.Fatal("ORM engine initialization failed: %v", err)
192-
}
193-
svg.Init()
194-
}
195-
}
196-
197-
// InstallPost response for submit install items
198-
func InstallPost(ctx *context.Context) {
198+
// SubmitInstall response for submit install items
199+
func SubmitInstall(ctx *context.Context) {
199200
form := *web.GetForm(ctx).(*forms.InstallForm)
200201
var err error
201202
ctx.Data["CurDbOption"] = form.DbType

routers/install/routes.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ func installRecovery() func(next http.Handler) http.Handler {
8282
}
8383
}
8484

85-
// InstallRoutes registers the install routes
86-
func InstallRoutes() *web.Route {
85+
// Routes registers the install routes
86+
func Routes() *web.Route {
8787
r := web.NewRoute()
8888
for _, middle := range common.Middlewares() {
8989
r.Use(middle)
@@ -121,9 +121,9 @@ func InstallRoutes() *web.Route {
121121
},
122122
))
123123

124-
r.Use(InstallInit)
124+
r.Use(Init)
125125
r.Get("/", Install)
126-
r.Post("/", web.Bind(forms.InstallForm{}), InstallPost)
126+
r.Post("/", web.Bind(forms.InstallForm{}), SubmitInstall)
127127
r.NotFound(func(w http.ResponseWriter, req *http.Request) {
128128
http.Redirect(w, req, setting.AppURL, http.StatusFound)
129129
})

0 commit comments

Comments
 (0)