Skip to content

Commit 069fa9d

Browse files
committed
Follow @zeripath's review
1 parent 140337a commit 069fa9d

File tree

7 files changed

+77
-72
lines changed

7 files changed

+77
-72
lines changed

cmd/web.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func runWeb(ctx *cli.Context) error {
8888
}
8989

9090
// Perform pre-initialization
91-
needsInstall := install.PreInit(graceful.GetManager().HammerContext())
91+
needsInstall := install.PreloadSettings(graceful.GetManager().HammerContext())
9292
if needsInstall {
9393
// Flag for port number in case first time run conflict
9494
if ctx.IsSet("port") {

routers/install/install.go

+2-38
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Copyright 2021 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

56
package install
67

78
import (
8-
std_context "context"
99
"fmt"
1010
"net/http"
1111
"os"
@@ -21,14 +21,12 @@ import (
2121
"code.gitea.io/gitea/modules/graceful"
2222
"code.gitea.io/gitea/modules/log"
2323
"code.gitea.io/gitea/modules/setting"
24-
"code.gitea.io/gitea/modules/svg"
2524
"code.gitea.io/gitea/modules/templates"
2625
"code.gitea.io/gitea/modules/translation"
2726
"code.gitea.io/gitea/modules/user"
2827
"code.gitea.io/gitea/modules/util"
2928
"code.gitea.io/gitea/modules/web"
3029
"code.gitea.io/gitea/modules/web/middleware"
31-
"code.gitea.io/gitea/routers/common"
3230
"code.gitea.io/gitea/services/forms"
3331

3432
"gitea.com/go-chi/session"
@@ -86,40 +84,6 @@ func Init(next http.Handler) http.Handler {
8684
})
8785
}
8886

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-
12387
// Install render installation page
12488
func Install(ctx *context.Context) {
12589
form := forms.InstallForm{}
@@ -446,7 +410,7 @@ func SubmitInstall(ctx *context.Context) {
446410
}
447411

448412
// Re-read settings
449-
PostInit(ctx)
413+
ReloadSettings(ctx)
450414

451415
// Create admin account
452416
if len(form.AdminName) > 0 {

routers/install/routes.go

+9-13
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@ import (
2121
"gitea.com/go-chi/session"
2222
)
2323

24-
type dataStore struct {
25-
Data map[string]interface{}
26-
}
24+
type dataStore map[string]interface{}
2725

2826
func (d *dataStore) GetData() map[string]interface{} {
29-
return d.Data
27+
return *d
3028
}
3129

3230
func installRecovery() func(next http.Handler) http.Handler {
@@ -56,21 +54,19 @@ func installRecovery() func(next http.Handler) http.Handler {
5654

5755
lc := middleware.Locale(w, req)
5856
var store = dataStore{
59-
Data: templates.Vars{
60-
"Language": lc.Language(),
61-
"CurrentURL": setting.AppSubURL + req.URL.RequestURI(),
62-
"i18n": lc,
63-
"SignedUserID": int64(0),
64-
"SignedUserName": "",
65-
},
57+
"Language": lc.Language(),
58+
"CurrentURL": setting.AppSubURL + req.URL.RequestURI(),
59+
"i18n": lc,
60+
"SignedUserID": int64(0),
61+
"SignedUserName": "",
6662
}
6763

6864
w.Header().Set(`X-Frame-Options`, `SAMEORIGIN`)
6965

7066
if !setting.IsProd() {
71-
store.Data["ErrorMsg"] = combinedErr
67+
store["ErrorMsg"] = combinedErr
7268
}
73-
err = rnd.HTML(w, 500, "status/500", templates.BaseVars().Merge(store.Data))
69+
err = rnd.HTML(w, 500, "status/500", templates.BaseVars().Merge(store))
7470
if err != nil {
7571
log.Error("%v", err)
7672
}

routers/install/setting.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 install
6+
7+
import (
8+
"context"
9+
10+
"code.gitea.io/gitea/modules/log"
11+
"code.gitea.io/gitea/modules/setting"
12+
"code.gitea.io/gitea/modules/svg"
13+
"code.gitea.io/gitea/modules/translation"
14+
"code.gitea.io/gitea/routers/common"
15+
)
16+
17+
// PreloadSettings preloads the configuration to check if we need to run install
18+
func PreloadSettings(ctx context.Context) bool {
19+
setting.NewContext()
20+
if !setting.InstallLock {
21+
log.Trace("AppPath: %s", setting.AppPath)
22+
log.Trace("AppWorkPath: %s", setting.AppWorkPath)
23+
log.Trace("Custom path: %s", setting.CustomPath)
24+
log.Trace("Log path: %s", setting.LogRootPath)
25+
log.Trace("Preparing to run install page")
26+
translation.InitLocales()
27+
if setting.EnableSQLite3 {
28+
log.Info("SQLite3 Supported")
29+
}
30+
setting.InitDBConfig()
31+
svg.Init()
32+
}
33+
34+
return !setting.InstallLock
35+
}
36+
37+
// ReloadSettings rereads the settings and starts up the database
38+
func ReloadSettings(ctx context.Context) {
39+
setting.NewContext()
40+
setting.InitDBConfig()
41+
if setting.InstallLock {
42+
if err := common.InitDBEngine(ctx); err == nil {
43+
log.Info("ORM engine initialization successful!")
44+
} else {
45+
log.Fatal("ORM engine initialization failed: %v", err)
46+
}
47+
svg.Init()
48+
}
49+
}

routers/web/base.go

+14-18
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,10 @@ func storageHandler(storageSetting setting.Storage, prefix string, objStore stor
113113
}
114114
}
115115

116-
type dataStore struct {
117-
Data map[string]interface{}
118-
}
116+
type dataStore map[string]interface{}
119117

120118
func (d *dataStore) GetData() map[string]interface{} {
121-
return d.Data
119+
return *d
122120
}
123121

124122
// Recovery returns a middleware that recovers from any panics and writes a 500 and a log if so.
@@ -144,11 +142,9 @@ func Recovery() func(next http.Handler) http.Handler {
144142

145143
var lc = middleware.Locale(w, req)
146144
var store = dataStore{
147-
Data: templates.Vars{
148-
"Language": lc.Language(),
149-
"CurrentURL": setting.AppSubURL + req.URL.RequestURI(),
150-
"i18n": lc,
151-
},
145+
"Language": lc.Language(),
146+
"CurrentURL": setting.AppSubURL + req.URL.RequestURI(),
147+
"i18n": lc,
152148
}
153149

154150
var user *models.User
@@ -165,22 +161,22 @@ func Recovery() func(next http.Handler) http.Handler {
165161
user = sso.SessionUser(sessionStore)
166162
}
167163
if user != nil {
168-
store.Data["IsSigned"] = true
169-
store.Data["SignedUser"] = user
170-
store.Data["SignedUserID"] = user.ID
171-
store.Data["SignedUserName"] = user.Name
172-
store.Data["IsAdmin"] = user.IsAdmin
164+
store["IsSigned"] = true
165+
store["SignedUser"] = user
166+
store["SignedUserID"] = user.ID
167+
store["SignedUserName"] = user.Name
168+
store["IsAdmin"] = user.IsAdmin
173169
} else {
174-
store.Data["SignedUserID"] = int64(0)
175-
store.Data["SignedUserName"] = ""
170+
store["SignedUserID"] = int64(0)
171+
store["SignedUserName"] = ""
176172
}
177173

178174
w.Header().Set(`X-Frame-Options`, `SAMEORIGIN`)
179175

180176
if !setting.IsProd() {
181-
store.Data["ErrorMsg"] = combinedErr
177+
store["ErrorMsg"] = combinedErr
182178
}
183-
err = rnd.HTML(w, 500, "status/500", templates.BaseVars().Merge(store.Data))
179+
err = rnd.HTML(w, 500, "status/500", templates.BaseVars().Merge(store))
184180
if err != nil {
185181
log.Error("%v", err)
186182
}

routers/web/explore/code.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const (
2121
)
2222

2323
// Codes render explore code page
24-
func Codes(ctx *context.Context) {
24+
func Code(ctx *context.Context) {
2525
if !setting.Indexer.RepoIndexerEnabled {
2626
ctx.Redirect(setting.AppSubURL+"/explore", 302)
2727
return

routers/web/web.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func RegisterRoutes(m *web.Route) {
233233
m.Get("/repos", explore.Repos)
234234
m.Get("/users", explore.Users)
235235
m.Get("/organizations", explore.Organizations)
236-
m.Get("/code", explore.Codes)
236+
m.Get("/code", explore.Code)
237237
}, ignExploreSignIn)
238238
m.Get("/issues", reqSignIn, user.Issues)
239239
m.Get("/pulls", reqSignIn, user.Pulls)

0 commit comments

Comments
 (0)