package main import ( "flag" "log/slog" "net/http" "os" "path/filepath" "time" cacheproxy "git.jeffthecoder.xyz/guochao/cache-proxy" "github.com/getsentry/sentry-go" "gopkg.in/yaml.v3" ) 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 configFromFile(path string) (*cacheproxy.Config, error) { file, err := os.Open(path) if err != nil { return nil, err } defer file.Close() config := &cacheproxy.Config{ Upstreams: []cacheproxy.Upstream{ { Server: "https://mirrors.ustc.edu.cn", }, }, Storage: cacheproxy.Storage{ Type: "local", Local: &cacheproxy.LocalStorage{ Path: "./data", TemporaryFilePattern: "temp.*", }, Accel: cacheproxy.Accel{ ResponseWithHeaders: []string{"X-Sendfile", "X-Accel-Redirect"}, }, }, Misc: cacheproxy.MiscConfig{ FirstChunkBytes: 1024 * 1024 * 50, ChunkBytes: 1024 * 1024, }, Cache: cacheproxy.Cache{ RefreshAfter: time.Hour, }, } if err := yaml.NewDecoder(file).Decode(&config); err != nil { return nil, err } if config.Storage.Local != nil { localPath, err := filepath.Abs(config.Storage.Local.Path) if err != nil { return nil, err } config.Storage.Local.Path = localPath } return config, nil } func main() { flag.Parse() lvl := slog.LevelInfo if err := lvl.UnmarshalText([]byte(logLevel)); err != nil { slog.With("error", err).Error("failed to parse log level") os.Exit(-1) } else { slog.SetLogLoggerLevel(lvl) } if sentrydsn != "" { if err := sentry.Init(sentry.ClientOptions{ Dsn: sentrydsn, }); err != nil { slog.With("dsn", sentrydsn, "error", err).Error("failed to setup sentry") os.Exit(-1) } defer sentry.Flush(time.Second * 3) } config, err := configFromFile(configFilePath) if err != nil { panic(err) } ch := make(chan any, 10) for idx := 0; idx < 10; idx += 1 { ch <- idx } server := cacheproxy.NewServer(*config) http.HandleFunc("GET /{path...}", server.HandleRequestWithCache) slog.With("addr", ":8881").Info("serving app") if err := http.ListenAndServe(":8881", nil); err != nil { slog.With("error", err).Error("failed to start server") os.Exit(-1) } }