forked from ortuman/jackal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
142 lines (123 loc) · 3.28 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* Copyright (c) 2018 Miguel Ángel Ortuño.
* See the LICENSE file for more information.
*/
package main
import (
"flag"
"fmt"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"github.com/ortuman/jackal/c2s"
"github.com/ortuman/jackal/host"
"github.com/ortuman/jackal/log"
"github.com/ortuman/jackal/router"
"github.com/ortuman/jackal/s2s"
"github.com/ortuman/jackal/storage"
"github.com/ortuman/jackal/version"
)
var logoStr = []string{
` __ __ __ `,
` |__|____ ____ | | _______ | | `,
` | \__ \ _/ ___\| |/ /\__ \ | | `,
` | |/ __ \\ \___| < / __ \| |__`,
` /\__| (____ /\___ >__|_ \(____ /____/`,
` \______| \/ \/ \/ \/ `,
}
const usageStr = `
Usage: jackal [options]
Server Options:
-c, --config <file> Configuration file path
Common Options:
-h, --help Show this message
-v, --version Show version
`
func main() {
var configFile string
var showVersion bool
var showUsage bool
flag.BoolVar(&showUsage, "help", false, "Show this message")
flag.BoolVar(&showUsage, "h", false, "Show this message")
flag.BoolVar(&showVersion, "version", false, "Print version information.")
flag.BoolVar(&showVersion, "v", false, "Print version information.")
flag.StringVar(&configFile, "config", "/etc/jackal/jackal.yml", "Configuration file path.")
flag.StringVar(&configFile, "c", "/etc/jackal/jackal.yml", "Configuration file path.")
flag.Usage = func() {
for i := range logoStr {
fmt.Fprintf(os.Stdout, "%s\n", logoStr[i])
}
fmt.Fprintf(os.Stdout, "%s\n", usageStr)
}
flag.Parse()
// print usage
if showUsage {
flag.Usage()
return
}
// print version
if showVersion {
fmt.Fprintf(os.Stdout, "jackal version: %v\n", version.ApplicationVersion)
return
}
// load configuration
var cfg Config
if err := cfg.FromFile(configFile); err != nil {
fmt.Fprintf(os.Stderr, "jackal: %v\n", err)
return
}
if len(cfg.VirtualHosts) == 0 {
fmt.Fprint(os.Stderr, "jackal: at least one virtual host configuration is required\n")
return
}
// initialize subsystems
log.Initialize(&cfg.Logger)
storage.Initialize(&cfg.Storage)
host.Initialize(cfg.Hosts)
router.Initialize(&router.Config{GetS2SOut: s2s.GetS2SOut})
// create PID file
if err := createPIDFile(cfg.PIDFile); err != nil {
log.Warnf("%v", err)
}
// start serving...
for i := range logoStr {
log.Infof("%s", logoStr[i])
}
log.Infof("")
log.Infof("jackal %v\n", version.ApplicationVersion)
if cfg.Debug.Port > 0 {
go initDebugServer(cfg.Debug.Port)
}
// start serving s2s...
s2s.Initialize(&cfg.S2S, &cfg.Modules)
// start serving c2s...
c2s.Initialize(cfg.VirtualHosts, &cfg.Modules)
}
var debugSrv *http.Server
func initDebugServer(port int) {
debugSrv = &http.Server{}
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
log.Fatalf("%v", err)
}
debugSrv.Serve(ln)
}
func createPIDFile(pidFile string) error {
if len(pidFile) == 0 {
return nil
}
if err := os.MkdirAll(filepath.Dir(pidFile), os.ModePerm); err != nil {
return err
}
file, err := os.Create(pidFile)
if err != nil {
return err
}
currentPid := os.Getpid()
if _, err := file.WriteString(strconv.FormatInt(int64(currentPid), 10)); err != nil {
return err
}
return nil
}