Skip to content

Commit 141499a

Browse files
committed
新增命令行参数支持
1 parent a986c02 commit 141499a

File tree

2 files changed

+54
-22
lines changed

2 files changed

+54
-22
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ docker run -d \
7676
2. 以无用户密码的方式运行
7777

7878
```bash
79-
./tiny-nav --port=8099 --enable_no_auth
79+
./tiny-nav --port=58080 --no-auth
8080
```
8181

82-
3. 打开浏览器访问 <http://localhost:8099> 即可。
82+
3. 打开浏览器访问 <http://localhost:58080> 即可。
8383
4. 以有用户密码的方式运行
8484

8585
```bash
86-
./tiny-nav --port=8099 --user=admin --password=123456
86+
./tiny-nav --port=58080 --user=admin --password=123456
8787
```
8888

8989
## 编译运行
@@ -99,10 +99,10 @@ sh build.sh
9999
### 启动
100100

101101
```
102-
ENABLE_NO_AUTH=true LISTEN_PORT=8099 ./tiny-nav
102+
ENABLE_NO_AUTH=true LISTEN_PORT=58080 ./tiny-nav
103103
```
104104

105-
网页访问 <http://localhost:8099> 即可。
105+
网页访问 <http://localhost:58080> 即可。
106106

107107
## 技术栈
108108

main.go

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"embed"
66
"encoding/base64"
77
"encoding/json"
8+
"flag"
89
"fmt"
910
"golang.org/x/net/html"
1011
"gopkg.in/ini.v1"
@@ -59,6 +60,13 @@ type Navigation struct {
5960
}
6061

6162
func loadConfig() {
63+
// Parse command line flags
64+
port := flag.String("port", "", "Port to listen on (e.g. 58080)")
65+
user := flag.String("user", "", "Username for authentication")
66+
password := flag.String("password", "", "Password for authentication")
67+
noAuth := flag.Bool("no-auth", false, "Enable no-auth mode")
68+
flag.Parse()
69+
6270
// 确保 data 目录存在
6371
if err := os.MkdirAll(dataDir, 0755); err != nil {
6472
fmt.Errorf("failed to create data directory: %v", err)
@@ -71,33 +79,47 @@ func loadConfig() {
7179
log.Printf("Failed to read config file: %v", err)
7280
}
7381

74-
port := os.Getenv("LISTEN_PORT")
75-
if port == "" && cfg != nil {
76-
port = cfg.Section("").Key("LISTEN_PORT").MustString("58080")
82+
// Priority: Command line args > Environment variables > Config file
83+
envPort = os.Getenv("LISTEN_PORT")
84+
if envPort == "" {
85+
if *port != "" {
86+
envPort = *port
87+
} else if cfg != nil {
88+
envPort = cfg.Section("").Key("LISTEN_PORT").MustString("58080")
89+
} else {
90+
envPort = "58080"
91+
}
7792
}
7893

79-
username := os.Getenv("NAV_USERNAME")
80-
if username == "" && cfg != nil {
81-
username = cfg.Section("").Key("NAV_USERNAME").String()
94+
envUsername = os.Getenv("NAV_USERNAME")
95+
if envUsername == "" {
96+
if *user != "" {
97+
envUsername = *user
98+
} else if cfg != nil {
99+
envUsername = cfg.Section("").Key("NAV_USERNAME").String()
100+
}
82101
}
83102

84-
password := os.Getenv("NAV_PASSWORD")
85-
if password == "" && cfg != nil {
86-
password = cfg.Section("").Key("NAV_PASSWORD").String()
103+
envPassword = os.Getenv("NAV_PASSWORD")
104+
if envPassword == "" {
105+
if *password != "" {
106+
envPassword = *password
107+
} else if cfg != nil {
108+
envPassword = cfg.Section("").Key("NAV_PASSWORD").String()
109+
}
87110
}
88111

89-
noAuth := os.Getenv("ENABLE_NO_AUTH")
112+
noAuthStr := os.Getenv("ENABLE_NO_AUTH")
90113
if cfg != nil {
91-
noAuth = cfg.Section("").Key("ENABLE_NO_AUTH").MustString("false")
114+
noAuthStr = cfg.Section("").Key("ENABLE_NO_AUTH").MustString("false")
92115
}
93-
94-
envPort = port
95-
envUsername = username
96-
envPassword = password
97-
if noAuth == "true" {
116+
if *noAuth {
98117
envEnableNoAuth = true
118+
} else {
119+
envEnableNoAuth = noAuthStr == "true"
99120
}
100-
log.Printf("Config loaded: LISTEN_PORT=%s, NAV_USERNAME=%s, ENABLE_NO_AUTH=%s", envPort, envUsername, noAuth)
121+
122+
log.Printf("Config loaded: LISTEN_PORT=%s, NAV_USERNAME=%s, ENABLE_NO_AUTH=%v", envPort, envUsername, envEnableNoAuth)
101123
}
102124

103125
func loadNavigation() (Navigation, error) {
@@ -790,6 +812,16 @@ func corsMiddleware(next http.Handler) http.Handler {
790812
}
791813

792814
func main() {
815+
// Add a simple usage message
816+
flag.Usage = func() {
817+
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
818+
fmt.Fprintf(os.Stderr, " %s [options]\n\n", os.Args[0])
819+
fmt.Fprintf(os.Stderr, "Options:\n")
820+
flag.PrintDefaults()
821+
fmt.Fprintf(os.Stderr, "\nExample:\n")
822+
fmt.Fprintf(os.Stderr, " %s --port=58080 --user=admin --password=123456\n", os.Args[0])
823+
}
824+
793825
loadConfig()
794826
tokenStore = NewTokenStore()
795827

0 commit comments

Comments
 (0)