From 1ab181d1d7d75fd66c97d231d6eb77e1f05e0b3e Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Sun, 18 Jun 2023 17:37:34 +0200 Subject: feat: Move params to flags Start using flags to set some params like key and database connection. As a flag parse I opt for a lib that uses posix style params. --- cmd/server/main.go | 60 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 11 deletions(-) (limited to 'cmd') diff --git a/cmd/server/main.go b/cmd/server/main.go index 59ea957..e9314ec 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -3,12 +3,16 @@ package main import ( "context" "encoding/hex" + "errors" "os" "os/signal" "github.com/fasthttp/router" "github.com/sirupsen/logrus" + flag "github.com/spf13/pflag" "github.com/valyala/fasthttp" + "gorm.io/driver/mysql" + "gorm.io/driver/postgres" "gorm.io/driver/sqlite" "gorm.io/gorm" @@ -22,13 +26,31 @@ import ( "git.sr.ht/~gabrielgio/img/pkg/worker" ) -const root = "/home/gabrielgio" - func main() { + var ( + key = flag.String("aes-key", "", "AES key, either 16, 24, or 32 bytes string to select AES-128, AES-192, or AES-256") + dbType = flag.String("db-type", "sqlite", "Database to be used. Choose either mysql, psql or sqlite") + dbCon = flag.String("db-con", "main.db", "Database string connection for given database type. Ref: https://gorm.io/docs/connecting_to_the_database.html") + logLevel = flag.String("log-level", "error", "Log level: Choose either trace, debug, info, warning, error, fatal or panic") + + // TODO: this will later be replaced by user specific root folder + root = flag.String("root", "", "root folder for the whole application. All the workers will use it as working directory") + ) + + flag.Parse() + + l, err := logrus.ParseLevel(*logLevel) + if err != nil { + panic("failed to parse log level" + err.Error()) + } logger := logrus.New() - logger.SetLevel(logrus.ErrorLevel) + logger.SetLevel(l) + + d, err := OpenDatabase(*dbType, *dbCon) + if err != nil { + panic("failed to parse database strings" + err.Error()) + } - d := sqlite.Open("test.db") db, err := gorm.Open(d, &gorm.Config{ Logger: ext.Wraplog(logger.WithField("context", "sql")), }) @@ -40,13 +62,16 @@ func main() { panic("failed to migrate database: " + err.Error()) } - // TODO: properly set this up - key, _ := hex.DecodeString("6368616e676520746869732070617373") + hexKey, err := hex.DecodeString(*key) + if err != nil { + panic("failed to decode key database: " + err.Error()) + } + r := router.New() r.ServeFiles("/static/{filepath:*}", "./static") r.NotFound = ext.NotFoundHTML - authMiddleware := ext.NewAuthMiddleware(key, logger.WithField("context", "auth")) + authMiddleware := ext.NewAuthMiddleware(hexKey, logger.WithField("context", "auth")) logMiddleware := ext.NewLogMiddleare(logger.WithField("context", "http")) extRouter := ext.NewRouter(r) @@ -60,7 +85,7 @@ func main() { var ( userRepository = sql.NewUserRepository(db) settingsRepository = sql.NewSettingsRespository(db) - fileSystemRepository = localfs.NewFileSystemRepository(root) + fileSystemRepository = localfs.NewFileSystemRepository(*root) mediaRepository = sql.NewMediaRepository(db) ) @@ -69,7 +94,7 @@ func main() { // controller var ( - userController = auth.NewController(userRepository, key) + userController = auth.NewController(userRepository, hexKey) fileSystemController = filesystem.NewController(fileSystemRepository) ) @@ -86,8 +111,8 @@ func main() { // worker var ( serverWorker = worker.NewServerWorker(&fasthttp.Server{Handler: r.Handler}) - fileScanner = worker.NewFileScanner(root, mediaRepository) - exifScanner = worker.NewEXIFScanner(root, mediaRepository) + fileScanner = worker.NewFileScanner(*root, mediaRepository) + exifScanner = worker.NewEXIFScanner(*root, mediaRepository) ) pool := worker.NewWorkerPool() @@ -101,3 +126,16 @@ func main() { pool.Start(ctx) pool.Wait() } + +func OpenDatabase(dbType string, dbConn string) (gorm.Dialector, error) { + switch dbType { + case "sqlite": + return sqlite.Open(dbConn), nil + case "psql": + return postgres.Open(dbConn), nil + case "mysql": + return mysql.Open(dbConn), nil + default: + return nil, errors.New("No valid db type given") + } +} -- cgit v1.2.3