Skip to content

Commit 6e7723c

Browse files
committed
refactor(main): extract start logic
1 parent 4185485 commit 6e7723c

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

src/main.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,29 @@ func Main() (ok bool) {
2929
// settings
3030
settings := setting.ParseFromEnv()
3131

32+
// start
33+
errs = Start(settings, params)
34+
if serverError.CheckError(errs...) {
35+
return
36+
}
37+
38+
return true
39+
}
40+
41+
func Start(settings *setting.Setting, params param.Params) (errs []error) {
3242
// CPU profile
3343
if len(settings.CPUProfileFile) > 0 {
34-
cpuProfileFile, err := StartCPUProfile(settings.CPUProfileFile)
35-
if serverError.CheckError(err) {
36-
return
44+
cpuProfileFile, err := startCPUProfile(settings.CPUProfileFile)
45+
if err != nil {
46+
return []error{err}
3747
}
38-
defer StopCPUProfile(cpuProfileFile)
48+
defer stopCPUProfile(cpuProfileFile)
3949
}
4050

4151
// pid file
4252
if len(settings.PidFile) > 0 {
4353
errs = writePidFile(settings.PidFile)
44-
if serverError.CheckError(errs...) {
54+
if len(errs) > 0 {
4555
return
4656
}
4757
}
@@ -56,11 +66,11 @@ func Main() (ok bool) {
5666

5767
// app
5868
appInst, errs := app.NewApp(params)
59-
if serverError.CheckError(errs...) {
69+
if len(errs) > 0 {
6070
return
6171
}
6272
if appInst == nil {
63-
serverError.CheckError(errors.New("failed to create application instance"))
73+
errs = []error{errors.New("failed to create application instance")}
6474
return
6575
}
6676

@@ -70,9 +80,5 @@ func Main() (ok bool) {
7080
printAccessibleURLs(appInst.GetAccessibleUrls(false))
7181
}
7282
errs = appInst.Open()
73-
if serverError.CheckError(errs...) {
74-
return
75-
}
76-
77-
return true
83+
return
7884
}

src/profile.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"runtime/pprof"
66
)
77

8-
func StartCPUProfile(profileFilePath string) (*os.File, error) {
8+
func startCPUProfile(profileFilePath string) (*os.File, error) {
99
profileFile, err := os.OpenFile(profileFilePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
1010
if err != nil {
1111
return nil, err
@@ -20,7 +20,7 @@ func StartCPUProfile(profileFilePath string) (*os.File, error) {
2020
return profileFile, nil
2121
}
2222

23-
func StopCPUProfile(profileFile *os.File) error {
23+
func stopCPUProfile(profileFile *os.File) error {
2424
pprof.StopCPUProfile()
2525
return profileFile.Close()
2626
}

0 commit comments

Comments
 (0)