/
main.go
174 lines (150 loc) · 3.95 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"encoding/json"
"flag"
"fmt"
"math/rand"
"net"
_ "net/http/pprof"
"os"
"os/signal"
"runtime/pprof"
"strconv"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/hashicorp/consul/api"
)
//GitHash set by ld flags at build time
var GitHash = ""
//Version set by git tag
var Version = ""
//BuildDate set by ld flags at build time
var BuildDate = ""
//AppName Application name
var AppName = "Nitro TFTP"
var debugFlag bool
var configFile string
var configString string
var consulConfig string
var versionFlag bool
var genconfigFlag bool
var cpuprofile string
var memprofile string
func init() {
cfgUsage := "Configuration file"
flag.StringVar(&configFile, "config", "", cfgUsage)
flag.StringVar(&configFile, "c", "", cfgUsage+" (shorthand)")
cfgstrUsage := "Configuration string in JSON format"
flag.StringVar(&configString, "config-string", "", cfgstrUsage)
flag.StringVar(&configString, "cs", "", cfgstrUsage+" (shorthand)")
debugUsage := "Enable debug logging"
flag.BoolVar(&debugFlag, "debug", false, debugUsage)
flag.BoolVar(&debugFlag, "d", false, debugUsage+" (shorthand)")
verUsage := "Display Version"
flag.BoolVar(&versionFlag, "version", false, verUsage)
flag.BoolVar(&versionFlag, "v", false, verUsage+" (shorthand)")
gencfgUsage := "Generate example config"
flag.BoolVar(&genconfigFlag, "gencfg", false, gencfgUsage)
flag.BoolVar(&genconfigFlag, "g", false, gencfgUsage+" (shorthand)")
//flag.StringVar(&cpuprofile, "cpuprofile", "", "write cpu profile to file")
//flag.StringVar(&memprofile, "memprofile", "", "write memory profile to file")
}
func main() {
flag.Parse()
// add handlers to help us track memory usage - they don't track memory until they're told to
//profiler.AddMemoryProfilingHandlers()
// Uncomment if you want to start profiling automatically
// profiler.StartProfiling()
// Enable pprof
if cpuprofile != "" {
f, err := os.Create(cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if memprofile != "" {
f, err := os.Create(memprofile)
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(f)
defer f.Close()
}
if debugFlag {
log.SetLevel(log.DebugLevel)
}
// listen on port 6060 (pick a port)
//go http.ListenAndServe(":6060", nil)
if versionFlag {
fmt.Printf("Built: %s\nVersion: %s\nGit Commit: %s\n", BuildDate, Version, GitHash)
os.Exit(0)
}
if genconfigFlag {
cfg := NewConfig()
c, err := json.MarshalIndent(cfg, "", "\t")
if err != nil {
log.Error(err)
os.Exit(1)
}
fmt.Println(string(c))
os.Exit(0)
}
s := &TFTPServer{Debug: debugFlag}
cfg := NewConfig()
if configFile != "" && configString == "" {
err := cfg.Open(configFile)
if err != nil {
log.Error(err)
os.Exit(1)
}
} else if configFile == "" && configString != "" {
err := cfg.StringParse(configString)
if err != nil {
log.Error(err)
os.Exit(1)
}
}
s.LoadConfig(cfg)
ctrlChan := s.Listen()
//register as a consul service
serviceid := strings.Join([]string{AppName, strconv.Itoa(int(time.Now().Unix() * rand.Int63()))}, "_")
if consulConfig != "" {
client, err := api.NewClient(api.DefaultConfig())
if err != nil {
log.Error(err)
os.Exit(1)
}
agent := client.Agent()
host, port, err := net.SplitHostPort(s.sock.LocalAddr().String())
if err != nil {
log.Error(err)
os.Exit(1)
}
iport, err := strconv.Atoi(port)
if err != nil {
log.Error(err)
os.Exit(1)
}
service := &api.AgentServiceRegistration{ID: serviceid, Name: "tftp", Address: host, Port: iport}
agent.ServiceRegister(service)
}
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, os.Kill)
<-sigChan
close(sigChan)
close(ctrlChan)
if consulConfig != "" {
client, err := api.NewClient(api.DefaultConfig())
if err != nil {
log.Error(err)
os.Exit(1)
}
agent := client.Agent()
agent.ServiceDeregister(serviceid)
}
log.Println("Shutting down $s", AppName)
os.Exit(0)
}