Skip to content

Commit 15762a8

Browse files
committed
feat: refactor Linuxdo configuration and update related handlers
1 parent 3980929 commit 15762a8

File tree

8 files changed

+83
-54
lines changed

8 files changed

+83
-54
lines changed

auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
// 认证中间件
1111
func AuthMiddleware(next http.Handler) http.Handler {
12-
if config.Secure != "false" || config.Linuxdo != "false" {
12+
if config.Secure != "false" || config.LinuxdoEnable != "false" {
1313
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1414
cookie, err := r.Cookie("auth")
1515
// log.Printf("请求路径: %s, Cookie状态: %+v, 错误信息: %v", r.URL.Path, cookie, err)

config.go

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
package main
22

3-
import (
4-
"os"
5-
)
6-
73
type Config struct {
8-
ImageDir string
9-
Secure string
10-
Password string
11-
Port string
12-
Title string
13-
Icon string
14-
Adderss string
15-
Dynamic string
16-
Linuxdo string
17-
ClientId string
18-
ClientSecret string
4+
ImageDir string `yaml:"image_dir"`
5+
Secure string `yaml:"secure"`
6+
Password string `yaml:"password"`
7+
Port string `yaml:"port"`
8+
Title string `yaml:"title"`
9+
Icon string `yaml:"icon"`
10+
Dynamic string `yaml:"dynamic"`
11+
WebAdderss string `yaml:"web_adderss"`
12+
LinuxdoEnable string `yaml:"linuxdo_enable"`
13+
LinuxdoClientId string `yaml:"linuxdo_client_id"`
14+
LinuxdoClientSecret string `yaml:"linuxdo_client_secret"`
1915
}
2016

2117
var config = Config{}
@@ -35,32 +31,3 @@ var imageExtensions = map[string]bool{
3531
".gif": true,
3632
".webp": true,
3733
}
38-
39-
func initEnv() {
40-
envDefaults := map[string]struct {
41-
target *string
42-
envKey string
43-
defaultVal string
44-
}{
45-
"ImageDir": {&config.ImageDir, "SITE_DIR", "./images"},
46-
"Port": {&config.Port, "SITE_PORT", "8008"},
47-
"Title": {&config.Title, "SITE_TITLE", "在线图集"},
48-
"Icon": {&config.Icon, "SITE_ICON", "https://i.obai.cc/favicon.ico"},
49-
"Dynamic": {&config.Dynamic, "SITE_DYNAMIC", "false"},
50-
"Linuxdo": {&config.Linuxdo, "SITE_LINUXDO", "false"},
51-
"Address": {&config.Adderss, "SITE_Address", "http://localhost:8008"},
52-
"ClientId": {&config.ClientId, "SITE_CLIENT_ID", ""},
53-
"ClientSecret": {&config.ClientSecret, "SITE_CLIENT_SECRET", ""},
54-
"Secure": {&config.Secure, "SITE_SECURE", "false"},
55-
"Password": {&config.Password, "SITE_PASSWORD", ""},
56-
}
57-
58-
for _, cfg := range envDefaults {
59-
if val := os.Getenv(cfg.envKey); val != "" {
60-
*cfg.target = val
61-
} else {
62-
*cfg.target = cfg.defaultVal
63-
}
64-
}
65-
66-
}

go.mod

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ module lovebai/plist
33
go 1.24.0
44

55
require (
6-
github.com/go-resty/resty/v2 v2.16.5 // indirect
6+
github.com/go-resty/resty/v2 v2.16.5
7+
github.com/gorilla/sessions v1.4.0
8+
gopkg.in/yaml.v2 v2.4.0
9+
)
10+
11+
require (
712
github.com/gorilla/securecookie v1.1.2 // indirect
8-
github.com/gorilla/sessions v1.4.0 // indirect
913
golang.org/x/net v0.33.0 // indirect
1014
)

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
github.com/go-resty/resty/v2 v2.16.5 h1:hBKqmWrr7uRc3euHVqmh1HTHcKn99Smr7o5spptdhTM=
22
github.com/go-resty/resty/v2 v2.16.5/go.mod h1:hkJtXbA2iKHzJheXYvQ8snQES5ZLGKMwQ07xAwp/fiA=
3+
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
4+
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
35
github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kXD8ePA=
46
github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo=
57
github.com/gorilla/sessions v1.4.0 h1:kpIYOp/oi6MG/p5PgxApU8srsSw9tuFbt46Lt7auzqQ=
68
github.com/gorilla/sessions v1.4.0/go.mod h1:FLWm50oby91+hl7p/wRxDth9bWSuk0qVL2emc7lT5ik=
79
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
810
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
11+
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
12+
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
13+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
14+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
15+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
16+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func loggingMiddleware(next http.Handler) http.Handler {
3030
func indexHandler(w http.ResponseWriter, r *http.Request) {
3131
session, _ := store.Get(r, "session-name")
3232
var username, avatar string
33-
if config.Linuxdo != "false" {
33+
if config.LinuxdoEnable != "false" {
3434
var ok bool
3535
username, ok = session.Values["username"].(string)
3636
if !ok {

init.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"os"
6+
7+
"gopkg.in/yaml.v2"
8+
)
9+
10+
func init() {
11+
configFileName := "config.yaml"
12+
if _, err := os.Stat(configFileName); os.IsNotExist(err) {
13+
defauleConfig := Config{
14+
ImageDir: "./images",
15+
Port: "8008",
16+
Title: "在线图集",
17+
Icon: "https://i.051214.xyz/favicon.ico",
18+
Dynamic: "true",
19+
LinuxdoEnable: "false",
20+
WebAdderss: "http://localhost:8008",
21+
LinuxdoClientId: "",
22+
LinuxdoClientSecret: "",
23+
Secure: "false",
24+
Password: "",
25+
}
26+
config = defauleConfig
27+
28+
content, err := yaml.Marshal(defauleConfig)
29+
if err != nil {
30+
log.Fatalf("无法序列化默认配置: %v", err)
31+
}
32+
if err := os.WriteFile(configFileName, content, 0644); err != nil {
33+
log.Fatalf("无法创建默认配置文件 %s: %v", configFileName, err)
34+
}
35+
36+
log.Println("配置文件不存在,已为您创建config.yaml,请根据需要修改配置。并重启服务。")
37+
} else {
38+
// 这里可以添加读取配置文件的逻辑
39+
log.Println("加载配置文件:", configFileName)
40+
file, err := os.Open(configFileName)
41+
if err != nil {
42+
log.Fatalf("无法读取配置文件 %s: %v", configFileName, err)
43+
}
44+
defer file.Close()
45+
deyaml := yaml.NewDecoder(file)
46+
if err := deyaml.Decode(&config); err != nil {
47+
log.Fatalf("无法解析配置文件 %s: %v", configFileName, err)
48+
}
49+
}
50+
}

linuxdo.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ func initiateAuthHandler(w http.ResponseWriter, r *http.Request) {
4848
// 构造授权 URL
4949
authURL := fmt.Sprintf("%s?client_id=%s&response_type=code&redirect_uri=%s&state=%s",
5050
AuthorizationEndpoint,
51-
config.ClientId,
52-
config.Adderss+"/oauth2/callback",
51+
config.LinuxdoClientId,
52+
config.WebAdderss+"/oauth2/callback",
5353
state,
5454
)
5555
http.Redirect(w, r, authURL, http.StatusFound)
@@ -75,12 +75,12 @@ func callbackHandler(w http.ResponseWriter, r *http.Request) {
7575

7676
// 请求 access token
7777
resp, err := client.R().
78-
SetBasicAuth(config.ClientId, config.ClientSecret).
78+
SetBasicAuth(config.LinuxdoClientId, config.LinuxdoClientSecret).
7979
SetHeader("Accept", "application/json").
8080
SetFormData(map[string]string{
8181
"grant_type": "authorization_code",
8282
"code": code,
83-
"redirect_uri": config.Adderss + "/oauth2/callback",
83+
"redirect_uri": config.WebAdderss + "/oauth2/callback",
8484
}).
8585
Post(TokenEndpoint)
8686

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ func scanCategories(imageDir string) []Category {
4949
}
5050

5151
func main() {
52-
initEnv()
52+
5353
categoryCache = scanCategories(config.ImageDir)
5454

5555
// 路由设置
5656
http.HandleFunc("/login", loginHandler)
5757

58-
if config.Linuxdo != "false" {
58+
if config.LinuxdoEnable != "false" {
5959
http.HandleFunc("/oauth2/linxdo", initiateAuthHandler)
6060
http.HandleFunc("/oauth2/callback", callbackHandler)
6161
}

0 commit comments

Comments
 (0)