This commit is contained in:
guochao 2024-12-18 10:47:18 +08:00
parent 561db9f840
commit 266f9f565b
6 changed files with 65 additions and 2 deletions

4
.dockerignore Normal file
View File

@ -0,0 +1,4 @@
.vscode
data
__debug*

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.vscode
data
__debug*

10
Dockerfile Normal file
View File

@ -0,0 +1,10 @@
FROM golang:1.23-alpine
ADD . /src
WORKDIR /src
RUN go build -o /cache-proxy ./cmd/proxy
FROM alpine:3.21 as runtime
COPY --from=0 /cache-proxy /bin/cache-proxy
WORKDIR /etc/
VOLUME /data
CMD cache-proxy

View File

@ -3,6 +3,7 @@ package main
import (
"bytes"
"context"
"flag"
"fmt"
"io"
"net/http"
@ -14,6 +15,8 @@ import (
_ "net/http/pprof"
"github.com/getsentry/sentry-go"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
@ -281,6 +284,7 @@ func (server *Server) streamOnline(w http.ResponseWriter, r *http.Request, mtime
}
if response.StatusCode == http.StatusNotModified {
logrus.WithField("upstreamIdx", selectedIdx).Trace("not modified. using local version")
os.Chtimes(key, zeroTime, time.Now())
http.ServeFile(w, r, key)
return
}
@ -546,13 +550,51 @@ func (server *Server) tryUpstream(ctx context.Context, upstreamIdx int, r *http.
return response, ch, nil
}
var (
configFilePath = "config.yaml"
logLevel = "info"
sentrydsn = ""
)
func init() {
if v, ok := os.LookupEnv("CONFIG_PATH"); ok {
configFilePath = v
}
if v, ok := os.LookupEnv("LOG_LEVEL"); ok {
logLevel = v
}
if v, ok := os.LookupEnv("SENTRY_DSN"); ok {
sentrydsn = v
}
flag.StringVar(&configFilePath, "config", configFilePath, "path to config file")
flag.StringVar(&logLevel, "log-level", logLevel, "log level. (trace, debug, info, warn, error)")
flag.StringVar(&sentrydsn, "sentry", sentrydsn, "sentry dsn to report errors")
}
func main() {
logrus.SetLevel(logrus.TraceLevel)
flag.Parse()
if lvl, err := logrus.ParseLevel(logLevel); err != nil {
logrus.WithError(err).Panic("failed to parse log level")
} else {
logrus.SetLevel(lvl)
}
if sentrydsn != "" {
if err := sentry.Init(sentry.ClientOptions{
Dsn: sentrydsn,
}); err != nil {
logrus.WithField("dsn", sentrydsn).WithError(err).Panic("failed to setup sentry")
}
defer sentry.Flush(time.Second * 3)
}
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02T15:04:05.000",
})
config, err := configFromFile("config.yaml")
config, err := configFromFile(configFilePath)
if err != nil {
panic(err)
}

1
go.mod
View File

@ -3,6 +3,7 @@ module git.jeffthecoder.xyz/guochao/cache-proxy
go 1.23.3
require (
github.com/getsentry/sentry-go v0.30.0
github.com/pkg/xattr v0.4.10
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.1

2
go.sum
View File

@ -52,6 +52,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/getsentry/sentry-go v0.30.0 h1:lWUwDnY7sKHaVIoZ9wYqRHJ5iEmoc0pqcRqFkosKzBo=
github.com/getsentry/sentry-go v0.30.0/go.mod h1:WU9B9/1/sHDqeV8T+3VwwbjeR5MSXs/6aqG3mqZrezA=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=