Skip to content

Commit 3e547f5

Browse files
committed
fix: config file loading logic
1 parent 5b911b6 commit 3e547f5

3 files changed

Lines changed: 80 additions & 6 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ copy:
2222

2323
run:
2424
@echo "Starting mkBlog..."
25-
./mkBlog & 1 & echo $$! > mkblog.pid
25+
nohup ./mkBlog & & 1 & echo $$! > mkblog.pid
2626
@echo "mkBlog started, pid saved to mkblog.pid"
2727

2828
stop:

config/config.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package config
22

33
import (
4-
"fmt"
54
"log/slog"
65
"mkBlog/models"
76
"os"
87
"path"
9-
"strings"
108

119
"go.yaml.in/yaml/v3"
1210
)
@@ -95,10 +93,13 @@ func Init() {
9593
return
9694
}
9795

98-
if !strings.Contains(Cfg.Site.Server, ":") {
99-
Cfg.Site.Server = fmt.Sprintf("%s:%d", Cfg.Site.Server, Cfg.Server.Port)
96+
if Cfg.Site.Server != "" {
97+
if normalized, err := normalizeServerURL(Cfg.Site.Server, Cfg.TLS.Enabled, Cfg.Server.Port); err != nil {
98+
slog.Warn("invalid site.server, using raw value", "server", Cfg.Site.Server, "error", err)
99+
} else {
100+
Cfg.Site.Server = normalized
101+
}
100102
}
101-
102103
Cfg.Site.DevMode = Cfg.Server.Devmode
103104

104105
if Cfg.TLS.Enabled {

config/utils.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"net"
6+
"net/url"
7+
"strings"
8+
)
9+
10+
func normalizeServerURL(raw string, tlsEnabled bool, defaultPort int) (string, error) {
11+
raw = strings.TrimSpace(raw)
12+
if raw == "" {
13+
return "", fmt.Errorf("site.server is empty")
14+
}
15+
16+
scheme := "http"
17+
if tlsEnabled {
18+
scheme = "https"
19+
}
20+
21+
var u *url.URL
22+
var err error
23+
if strings.Contains(raw, "://") {
24+
u, err = url.Parse(raw)
25+
} else {
26+
u, err = url.Parse("//" + raw)
27+
}
28+
if err != nil {
29+
return "", fmt.Errorf("invalid server %q: %w", raw, err)
30+
}
31+
32+
host := u.Host
33+
if host == "" && u.Path != "" && !strings.Contains(u.Path, "/") {
34+
host = u.Path
35+
}
36+
if host == "" {
37+
return "", fmt.Errorf("invalid server %q: missing host", raw)
38+
}
39+
40+
hostName, port := splitHostPort(host)
41+
if hostName == "" {
42+
return "", fmt.Errorf("invalid server %q: missing host", raw)
43+
}
44+
if port == "" {
45+
if defaultPort > 0 {
46+
port = fmt.Sprintf("%d", defaultPort)
47+
} else if scheme == "https" {
48+
port = "443"
49+
} else {
50+
port = "80"
51+
}
52+
}
53+
54+
return fmt.Sprintf("%s://%s", scheme, net.JoinHostPort(hostName, port)), nil
55+
}
56+
57+
func splitHostPort(host string) (string, string) {
58+
host = strings.TrimSpace(host)
59+
if host == "" {
60+
return "", ""
61+
}
62+
if h, p, err := net.SplitHostPort(host); err == nil {
63+
return trimIPv6Brackets(h), p
64+
}
65+
return trimIPv6Brackets(host), ""
66+
}
67+
68+
func trimIPv6Brackets(h string) string {
69+
if strings.HasPrefix(h, "[") && strings.HasSuffix(h, "]") {
70+
return strings.TrimSuffix(strings.TrimPrefix(h, "["), "]")
71+
}
72+
return h
73+
}

0 commit comments

Comments
 (0)