-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (46 loc) · 1.41 KB
/
main.go
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
package main
import (
"flag"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"github.com/neboman11/music-wishlist-api/api"
_ "github.com/neboman11/music-wishlist-api/docs"
)
// Folder to search for config file
const configFileLocation = "."
// Name of config file (without extension)
const configFileName = "configuration"
// Type of config file
const configFileExtension = "toml"
func main() {
log.Info().Msgf("API version: %s", CurrentVersionNumber)
readConfigFile()
log.Trace().Msg("Successfully loaded config")
portPtr := flag.Int("p", 3001, "Port to listen for requests on.")
flag.Parse()
// Verify the given port is a valid port number
if *portPtr < 1 || *portPtr > 65535 {
log.Fatal().Msg("Invalid port specified.")
}
startDaemon(*portPtr)
}
// Read in config values to viper and check that necessary values are set
func readConfigFile() {
viper.SetConfigName(configFileName)
viper.SetConfigType(configFileExtension)
viper.AddConfigPath(configFileLocation)
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found ignore error and use defaults
log.Info().Msg("Configuration file not found, using defaults.")
} else {
log.Fatal().Msg(err.Error())
}
}
}
// Setup IPFS and start listening for requests
func startDaemon(port int) {
open_database()
log.Info().Msg("Ready for requests")
api.HandleRequests(port, db)
}