优化本地调试
This commit is contained in:
@@ -2,20 +2,24 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"gopkg.d7z.net/gitea-pages/pkg"
|
"gopkg.d7z.net/gitea-pages/pkg"
|
||||||
|
"gopkg.d7z.net/gitea-pages/pkg/core"
|
||||||
"gopkg.d7z.net/gitea-pages/pkg/providers"
|
"gopkg.d7z.net/gitea-pages/pkg/providers"
|
||||||
"gopkg.d7z.net/middleware/cache"
|
"gopkg.d7z.net/middleware/cache"
|
||||||
"gopkg.d7z.net/middleware/kv"
|
"gopkg.d7z.net/middleware/kv"
|
||||||
"gopkg.d7z.net/middleware/subscribe"
|
"gopkg.d7z.net/middleware/subscribe"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -53,6 +57,18 @@ func main() {
|
|||||||
provider := providers.NewLocalProvider(map[string][]string{
|
provider := providers.NewLocalProvider(map[string][]string{
|
||||||
org: {repo},
|
org: {repo},
|
||||||
}, path)
|
}, path)
|
||||||
|
|
||||||
|
file, _ := os.ReadFile(filepath.Join(path, ".pages.yaml"))
|
||||||
|
if file != nil {
|
||||||
|
var info core.PageConfig
|
||||||
|
err := yaml.Unmarshal(file, &info)
|
||||||
|
if err != nil {
|
||||||
|
zap.L().Fatal("parse yaml", zap.Error(err))
|
||||||
|
}
|
||||||
|
info.Alias = []string{}
|
||||||
|
marshal, _ := json.Marshal(info)
|
||||||
|
provider.AddOverlay(".pages.yaml", marshal)
|
||||||
|
}
|
||||||
memory, err := kv.NewMemory("")
|
memory, err := kv.NewMemory("")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
zap.L().Fatal("failed to init memory provider", zap.Error(err))
|
zap.L().Fatal("failed to init memory provider", zap.Error(err))
|
||||||
@@ -66,7 +82,11 @@ func main() {
|
|||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
}, make(map[string]map[string]any))
|
}, map[string]map[string]any{
|
||||||
|
"redirect": {
|
||||||
|
"scheme": "http",
|
||||||
|
},
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
zap.L().Fatal("failed to init page", zap.Error(err))
|
zap.L().Fatal("failed to init page", zap.Error(err))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,14 +12,16 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gopkg.d7z.net/gitea-pages/pkg/core"
|
"gopkg.d7z.net/gitea-pages/pkg/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
type LocalProvider struct {
|
type LocalProvider struct {
|
||||||
graph map[string][]string
|
graph map[string][]string
|
||||||
path string
|
path string
|
||||||
|
overlay map[string][]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLocalProvider(
|
func NewLocalProvider(
|
||||||
@@ -27,11 +29,16 @@ func NewLocalProvider(
|
|||||||
path string,
|
path string,
|
||||||
) *LocalProvider {
|
) *LocalProvider {
|
||||||
return &LocalProvider{
|
return &LocalProvider{
|
||||||
graph: graph,
|
graph: graph,
|
||||||
path: path,
|
path: path,
|
||||||
|
overlay: map[string][]byte{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *LocalProvider) AddOverlay(path string, overlay []byte) {
|
||||||
|
l.overlay[strings.Trim(path, "/")] = overlay
|
||||||
|
}
|
||||||
|
|
||||||
func (l *LocalProvider) Close() error {
|
func (l *LocalProvider) Close() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -65,20 +72,28 @@ func (l *LocalProvider) Branches(_ context.Context, owner, repo string) (map[str
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *LocalProvider) Open(_ context.Context, _, _, _, path string, _ http.Header) (*http.Response, error) {
|
func (l *LocalProvider) Open(_ context.Context, _, _, _, path string, _ http.Header) (*http.Response, error) {
|
||||||
open, err := os.Open(filepath.Join(l.path, path))
|
var all []byte
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Join(err, os.ErrNotExist)
|
|
||||||
}
|
|
||||||
defer open.Close()
|
|
||||||
all, err := io.ReadAll(open)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Join(err, os.ErrNotExist)
|
|
||||||
}
|
|
||||||
recorder := httptest.NewRecorder()
|
recorder := httptest.NewRecorder()
|
||||||
|
if data, ok := l.overlay[strings.Trim(path, "/")]; ok {
|
||||||
|
all = data
|
||||||
|
recorder.Header().Add("Content-Length", strconv.FormatInt(int64(len(data)), 10))
|
||||||
|
} else {
|
||||||
|
open, err := os.Open(filepath.Join(l.path, path))
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Join(err, os.ErrNotExist)
|
||||||
|
}
|
||||||
|
defer open.Close()
|
||||||
|
all, err = io.ReadAll(open)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Join(err, os.ErrNotExist)
|
||||||
|
}
|
||||||
|
stat, _ := open.Stat()
|
||||||
|
recorder.Header().Add("Content-Length", strconv.FormatInt(stat.Size(), 10))
|
||||||
|
recorder.Header().Add("Last-Modified", stat.ModTime().Format(http.TimeFormat))
|
||||||
|
}
|
||||||
|
|
||||||
recorder.Body = bytes.NewBuffer(all)
|
recorder.Body = bytes.NewBuffer(all)
|
||||||
recorder.Header().Add("Content-Type", mime.TypeByExtension(filepath.Ext(path)))
|
recorder.Header().Add("Content-Type", mime.TypeByExtension(filepath.Ext(path)))
|
||||||
stat, _ := open.Stat()
|
|
||||||
recorder.Header().Add("Content-Length", strconv.FormatInt(stat.Size(), 10))
|
|
||||||
recorder.Header().Add("Last-Modified", stat.ModTime().Format(http.TimeFormat))
|
|
||||||
return recorder.Result(), nil
|
return recorder.Result(), nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user