aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: e5f530a23f76da00b3bc60954af09f5fe592c349 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main

import (
	"sync"

	"git.capotej.com/capotej/communique/cgi"
	"git.capotej.com/capotej/communique/config"
	"git.capotej.com/capotej/communique/http"
	"git.capotej.com/capotej/communique/models"
	"git.capotej.com/capotej/communique/registry"
	"github.com/BurntSushi/toml"
	"github.com/dgraph-io/badger/v3"
	"go.uber.org/zap"
	"go.uber.org/zap/zapio"
)

func main() {
	// ctx := context.Background()
	// Logger
	logger, _ := zap.NewDevelopment()
	defer logger.Sync() // flushes buffer, if any
	log := logger.Sugar()

	// Config
	var cfg config.Config
	//TODO use a flag here
	_, err := toml.DecodeFile("sample-config.toml", &cfg)
	if err != nil {
		log.Fatal(err)
	}
	log.Debugf("Loaded TOML Config: %+v", cfg)

	// DB
	dbOpts := badger.DefaultOptions(cfg.DbPath)
	// dbOpts.Logger = log // TODO needs to adapt Warningf to Warnf
	db, err := badger.Open(dbOpts)
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	log.With("type", "db").Debug("Running GC")
	err = db.RunValueLogGC(0.5)
	if err == badger.ErrNoRewrite {
		log.With("type", "db").Debug("Nothing to GC")
	} else {
		log.Fatal(err)
	}

	// Persister
	persister := models.NewPersister(log, db)

	// Registry
	registry, err := registry.NewRegistry(cfg, persister, log)
	if err != nil {
		log.Fatal(err)
	}

	// Servers
	var mainWg sync.WaitGroup

	// // Internal CGI Servers
	cgiServers := cgi.NewServers(log, persister)
	mainWg.Add(1)
	go cgiServers.Start(cfg)

	// // External Http Server
	writer := &zapio.Writer{Log: logger, Level: zap.DebugLevel}
	defer writer.Close()
	router := http.NewRouter(registry, log)
	mainWg.Add(1)
	go router.Start(writer)

	mainWg.Wait()
}