This repository was archived by the owner on Sep 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
102 lines (80 loc) · 2.53 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
//go:generate go run github.com/UnnoTed/fileb0x ./pkg/assets/config.yaml
package main
import (
"net/http"
"strings"
"time"
"github.com/fatih/color"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tja/aykroyd/pkg/backend"
)
// main is the main entry point of the app.
func main() {
// Print banner
color.NoColor = false
color.HiCyan(" __ __ ")
color.HiCyan(".---.-.--.--| |--.-.--.-----.--.--.--| |")
color.HiCyan("| - | | | <| .-| - | | | - |")
color.HiCyan("|___._|___ |__|__|__| |_____|___ |_____|")
color.HiCyan(" |__| |__| ")
color.HiCyan(" ")
// Cobra command
cmd := &cobra.Command{
Use: "aykroyd",
Long: "Email forwards via PostFix.",
Args: cobra.NoArgs,
Version: "2.1.0",
Run: aykroyd,
}
cmd.Flags().BoolP("verbose", "v", false, "Write more")
cmd.Flags().StringP("listen", "l", "0.0.0.0:80", "IP and port on which the server will listen")
cmd.Flags().StringP("assets", "a", "", "Path to static web assets")
cmd.Flags().StringP("db-host", "H", "localhost", "MySQL host")
cmd.Flags().StringP("db-database", "d", "postfix", "MySQL database")
cmd.Flags().StringP("db-username", "u", "postfix", "MySQL username")
cmd.Flags().StringP("db-password", "p", "", "MySQL password")
// Viper config
viper.SetEnvPrefix("AYKROYD")
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.AutomaticEnv()
viper.BindPFlags(cmd.Flags())
viper.SetConfigName("config")
viper.AddConfigPath("/etc/aykroyd")
viper.AddConfigPath("$HOME/.config/aykroyd")
viper.AddConfigPath(".")
viper.ReadInConfig()
// Run command
cmd.Execute()
}
// aykroyd is called if the CLI interfaces has been satisfied.
func aykroyd(cmd *cobra.Command, args []string) {
// Set logging level
if viper.GetBool("verbose") {
logrus.SetLevel(logrus.DebugLevel)
} else {
logrus.SetLevel(logrus.InfoLevel)
}
// Set up server
server, err := backend.NewServer(
viper.GetString("assets"),
viper.GetString("db-host"),
viper.GetString("db-database"),
viper.GetString("db-username"),
viper.GetString("db-password"),
)
if err != nil {
logrus.Fatal(err)
}
defer server.Close()
// Start listening
httpServer := &http.Server{
Handler: server.Router,
Addr: viper.GetString("listen"),
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
logrus.Infof("Listening on %s", httpServer.Addr)
logrus.Fatal(httpServer.ListenAndServe())
}