aboutsummaryrefslogtreecommitdiff
path: root/caronte.go
blob: 456eb684e75df6b4c03a768fa8c49df665508718 (plain) (blame)
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
76
77
78
79
80
81
82
83
84
85
86
/*
 * This file is part of caronte (https://github.com/eciavatta/caronte).
 * Copyright (c) 2020 Emiliano Ciavatta.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package main

import (
	"flag"
	"fmt"
	"os"

	"github.com/rs/zerolog"
	log "github.com/rs/zerolog/log"
)

var Version string

var (
	mongoHost   = flag.String("mongo-host", "localhost", "address of MongoDB")
	mongoPort   = flag.Int("mongo-port", 27017, "port of MongoDB")
	dbName      = flag.String("db-name", "caronte", "name of database to use")
	bindAddress = flag.String("bind-address", "0.0.0.0", "address where server is bind")
	bindPort    = flag.Int("bind-port", 3333, "port where server is bind")
)

func main() {

	log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})

	flag.Parse()

	log.Debug().Msg("starting caronte")

	log.Debug().Str("host", *mongoHost).Int("port", *mongoPort).Str("dbName", *dbName).
		Msg("connecting to MongoDB")
	storage, err := NewMongoStorage(*mongoHost, *mongoPort, *dbName)
	if err != nil {
		log.Fatal().Err(err).Msg("failed to connect to MongoDB")
	}

	if Version == "" {
		Version = "undefined"
	}

	log.Debug().Msg("creating application context")
	applicationContext, err := CreateApplicationContext(storage, Version)
	if err != nil {
		log.Fatal().Err(err).Msg("failed to create application context")
	}
	log.Debug().Msg("application context created")

	log.Debug().Msg("creating notification controller")
	notificationController := NewNotificationController(applicationContext)
	go notificationController.Run()
	applicationContext.SetNotificationController(notificationController)
	log.Debug().Msg("notification controller created")

	log.Debug().Msg("creating resources controller")
	resourcesController := NewResourcesController(notificationController)
	go resourcesController.Run()
	log.Debug().Msg("resources controller created")

	log.Debug().Msg("creating application router")
	applicationContext.Configure()
	applicationRouter := CreateApplicationRouter(applicationContext, notificationController, resourcesController)
	log.Debug().Msg("application router created")

	log.Debug().Msg("starting server")
	err = applicationRouter.Run(fmt.Sprintf("%s:%v", *bindAddress, *bindPort))
	if err != nil {
		log.Fatal().Err(err).Msg("failed to create the server")
	}
}