1
0
Fork 0
mirror of https://github.com/Luzifer/preserve.git synced 2024-12-20 17:51:18 +00:00
preserve/main.go

182 lines
4.9 KiB
Go
Raw Normal View History

2020-06-14 00:09:06 +00:00
package main
import (
"context"
"encoding/base64"
"errors"
2020-06-14 00:09:06 +00:00
"fmt"
"io/fs"
2020-06-14 00:09:06 +00:00
"net/http"
"net/url"
"os"
"strings"
"time"
2020-06-14 00:09:06 +00:00
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
2020-06-14 00:09:06 +00:00
httpHelpers "github.com/Luzifer/go_helpers/v2/http"
"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 {
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
}{}
store storage.Storage
2020-06-14 00:09:06 +00:00
version = "dev"
)
func initApp() error {
2020-06-14 00:09:06 +00:00
rconfig.AutoEnv(true)
if err := rconfig.ParseAndValidate(&cfg); err != nil {
return fmt.Errorf("parsing cli options: %w", err)
2020-06-14 00:09:06 +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
}
logrus.SetLevel(l)
2020-06-14 00:09:06 +00:00
return nil
2020-06-14 00:09:06 +00:00
}
func main() {
var err error
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)
}
switch cfg.StorageProvider {
case "gcs":
if store, err = gcs.New(cfg.BucketURI); err != nil {
logrus.WithError(err).Fatal("creating GCS storage")
}
case "list":
// Special "provider" to list possible providers
logrus.Println("Available Storage Providers: gcs, local")
return
case "local":
store = local.New(cfg.StorageDir)
default:
logrus.Fatalf("invalid storage provider: %q", cfg.StorageProvider)
}
2020-06-14 00:09:06 +00:00
r := mux.NewRouter()
r.PathPrefix("/latest/").HandlerFunc(handleCacheLatest)
r.PathPrefix("/").HandlerFunc(handleCacheOnce)
r.SkipClean(true)
r.Use(httpHelpers.NewHTTPLogHandler)
r.Use(httpHelpers.GzipHandler)
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)
}
//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) {
if strings.HasPrefix(uri, "b64:") {
u, err := base64.URLEncoding.DecodeString(strings.TrimPrefix(uri, "b64:"))
if err != nil {
http.Error(w, "decoding base64 URL", http.StatusBadRequest)
return
}
uri = string(u)
}
var (
cachePath = urlToCachePath(uri)
cacheHeader = "HIT"
logger = logrus.WithFields(logrus.Fields{
"url": uri,
"path": cachePath,
})
)
2020-06-14 00:09:06 +00:00
if u, err := url.Parse(uri); err != nil || u.Scheme == "" {
http.Error(w, "parsing requested URL", http.StatusBadRequest)
2020-06-14 00:09:06 +00:00
return
}
logger.Debug("Received request")
metadata, err := store.LoadMeta(r.Context(), cachePath)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
logrus.WithError(err).Error("loading meta")
http.Error(w, "accessing entry metadata", http.StatusInternalServerError)
2020-06-14 00:09:06 +00:00
return
}
if update || errors.Is(err, fs.ErrNotExist) {
logger.Debug("updating cache")
2020-06-14 00:09:06 +00:00
cacheHeader = "MISS"
// Using background context to cache the file even in case of the request being aborted
metadata, err = renewCache(context.Background(), uri) //nolint:contextcheck // See line above
2020-06-14 00:09:06 +00:00
if err != nil {
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)
f, err := store.GetFile(r.Context(), cachePath)
2020-06-14 00:09:06 +00:00
if err != nil {
logrus.WithError(err).Error("loading cached file")
http.Error(w, "accessing cache entry", http.StatusInternalServerError)
2020-06-14 00:09:06 +00:00
return
}
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)
}