go-latestver/main.go
Knut Ahlers 09e0a66976
Add PR testing, fix linter errors
Squashed commit of the following:

commit 2a83adf6c54d6abcf6762760fd38f2505511f545
Author: Knut Ahlers <knut@ahlers.me>
Date:   Wed Dec 1 03:34:49 2021 +0100

    Lint: Fix copylocks errors

    Signed-off-by: Knut Ahlers <knut@ahlers.me>

commit 418f85d504203a6968329e280ecd9cf7d2365373
Author: Knut Ahlers <knut@ahlers.me>
Date:   Wed Dec 1 03:31:38 2021 +0100

    Lint: Fix gosec warnings

    Signed-off-by: Knut Ahlers <knut@ahlers.me>

commit 1a977875740be3c40884aa0985578721ceb4ae37
Author: Knut Ahlers <knut@ahlers.me>
Date:   Wed Dec 1 03:28:02 2021 +0100

    Lint: Disable gomnd for certain cases

    Signed-off-by: Knut Ahlers <knut@ahlers.me>

commit 5e81cf79ba7256b321442530715a2b53de0a18e1
Author: Knut Ahlers <knut@ahlers.me>
Date:   Wed Dec 1 03:26:01 2021 +0100

    Lint: fix ineffassign errors

    Signed-off-by: Knut Ahlers <knut@ahlers.me>

commit cb14fae2dad985368e1f05d62f8e778817d01c6f
Author: Knut Ahlers <knut@ahlers.me>
Date:   Wed Dec 1 03:23:42 2021 +0100

    Lint: Fix revive linter errors

    Signed-off-by: Knut Ahlers <knut@ahlers.me>

commit b3390b8dff9b939caa4e3821a48dd848af0bfba4
Author: Knut Ahlers <knut@ahlers.me>
Date:   Wed Dec 1 03:21:35 2021 +0100

    Lint: Remove unrequired dereference

    Signed-off-by: Knut Ahlers <knut@ahlers.me>

commit f9052e6aa530c5b5017249fc6c31bdbb94252760
Author: Knut Ahlers <knut@ahlers.me>
Date:   Wed Dec 1 03:20:43 2021 +0100

    Lint: Remove deadcode

    Signed-off-by: Knut Ahlers <knut@ahlers.me>

commit 72b88adaa25a3bb5a7af21da7ed12f08cae36573
Author: Knut Ahlers <knut@ahlers.me>
Date:   Wed Dec 1 02:52:27 2021 +0100

    Add PR-testing

    Signed-off-by: Knut Ahlers <knut@ahlers.me>

Signed-off-by: Knut Ahlers <knut@ahlers.me>
2021-12-01 03:38:52 +01:00

102 lines
3.7 KiB
Go

package main
import (
"fmt"
"net/http"
"os"
"time"
"github.com/gorilla/mux"
"github.com/robfig/cron/v3"
log "github.com/sirupsen/logrus"
"github.com/Luzifer/go-latestver/internal/config"
"github.com/Luzifer/go-latestver/internal/database"
httpHelper "github.com/Luzifer/go_helpers/v2/http"
"github.com/Luzifer/rconfig/v2"
)
var (
cfg = struct {
BadgeGenInstance string `flag:"badge-gen-instance" default:"https://badges.fyi/" description:"Where to find the badge-gen instance to use badges from"`
BaseURL string `flag:"base-url" default:"https://example.com/" description:"Base-URL the application is reachable at"`
Config string `flag:"config,c" default:"config.yaml" description:"Configuration file with catalog entries"`
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)"`
CheckDistribution time.Duration `flag:"check-distribution" default:"1h" description:"Checks are executed at static times every [value]"`
Storage string `flag:"storage" default:"sqlite" description:"Storage adapter to use (mysql, sqlite)"`
StorageDSN string `flag:"storage-dsn" default:"file::memory:?cache=shared" description:"DSN to connect to the database"`
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
}{}
configFile = config.New()
router *mux.Router
storage *database.Client
version = "dev"
)
func initApp() {
rconfig.AutoEnv(true)
if err := rconfig.ParseAndValidate(&cfg); err != nil {
log.Fatalf("Unable to parse commandline options: %s", err)
}
if cfg.VersionAndExit {
fmt.Printf("go-latestver %s\n", version)
os.Exit(0)
}
if l, err := log.ParseLevel(cfg.LogLevel); err != nil {
log.WithError(err).Fatal("Unable to parse log level")
} else {
log.SetLevel(l)
}
}
func main() {
initApp()
var err error
if err = configFile.Load(cfg.Config); err != nil {
log.WithError(err).Fatal("Unable to load configuration")
}
if err = configFile.ValidateCatalog(); err != nil {
log.WithError(err).Fatal("Configuration is not valid")
}
storage, err = database.NewClient(cfg.Storage, cfg.StorageDSN)
if err != nil {
log.WithError(err).Fatal("Unable to connect to database")
}
scheduler := cron.New()
scheduler.AddFunc(fmt.Sprintf("@every %s", schedulerInterval), schedulerRun)
scheduler.Start()
router = mux.NewRouter()
router.HandleFunc("/v1/catalog", handleCatalogList).Methods(http.MethodGet)
router.HandleFunc("/v1/catalog/{name}/{tag}", handleCatalogGet).Methods(http.MethodGet)
router.HandleFunc("/v1/catalog/{name}/{tag}/log", handleLog).Methods(http.MethodGet)
router.HandleFunc("/v1/catalog/{name}/{tag}/version", handleCatalogGetVersion).Methods(http.MethodGet)
router.HandleFunc("/v1/log", handleLog).Methods(http.MethodGet)
router.HandleFunc("/{name}/{tag}.svg", handleBadgeRedirect).Methods(http.MethodGet).Name("catalog-entry-badge")
router.HandleFunc("/{name}/{tag}/log.rss", handleLogFeed).Methods(http.MethodGet).Name("catalog-entry-rss")
router.HandleFunc("/log.rss", handleLogFeed).Methods(http.MethodGet).Name("log-rss")
router.HandleFunc("/", handleSinglePage).Methods(http.MethodGet).Name("catalog")
router.HandleFunc("/{name}/{tag}", handleSinglePage).Methods(http.MethodGet).Name("catalog-entry")
router.PathPrefix("/").HandlerFunc(handleSinglePage)
var handler http.Handler = router
handler = httpHelper.GzipHandler(handler)
handler = httpHelper.NewHTTPLogHandler(handler)
if err := http.ListenAndServe(cfg.Listen, handler); err != nil {
log.WithError(err).Fatal("HTTP server exited unclean")
}
}