优化部分代码
This commit is contained in:
27
main.go
27
main.go
@@ -1,10 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
|
||||||
@@ -27,21 +30,29 @@ func init() {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
inject := debugInject()
|
call := logInject()
|
||||||
defer inject()
|
defer call()
|
||||||
loadConf()
|
loadConf()
|
||||||
gitea, err := providers.NewGitea(config.Auth.Server, config.Auth.Token)
|
gitea, err := providers.NewGitea(config.Auth.Server, config.Auth.Token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
server := pkg.NewPageServer(gitea, pkg.DefaultOptions(config.Domain))
|
giteaServer := pkg.NewPageServer(gitea, pkg.DefaultOptions(config.Domain))
|
||||||
mux := http.NewServeMux()
|
defer giteaServer.Close()
|
||||||
mux.Handle("/", server)
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
|
||||||
defer server.Close()
|
defer stop()
|
||||||
_ = http.ListenAndServe(config.Bind, mux)
|
svc := http.Server{Addr: config.Bind, Handler: giteaServer}
|
||||||
|
go func() {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
}
|
||||||
|
zap.L().Debug("shutdown gracefully")
|
||||||
|
_ = svc.Close()
|
||||||
|
}()
|
||||||
|
_ = svc.ListenAndServe()
|
||||||
}
|
}
|
||||||
|
|
||||||
func debugInject() func() error {
|
func logInject() func() error {
|
||||||
atom := zap.NewAtomicLevel()
|
atom := zap.NewAtomicLevel()
|
||||||
if debug {
|
if debug {
|
||||||
atom.SetLevel(zap.DebugLevel)
|
atom.SetLevel(zap.DebugLevel)
|
||||||
|
|||||||
50
pkg/core/alias.go
Normal file
50
pkg/core/alias.go
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
|
"code.d7z.net/d7z-project/gitea-pages/pkg/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Alias struct {
|
||||||
|
Owner string `json:"owner"`
|
||||||
|
Repo string `json:"repo"`
|
||||||
|
Branch string `json:"branch"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type DomainAlias struct {
|
||||||
|
config utils.Config
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDomainAlias(config utils.Config) *DomainAlias {
|
||||||
|
return &DomainAlias{config: config}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *DomainAlias) Query(domain string) (*Alias, error) {
|
||||||
|
get, err := a.config.Get("alias/" + domain)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
rel := &Alias{}
|
||||||
|
if err = json.Unmarshal([]byte(get), rel); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return rel, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *DomainAlias) Bind(domain, owner, repo, branch string) error {
|
||||||
|
save := &Alias{
|
||||||
|
Owner: owner,
|
||||||
|
Repo: repo,
|
||||||
|
Branch: branch,
|
||||||
|
}
|
||||||
|
saveB, err := json.Marshal(save)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return a.config.Put("domain/"+domain, string(saveB), utils.TtlKeep)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *DomainAlias) Unbind(domain string) error {
|
||||||
|
return a.config.Delete("domain/" + domain)
|
||||||
|
}
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestAny(t *testing.T) {
|
|
||||||
data := make(map[string]string)
|
|
||||||
err := json.Unmarshal([]byte(`{}`), &data)
|
|
||||||
require.NoError(t, err)
|
|
||||||
}
|
|
||||||
98
pkg/core/domain.go
Normal file
98
pkg/core/domain.go
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"code.d7z.net/d7z-project/gitea-pages/pkg/utils"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
)
|
||||||
|
|
||||||
|
var portExp = regexp.MustCompile(`:\d+$`)
|
||||||
|
|
||||||
|
type PageDomain struct {
|
||||||
|
*ServerMeta
|
||||||
|
|
||||||
|
alias *DomainAlias
|
||||||
|
baseDomain string
|
||||||
|
defaultBranch string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPageDomain(meta *ServerMeta, config utils.Config, baseDomain, defaultBranch string) *PageDomain {
|
||||||
|
return &PageDomain{
|
||||||
|
baseDomain: baseDomain,
|
||||||
|
defaultBranch: defaultBranch,
|
||||||
|
ServerMeta: meta,
|
||||||
|
alias: NewDomainAlias(config),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type PageDomainContent struct {
|
||||||
|
*PageMetaContent
|
||||||
|
|
||||||
|
Owner string
|
||||||
|
Repo string
|
||||||
|
Path string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *PageDomainContent) CacheKey() string {
|
||||||
|
return fmt.Sprintf("%s/%s/%s/%s", m.Owner, m.Repo, m.CommitID, m.Path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PageDomain) ParseDomainMeta(domain, path, branch string) (*PageDomainContent, error) {
|
||||||
|
if branch == "" {
|
||||||
|
branch = p.defaultBranch
|
||||||
|
}
|
||||||
|
domain = portExp.ReplaceAllString(strings.ToLower(domain), "")
|
||||||
|
pathS := strings.Split(strings.TrimPrefix(path, "/"), "/")
|
||||||
|
|
||||||
|
if !strings.HasSuffix(domain, "."+p.baseDomain) {
|
||||||
|
alias, err := p.alias.Query(domain)
|
||||||
|
if err != nil {
|
||||||
|
zap.L().Warn("未知域名", zap.String("base", p.baseDomain), zap.String("domain", domain))
|
||||||
|
return nil, os.ErrNotExist
|
||||||
|
}
|
||||||
|
zap.L().Debug("命中别名", zap.String("domain", domain), zap.Any("alias", alias))
|
||||||
|
return p.ReturnMeta(alias.Owner, alias.Repo, alias.Branch, pathS)
|
||||||
|
}
|
||||||
|
owner := strings.TrimSuffix(domain, "."+p.baseDomain)
|
||||||
|
repo := pathS[0]
|
||||||
|
if repo == "" {
|
||||||
|
// 回退到默认仓库
|
||||||
|
repo = p.baseDomain
|
||||||
|
zap.L().Debug("fail back to default repo", zap.String("repo", repo))
|
||||||
|
}
|
||||||
|
returnMeta, err := p.ReturnMeta(owner, repo, branch, pathS[1:])
|
||||||
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||||
|
return nil, err
|
||||||
|
} else if err == nil {
|
||||||
|
return returnMeta, nil
|
||||||
|
}
|
||||||
|
// 回退到默认页面
|
||||||
|
return p.ReturnMeta(owner, repo, domain, pathS)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PageDomain) ReturnMeta(owner string, repo string, branch string, path []string) (*PageDomainContent, error) {
|
||||||
|
rel := &PageDomainContent{}
|
||||||
|
if meta, err := p.GetMeta(owner, repo, branch); err == nil {
|
||||||
|
rel.PageMetaContent = meta
|
||||||
|
rel.Owner = owner
|
||||||
|
rel.Repo = repo
|
||||||
|
rel.Path = strings.Join(path, "/")
|
||||||
|
if strings.HasSuffix(rel.Path, "/") || rel.Path == "" {
|
||||||
|
rel.Path = rel.Path + "index.html"
|
||||||
|
}
|
||||||
|
if meta.Domain != "" {
|
||||||
|
err = p.alias.Bind(meta.Domain, rel.Owner, rel.Repo, branch)
|
||||||
|
if err != nil {
|
||||||
|
zap.L().Warn("别名绑定失败", zap.Error(err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return rel, nil
|
||||||
|
}
|
||||||
|
return nil, os.ErrNotExist
|
||||||
|
}
|
||||||
@@ -6,14 +6,19 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"go.uber.org/zap"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
"code.d7z.net/d7z-project/gitea-pages/pkg/utils"
|
"code.d7z.net/d7z-project/gitea-pages/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var regexpHostname = regexp.MustCompile(`^(?:([a-z0-9-]+|\*)\.)?([a-z0-9-]{1,61})\.([a-z0-9]{2,7})$`)
|
||||||
|
|
||||||
type ServerMeta struct {
|
type ServerMeta struct {
|
||||||
Backend
|
Backend
|
||||||
|
|
||||||
@@ -111,7 +116,12 @@ func (s *ServerMeta) GetMeta(owner, repo, branch string) (*PageMetaContent, erro
|
|||||||
if cname, err := s.ReadString(owner, repo, rel.CommitID, "CNAME"); err != nil && !errors.Is(err, os.ErrNotExist) {
|
if cname, err := s.ReadString(owner, repo, rel.CommitID, "CNAME"); err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
} else {
|
||||||
rel.Domain = strings.TrimSpace(cname)
|
cname = strings.TrimSpace(cname)
|
||||||
|
if regexpHostname.MatchString(cname) {
|
||||||
|
rel.Domain = cname
|
||||||
|
} else {
|
||||||
|
zap.L().Debug("指定的 CNAME 不合法", zap.String("cname", cname))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if find, _ := s.FileExists(owner, repo, rel.CommitID, ".history"); find {
|
if find, _ := s.FileExists(owner, repo, rel.CommitID, ".history"); find {
|
||||||
rel.HistoryRouteMode = true
|
rel.HistoryRouteMode = true
|
||||||
|
|||||||
@@ -1,87 +0,0 @@
|
|||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"go.uber.org/zap"
|
|
||||||
)
|
|
||||||
|
|
||||||
type PageDomain struct {
|
|
||||||
*ServerMeta
|
|
||||||
|
|
||||||
baseDomain string
|
|
||||||
defaultBranch string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewPageDomain(meta *ServerMeta, baseDomain, defaultBranch string) *PageDomain {
|
|
||||||
return &PageDomain{
|
|
||||||
baseDomain: baseDomain,
|
|
||||||
defaultBranch: defaultBranch,
|
|
||||||
ServerMeta: meta,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type PageDomainContent struct {
|
|
||||||
*PageMetaContent
|
|
||||||
|
|
||||||
Owner string
|
|
||||||
Repo string
|
|
||||||
Path string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *PageDomainContent) CacheKey() string {
|
|
||||||
return fmt.Sprintf("%s/%s/%s/%s", m.Owner, m.Repo, m.CommitID, m.Path)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *PageDomain) ParseDomainMeta(domain, path, branch string) (*PageDomainContent, error) {
|
|
||||||
if branch == "" {
|
|
||||||
branch = p.defaultBranch
|
|
||||||
}
|
|
||||||
domain = regexp.MustCompile(`:\d+$`).ReplaceAllString(domain, "")
|
|
||||||
|
|
||||||
rel := &PageDomainContent{}
|
|
||||||
if !strings.HasSuffix(domain, "."+p.baseDomain) {
|
|
||||||
zap.L().Warn("Page Domain does not end with ."+p.baseDomain, zap.String("domain", domain))
|
|
||||||
return nil, os.ErrNotExist
|
|
||||||
}
|
|
||||||
rel.Owner = strings.TrimSuffix(domain, "."+p.baseDomain)
|
|
||||||
pathS := strings.Split(strings.TrimPrefix(path, "/"), "/")
|
|
||||||
rel.Repo = pathS[0]
|
|
||||||
defaultRepo := rel.Owner + "." + p.baseDomain
|
|
||||||
if rel.Repo == "" {
|
|
||||||
// 回退到默认仓库
|
|
||||||
rel.Repo = defaultRepo
|
|
||||||
zap.L().Debug("fail back to default repo", zap.String("repo", defaultRepo))
|
|
||||||
}
|
|
||||||
|
|
||||||
meta, err := p.GetMeta(rel.Owner, rel.Repo, branch)
|
|
||||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if err == nil {
|
|
||||||
rel.Path = strings.Join(pathS[1:], "/")
|
|
||||||
if strings.HasSuffix(rel.Path, "/") || rel.Path == "" {
|
|
||||||
rel.Path = rel.Path + "index.html"
|
|
||||||
}
|
|
||||||
rel.PageMetaContent = meta
|
|
||||||
return rel, nil
|
|
||||||
}
|
|
||||||
if defaultRepo == rel.Repo {
|
|
||||||
return nil, os.ErrNotExist
|
|
||||||
}
|
|
||||||
if meta, err := p.GetMeta(rel.Owner, defaultRepo, branch); err == nil {
|
|
||||||
rel.PageMetaContent = meta
|
|
||||||
rel.Repo = defaultRepo
|
|
||||||
rel.Path = strings.Join(pathS[1:], "/")
|
|
||||||
if strings.HasSuffix(rel.Path, "/") || rel.Path == "" {
|
|
||||||
rel.Path = rel.Path + "index.html"
|
|
||||||
}
|
|
||||||
return rel, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, os.ErrNotExist
|
|
||||||
}
|
|
||||||
@@ -8,12 +8,10 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"go.uber.org/zap"
|
|
||||||
|
|
||||||
"code.d7z.net/d7z-project/gitea-pages/pkg/core"
|
"code.d7z.net/d7z-project/gitea-pages/pkg/core"
|
||||||
"code.d7z.net/d7z-project/gitea-pages/pkg/utils"
|
"code.d7z.net/d7z-project/gitea-pages/pkg/utils"
|
||||||
"github.com/pbnjay/memory"
|
"github.com/pbnjay/memory"
|
||||||
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ServerOptions struct {
|
type ServerOptions struct {
|
||||||
@@ -49,7 +47,7 @@ type Server struct {
|
|||||||
func NewPageServer(backend core.Backend, options ServerOptions) *Server {
|
func NewPageServer(backend core.Backend, options ServerOptions) *Server {
|
||||||
backend = core.NewCacheBackend(backend, options.Config, time.Minute)
|
backend = core.NewCacheBackend(backend, options.Config, time.Minute)
|
||||||
svcMeta := core.NewServerMeta(options.HttpClient, backend, options.Config, time.Minute)
|
svcMeta := core.NewServerMeta(options.HttpClient, backend, options.Config, time.Minute)
|
||||||
pageMeta := core.NewPageDomain(svcMeta, options.Domain, options.DefaultBranch)
|
pageMeta := core.NewPageDomain(svcMeta, options.Config, options.Domain, options.DefaultBranch)
|
||||||
reader := core.NewCacheBackendBlobReader(options.HttpClient, backend, options.Cache, options.MaxCacheSize)
|
reader := core.NewCacheBackendBlobReader(options.HttpClient, backend, options.Cache, options.MaxCacheSize)
|
||||||
return &Server{
|
return &Server{
|
||||||
meta: pageMeta,
|
meta: pageMeta,
|
||||||
@@ -59,45 +57,36 @@ func NewPageServer(backend core.Backend, options ServerOptions) *Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||||
|
err := s.Serve(writer, request)
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
http.Error(writer, "page not found.", http.StatusNotFound)
|
||||||
|
} else {
|
||||||
|
http.Error(writer, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) Serve(writer http.ResponseWriter, request *http.Request) error {
|
||||||
meta, err := s.meta.ParseDomainMeta(request.Host, request.URL.Path, request.URL.Query().Get("branch"))
|
meta, err := s.meta.ParseDomainMeta(request.Host, request.URL.Path, request.URL.Query().Get("branch"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
return err
|
||||||
s.writeNotfoundError(writer, request.RequestURI)
|
|
||||||
} else {
|
|
||||||
s.writeError(writer, err)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
result, err := s.reader.Open(meta.Owner, meta.Repo, meta.CommitID, meta.Path)
|
result, err := s.reader.Open(meta.Owner, meta.Repo, meta.CommitID, meta.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// todo: 添加默认返回
|
return err
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
|
||||||
s.writeNotfoundError(writer, request.RequestURI)
|
|
||||||
} else {
|
|
||||||
s.writeError(writer, err)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
fileName := filepath.Base(meta.Path)
|
fileName := filepath.Base(meta.Path)
|
||||||
if reader, ok := result.(*utils.CacheContent); ok {
|
if reader, ok := result.(*utils.CacheContent); ok {
|
||||||
|
writer.Header().Add("X-Cache", "HIT")
|
||||||
http.ServeContent(writer, request, fileName, reader.LastModified, reader)
|
http.ServeContent(writer, request, fileName, reader.LastModified, reader)
|
||||||
_ = reader.Close()
|
_ = reader.Close()
|
||||||
return
|
} else {
|
||||||
}
|
writer.Header().Add("X-Cache", "MISS")
|
||||||
writer.Header().Set("Content-Type", mime.TypeByExtension(meta.Path))
|
writer.Header().Set("Content-Type", mime.TypeByExtension(meta.Path))
|
||||||
writer.WriteHeader(http.StatusOK)
|
writer.WriteHeader(http.StatusOK)
|
||||||
_, _ = io.Copy(writer, result)
|
_, _ = io.Copy(writer, result)
|
||||||
_ = result.Close()
|
_ = result.Close()
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
func (s *Server) writeError(writer http.ResponseWriter, err error) {
|
|
||||||
zap.L().Error("write error", zap.Error(err))
|
|
||||||
http.Error(writer, err.Error(), http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) writeNotfoundError(writer http.ResponseWriter, path string) {
|
|
||||||
http.Error(writer, "page not found.", http.StatusNotFound)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Close() error {
|
func (s *Server) Close() error {
|
||||||
|
|||||||
@@ -17,6 +17,14 @@ type CacheContent struct {
|
|||||||
LastModified time.Time
|
LastModified time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *CacheContent) ReadToString() (string, error) {
|
||||||
|
all, err := io.ReadAll(c)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(all), nil
|
||||||
|
}
|
||||||
|
|
||||||
type Cache interface {
|
type Cache interface {
|
||||||
Put(key string, reader io.Reader) error
|
Put(key string, reader io.Reader) error
|
||||||
// Get return CacheContent or nil when put nil io.reader
|
// Get return CacheContent or nil when put nil io.reader
|
||||||
|
|||||||
@@ -6,8 +6,12 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const TtlKeep = -1
|
||||||
|
|
||||||
type Config interface {
|
type Config interface {
|
||||||
Put(key string, value string, ttl time.Duration) error
|
Put(key string, value string, ttl time.Duration) error
|
||||||
Get(key string) (string, error)
|
Get(key string) (string, error)
|
||||||
@@ -36,7 +40,7 @@ func NewConfigMemory(store string) (Config, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for key, content := range item {
|
for key, content := range item {
|
||||||
if time.Now().Before(content.ttl) {
|
if content.Ttl == nil || time.Now().Before(*content.Ttl) {
|
||||||
ret.data.Store(key, content)
|
ret.data.Store(key, content)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -46,14 +50,19 @@ func NewConfigMemory(store string) (Config, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type configContent struct {
|
type configContent struct {
|
||||||
data string
|
Data string `json:"data"`
|
||||||
ttl time.Time
|
Ttl *time.Time `json:"ttl,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *ConfigMemory) Put(key string, value string, ttl time.Duration) error {
|
func (m *ConfigMemory) Put(key string, value string, ttl time.Duration) error {
|
||||||
|
d := time.Now().Add(ttl)
|
||||||
|
td := &d
|
||||||
|
if ttl == -1 {
|
||||||
|
td = nil
|
||||||
|
}
|
||||||
m.data.Store(key, configContent{
|
m.data.Store(key, configContent{
|
||||||
data: value,
|
Data: value,
|
||||||
ttl: time.Now().Add(ttl),
|
Ttl: td,
|
||||||
})
|
})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -61,10 +70,10 @@ func (m *ConfigMemory) Put(key string, value string, ttl time.Duration) error {
|
|||||||
func (m *ConfigMemory) Get(key string) (string, error) {
|
func (m *ConfigMemory) Get(key string) (string, error) {
|
||||||
if value, ok := m.data.Load(key); ok {
|
if value, ok := m.data.Load(key); ok {
|
||||||
content := value.(configContent)
|
content := value.(configContent)
|
||||||
if time.Now().After(content.ttl) {
|
if content.Ttl != nil && time.Now().After(*content.Ttl) {
|
||||||
return "", os.ErrNotExist
|
return "", os.ErrNotExist
|
||||||
}
|
}
|
||||||
return content.data, nil
|
return content.Data, nil
|
||||||
}
|
}
|
||||||
return "", os.ErrNotExist
|
return "", os.ErrNotExist
|
||||||
}
|
}
|
||||||
@@ -78,11 +87,16 @@ func (m *ConfigMemory) Close() error {
|
|||||||
defer m.data.Clear()
|
defer m.data.Clear()
|
||||||
if m.store != "" {
|
if m.store != "" {
|
||||||
item := make(map[string]configContent)
|
item := make(map[string]configContent)
|
||||||
|
now := time.Now()
|
||||||
m.data.Range(
|
m.data.Range(
|
||||||
func(key, value interface{}) bool {
|
func(key, value interface{}) bool {
|
||||||
item[key.(string)] = value.(configContent)
|
content := value.(configContent)
|
||||||
|
if content.Ttl == nil || now.Before(*content.Ttl) {
|
||||||
|
item[key.(string)] = content
|
||||||
|
}
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
zap.L().Debug("回写内容到本地存储", zap.String("store", m.store), zap.Int("length", len(item)))
|
||||||
saved, err := json.Marshal(item)
|
saved, err := json.Marshal(item)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
package utils
|
|
||||||
|
|
||||||
import "github.com/pkg/errors"
|
|
||||||
|
|
||||||
type StackTracer interface {
|
|
||||||
StackTrace() errors.StackTrace
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user