summaryrefslogtreecommitdiffstats
path: root/main.go
blob: 4250a247068c51578dad890235407c766ee879f6 (plain)
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
package main

import (
	"fmt"
	"log/slog"
	"net/http"
	"os"

	"git.leonardobishop.net/history/api"
	"git.leonardobishop.net/history/internal/config"
	"git.leonardobishop.net/history/internal/constants"
	"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(constants.SysConfPrefix+"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,

		Config: c,
	})

	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
}