From f1741a7faa9538e9b12ac60e0fbf6c7721a36059 Mon Sep 17 00:00:00 2001 From: Leonardo Bishop Date: Tue, 9 Sep 2025 22:44:09 +0100 Subject: Initial commit --- main.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 main.go (limited to 'main.go') diff --git a/main.go b/main.go new file mode 100644 index 0000000..e337b0e --- /dev/null +++ b/main.go @@ -0,0 +1,54 @@ +package main + +import ( + "fmt" + "log/slog" + "net/http" + "os" + + "git.leonardobishop.net/history/api" + "git.leonardobishop.net/history/internal/config" + "git.leonardobishop.net/history/pkg/database" + "git.leonardobishop.net/history/pkg/entries" + "git.leonardobishop.net/history/pkg/html" +) + +func main() { + if err := run(); err != nil { + slog.Error("Unhandled error", "error", err) + os.Exit(1) + } +} + +func run() error { + c := &config.Config{} + err := config.ReadConfig("config.yaml", c) + if err != nil { + return fmt.Errorf("failed to load config: %w", err) + } + + db, err := database.Connect("database.db") + if err != nil { + return fmt.Errorf("failed to connect to database: %w", err) + } + + if err := database.Migrate(db); err != nil { + return fmt.Errorf("database migration failed: %w", err) + } + + entriesService := entries.NewService(db) + htmlService := html.NewService() + + api := api.NewServer(api.ApiServices{ + EntiresService: entriesService, + HtmlService: htmlService, + }) + + slog.Info("starting HTTP server", "host", c.Server.Host, "port", c.Server.Port) + + if err := http.ListenAndServe(fmt.Sprintf("%s:%s", c.Server.Host, c.Server.Port), api); err != nil { + return fmt.Errorf("failed to start server: %w", err) + } + + return nil +} -- cgit v1.2.3-70-g09d2