Skip to content

Commit

Permalink
Follow @zeripath's review
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Jun 8, 2021
1 parent 140337a commit 069fa9d
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 72 deletions.
2 changes: 1 addition & 1 deletion cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func runWeb(ctx *cli.Context) error {
}

// Perform pre-initialization
needsInstall := install.PreInit(graceful.GetManager().HammerContext())
needsInstall := install.PreloadSettings(graceful.GetManager().HammerContext())
if needsInstall {
// Flag for port number in case first time run conflict
if ctx.IsSet("port") {
Expand Down
40 changes: 2 additions & 38 deletions routers/install/install.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright 2014 The Gogs Authors. All rights reserved.
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package install

import (
std_context "context"
"fmt"
"net/http"
"os"
Expand All @@ -21,14 +21,12 @@ import (
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/svg"
"code.gitea.io/gitea/modules/templates"
"code.gitea.io/gitea/modules/translation"
"code.gitea.io/gitea/modules/user"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/modules/web/middleware"
"code.gitea.io/gitea/routers/common"
"code.gitea.io/gitea/services/forms"

"gitea.com/go-chi/session"
Expand Down Expand Up @@ -86,40 +84,6 @@ func Init(next http.Handler) http.Handler {
})
}

// PreInit preloads the configuration to check if we need to run install
func PreInit(ctx std_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
}

// PostInit rereads the settings and starts up the database
func PostInit(ctx std_context.Context) {
setting.NewContext()
setting.InitDBConfig()
if setting.InstallLock {
if err := common.InitDBEngine(ctx); err == nil {
log.Info("ORM engine initialization successful!")
} else {
log.Fatal("ORM engine initialization failed: %v", err)
}
svg.Init()
}
}

// Install render installation page
func Install(ctx *context.Context) {
form := forms.InstallForm{}
Expand Down Expand Up @@ -446,7 +410,7 @@ func SubmitInstall(ctx *context.Context) {
}

// Re-read settings
PostInit(ctx)
ReloadSettings(ctx)

// Create admin account
if len(form.AdminName) > 0 {
Expand Down
22 changes: 9 additions & 13 deletions routers/install/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ import (
"gitea.com/go-chi/session"
)

type dataStore struct {
Data map[string]interface{}
}
type dataStore map[string]interface{}

func (d *dataStore) GetData() map[string]interface{} {
return d.Data
return *d
}

func installRecovery() func(next http.Handler) http.Handler {
Expand Down Expand Up @@ -56,21 +54,19 @@ func installRecovery() func(next http.Handler) http.Handler {

lc := middleware.Locale(w, req)
var store = dataStore{
Data: templates.Vars{
"Language": lc.Language(),
"CurrentURL": setting.AppSubURL + req.URL.RequestURI(),
"i18n": lc,
"SignedUserID": int64(0),
"SignedUserName": "",
},
"Language": lc.Language(),
"CurrentURL": setting.AppSubURL + req.URL.RequestURI(),
"i18n": lc,
"SignedUserID": int64(0),
"SignedUserName": "",
}

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

if !setting.IsProd() {
store.Data["ErrorMsg"] = combinedErr
store["ErrorMsg"] = combinedErr
}
err = rnd.HTML(w, 500, "status/500", templates.BaseVars().Merge(store.Data))
err = rnd.HTML(w, 500, "status/500", templates.BaseVars().Merge(store))
if err != nil {
log.Error("%v", err)
}
Expand Down
49 changes: 49 additions & 0 deletions routers/install/setting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package install

import (
"context"

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/svg"
"code.gitea.io/gitea/modules/translation"
"code.gitea.io/gitea/routers/common"
)

// PreloadSettings preloads the configuration to check if we need to run install
func PreloadSettings(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
}

// ReloadSettings rereads the settings and starts up the database
func ReloadSettings(ctx context.Context) {
setting.NewContext()
setting.InitDBConfig()
if setting.InstallLock {
if err := common.InitDBEngine(ctx); err == nil {
log.Info("ORM engine initialization successful!")
} else {
log.Fatal("ORM engine initialization failed: %v", err)
}
svg.Init()
}
}
32 changes: 14 additions & 18 deletions routers/web/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,10 @@ func storageHandler(storageSetting setting.Storage, prefix string, objStore stor
}
}

type dataStore struct {
Data map[string]interface{}
}
type dataStore map[string]interface{}

func (d *dataStore) GetData() map[string]interface{} {
return d.Data
return *d
}

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

var lc = middleware.Locale(w, req)
var store = dataStore{
Data: templates.Vars{
"Language": lc.Language(),
"CurrentURL": setting.AppSubURL + req.URL.RequestURI(),
"i18n": lc,
},
"Language": lc.Language(),
"CurrentURL": setting.AppSubURL + req.URL.RequestURI(),
"i18n": lc,
}

var user *models.User
Expand All @@ -165,22 +161,22 @@ func Recovery() func(next http.Handler) http.Handler {
user = sso.SessionUser(sessionStore)
}
if user != nil {
store.Data["IsSigned"] = true
store.Data["SignedUser"] = user
store.Data["SignedUserID"] = user.ID
store.Data["SignedUserName"] = user.Name
store.Data["IsAdmin"] = user.IsAdmin
store["IsSigned"] = true
store["SignedUser"] = user
store["SignedUserID"] = user.ID
store["SignedUserName"] = user.Name
store["IsAdmin"] = user.IsAdmin
} else {
store.Data["SignedUserID"] = int64(0)
store.Data["SignedUserName"] = ""
store["SignedUserID"] = int64(0)
store["SignedUserName"] = ""
}

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

if !setting.IsProd() {
store.Data["ErrorMsg"] = combinedErr
store["ErrorMsg"] = combinedErr
}
err = rnd.HTML(w, 500, "status/500", templates.BaseVars().Merge(store.Data))
err = rnd.HTML(w, 500, "status/500", templates.BaseVars().Merge(store))
if err != nil {
log.Error("%v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion routers/web/explore/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
)

// Codes render explore code page
func Codes(ctx *context.Context) {
func Code(ctx *context.Context) {
if !setting.Indexer.RepoIndexerEnabled {
ctx.Redirect(setting.AppSubURL+"/explore", 302)
return
Expand Down
2 changes: 1 addition & 1 deletion routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func RegisterRoutes(m *web.Route) {
m.Get("/repos", explore.Repos)
m.Get("/users", explore.Users)
m.Get("/organizations", explore.Organizations)
m.Get("/code", explore.Codes)
m.Get("/code", explore.Code)
}, ignExploreSignIn)
m.Get("/issues", reqSignIn, user.Issues)
m.Get("/pulls", reqSignIn, user.Pulls)
Expand Down

0 comments on commit 069fa9d

Please sign in to comment.