-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
103 lines (92 loc) · 2.31 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"flag"
"fmt"
"io"
"os"
"path/filepath"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/zulkhair/taxi-fares/controller/console"
"github.com/zulkhair/taxi-fares/domain/config"
"gopkg.in/yaml.v2"
)
func main() {
// mode for future improvement
var mode string
flag.StringVar(&mode, "mode", "console", "console/http")
flag.Parse()
// Read Config file
c := readConfigFile()
if c == nil {
log.Info().Msgf("Using default values")
}
// Setup log
logFile := "log/app.log"
if c != nil && c.Log.File != "" {
logFile = c.Log.File
}
// Check if log file exists. if dir not exist then create
if _, err := os.Stat(logFile); os.IsNotExist(err) {
os.MkdirAll(filepath.Dir(logFile), 0700)
}
file, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0664)
if err != nil {
log.Fatal().Msgf("Cannot open log file, err : %v", err)
}
log.Logger = zerolog.New(file).With().Timestamp().Logger()
// mode switch
switch mode {
case "http":
startHttp(c)
case "console":
startConsole(c)
default:
log.Fatal().Msgf("Mode '%s' not found", mode)
}
}
// readConfigFile is function to read and parse config file
func readConfigFile() *config.Config {
// Open file
file, err := os.Open("config.yaml")
if err != nil {
log.Error().Msgf("Cannot open config file, err : %v", err)
return nil
}
// Read the content
content, err := io.ReadAll(file)
if err != nil {
log.Error().Msgf("Cannot read config file, err : %v", err)
return nil
}
// Parse the content
var cfg config.Config
err = yaml.Unmarshal(content, &cfg)
if err != nil {
log.Error().Msgf("Cannot unmarshal config file, err : %v", err)
return nil
}
return &cfg
}
// startHttp is function to start and serve HTTP server
func startHttp(config *config.Config) {
// Todo on the next improvement
fmt.Println("HTTP mode coming soon, still on development")
fmt.Println("Switching to console mode")
startConsole(config)
}
// startConsole is function to start console mode, open stdin and print stdout
func startConsole(config *config.Config) {
log.Info().Msgf("Starting console")
// Create console instance
c, err := console.New(config)
if err != nil {
log.Fatal().Msgf("Cannot start console, err : %v", err)
}
// Start the app
err = c.StartApp()
if err != nil {
log.Error().Msg(err.Error())
os.Exit(1)
}
}