2020-06-14 00:09:06 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-02-23 13:08:53 +00:00
|
|
|
"context"
|
2021-11-05 15:05:02 +00:00
|
|
|
"encoding/base64"
|
2023-12-29 17:51:52 +00:00
|
|
|
"errors"
|
2020-06-14 00:09:06 +00:00
|
|
|
"fmt"
|
2023-12-29 17:51:52 +00:00
|
|
|
"io/fs"
|
2020-06-14 00:09:06 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2023-12-29 16:58:30 +00:00
|
|
|
"time"
|
2020-06-14 00:09:06 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2023-12-29 16:58:30 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-06-14 00:09:06 +00:00
|
|
|
|
2021-06-09 08:59:30 +00:00
|
|
|
httpHelpers "github.com/Luzifer/go_helpers/v2/http"
|
2023-12-29 16:58:30 +00:00
|
|
|
"github.com/Luzifer/preserve/pkg/storage"
|
|
|
|
"github.com/Luzifer/preserve/pkg/storage/gcs"
|
|
|
|
"github.com/Luzifer/preserve/pkg/storage/local"
|
2020-06-14 00:09:06 +00:00
|
|
|
"github.com/Luzifer/rconfig/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
cfg = struct {
|
2021-02-23 13:08:53 +00:00
|
|
|
BucketURI string `flag:"bucket-uri" default:"" description:"[gcs] Format: gs://bucket/prefix"`
|
|
|
|
Listen string `flag:"listen" default:":3000" description:"Port/IP to listen on"`
|
|
|
|
LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"`
|
|
|
|
StorageDir string `flag:"storage-dir" default:"./data/" description:"[local] Where to store cached files"`
|
|
|
|
StorageProvider string `flag:"storage-provider" default:"local" description:"Storage providers to use ('list' to print a list)"`
|
|
|
|
UserAgent string `flag:"user-agent" default:"" description:"Override user-agent"`
|
|
|
|
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
|
2020-06-14 00:09:06 +00:00
|
|
|
}{}
|
|
|
|
|
2023-12-29 16:58:30 +00:00
|
|
|
store storage.Storage
|
2020-06-14 00:09:06 +00:00
|
|
|
version = "dev"
|
|
|
|
)
|
|
|
|
|
2023-12-29 16:58:30 +00:00
|
|
|
func initApp() error {
|
2020-06-14 00:09:06 +00:00
|
|
|
rconfig.AutoEnv(true)
|
|
|
|
if err := rconfig.ParseAndValidate(&cfg); err != nil {
|
2023-12-29 16:58:30 +00:00
|
|
|
return fmt.Errorf("parsing cli options: %w", err)
|
2020-06-14 00:09:06 +00:00
|
|
|
}
|
|
|
|
|
2023-12-29 16:58:30 +00:00
|
|
|
l, err := logrus.ParseLevel(cfg.LogLevel)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing log-level: %w", err)
|
2020-06-14 00:09:06 +00:00
|
|
|
}
|
2023-12-29 16:58:30 +00:00
|
|
|
logrus.SetLevel(l)
|
2020-06-14 00:09:06 +00:00
|
|
|
|
2023-12-29 16:58:30 +00:00
|
|
|
return nil
|
2020-06-14 00:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2021-02-23 13:08:53 +00:00
|
|
|
var err error
|
|
|
|
|
2023-12-29 16:58:30 +00:00
|
|
|
if err = initApp(); err != nil {
|
|
|
|
logrus.WithError(err).Fatal("initializing app")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.VersionAndExit {
|
|
|
|
fmt.Printf("preserve %s\n", version) //nolint:forbidigo // Fine here
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2021-02-23 13:08:53 +00:00
|
|
|
switch cfg.StorageProvider {
|
|
|
|
case "gcs":
|
2023-12-29 16:58:30 +00:00
|
|
|
if store, err = gcs.New(cfg.BucketURI); err != nil {
|
|
|
|
logrus.WithError(err).Fatal("creating GCS storage")
|
2021-02-23 13:08:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case "list":
|
|
|
|
// Special "provider" to list possible providers
|
2023-12-29 16:58:30 +00:00
|
|
|
logrus.Println("Available Storage Providers: gcs, local")
|
2021-02-23 13:08:53 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
case "local":
|
2023-12-29 16:58:30 +00:00
|
|
|
store = local.New(cfg.StorageDir)
|
2021-02-23 13:08:53 +00:00
|
|
|
|
|
|
|
default:
|
2023-12-29 16:58:30 +00:00
|
|
|
logrus.Fatalf("invalid storage provider: %q", cfg.StorageProvider)
|
2021-02-23 13:08:53 +00:00
|
|
|
}
|
|
|
|
|
2020-06-14 00:09:06 +00:00
|
|
|
r := mux.NewRouter()
|
|
|
|
r.PathPrefix("/latest/").HandlerFunc(handleCacheLatest)
|
|
|
|
r.PathPrefix("/").HandlerFunc(handleCacheOnce)
|
|
|
|
|
|
|
|
r.SkipClean(true)
|
|
|
|
|
2021-06-09 08:59:30 +00:00
|
|
|
r.Use(httpHelpers.NewHTTPLogHandler)
|
|
|
|
r.Use(httpHelpers.GzipHandler)
|
|
|
|
|
2023-12-29 16:58:30 +00:00
|
|
|
server := http.Server{
|
|
|
|
Addr: cfg.Listen,
|
|
|
|
Handler: r,
|
|
|
|
ReadHeaderTimeout: time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.WithFields(logrus.Fields{"addr": cfg.Listen, "version": version}).Info("preserve starting...")
|
|
|
|
if err = server.ListenAndServe(); err != nil {
|
|
|
|
logrus.WithError(err).Fatal("running HTTP server")
|
|
|
|
}
|
2020-06-14 00:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleCacheLatest(w http.ResponseWriter, r *http.Request) {
|
|
|
|
handleCache(w, r, strings.TrimPrefix(r.RequestURI, "/latest/"), true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleCacheOnce(w http.ResponseWriter, r *http.Request) {
|
|
|
|
handleCache(w, r, strings.TrimPrefix(r.RequestURI, "/"), false)
|
|
|
|
}
|
|
|
|
|
2023-12-29 16:58:30 +00:00
|
|
|
//revive:disable-next-line:flag-parameter // This is fine in this case
|
2020-06-14 00:09:06 +00:00
|
|
|
func handleCache(w http.ResponseWriter, r *http.Request, uri string, update bool) {
|
2021-11-05 15:05:02 +00:00
|
|
|
if strings.HasPrefix(uri, "b64:") {
|
|
|
|
u, err := base64.URLEncoding.DecodeString(strings.TrimPrefix(uri, "b64:"))
|
|
|
|
if err != nil {
|
2023-12-29 16:58:30 +00:00
|
|
|
http.Error(w, "decoding base64 URL", http.StatusBadRequest)
|
2021-11-05 15:05:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
uri = string(u)
|
|
|
|
}
|
|
|
|
|
2021-11-05 15:16:22 +00:00
|
|
|
var (
|
|
|
|
cachePath = urlToCachePath(uri)
|
|
|
|
cacheHeader = "HIT"
|
2023-12-29 16:58:30 +00:00
|
|
|
logger = logrus.WithFields(logrus.Fields{
|
2021-11-05 15:16:22 +00:00
|
|
|
"url": uri,
|
|
|
|
"path": cachePath,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2020-06-14 00:09:06 +00:00
|
|
|
if u, err := url.Parse(uri); err != nil || u.Scheme == "" {
|
2023-12-29 16:58:30 +00:00
|
|
|
http.Error(w, "parsing requested URL", http.StatusBadRequest)
|
2020-06-14 00:09:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debug("Received request")
|
|
|
|
|
2021-02-23 13:08:53 +00:00
|
|
|
metadata, err := store.LoadMeta(r.Context(), cachePath)
|
2023-12-29 17:51:52 +00:00
|
|
|
if err != nil && !errors.Is(err, fs.ErrNotExist) {
|
2023-12-29 16:58:30 +00:00
|
|
|
logrus.WithError(err).Error("loading meta")
|
|
|
|
http.Error(w, "accessing entry metadata", http.StatusInternalServerError)
|
2020-06-14 00:09:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-29 17:51:52 +00:00
|
|
|
if update || errors.Is(err, fs.ErrNotExist) {
|
2023-12-29 16:58:30 +00:00
|
|
|
logger.Debug("updating cache")
|
2020-06-14 00:09:06 +00:00
|
|
|
cacheHeader = "MISS"
|
|
|
|
|
2021-02-23 13:08:53 +00:00
|
|
|
// Using background context to cache the file even in case of the request being aborted
|
2023-12-29 16:58:30 +00:00
|
|
|
metadata, err = renewCache(context.Background(), uri) //nolint:contextcheck // See line above
|
2020-06-14 00:09:06 +00:00
|
|
|
if err != nil {
|
2023-12-29 16:58:30 +00:00
|
|
|
logger.WithError(err).Warn("refreshing file")
|
2020-06-14 00:09:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if metadata == nil {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", metadata.ContentType)
|
|
|
|
w.Header().Set("X-Last-Cached", metadata.LastCached.UTC().Format(http.TimeFormat))
|
|
|
|
w.Header().Set("X-Cache", cacheHeader)
|
|
|
|
|
2021-02-23 13:08:53 +00:00
|
|
|
f, err := store.GetFile(r.Context(), cachePath)
|
2020-06-14 00:09:06 +00:00
|
|
|
if err != nil {
|
2023-12-29 16:58:30 +00:00
|
|
|
logrus.WithError(err).Error("loading cached file")
|
|
|
|
http.Error(w, "accessing cache entry", http.StatusInternalServerError)
|
2020-06-14 00:09:06 +00:00
|
|
|
return
|
|
|
|
}
|
2023-12-29 16:58:30 +00:00
|
|
|
defer func() {
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
logrus.WithError(err).Error("closing storage file (leaked fd)")
|
|
|
|
}
|
|
|
|
}()
|
2020-06-14 00:09:06 +00:00
|
|
|
|
|
|
|
http.ServeContent(w, r, "", metadata.LastModified, f)
|
|
|
|
}
|