Skip to content

Commit aa5d6b0

Browse files
切换存储方式
1 parent a0e6bb6 commit aa5d6b0

File tree

21 files changed

+388
-165
lines changed

21 files changed

+388
-165
lines changed

.github/workflows/go-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
run: go mod download
2929

3030
- name: Run tests with coverage
31-
run: go test -v -coverprofile=coverage.txt ./...
31+
run: make test lint
3232

3333
- uses: codecov/codecov-action@v5
3434
with:

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,5 @@ fabric.properties
6363
config-local.yaml
6464
gitea-pages
6565
*.zip
66-
dist/
66+
dist/
67+
coverage.txt

.golangci.yml

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
# fork from gitea https://github.com/go-gitea/gitea/blob/main/.golangci.yml
2+
version: "2"
3+
output:
4+
sort-order:
5+
- file
6+
linters:
7+
default: none
8+
enable:
9+
- bidichk
10+
- depguard
11+
- dupl
12+
- errcheck
13+
- forbidigo
14+
- gocritic
15+
- govet
16+
- ineffassign
17+
- mirror
18+
- nakedret
19+
- nolintlint
20+
- perfsprint
21+
- revive
22+
- staticcheck
23+
- testifylint
24+
- unconvert
25+
- unparam
26+
- unused
27+
- usestdlibvars
28+
- usetesting
29+
- wastedassign
30+
settings:
31+
depguard:
32+
rules:
33+
main:
34+
deny:
35+
- pkg: github.com/unknwon/com
36+
desc: use gitea's util and replacements
37+
- pkg: io/ioutil
38+
desc: use os or io instead
39+
- pkg: golang.org/x/exp
40+
desc: it's experimental and unreliable
41+
- pkg: code.gitea.io/gitea/modules/git/internal
42+
desc: do not use the internal package, use AddXxx function instead
43+
- pkg: gopkg.in/ini.v1
44+
desc: do not use the ini package, use gitea's config system instead
45+
- pkg: gitea.com/go-chi/cache
46+
desc: do not use the go-chi cache package, use gitea's cache system
47+
nolintlint:
48+
allow-unused: false
49+
require-explanation: true
50+
require-specific: true
51+
gocritic:
52+
enabled-checks:
53+
- equalFold
54+
disabled-checks:
55+
- ifElseChain
56+
- singleCaseSwitch # Every time this occurred in the code, there was no other way.
57+
revive:
58+
severity: error
59+
rules:
60+
- name: atomic
61+
- name: bare-return
62+
- name: blank-imports
63+
- name: constant-logical-expr
64+
- name: context-as-argument
65+
- name: context-keys-type
66+
- name: dot-imports
67+
- name: duplicated-imports
68+
- name: empty-lines
69+
- name: error-naming
70+
- name: error-return
71+
- name: error-strings
72+
- name: errorf
73+
- name: exported
74+
- name: identical-branches
75+
- name: if-return
76+
- name: increment-decrement
77+
- name: indent-error-flow
78+
- name: modifies-value-receiver
79+
- name: package-comments
80+
- name: range
81+
- name: receiver-naming
82+
- name: redefines-builtin-id
83+
- name: string-of-int
84+
- name: superfluous-else
85+
- name: time-naming
86+
- name: unconditional-recursion
87+
- name: unexported-return
88+
- name: unreachable-code
89+
- name: var-declaration
90+
- name: var-naming
91+
arguments:
92+
- [] # AllowList - do not remove as args for the rule are positional and won't work without lists first
93+
- [] # DenyList
94+
- - skip-package-name-checks: true # supress errors from underscore in migration packages
95+
staticcheck:
96+
checks:
97+
- all
98+
- -ST1003
99+
- -ST1005
100+
- -QF1001
101+
- -QF1006
102+
- -QF1008
103+
testifylint:
104+
disable:
105+
- go-require
106+
- require-error
107+
usetesting:
108+
os-temp-dir: true
109+
exclusions:
110+
generated: lax
111+
presets:
112+
- comments
113+
- common-false-positives
114+
- legacy
115+
- std-error-handling
116+
rules:
117+
- linters:
118+
- dupl
119+
- errcheck
120+
- gocyclo
121+
- gosec
122+
- staticcheck
123+
- unparam
124+
path: _test\.go
125+
- linters:
126+
- dupl
127+
- errcheck
128+
- gocyclo
129+
- gosec
130+
path: models/migrations/v
131+
- linters:
132+
- forbidigo
133+
path: cmd
134+
- linters:
135+
- dupl
136+
text: (?i)webhook
137+
- linters:
138+
- gocritic
139+
text: (?i)`ID' should not be capitalized
140+
- linters:
141+
- deadcode
142+
- unused
143+
text: (?i)swagger
144+
- linters:
145+
- staticcheck
146+
text: (?i)argument x is overwritten before first use
147+
- linters:
148+
- gocritic
149+
text: '(?i)commentFormatting: put a space between `//` and comment text'
150+
- linters:
151+
- gocritic
152+
text: '(?i)exitAfterDefer:'
153+
paths:
154+
- node_modules
155+
- .venv
156+
- public
157+
- web_src
158+
- third_party$
159+
- builtin$
160+
- examples$
161+
issues:
162+
max-issues-per-linter: 0
163+
max-same-issues: 0
164+
formatters:
165+
enable:
166+
- gofmt
167+
- gofumpt
168+
settings:
169+
gofumpt:
170+
extra-rules: true
171+
exclusions:
172+
generated: lax
173+
paths:
174+
- node_modules
175+
- .venv
176+
- public
177+
- web_src
178+
- third_party$
179+
- builtin$
180+
- examples$
181+
182+
run:
183+
timeout: 10m

Makefile

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ debug: gitea-pages
3535

3636
.PHONY: test
3737
test:
38-
@go test -v ./...
38+
@go test -v -coverprofile=coverage.txt ./...
3939

4040

4141
.PHONY: releases
@@ -44,3 +44,13 @@ releases:
4444
make release GOOS=linux GOARCH=arm64 && \
4545
make release GOOS=linux GOARCH=loong64 && \
4646
make release GOOS=windows GOARCH=amd64
47+
48+
.PHONY: lint
49+
lint:
50+
@(test -f "$(GOPATH)/bin/golangci-lint" || go install github.com/golangci/golangci-lint/v2/cmd/[email protected]) && \
51+
"$(GOPATH)/bin/golangci-lint" run -c .golangci.yml
52+
53+
.PHONY: lint-fix
54+
lint-fix:
55+
@(test -f "$(GOPATH)/bin/golangci-lint" || go install github.com/golangci/golangci-lint/v2/cmd/[email protected]) && \
56+
"$(GOPATH)/bin/golangci-lint" run -c .golangci.yml --fix

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This project focuses on functional implementation and does not consider any perf
1010

1111
## Get Started
1212

13-
Install `go1.24` or later, along with the `Make` tool, and then execute the following command:
13+
Install `go1.25` or later, along with the `Make` tool, and then execute the following command:
1414

1515
```bash
1616
make gitea-pages

README_zh.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
## Get Started
1212

13-
安装 `go1.24` 或更高版本,同时安装 `Make` 工具 ,然后执行如下命令:
13+
安装 `go1.25` 或更高版本,同时安装 `Make` 工具 ,然后执行如下命令:
1414

1515
```bash
1616
make gitea-pages

config.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type Config struct {
2424
Bind string `yaml:"bind"` // HTTP 绑定
2525
Domain string `yaml:"domain"` // 基础域名
2626

27-
Config string `yaml:"config"` // 配置
27+
Database ConfigDatabase `yaml:"database"` // 配置
2828

2929
Auth ConfigAuth `yaml:"auth"` // 后端认证配置
3030

@@ -46,7 +46,7 @@ func (c *Config) NewPageServerOptions() (*pkg.ServerOptions, error) {
4646
}
4747
var err error
4848

49-
if c.Config == "" {
49+
if c.Database.URL == "" {
5050
return nil, errors.New("config is required")
5151
}
5252
if c.StaticDir != "" {
@@ -88,7 +88,7 @@ func (c *Config) NewPageServerOptions() (*pkg.ServerOptions, error) {
8888
if err != nil {
8989
return nil, errors.Wrap(err, "create cache")
9090
}
91-
alias, err := kv.NewKVFromURL(c.Config)
91+
alias, err := kv.NewKVFromURL(c.Database.URL)
9292
if err != nil {
9393
return nil, errors.Wrapf(err, "failed to init alias config")
9494
}
@@ -105,7 +105,7 @@ func (c *Config) NewPageServerOptions() (*pkg.ServerOptions, error) {
105105
CacheBlob: memoryCache,
106106
CacheBlobTTL: c.Cache.BlobTTL,
107107
CacheBlobLimit: uint64(c.Cache.BlobLimit),
108-
HttpClient: http.DefaultClient,
108+
HTTPClient: http.DefaultClient,
109109
EnableRender: c.Render.Enable,
110110
EnableProxy: c.Proxy.Enable,
111111
StaticDir: c.StaticDir,
@@ -151,6 +151,10 @@ type ConfigPage struct {
151151
ErrUnknownPage string `yaml:"500"`
152152
}
153153

154+
type ConfigDatabase struct {
155+
URL string `yaml:"url"`
156+
}
157+
154158
type ConfigProxy struct {
155159
Enable bool `yaml:"enable"` // 是否允许反向代理
156160
}

config.yaml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@
22
bind: 127.0.0.1:18080
33
# 基础域名
44
domain: example.com
5+
# 持久化存储
6+
database:
7+
# 持久化存储配置
8+
url: "memory://"
59
auth:
10+
type: gitea
611
server: https://gitea.com
712
# 需要 user , org , repo 的 read 权限
813
token: token
914
cache:
10-
# 配置存储
11-
storage: /path/to/config.json
12-
# 配置缓存时长
13-
ttl: 10m
14-
15-
# 单个文件最大缓存大小
16-
size: 10MB
17-
# 总缓存大小
18-
max: 1GB
15+
# 元数据缓存
16+
meta: "memory://"
17+
# 元数据缓存时长
18+
meta_ttl: 1m
19+
# 响应数据缓存
20+
blob: "memory://"
21+
# 响应数据缓存时长
22+
blob_ttl: 1m
23+
# 最大单个响应数据缓存大小
24+
blob_limit: 10MB
1925
page:
2026
# 默认页面分支
2127
default_branch: gh-pages

main.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,48 @@ import (
1010
"syscall"
1111

1212
"go.uber.org/zap"
13+
"gopkg.in/yaml.v3"
1314

1415
"gopkg.d7z.net/gitea-pages/pkg"
1516
"gopkg.d7z.net/gitea-pages/pkg/providers"
17+
_ "gopkg.d7z.net/gitea-pages/pkg/renders"
1618
)
1719

1820
var (
1921
configPath = "config-local.yaml"
2022
debug = false
23+
generate = false
2124
)
2225

2326
func init() {
2427
flag.StringVar(&configPath, "conf", configPath, "config file path")
28+
flag.BoolVar(&generate, "generate", debug, "generate config file")
2529
flag.BoolVar(&debug, "debug", debug, "debug mode")
30+
flag.Parse()
2631
}
2732

2833
func main() {
29-
flag.Parse()
34+
if generate {
35+
var cfg Config
36+
file, err := os.ReadFile(configPath)
37+
if err == nil {
38+
_ = yaml.Unmarshal(file, &cfg)
39+
}
40+
out, err := yaml.Marshal(&cfg)
41+
if err != nil {
42+
log.Fatal("marshal config file failed", zap.Error(err))
43+
}
44+
err = os.WriteFile(configPath, out, 0o644)
45+
if err != nil {
46+
log.Fatal("write config file failed", zap.Error(err))
47+
}
48+
return
49+
}
50+
3051
call := logInject()
31-
defer call()
52+
defer func() {
53+
_ = call()
54+
}()
3255
config, err := LoadConfig(configPath)
3356
if err != nil {
3457
log.Fatalf("fail to load config file: %v", err)
@@ -48,9 +71,7 @@ func main() {
4871

4972
svc := http.Server{Addr: config.Bind, Handler: giteaServer}
5073
go func() {
51-
select {
52-
case <-ctx.Done():
53-
}
74+
<-ctx.Done()
5475
zap.L().Debug("shutdown gracefully")
5576
_ = svc.Close()
5677
}()

pkg/core/alias.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (a *DomainAlias) Bind(ctx context.Context, domains []string, owner, repo, b
4545
return err
4646
}
4747
}
48-
if domains == nil || len(domains) == 0 {
48+
if len(domains) == 0 {
4949
return nil
5050
}
5151
aliasMeta := &Alias{

0 commit comments

Comments
 (0)