package main import ( "sync" "time" "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" "github.com/microcosm-cc/bluemonday" "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 //TODO ensure that handler names only contain AZa-z and '-' _, 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() ticker := time.NewTicker(5 * time.Minute) defer ticker.Stop() go func() { for { select { case _ = <-ticker.C: log.With("type", "db").Debug("Running GC") db.RunValueLogGC(0.7) } } }() // 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 policy := bluemonday.StrictPolicy() cgiServers := cgi.NewServers(log, persister, cfg, policy) mainWg.Add(1) go cgiServers.Start() // // 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() }