diff options
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 54 |
1 files changed, 54 insertions, 0 deletions
@@ -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 +} |
