Skip to content

Commit 0d49f33

Browse files
优化本地调试
1 parent c195f18 commit 0d49f33

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

cmd/local/main.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@ package main
22

33
import (
44
"context"
5+
"encoding/json"
56
"flag"
67
"fmt"
78
"io"
89
"net/http"
910
"os"
11+
"path/filepath"
1012
"time"
1113

1214
"github.com/pkg/errors"
1315
"go.uber.org/zap"
1416
"gopkg.d7z.net/gitea-pages/pkg"
17+
"gopkg.d7z.net/gitea-pages/pkg/core"
1518
"gopkg.d7z.net/gitea-pages/pkg/providers"
1619
"gopkg.d7z.net/middleware/cache"
1720
"gopkg.d7z.net/middleware/kv"
1821
"gopkg.d7z.net/middleware/subscribe"
22+
"gopkg.in/yaml.v3"
1923
)
2024

2125
var (
@@ -53,6 +57,18 @@ func main() {
5357
provider := providers.NewLocalProvider(map[string][]string{
5458
org: {repo},
5559
}, path)
60+
61+
file, _ := os.ReadFile(filepath.Join(path, ".pages.yaml"))
62+
if file != nil {
63+
var info core.PageConfig
64+
err := yaml.Unmarshal(file, &info)
65+
if err != nil {
66+
zap.L().Fatal("parse yaml", zap.Error(err))
67+
}
68+
info.Alias = []string{}
69+
marshal, _ := json.Marshal(info)
70+
provider.AddOverlay(".pages.yaml", marshal)
71+
}
5672
memory, err := kv.NewMemory("")
5773
if err != nil {
5874
zap.L().Fatal("failed to init memory provider", zap.Error(err))
@@ -66,7 +82,11 @@ func main() {
6682
} else if err != nil {
6783
http.Error(w, err.Error(), http.StatusInternalServerError)
6884
}
69-
}, make(map[string]map[string]any))
85+
}, map[string]map[string]any{
86+
"redirect": {
87+
"scheme": "http",
88+
},
89+
})
7090
if err != nil {
7191
zap.L().Fatal("failed to init page", zap.Error(err))
7292
}

pkg/providers/local.go

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,33 @@ import (
1212
"path/filepath"
1313
"slices"
1414
"strconv"
15+
"strings"
1516
"time"
1617

1718
"gopkg.d7z.net/gitea-pages/pkg/core"
1819
)
1920

2021
type LocalProvider struct {
21-
graph map[string][]string
22-
path string
22+
graph map[string][]string
23+
path string
24+
overlay map[string][]byte
2325
}
2426

2527
func NewLocalProvider(
2628
graph map[string][]string,
2729
path string,
2830
) *LocalProvider {
2931
return &LocalProvider{
30-
graph: graph,
31-
path: path,
32+
graph: graph,
33+
path: path,
34+
overlay: map[string][]byte{},
3235
}
3336
}
3437

38+
func (l *LocalProvider) AddOverlay(path string, overlay []byte) {
39+
l.overlay[strings.Trim(path, "/")] = overlay
40+
}
41+
3542
func (l *LocalProvider) Close() error {
3643
return nil
3744
}
@@ -65,20 +72,28 @@ func (l *LocalProvider) Branches(_ context.Context, owner, repo string) (map[str
6572
}
6673

6774
func (l *LocalProvider) Open(_ context.Context, _, _, _, path string, _ http.Header) (*http.Response, error) {
68-
open, err := os.Open(filepath.Join(l.path, path))
69-
if err != nil {
70-
return nil, errors.Join(err, os.ErrNotExist)
71-
}
72-
defer open.Close()
73-
all, err := io.ReadAll(open)
74-
if err != nil {
75-
return nil, errors.Join(err, os.ErrNotExist)
76-
}
75+
var all []byte
7776
recorder := httptest.NewRecorder()
77+
if data, ok := l.overlay[strings.Trim(path, "/")]; ok {
78+
all = data
79+
recorder.Header().Add("Content-Length", strconv.FormatInt(int64(len(data)), 10))
80+
} else {
81+
open, err := os.Open(filepath.Join(l.path, path))
82+
if err != nil {
83+
return nil, errors.Join(err, os.ErrNotExist)
84+
}
85+
defer open.Close()
86+
all, err = io.ReadAll(open)
87+
if err != nil {
88+
return nil, errors.Join(err, os.ErrNotExist)
89+
}
90+
stat, _ := open.Stat()
91+
recorder.Header().Add("Content-Length", strconv.FormatInt(stat.Size(), 10))
92+
recorder.Header().Add("Last-Modified", stat.ModTime().Format(http.TimeFormat))
93+
}
94+
7895
recorder.Body = bytes.NewBuffer(all)
7996
recorder.Header().Add("Content-Type", mime.TypeByExtension(filepath.Ext(path)))
80-
stat, _ := open.Stat()
81-
recorder.Header().Add("Content-Length", strconv.FormatInt(stat.Size(), 10))
82-
recorder.Header().Add("Last-Modified", stat.ModTime().Format(http.TimeFormat))
97+
8398
return recorder.Result(), nil
8499
}

0 commit comments

Comments
 (0)