Skip to content

Commit f07c976

Browse files
committed
refactor
1 parent f47744c commit f07c976

30 files changed

+473
-455
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ cpu.out
5353
/bin
5454
/dist
5555
/custom/*
56-
!/custom/conf
57-
/custom/conf/*
5856
!/custom/conf/app.example.ini
5957
/data
6058
/indexers

cmd/actions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func runGenerateActionsRunnerToken(c *cli.Context) error {
4242
ctx, cancel := installSignals()
4343
defer cancel()
4444

45-
setting.Init(&setting.Options{})
45+
setting.MustInstalled()
4646

4747
scope := c.String("scope")
4848

cmd/cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func confirm() (bool, error) {
5858
}
5959

6060
func initDB(ctx context.Context) error {
61-
setting.Init(&setting.Options{})
61+
setting.MustInstalled()
6262
setting.LoadDBSetting()
6363
setting.InitSQLLoggersForCli(log.INFO)
6464

cmd/doctor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func runRecreateTable(ctx *cli.Context) error {
9191
golog.SetOutput(log.LoggerToWriter(log.GetLogger(log.DEFAULT).Info))
9292

9393
debug := ctx.Bool("debug")
94-
setting.Init(&setting.Options{})
94+
setting.MustInstalled()
9595
setting.LoadDBSetting()
9696

9797
if debug {

cmd/dump.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func runDump(ctx *cli.Context) error {
182182
}
183183
fileName += "." + outType
184184
}
185-
setting.Init(&setting.Options{})
185+
setting.MustInstalled()
186186

187187
// make sure we are logging to the console no matter what the configuration tells us do to
188188
// FIXME: don't use CfgProvider directly

cmd/embedded.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,6 @@ type assetFile struct {
9999
func initEmbeddedExtractor(c *cli.Context) error {
100100
setupConsoleLogger(log.ERROR, log.CanColorStderr, os.Stderr)
101101

102-
// Read configuration file
103-
setting.Init(&setting.Options{
104-
AllowEmpty: true,
105-
})
106-
107102
patterns, err := compileCollectPatterns(c.Args())
108103
if err != nil {
109104
return err

cmd/mailer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func runSendMail(c *cli.Context) error {
1616
ctx, cancel := installSignals()
1717
defer cancel()
1818

19-
setting.Init(&setting.Options{})
19+
setting.MustInstalled()
2020

2121
if err := argsSet(c, "title"); err != nil {
2222
return err

cmd/restore_repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func runRestoreRepository(c *cli.Context) error {
5151
ctx, cancel := installSignals()
5252
defer cancel()
5353

54-
setting.Init(&setting.Options{})
54+
setting.MustInstalled()
5555
var units []string
5656
if s := c.String("units"); s != "" {
5757
units = strings.Split(s, ",")

cmd/serv.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func setup(ctx context.Context, debug bool) {
6161
} else {
6262
setupConsoleLogger(log.FATAL, false, os.Stderr)
6363
}
64-
setting.Init(&setting.Options{})
64+
setting.MustInstalled()
6565
if debug {
6666
setting.RunMode = "dev"
6767
}

cmd/web.go

Lines changed: 96 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,98 @@ func createPIDFile(pidPath string) {
101101
}
102102
}
103103

104+
func serveInstall(ctx *cli.Context) error {
105+
log.Info("Gitea version: %s%s", setting.AppVer, setting.AppBuiltWith)
106+
log.Info("App path: %s", setting.AppPath)
107+
log.Info("Work path: %s", setting.AppWorkPath)
108+
log.Info("Custom path: %s", setting.CustomPath)
109+
log.Info("Config file: %s", setting.CustomConf)
110+
log.Info("Prepare to run install page")
111+
112+
routers.InitWebInstallPage(graceful.GetManager().HammerContext())
113+
114+
// Flag for port number in case first time run conflict
115+
if ctx.IsSet("port") {
116+
if err := setPort(ctx.String("port")); err != nil {
117+
return err
118+
}
119+
}
120+
if ctx.IsSet("install-port") {
121+
if err := setPort(ctx.String("install-port")); err != nil {
122+
return err
123+
}
124+
}
125+
c := install.Routes()
126+
err := listen(c, false)
127+
if err != nil {
128+
log.Critical("Unable to open listener for installer. Is Gitea already running?")
129+
graceful.GetManager().DoGracefulShutdown()
130+
}
131+
select {
132+
case <-graceful.GetManager().IsShutdown():
133+
<-graceful.GetManager().Done()
134+
log.Info("PID: %d Gitea Web Finished", os.Getpid())
135+
log.GetManager().Close()
136+
return err
137+
default:
138+
}
139+
return nil
140+
}
141+
142+
func serveInstalled(ctx *cli.Context) error {
143+
setting.InitCfgProvider(setting.CustomConf)
144+
setting.LoadCommonSettings()
145+
setting.MustInstalled()
146+
147+
log.Info("Gitea version: %s%s", setting.AppVer, setting.AppBuiltWith)
148+
log.Info("App path: %s", setting.AppPath)
149+
log.Info("Work path: %s", setting.AppWorkPath)
150+
log.Info("Custom path: %s", setting.CustomPath)
151+
log.Info("Config file: %s", setting.CustomConf)
152+
log.Info("Run mode: %s", setting.RunMode)
153+
log.Info("Prepare to run web server")
154+
155+
if setting.CfgProvider.Section("").Key("WORK_PATH").String() == "" {
156+
setting.CfgProvider.Section("").Key("WORK_PATH").SetValue(setting.AppWorkPath)
157+
if err := setting.CfgProvider.Save(); err != nil {
158+
log.Error("Unable to update WORK_PATH=%s to config %q: %v\nYou must set it manually, otherwise there might be bugs when accessing the git repositories.", setting.AppWorkPath, setting.CustomConf, err)
159+
}
160+
}
161+
162+
routers.InitWebInstalled(graceful.GetManager().HammerContext())
163+
164+
// We check that AppDataPath exists here (it should have been created during installation)
165+
// We can't check it in `InitWebInstalled`, because some integration tests
166+
// use cmd -> InitWebInstalled, but the AppDataPath doesn't exist during those tests.
167+
if _, err := os.Stat(setting.AppDataPath); err != nil {
168+
log.Fatal("Can not find APP_DATA_PATH %q", setting.AppDataPath)
169+
}
170+
171+
// Override the provided port number within the configuration
172+
if ctx.IsSet("port") {
173+
if err := setPort(ctx.String("port")); err != nil {
174+
return err
175+
}
176+
}
177+
178+
// Set up Chi routes
179+
c := routers.NormalRoutes()
180+
err := listen(c, true)
181+
<-graceful.GetManager().Done()
182+
log.Info("PID: %d Gitea Web Finished", os.Getpid())
183+
log.GetManager().Close()
184+
return err
185+
}
186+
187+
func servePprof() {
188+
http.DefaultServeMux.Handle("/debug/fgprof", fgprof.Handler())
189+
_, _, finished := process.GetManager().AddTypedContext(context.Background(), "Web: PProf Server", process.SystemProcessType, true)
190+
// The pprof server is for debug purpose only, it shouldn't be exposed on public network. At the moment it's not worth to introduce a configurable option for it.
191+
log.Info("Starting pprof server on localhost:6060")
192+
log.Info("Stopped pprof server: %v", http.ListenAndServe("localhost:6060", nil))
193+
finished()
194+
}
195+
104196
func runWeb(ctx *cli.Context) error {
105197
if ctx.Bool("verbose") {
106198
setupConsoleLogger(log.TRACE, log.CanColorStdout, os.Stdout)
@@ -128,75 +220,19 @@ func runWeb(ctx *cli.Context) error {
128220
createPIDFile(ctx.String("pid"))
129221
}
130222

131-
// Perform pre-initialization
132-
needsInstall := install.PreloadSettings(graceful.GetManager().HammerContext())
133-
if needsInstall {
134-
// Flag for port number in case first time run conflict
135-
if ctx.IsSet("port") {
136-
if err := setPort(ctx.String("port")); err != nil {
137-
return err
138-
}
139-
}
140-
if ctx.IsSet("install-port") {
141-
if err := setPort(ctx.String("install-port")); err != nil {
142-
return err
143-
}
144-
}
145-
c := install.Routes()
146-
err := listen(c, false)
147-
if err != nil {
148-
log.Critical("Unable to open listener for installer. Is Gitea already running?")
149-
graceful.GetManager().DoGracefulShutdown()
150-
}
151-
select {
152-
case <-graceful.GetManager().IsShutdown():
153-
<-graceful.GetManager().Done()
154-
log.Info("PID: %d Gitea Web Finished", os.Getpid())
155-
log.GetManager().Close()
223+
if !setting.InstallLock {
224+
if err := serveInstall(ctx); err != nil {
156225
return err
157-
default:
158226
}
159227
} else {
160228
NoInstallListener()
161229
}
162230

163231
if setting.EnablePprof {
164-
go func() {
165-
http.DefaultServeMux.Handle("/debug/fgprof", fgprof.Handler())
166-
_, _, finished := process.GetManager().AddTypedContext(context.Background(), "Web: PProf Server", process.SystemProcessType, true)
167-
// The pprof server is for debug purpose only, it shouldn't be exposed on public network. At the moment it's not worth to introduce a configurable option for it.
168-
log.Info("Starting pprof server on localhost:6060")
169-
log.Info("Stopped pprof server: %v", http.ListenAndServe("localhost:6060", nil))
170-
finished()
171-
}()
232+
go servePprof()
172233
}
173234

174-
log.Info("Global init")
175-
// Perform global initialization
176-
setting.Init(&setting.Options{})
177-
routers.GlobalInitInstalled(graceful.GetManager().HammerContext())
178-
179-
// We check that AppDataPath exists here (it should have been created during installation)
180-
// We can't check it in `GlobalInitInstalled`, because some integration tests
181-
// use cmd -> GlobalInitInstalled, but the AppDataPath doesn't exist during those tests.
182-
if _, err := os.Stat(setting.AppDataPath); err != nil {
183-
log.Fatal("Can not find APP_DATA_PATH '%s'", setting.AppDataPath)
184-
}
185-
186-
// Override the provided port number within the configuration
187-
if ctx.IsSet("port") {
188-
if err := setPort(ctx.String("port")); err != nil {
189-
return err
190-
}
191-
}
192-
193-
// Set up Chi routes
194-
c := routers.NormalRoutes()
195-
err := listen(c, true)
196-
<-graceful.GetManager().Done()
197-
log.Info("PID: %d Gitea Web Finished", os.Getpid())
198-
log.GetManager().Close()
199-
return err
235+
return serveInstalled(ctx)
200236
}
201237

202238
func setPort(port string) error {

contrib/environment-to-ini/environment-to-ini.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,20 @@ func main() {
8181
},
8282
}
8383
app.Action = runEnvironmentToIni
84-
setting.SetCustomPathAndConf("", "", "")
85-
8684
err := app.Run(os.Args)
8785
if err != nil {
8886
log.Fatal("Failed to run app with %s: %v", os.Args, err)
8987
}
9088
}
9189

9290
func runEnvironmentToIni(c *cli.Context) error {
93-
providedCustom := c.String("custom-path")
94-
providedConf := c.String("config")
95-
providedWorkPath := c.String("work-path")
96-
setting.SetCustomPathAndConf(providedCustom, providedConf, providedWorkPath)
91+
setting.InitWorkPathAndCommonConfig(os.Getenv, setting.ArgWorkPathAndCustomConf{
92+
WorkPath: c.String("work-path"),
93+
CustomPath: c.String("custom-path"),
94+
CustomConf: c.String("config"),
95+
})
9796

98-
cfg, err := setting.NewConfigProviderFromFile(&setting.Options{CustomConf: setting.CustomConf, AllowEmpty: true})
97+
cfg, err := setting.NewConfigProviderFromFile(setting.CustomConf)
9998
if err != nil {
10099
log.Fatal("Failed to load custom conf '%s': %v", setting.CustomConf, err)
101100
}

0 commit comments

Comments
 (0)