143 lines
3.2 KiB
Go
Raw Permalink Normal View History

2024-12-16 21:11:32 +08:00
package main
import (
2024-12-18 10:47:18 +08:00
"flag"
2024-12-25 11:00:02 +08:00
"log/slog"
2024-12-16 21:11:32 +08:00
"net/http"
2025-03-03 00:26:29 +08:00
"net/http/pprof"
2024-12-16 21:11:32 +08:00
"os"
"path/filepath"
"time"
2025-01-09 23:30:42 +08:00
cacheproxy "git.jeffthecoder.xyz/guochao/cache-proxy"
2025-01-10 01:20:45 +08:00
"git.jeffthecoder.xyz/guochao/cache-proxy/pkgs/middleware"
2025-03-03 09:59:19 +08:00
"git.jeffthecoder.xyz/guochao/cache-proxy/pkgs/middleware/handlerctx"
2025-01-10 01:20:45 +08:00
"git.jeffthecoder.xyz/guochao/cache-proxy/pkgs/middleware/httplog"
"git.jeffthecoder.xyz/guochao/cache-proxy/pkgs/middleware/recover"
2024-12-18 10:47:18 +08:00
"github.com/getsentry/sentry-go"
2024-12-16 21:11:32 +08:00
"gopkg.in/yaml.v3"
)
2025-01-09 23:30:42 +08:00
var (
configFilePath = "config.yaml"
logLevel = "info"
sentrydsn = ""
2025-03-03 00:26:29 +08:00
pprofEnabled = false
2025-01-09 23:30:42 +08:00
)
2024-12-16 21:11:32 +08:00
2025-01-09 23:30:42 +08:00
func init() {
if v, ok := os.LookupEnv("CONFIG_PATH"); ok {
configFilePath = v
2024-12-16 21:11:32 +08:00
}
2025-01-09 23:30:42 +08:00
if v, ok := os.LookupEnv("LOG_LEVEL"); ok {
logLevel = v
2024-12-16 21:11:32 +08:00
}
2025-01-09 23:30:42 +08:00
flag.StringVar(&configFilePath, "config", configFilePath, "path to config file")
flag.StringVar(&logLevel, "log-level", logLevel, "log level. (trace, debug, info, warn, error)")
2024-12-16 21:11:32 +08:00
}
2025-01-09 23:30:42 +08:00
func configFromFile(path string) (*cacheproxy.Config, error) {
2024-12-16 21:11:32 +08:00
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
2025-01-09 23:30:42 +08:00
config := &cacheproxy.Config{
Upstreams: []cacheproxy.Upstream{
2024-12-16 21:11:32 +08:00
{
Server: "https://mirrors.ustc.edu.cn",
},
},
2025-01-09 23:30:42 +08:00
Storage: cacheproxy.Storage{
2024-12-16 21:11:32 +08:00
Type: "local",
2025-01-09 23:30:42 +08:00
Local: &cacheproxy.LocalStorage{
2024-12-25 11:00:02 +08:00
Path: "./data",
TemporaryFilePattern: "temp.*",
Accel: cacheproxy.Accel{
RespondWithHeaders: []string{"X-Sendfile", "X-Accel-Redirect"},
},
2024-12-18 17:16:55 +08:00
},
2024-12-16 21:11:32 +08:00
},
2025-01-09 23:30:42 +08:00
Misc: cacheproxy.MiscConfig{
2024-12-16 21:11:32 +08:00
FirstChunkBytes: 1024 * 1024 * 50,
ChunkBytes: 1024 * 1024,
},
2025-01-09 23:30:42 +08:00
Cache: cacheproxy.Cache{
2024-12-19 00:03:22 +08:00
RefreshAfter: time.Hour,
2024-12-16 21:11:32 +08:00
},
}
if err := yaml.NewDecoder(file).Decode(&config); err != nil {
return nil, err
}
2024-12-18 17:08:47 +08:00
if config.Storage.Local != nil {
localPath, err := filepath.Abs(config.Storage.Local.Path)
if err != nil {
return nil, err
}
config.Storage.Local.Path = localPath
}
2024-12-16 21:11:32 +08:00
return config, nil
}
func main() {
2024-12-18 10:47:18 +08:00
flag.Parse()
2024-12-25 11:00:02 +08:00
lvl := slog.LevelInfo
if err := lvl.UnmarshalText([]byte(logLevel)); err != nil {
slog.With("error", err).Error("failed to parse log level")
os.Exit(-1)
2024-12-18 10:47:18 +08:00
} else {
2024-12-25 11:00:02 +08:00
slog.SetLogLoggerLevel(lvl)
2024-12-18 10:47:18 +08:00
}
if sentrydsn != "" {
if err := sentry.Init(sentry.ClientOptions{
Dsn: sentrydsn,
}); err != nil {
2024-12-25 11:00:02 +08:00
slog.With("dsn", sentrydsn, "error", err).Error("failed to setup sentry")
os.Exit(-1)
2024-12-18 10:47:18 +08:00
}
defer sentry.Flush(time.Second * 3)
}
config, err := configFromFile(configFilePath)
2024-12-16 21:11:32 +08:00
if err != nil {
panic(err)
}
ch := make(chan any, 10)
for idx := 0; idx < 10; idx += 1 {
ch <- idx
}
2025-01-09 23:30:42 +08:00
server := cacheproxy.NewServer(*config)
2024-12-16 21:11:32 +08:00
2025-01-10 01:20:45 +08:00
mux := http.NewServeMux()
mux.HandleFunc("GET /", server.HandleRequestWithCache)
2025-03-03 00:26:29 +08:00
if pprofEnabled {
mux.HandleFunc("GET /debug/pprof/", pprof.Index)
mux.HandleFunc("GET /debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("GET /debug/pprof/profile", pprof.Profile)
mux.HandleFunc("GET /debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("GET /debug/pprof/trace", pprof.Trace)
}
2024-12-25 11:00:02 +08:00
slog.With("addr", ":8881").Info("serving app")
2025-01-10 01:20:45 +08:00
if err := http.ListenAndServe(":8881", middleware.Use(mux,
recover.Recover(),
2025-03-03 09:59:19 +08:00
handlerctx.FindHandler(mux),
2025-01-10 01:20:45 +08:00
httplog.Log(httplog.Config{LogStart: true, LogFinish: true}),
)); err != nil {
2025-01-09 23:15:39 +08:00
slog.With("error", err).Error("failed to start server")
os.Exit(-1)
}
2024-12-16 21:11:32 +08:00
}