diff options
| author | Leonardo Bishop <me@leonardobishop.net> | 2025-07-08 23:26:05 +0100 |
|---|---|---|
| committer | Leonardo Bishop <me@leonardobishop.net> | 2025-07-08 23:26:05 +0100 |
| commit | cdb75d3fcbc9339b897f8c6ff4d69a577f017393 (patch) | |
| tree | 5e757cd236540c2cea9874c1bc09f19548db05d5 /main.go | |
| parent | b56101f1a11552067f594679a497ebd4cf7427d4 (diff) | |
Rewrite in Go
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 52 |
1 files changed, 52 insertions, 0 deletions
@@ -0,0 +1,52 @@ +package main + +import ( + "fmt" + "log/slog" + "net/http" + "path" + "path/filepath" + + "github.com/LMBishop/scrapbook/api" + "github.com/LMBishop/scrapbook/pkg/config" + "github.com/LMBishop/scrapbook/pkg/constants" + "github.com/LMBishop/scrapbook/pkg/index" + "github.com/LMBishop/scrapbook/pkg/server" + "github.com/LMBishop/scrapbook/web" +) + +func main() { + slog.Info("welcome to scrapbook") + + var cfg config.MainConfig + err := config.ReadMainConfig(filepath.Join(constants.SysConfDir, "config.toml"), &cfg) + if err != nil { + panic(fmt.Errorf("main config read failed: %w", err)) + } + err = config.ValidateMainConfig(&cfg) + if err != nil { + panic(fmt.Errorf("main config validation failed: %w", err)) + } + + siteIndex := index.NewSiteIndex() + + err = index.ScanDirectory(path.Join(constants.SysDataDir, "sites"), siteIndex) + if err != nil { + panic(fmt.Errorf("could not scan data directory: %w", err)) + } + + slog.Info("initial data directory scan complete", "sites", len(siteIndex.GetSites())) + + if cfg.Command.Host == "" { + slog.Warn("command interface host is empty - neither api or web interface will be accessible") + } + + mux := http.NewServeMux() + + mux.Handle(fmt.Sprintf("%s/api/", cfg.Command.Host), http.StripPrefix("/api/", api.NewMux(&cfg, siteIndex))) + mux.Handle(fmt.Sprintf("%s/", cfg.Command.Host), web.NewMux(&cfg, siteIndex)) + mux.HandleFunc("/", server.ServeSite(siteIndex)) + + err = http.ListenAndServe(fmt.Sprintf("%s:%d", cfg.Listen.Address, cfg.Listen.Port), mux) + slog.Error("http server closing", "reason", err.Error()) +} |
