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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
package main
import (
"context"
"encoding/base64"
"errors"
"net/http"
"os"
"os/signal"
"time"
"github.com/glebarez/sqlite"
"github.com/gorilla/mux"
flag "github.com/spf13/pflag"
"golang.org/x/exp/slog"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"git.sr.ht/~gabrielgio/img/pkg/database/localfs"
"git.sr.ht/~gabrielgio/img/pkg/database/repository"
"git.sr.ht/~gabrielgio/img/pkg/database/sql"
"git.sr.ht/~gabrielgio/img/pkg/ext"
"git.sr.ht/~gabrielgio/img/pkg/service"
"git.sr.ht/~gabrielgio/img/pkg/view"
"git.sr.ht/~gabrielgio/img/pkg/worker"
"git.sr.ht/~gabrielgio/img/pkg/worker/scanner"
"git.sr.ht/~gabrielgio/img/static"
)
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 debug, info, warning, error")
schedulerCount = flag.Uint("scheduler-count", 10, "How many workers are created to process media files")
cachePath = flag.String("cache-path", "", "Folder to store thumbnail image")
)
flag.Parse()
level := parseLogLevel(*logLevel)
handler := slog.NewTextHandler(
os.Stdout,
&slog.HandlerOptions{Level: level},
)
logger := slog.New(handler)
d, err := OpenDatabase(*dbType, *dbCon)
if err != nil {
panic("failed to parse database strings" + err.Error())
}
db, err := gorm.Open(d, &gorm.Config{
Logger: ext.Wraplog(logger.With("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())
}
if *dbType == "sqlite" {
*schedulerCount = 1
}
baseKey, err := base64.StdEncoding.DecodeString(*key)
if err != nil {
panic("failed to decode key database: " + err.Error())
}
r := mux.NewRouter().StrictSlash(false)
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.FS(static.Static))))
// repository
var (
userRepository = sql.NewUserRepository(db)
settingsRepository = sql.NewSettingsRespository(db)
fileSystemRepository = localfs.NewFileSystemRepository()
mediaRepository = sql.NewMediaRepository(db)
)
// middleware
var (
authMiddleware = ext.NewAuthMiddleware(baseKey, logger.With("context", "auth"), userRepository)
logMiddleware = ext.NewLogMiddleare(logger.With("context", "http"))
initialMiddleware = ext.NewInitialSetupMiddleware(userRepository)
)
extRouter := ext.NewRouter(r)
extRouter.AddMiddleware(ext.HTML)
extRouter.AddMiddleware(initialMiddleware.Check)
extRouter.AddMiddleware(authMiddleware.LoggedIn)
extRouter.AddMiddleware(logMiddleware.HTTP)
scheduler := worker.NewScheduler(*schedulerCount)
// controller
var (
userController = service.NewAuthController(userRepository, userRepository, baseKey)
fileSystemController = service.NewFileSystemController(fileSystemRepository, userRepository)
)
// view
for _, v := range []view.View{
view.NewAuthView(userController),
view.NewFileSystemView(*fileSystemController, settingsRepository),
view.NewSettingsView(settingsRepository, userController),
view.NewMediaView(mediaRepository, userRepository, settingsRepository),
view.NewAlbumView(mediaRepository, userRepository, settingsRepository),
} {
v.SetMyselfIn(extRouter)
}
// processors
var (
fileScanner = scanner.NewFileScanner(mediaRepository, userRepository)
exifScanner = scanner.NewEXIFScanner(mediaRepository)
thumbnailScanner = scanner.NewThumbnailScanner(*cachePath, mediaRepository)
albumScanner = scanner.NewAlbumScanner(mediaRepository)
)
// tasks
var (
serverTask = worker.NewServerTask(&http.Server{Handler: r, Addr: "0.0.0.0:8080"})
fileTask = worker.NewTaskFromChanProcessor[string](
fileScanner,
scheduler,
logger.With("context", "file scanner"),
)
exifTask = worker.NewTaskFromBatchProcessor[*repository.Media](
exifScanner,
scheduler,
logger.With("context", "exif scanner"),
)
thumbnailTask = worker.NewTaskFromBatchProcessor[*repository.Media](
thumbnailScanner,
scheduler,
logger.With("context", "thumbnail scanner"),
)
albumTask = worker.NewTaskFromSerialProcessor[*repository.Media](
albumScanner,
scheduler,
logger.With("context", "thumbnail scanner"),
)
)
pool := worker.NewTaskPool()
pool.AddTask("http server", time.Minute, serverTask)
pool.AddTask("exif scanner", 15*time.Minute, exifTask)
pool.AddTask("file scanner", 2*time.Hour, fileTask)
pool.AddTask("thumbnail scanner", 15*time.Minute, thumbnailTask)
pool.AddTask("album scanner", 15*time.Minute, albumTask)
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
pool.Start(ctx)
}
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")
}
}
func parseLogLevel(input string) slog.Level {
switch input {
case "debug":
return slog.LevelDebug
case "info":
return slog.LevelInfo
case "warn":
return slog.LevelWarn
case "error":
return slog.LevelError
default:
return slog.LevelWarn
}
}
|