You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

61 lines
1.5 KiB

// Command webfilebrowser starts the HTTP server for the file browser.
package main
import (
"errors"
"log/slog"
"net/http"
"os"
"webfilebrowser/internal/auth"
"webfilebrowser/internal/config"
"webfilebrowser/internal/web"
)
const (
defaultAddress = ":8080"
defaultDatabasePath = "/var/lib/webfilebrowser/webfilebrowser.db"
)
func main() {
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
applicationConfig, err := config.Load()
if err != nil {
logger.Error("load root configuration", "error", err)
os.Exit(1)
}
for _, root := range applicationConfig.Roots {
logger.Info("configured browse root", "name", root.Name, "path", root.Path)
}
databasePath := os.Getenv("WEB_FILE_BROWSER_DATABASE_PATH")
if databasePath == "" {
databasePath = defaultDatabasePath
}
store, err := auth.Open(databasePath, auth.Options{})
if err != nil {
logger.Error("open authentication database", "error", err)
os.Exit(1)
}
defer func() {
if err := store.Close(); err != nil {
logger.Error("close authentication database", "error", err)
}
}()
address := os.Getenv("WEB_FILE_BROWSER_ADDR")
if address == "" {
address = defaultAddress
}
server := &http.Server{
Addr: address,
Handler: web.New(store, applicationConfig.Roots...),
}
logger.Info("starting web file browser", "address", address)
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Error("serve HTTP", "error", err)
os.Exit(1)
}
}