implemented CLI interface

This commit is contained in:
Djeeberjr 2021-09-09 13:41:38 +02:00
parent 9616450cff
commit dff54e2650
5 changed files with 43 additions and 11 deletions

View File

@ -4,16 +4,39 @@ import (
"time" "time"
s3browser "git.kapelle.org/niklas/s3browser/internal" s3browser "git.kapelle.org/niklas/s3browser/internal"
"github.com/alexflint/go-arg"
) )
type args struct {
S3Endpoint string `arg:"--s3-endpoint,required,env:S3_ENDPOINT" help:"host[:port]" placeholder:"ENDPOINT"`
S3AccessKey string `arg:"--s3-access-key,required,env:S3_ACCESS_KEY" placeholder:"ACCESS_KEY"`
S3SecretKey string `arg:"--s3-secret-key,required,env:S3_SECRET_KEY" placeholder:"SECRET_KEY"`
S3Buket string `arg:"--s3-buket,required,env:S3_BUKET" placeholder:"BUKET"`
S3DisableSSL bool `arg:"--s3-disable-ssl,env:S3_DISABLE_SSL" default:"false"`
Address string `arg:"--address,env:ADDRESS" default:":3000" help:"what address to listen on" placeholder:"ADDRESS"`
CacheTTL int64 `arg:"--cache-ttl,env:CACHE_TTL" help:"Time in seconds" default:"30" placeholder:"TTL"`
CacheCleanup int64 `arg:"--cache-cleanup,env:CACHE_CLEANUP" help:"Time in seconds" default:"60" placeholder:"CLEANUP"`
Verbose bool `arg:"-v,--verbose,env:VERBOSE" help:"verbosity level" default:"false"`
}
func (args) Version() string {
// TODO
return "s3Browser 0.1"
}
func main() { func main() {
var args args
arg.MustParse(&args)
s3browser.Start(s3browser.AppConfig{ s3browser.Start(s3browser.AppConfig{
S3Endoint: "localhost:9000", S3Endoint: args.S3Endpoint,
S3SSL: false, S3SSL: !args.S3DisableSSL,
S3AccessKey: "testo", S3AccessKey: args.S3AccessKey,
S3SecretKey: "testotesto", S3SecretKey: args.S3SecretKey,
S3Buket: "dev", S3Buket: args.S3Buket,
CacheTTL: 20 * time.Second, CacheTTL: time.Duration(args.CacheTTL) * time.Second,
CacheCleanup: 1 * time.Minute, CacheCleanup: time.Duration(args.CacheCleanup) * time.Second,
Address: args.Address,
LogDebug: args.Verbose,
}) })
} }

1
go.mod
View File

@ -3,6 +3,7 @@ module git.kapelle.org/niklas/s3browser
go 1.16 go 1.16
require ( require (
github.com/alexflint/go-arg v1.4.2
github.com/graph-gophers/dataloader v5.0.0+incompatible github.com/graph-gophers/dataloader v5.0.0+incompatible
github.com/graphql-go/graphql v0.7.9 github.com/graphql-go/graphql v0.7.9
github.com/graphql-go/handler v0.2.3 github.com/graphql-go/handler v0.2.3

4
go.sum
View File

@ -1,3 +1,7 @@
github.com/alexflint/go-arg v1.4.2 h1:lDWZAXxpAnZUq4qwb86p/3rIJJ2Li81EoMbTMujhVa0=
github.com/alexflint/go-arg v1.4.2/go.mod h1:9iRbDxne7LcR/GSvEr7ma++GLpdIU1zrghf2y2768kM=
github.com/alexflint/go-scalar v1.0.0 h1:NGupf1XV/Xb04wXskDFzS0KWOLH632W/EO4fAFi+A70=
github.com/alexflint/go-scalar v1.0.0/go.mod h1:GpHzbCOZXEKMEcygYQ5n/aa4Aq84zbxjy3MxYW0gjYw=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

View File

@ -17,7 +17,7 @@ import (
) )
// initHttp setup and start the http server. Blocking // initHttp setup and start the http server. Blocking
func initHttp(resolveContext context.Context, schema graphql.Schema) error { func initHttp(resolveContext context.Context, schema graphql.Schema, address string) error {
h := handler.New(&handler.Config{ h := handler.New(&handler.Config{
Schema: &schema, Schema: &schema,
Pretty: true, Pretty: true,
@ -53,7 +53,7 @@ func initHttp(resolveContext context.Context, schema graphql.Schema) error {
// Init the embedded static files // Init the embedded static files
initStatic() initStatic()
return http.ListenAndServe(":8080", nil) return http.ListenAndServe(address, nil)
} }
func httpGetFile(ctx context.Context, rw http.ResponseWriter, r *http.Request) { func httpGetFile(ctx context.Context, rw http.ResponseWriter, r *http.Request) {

View File

@ -19,6 +19,8 @@ type AppConfig struct {
S3Buket string S3Buket string
CacheTTL time.Duration CacheTTL time.Duration
CacheCleanup time.Duration CacheCleanup time.Duration
Address string
LogDebug bool
} }
// File represents a file with its metadata // File represents a file with its metadata
@ -68,7 +70,9 @@ func setupS3Client(config AppConfig) (*minio.Client, error) {
// Start starts the app // Start starts the app
func Start(config AppConfig) { func Start(config AppConfig) {
log.SetLevel(log.DebugLevel) if config.LogDebug {
log.SetLevel(log.DebugLevel)
}
log.Info("Starting") log.Info("Starting")
log.Debug("Setting up s3 client") log.Debug("Setting up s3 client")
@ -96,7 +100,7 @@ func Start(config AppConfig) {
resolveContext = context.WithValue(resolveContext, "loader", loaderMap) resolveContext = context.WithValue(resolveContext, "loader", loaderMap)
log.Debug("Starting HTTP server") log.Debug("Starting HTTP server")
err = initHttp(resolveContext, schema) err = initHttp(resolveContext, schema, config.Address)
if err != nil { if err != nil {
log.Error("Failed to start webserver: ", err.Error()) log.Error("Failed to start webserver: ", err.Error())