diff options
author | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-02-26 19:54:48 +0100 |
---|---|---|
committer | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-06-18 16:30:36 +0200 |
commit | c8e1328164e9ffbd681c3c0e449f1e6b9856b896 (patch) | |
tree | faee639a4c55c5dc3bfc59a5400026822c40221d /cmd | |
download | lens-c8e1328164e9ffbd681c3c0e449f1e6b9856b896.tar.gz lens-c8e1328164e9ffbd681c3c0e449f1e6b9856b896.tar.bz2 lens-c8e1328164e9ffbd681c3c0e449f1e6b9856b896.zip |
feat: Inicial commit
It contains rough template for the server and runners.
It contains rough template for the server and runners.
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/ggen/main.go | 43 | ||||
-rw-r--r-- | cmd/server/main.go | 103 |
2 files changed, 146 insertions, 0 deletions
diff --git a/cmd/ggen/main.go b/cmd/ggen/main.go new file mode 100644 index 0000000..b519739 --- /dev/null +++ b/cmd/ggen/main.go @@ -0,0 +1,43 @@ +package main + +import ( + "github.com/sirupsen/logrus" + "gorm.io/driver/sqlite" + "gorm.io/gen" + "gorm.io/gorm" + + "git.sr.ht/~gabrielgio/img/pkg/database/sql" + "git.sr.ht/~gabrielgio/img/pkg/ext" +) + +func main() { + cfg := gen.Config{ + OutPath: "./pkg/database/sql/query", + WithUnitTest: true, + Mode: gen.WithoutContext | gen.WithDefaultQuery | gen.WithQueryInterface, // generate mode + } + + g := gen.NewGenerator(cfg) + + logger := logrus.New() + logger.SetLevel(logrus.TraceLevel) + d := sqlite.Open("test.db") + db, err := gorm.Open(d, &gorm.Config{ + Logger: ext.Wraplog(logger.WithField("context", "sql")), + }) + if err != nil { + panic("failed to gen database: " + err.Error()) + } + + g.UseDB(db) + + for _, m := range []any{ + &sql.User{}, + &sql.Settings{}, + &sql.Media{}, + } { + g.ApplyBasic(m) + } + + g.Execute() +} diff --git a/cmd/server/main.go b/cmd/server/main.go new file mode 100644 index 0000000..375a26c --- /dev/null +++ b/cmd/server/main.go @@ -0,0 +1,103 @@ +package main + +import ( + "context" + "encoding/hex" + "os" + "os/signal" + + "github.com/fasthttp/router" + "github.com/sirupsen/logrus" + "github.com/valyala/fasthttp" + "gorm.io/driver/sqlite" + "gorm.io/gorm" + + "git.sr.ht/~gabrielgio/img/pkg/components/auth" + "git.sr.ht/~gabrielgio/img/pkg/components/filesystem" + "git.sr.ht/~gabrielgio/img/pkg/components/media" + "git.sr.ht/~gabrielgio/img/pkg/database/localfs" + "git.sr.ht/~gabrielgio/img/pkg/database/sql" + "git.sr.ht/~gabrielgio/img/pkg/ext" + "git.sr.ht/~gabrielgio/img/pkg/view" + "git.sr.ht/~gabrielgio/img/pkg/worker" +) + +const root = "/home/gabrielgio" + +func main() { + logger := logrus.New() + logger.SetLevel(logrus.ErrorLevel) + + d := sqlite.Open("test.db") + db, err := gorm.Open(d, &gorm.Config{ + Logger: ext.Wraplog(logger.WithField("context", "sql")), + }) + if err != nil { + panic("failed to connect database: " + err.Error()) + } + + if err = sql.Migrate(db); err != nil { + panic("failed to migrate database: " + err.Error()) + } + + // TODO: properly set this up + key, _ := hex.DecodeString("6368616e676520746869732070617373") + r := router.New() + r.ServeFiles("/static/{filepath:*}", "./static") + r.NotFound = ext.NotFoundHTML + + authMiddleware := ext.NewAuthMiddleware(key, logger.WithField("context", "auth")) + logMiddleware := ext.NewLogMiddleare(logger.WithField("context", "http")) + + extRouter := ext.NewRouter(r) + extRouter.AddMiddleware(logMiddleware.HTTP) + extRouter.AddMiddleware(authMiddleware.LoggedIn) + extRouter.AddMiddleware(ext.HTML) + + scheduler := worker.NewScheduler(10) + + // repository + var ( + userRepository = sql.NewUserRepository(db) + settingsRepository = sql.NewSettingsRespository(db) + fileSystemRepository = localfs.NewFileSystemRepository(root) + mediaRepository = sql.NewMediaRepository(db) + ) + + //TODO: remove later + userRepository.EnsureAdmin(context.Background()) + + // controller + var ( + userController = auth.NewController(userRepository, key) + fileSystemController = filesystem.NewController(fileSystemRepository) + ) + + // view + for _, v := range []view.View{ + view.NewAuthView(userController), + view.NewFileSystemView(*fileSystemController, settingsRepository), + view.NewSettingsView(settingsRepository), + view.NewMediaView(mediaRepository), + } { + v.SetMyselfIn(extRouter) + } + + // worker + var ( + serverWorker = worker.NewServerWorker(&fasthttp.Server{Handler: r.Handler}) + fileScanner = worker.NewFileScanner(root, mediaRepository) + exifScanner = worker.NewEXIFScanner(root, mediaRepository) + ) + + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt) + defer stop() + + pool := worker.NewWorkerPool() + pool.AddWorker("http server", serverWorker) + pool.AddWorker("exif scanner", worker.NewWorkerFromListProcessor[*media.Media](exifScanner, scheduler)) + pool.AddWorker("file scanner", worker.NewWorkerFromChanProcessor[string](fileScanner, scheduler)) + + pool.Start(ctx) + pool.Wait() +} |