This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (63 loc) · 2.7 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"github.com/alecthomas/kong"
"github.com/rs/zerolog/log"
"github.com/terrycain/actions-cache-server/pkg/database"
"github.com/terrycain/actions-cache-server/pkg/storage"
"github.com/terrycain/actions-cache-server/pkg/utils/logging"
"github.com/terrycain/actions-cache-server/pkg/web"
)
var cli struct {
// Database backends
DBSqlite string `env:"DB_SQLITE" required:"" xor:"db" help:"SQLite filepath e.g. /tmp/db.sqlite"`
DBPostgres string `env:"DB_POSTGRES" required:"" xor:"db" help:"Postgres URI e.g. postgresql://blah"`
// Storage backends
StorageDisk string `env:"STORAGE_DISK" required:"" xor:"storage" help:"Use disk storage for cache data e.g. /tmp/cache"`
StorageS3 string `env:"STORAGE_S3" required:"" xor:"storage" name:"storage-s3" help:"Use S3 storage for cache data e.g. s3://bucket"`
StorageAzureBlob string `env:"STORAGE_AZUREBLOB" required:"" xor:"storage" name:"storage-azureblob" help:"Use Azure Blob Storage for cache data e.g. connectionstring with ;Container=blah on the end"`
// Misc
LogLevel string `env:"LOG_LEVEL" default:"info" enum:"debug,info,warn,error"`
ListenAddress string `env:"LISTEN_ADDR" default:"0.0.0.0:8080" help:"Listen address e.g. 0.0.0.0:8080"`
MetricsListenAddress string `env:"METRICS_LISTEN_ADDR" default:"0.0.0.0:9102" help:"Listen address for prometheus metrics e.g. 0.0.0.0:9102"`
Debug bool `env:"DEBUG" help:"Enable debug mode"`
}
func main() {
kong.Parse(&cli)
logging.SetupLogging(cli.LogLevel)
var databaseBackendName, dbConnectionString string
if cli.DBSqlite != "" {
databaseBackendName = "sqlite"
dbConnectionString = cli.DBSqlite
}
var storageBackendName, storageConnectionString string
if cli.StorageDisk != "" {
storageBackendName = "disk"
storageConnectionString = cli.StorageDisk
}
if cli.StorageS3 != "" {
storageBackendName = "s3"
storageConnectionString = cli.StorageS3
}
if cli.StorageAzureBlob != "" {
storageBackendName = "azureblob"
storageConnectionString = cli.StorageAzureBlob
}
dbBackend, err := database.GetBackend(databaseBackendName, dbConnectionString)
if err != nil {
log.Fatal().Err(err).Msg("Failed to initiate database backend")
}
storageBackend, err := storage.GetStorageBackend(storageBackendName, storageConnectionString)
if err != nil {
log.Fatal().Err(err).Msg("Failed to initiate storage backend")
}
handlers := web.Handlers{
Database: dbBackend,
Storage: storageBackend,
Debug: cli.Debug,
}
router := web.GetRouter(cli.MetricsListenAddress, handlers, true)
log.Info().Msgf("Listening on %s", cli.ListenAddress)
if err = router.Run(cli.ListenAddress); err != nil {
log.Fatal().Err(err).Msg("Failed HTTP server loop")
}
}