Skip to content

Commit cffdb4a

Browse files
committed
refactor: move printing accessible URL logic out of app
1 parent 4aa5021 commit cffdb4a

File tree

4 files changed

+40
-29
lines changed

4 files changed

+40
-29
lines changed

src/accessibleUrls.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package src
2+
3+
import (
4+
"mjpclab.dev/ghfs/src/util"
5+
"strconv"
6+
)
7+
8+
func printAccessibleURLs(accessibleUrls [][]string) {
9+
file, teardown := util.GetTTYFile()
10+
11+
for vhIndex, vhUrls := range accessibleUrls {
12+
file.WriteString("Host " + strconv.Itoa(vhIndex) + " may be accessed by URLs:\n")
13+
for _, url := range vhUrls {
14+
file.WriteString(" " + url + "\n")
15+
}
16+
}
17+
18+
teardown()
19+
}

src/app/accessibleUrls.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
package app
22

3-
import (
4-
"mjpclab.dev/ghfs/src/goVirtualHost"
5-
"mjpclab.dev/ghfs/src/param"
6-
"mjpclab.dev/ghfs/src/util"
7-
"strconv"
8-
)
3+
func (app *App) GetAccessibleUrls(includeLoopback bool) (allUrls [][]string) {
4+
allOrigins := app.vhostSvc.GetAccessibleURLs(includeLoopback)
5+
allUrls = make([][]string, len(allOrigins))
96

10-
func printAccessibleURLs(vhSvc *goVirtualHost.Service, params param.Params) {
11-
vhostsUrls := vhSvc.GetAccessibleURLs(false)
12-
file, teardown := util.GetTTYFile()
13-
14-
for vhIndex := range vhostsUrls {
15-
prefix := ""
16-
if len(params[vhIndex].PrefixUrls) > 0 {
17-
prefix = params[vhIndex].PrefixUrls[0]
7+
for vhIndex, vhOrigins := range allOrigins {
8+
vhPrefixes := app.params[vhIndex].PrefixUrls
9+
if len(vhPrefixes) == 0 {
10+
vhPrefixes = []string{""}
1811
}
1912

20-
file.WriteString("Host " + strconv.Itoa(vhIndex) + " may be accessed by URLs:\n")
21-
for urlIndex := range vhostsUrls[vhIndex] {
22-
file.WriteString(" " + vhostsUrls[vhIndex][urlIndex] + prefix + "/\n")
13+
allUrls[vhIndex] = make([]string, 0, len(vhOrigins)*len(vhPrefixes))
14+
15+
for _, origin := range vhOrigins {
16+
for _, prefix := range vhPrefixes {
17+
allUrls[vhIndex] = append(allUrls[vhIndex], origin+prefix+"/")
18+
}
2319
}
2420
}
2521

26-
teardown()
22+
return
2723
}

src/app/main.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import (
66
"mjpclab.dev/ghfs/src/param"
77
"mjpclab.dev/ghfs/src/serverHandler"
88
"mjpclab.dev/ghfs/src/serverLog"
9-
"mjpclab.dev/ghfs/src/setting"
109
"mjpclab.dev/ghfs/src/tpl/theme"
1110
"net/http"
1211
"time"
1312
)
1413

1514
type App struct {
15+
params param.Params
1616
vhostSvc *goVirtualHost.Service
1717
logMan *serverLog.Man
1818
}
@@ -49,11 +49,7 @@ func (app *App) ReLoadCertificates() []error {
4949
return app.vhostSvc.ReloadCertificates()
5050
}
5151

52-
func (app *App) GetAccessibleOrigins(includeLoopback bool) [][]string {
53-
return app.vhostSvc.GetAccessibleURLs(includeLoopback)
54-
}
55-
56-
func NewApp(params param.Params, settings *setting.Setting) (*App, []error) {
52+
func NewApp(params param.Params) (*App, []error) {
5753
vhSvc := goVirtualHost.NewService()
5854
logMan := serverLog.NewMan()
5955
themePool := make(map[string]theme.Theme)
@@ -112,11 +108,8 @@ func NewApp(params param.Params, settings *setting.Setting) (*App, []error) {
112108
}
113109
}
114110

115-
if !settings.Quiet {
116-
go printAccessibleURLs(vhSvc, params)
117-
}
118-
119111
return &App{
112+
params: params,
120113
vhostSvc: vhSvc,
121114
logMan: logMan,
122115
}, nil

src/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func Main() (ok bool) {
8888
}
8989

9090
// app
91-
appInst, errs := app.NewApp(params, settings)
91+
appInst, errs := app.NewApp(params)
9292
if serverError.CheckError(errs...) {
9393
return
9494
}
@@ -99,6 +99,9 @@ func Main() (ok bool) {
9999

100100
cleanupOnEnd(appInst)
101101
reInitOnHup(appInst)
102+
if !settings.Quiet {
103+
printAccessibleURLs(appInst.GetAccessibleUrls(false))
104+
}
102105
errs = appInst.Open()
103106
if serverError.CheckError(errs...) {
104107
return

0 commit comments

Comments
 (0)