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

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)
}