/
natsmain.go
90 lines (76 loc) · 1.94 KB
/
natsmain.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
package engine
import (
"flag"
"fmt"
"log"
"os"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nuid"
)
// StartNATSServer xxx
func StartNATSServer() {
_ = MyNUID() // to make sure nuid.json is initialized
exe := "nats-server"
// Create a FlagSet and sets the usage
fs := flag.NewFlagSet(exe, flag.ExitOnError)
natsconf := ConfigValue("natsconf")
if natsconf == "" {
natsconf = "natsalone.conf"
}
// Configure the options from the flags/config file
conf := ConfigFilePath(natsconf)
args := []string{"-c", conf}
opts, err := server.ConfigureOptions(fs, args,
server.PrintServerAndExit,
fs.Usage,
server.PrintTLSHelpAndDie)
if err != nil {
server.PrintAndDie(fmt.Sprintf("%s: %s", exe, err))
}
// Create the server with appropriate options.
s, err := server.NewServer(opts)
if err != nil {
server.PrintAndDie(fmt.Sprintf("%s: %s", exe, err))
}
// Configure the logger based on the flags
s.ConfigureLogger()
// Start things up. Block here until done.
if err := server.Run(s); err != nil {
server.PrintAndDie(err.Error())
}
s.WaitForShutdown()
}
var myNUID = ""
// MyNUID xxx
func MyNUID() string {
if myNUID == "" {
myNUID = GetNUID()
}
return myNUID
}
// GetNUID xxx
func GetNUID() string {
nuidpath := LocalConfigFilePath("nuid.json")
if fileExists(nuidpath) {
nuidmap, err := ReadConfigFile(nuidpath)
if err == nil {
nuid, ok := nuidmap["nuid"]
if ok {
return nuid
}
log.Printf("GetNUID: no NUID in %s, rewriting it", nuidpath)
} else {
log.Printf("GetNUID: unable to read/interpret %s, rewriting it", nuidpath)
}
}
file, err := os.OpenFile(nuidpath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
log.Printf("InitLogs: Unable to open %s err=%s", nuidpath, err)
return "UnableToOpenNUIDFile"
}
nuid := nuid.Next()
file.WriteString("{\n\t\"nuid\": \"" + nuid + "\"\n}\n")
file.Close()
log.Printf("GetNUID: generated nuid.json for %s\n", nuid)
return nuid
}