lens @ 1ab181d1d7d75fd66c97d231d6eb77e1f05e0b3e

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.
diff --git a/.gitignore b/.gitignore
index b06c8abc37de3837d42f95e6a132cf7d0b810f1f..eff712c758daabaa7a6949a31353757063409a7f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,3 +5,4 @@ media_cache/
 
 .env
 *.db
+*.db-journal
diff --git a/Makefile b/Makefile
index 9d82dd93b49920d80cc38fee9643687ecb0fc8ea..39c579c0bb9e29d7a7407c5f2178d1e6178f32ee 100644
--- a/Makefile
+++ b/Makefile
@@ -12,7 +12,10 @@ build:
 	$(GO_BUILD) -o $(OUT) $(SERVER)
 
 run: sass
-	$(GO_RUN) $(SERVER)
+	$(GO_RUN) $(SERVER) \
+		--log-level error \
+		--aes-key=6368616e676520746869732070617373 \
+		--root=${HOME} \
 
 sass:
 	@mkdir -p static
diff --git a/cmd/server/main.go b/cmd/server/main.go
index 59ea957fb60a560bf7e1d3f8c39cf748535f9332..e9314ec813338fb63cf8b4e536eab2ce20a8a446 100644
--- a/cmd/server/main.go
+++ b/cmd/server/main.go
@@ -3,12 +3,16 @@
 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 @@ 	"git.sr.ht/~gabrielgio/img/pkg/view"
 	"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")
 
-func main() {
+		// 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 @@ 	if err = sql.Migrate(db); err != nil {
 		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 @@ 	// repository
 	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 @@ 	userRepository.EnsureAdmin(context.Background())
 
 	// controller
 	var (
-		userController       = auth.NewController(userRepository, key)
+		userController       = auth.NewController(userRepository, hexKey)
 		fileSystemController = filesystem.NewController(fileSystemRepository)
 	)
 
@@ -86,8 +111,8 @@
 	// 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 @@
 	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")
+	}
+}