Skip to content

Commit 3492dea

Browse files
重构项目
1 parent d67dbf8 commit 3492dea

34 files changed

+715
-444
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,8 @@ lint:
5454
lint-fix:
5555
@(test -f "$(GOPATH)/bin/golangci-lint" || go install github.com/golangci/golangci-lint/v2/cmd/[email protected]) && \
5656
"$(GOPATH)/bin/golangci-lint" run -c .golangci.yml --fix
57+
58+
EXAMPLE_DIRS := $(shell find examples -maxdepth 1 -type d ! -path "examples" | sort)
59+
.PHONY: $(EXAMPLE_DIRS)
60+
$(EXAMPLE_DIRS):
61+
@go run ./cmd/local/main.go -path $@

cmd/local/main.go

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
package main
22

33
import (
4+
"context"
45
"flag"
6+
"fmt"
7+
"io"
8+
"net/http"
59
"os"
10+
"time"
11+
12+
"github.com/pkg/errors"
13+
"go.uber.org/zap"
14+
"gopkg.d7z.net/gitea-pages/pkg"
15+
"gopkg.d7z.net/gitea-pages/pkg/providers"
16+
"gopkg.d7z.net/middleware/cache"
17+
"gopkg.d7z.net/middleware/kv"
618
)
719

820
var (
@@ -11,19 +23,68 @@ var (
1123
repo = org + "." + domain
1224
path = ""
1325

14-
port = 8080
26+
port = ":8080"
1527
)
1628

1729
func init() {
30+
atom := zap.NewAtomicLevel()
31+
atom.SetLevel(zap.DebugLevel)
32+
cfg := zap.NewProductionConfig()
33+
cfg.Level = atom
34+
logger, _ := cfg.Build()
35+
zap.ReplaceGlobals(logger)
1836
dir, _ := os.Getwd()
1937
path = dir
38+
zap.L().Info("exec workdir", zap.String("path", path))
2039
flag.StringVar(&org, "org", org, "org")
2140
flag.StringVar(&repo, "repo", repo, "repo")
2241
flag.StringVar(&domain, "domain", domain, "domain")
2342
flag.StringVar(&path, "path", path, "path")
24-
flag.IntVar(&port, "port", port, "port")
43+
flag.StringVar(&port, "port", port, "port")
2544
flag.Parse()
2645
}
2746

2847
func main() {
48+
fmt.Printf("请访问 http://%s%s/", repo, port)
49+
if stat, err := os.Stat(path); err != nil || !stat.IsDir() {
50+
zap.L().Fatal("path is not a directory", zap.String("path", path))
51+
}
52+
provider := providers.NewLocalProvider(map[string][]string{
53+
org: {repo},
54+
}, path)
55+
memory, err := kv.NewMemory("")
56+
if err != nil {
57+
zap.L().Fatal("failed to init memory provider", zap.Error(err))
58+
}
59+
server := pkg.NewPageServer(http.DefaultClient,
60+
provider, domain, "gh-pages", memory, memory, 0, &nopCache{},
61+
func(w http.ResponseWriter, r *http.Request, err error) {
62+
if errors.Is(err, os.ErrNotExist) {
63+
http.Error(w, "page not found.", http.StatusNotFound)
64+
} else if err != nil {
65+
http.Error(w, err.Error(), http.StatusInternalServerError)
66+
}
67+
})
68+
err = http.ListenAndServe(port, server)
69+
if err != nil && !errors.Is(err, http.ErrServerClosed) {
70+
zap.L().Fatal("failed to start server", zap.Error(err))
71+
}
72+
}
73+
74+
type nopCache struct{}
75+
76+
func (n *nopCache) Child(_ string) cache.Cache {
77+
return n
78+
}
79+
80+
func (n *nopCache) Put(_ context.Context, _ string, _ map[string]string, _ io.Reader, _ time.Duration) error {
81+
return nil
82+
}
83+
84+
func (n *nopCache) Get(_ context.Context, _ string) (*cache.Content, error) {
85+
return nil, os.ErrNotExist
86+
}
87+
88+
func (n *nopCache) Delete(_ context.Context, _ string) error {
89+
return nil
2990
}

cmd/server/main.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"errors"
56
"flag"
67
"fmt"
78
"log"
@@ -36,7 +37,7 @@ func main() {
3637
log.Fatalf("fail to load config file: %v", err)
3738
}
3839

39-
gitea, err := providers.NewGitea(config.Auth.Server, config.Auth.Token)
40+
gitea, err := providers.NewGitea(http.DefaultClient, config.Auth.Server, config.Auth.Token)
4041
if err != nil {
4142
log.Fatalln(err)
4243
}
@@ -51,22 +52,27 @@ func main() {
5152
}
5253
defer cacheBlob.Close()
5354
backend := providers.NewProviderCache(gitea, cacheMeta, config.Cache.MetaTTL,
54-
cacheBlob, uint64(config.Cache.BlobLimit),
55+
cacheBlob.Child("backend"), uint64(config.Cache.BlobLimit),
5556
)
5657
defer backend.Close()
5758
db, err := kv.NewKVFromURL(config.Database.URL)
5859
if err != nil {
5960
log.Fatalln(err)
6061
}
6162
defer db.Close()
63+
cdb, ok := db.(kv.RawKV).Raw().(kv.CursorPagedKV)
64+
if !ok {
65+
log.Fatalln(errors.New("database not support cursor"))
66+
}
6267
pageServer := pkg.NewPageServer(
6368
http.DefaultClient,
6469
backend,
6570
config.Domain,
6671
config.Page.DefaultBranch,
67-
db,
72+
cdb,
6873
cacheMeta,
6974
config.Cache.MetaTTL,
75+
cacheBlob.Child("filter"),
7076
config.ErrorHandler,
7177
)
7278
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)

examples/HelloWorld/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport"
6+
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
7+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
8+
<title>qjs 验证</title>
9+
</head>
10+
<body>
11+
<p>Hello World</p>
12+
</body>
13+
</html>

examples/QuickJS/.pages.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
routes:
2+
- path: "api/v1/**"
3+
qjs:
4+
exec: "index.js"
5+
debug: true

examples/QuickJS/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport"
6+
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
7+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
8+
<title>Document</title>
9+
</head>
10+
<body>
11+
12+
</body>
13+
</html>

examples/QuickJS/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
response.write("hello world")
2+
console.log("hello world")
3+
console.log(req.methaaod)
4+
function aaa(){
5+
throw Error("Method not implemented")
6+
}
7+
aaa()

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/pkg/errors v0.9.1
1212
github.com/stretchr/testify v1.11.1
1313
go.uber.org/zap v1.27.0
14-
gopkg.d7z.net/middleware v0.0.0-20251114092249-67753b883a45
14+
gopkg.d7z.net/middleware v0.0.0-20251114145539-bb74bd940f32
1515
gopkg.in/yaml.v3 v3.0.1
1616
)
1717

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ gopkg.d7z.net/middleware v0.0.0-20251113064153-9f946bf959f5 h1:RwEXivoUP8qEbKxRW
195195
gopkg.d7z.net/middleware v0.0.0-20251113064153-9f946bf959f5/go.mod h1:BJ8ySXqmlBpM9B2zFJfmvYQ61XPA+G0O1VDmYomxyrM=
196196
gopkg.d7z.net/middleware v0.0.0-20251114092249-67753b883a45 h1:ujyhl4Di/z6fGOcIAqydzRQNwI13F9JD3xj8+s+rTVM=
197197
gopkg.d7z.net/middleware v0.0.0-20251114092249-67753b883a45/go.mod h1:BJ8ySXqmlBpM9B2zFJfmvYQ61XPA+G0O1VDmYomxyrM=
198+
gopkg.d7z.net/middleware v0.0.0-20251114132513-93389190aeca h1:5RUUXCIUFBMNvCXiYNDdcEu27HeNr3mfH7MRKS1ftdo=
199+
gopkg.d7z.net/middleware v0.0.0-20251114132513-93389190aeca/go.mod h1:/1/EuissKhUbuhUe01rcWuwpA5mt7jASb4uKVNOLtR8=
200+
gopkg.d7z.net/middleware v0.0.0-20251114144707-95f41bfca5bc h1:mPcaskQN8j32dI59txtCAFVIKUb427bh7sXS9adv2jM=
201+
gopkg.d7z.net/middleware v0.0.0-20251114144707-95f41bfca5bc/go.mod h1:/1/EuissKhUbuhUe01rcWuwpA5mt7jASb4uKVNOLtR8=
202+
gopkg.d7z.net/middleware v0.0.0-20251114145539-bb74bd940f32 h1:3JvqnWFLWzAoS57vLBT1LVePO3RqR32ijM3ZyjyoqyY=
203+
gopkg.d7z.net/middleware v0.0.0-20251114145539-bb74bd940f32/go.mod h1:/1/EuissKhUbuhUe01rcWuwpA5mt7jASb4uKVNOLtR8=
198204
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
199205
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
200206
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=

pkg/core/backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ type Backend interface {
1919
// Branches return branch + commit id
2020
Branches(ctx context.Context, owner, repo string) (map[string]*BranchInfo, error)
2121
// Open return file or error (error)
22-
Open(ctx context.Context, client *http.Client, owner, repo, commit, path string, headers http.Header) (*http.Response, error)
22+
Open(ctx context.Context, owner, repo, commit, path string, headers http.Header) (*http.Response, error)
2323
}

0 commit comments

Comments
 (0)