-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflectsvc.go
143 lines (123 loc) · 3.55 KB
/
reflectsvc.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
package main
import (
"context"
"encoding/json"
"errors"
httpTransport "github.com/go-kit/kit/transport/http"
"net"
"net/http"
"os"
"os/signal"
"reflectsvc/misc"
"time"
)
// ErrEmpty is returned when an input string is empty.
var ErrEmpty = errors.New("empty string")
var signalChan chan os.Signal
func handleSignal() {
sig := <-signalChan
xLog.Printf("Got signal %v, exiting immediately\n", sig)
myFatal(-2)
}
func main() {
var err error
initLog("reflectsvc.log")
defer closeLog()
initFlags()
signalChan = make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, os.Kill)
go handleSignal()
if FlagDebug || FlagVerbose {
ifaces, err := net.Interfaces()
if err != nil {
xLog.Printf("error getting interfaces because %s", err.Error())
myFatal()
}
// handle err
for _, i := range ifaces {
addrs, err := i.Addrs()
if nil == err {
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
// process IP address
xLog.Printf("found ip address %s\n", ip.String())
}
}
}
}
// setup for specific services
svc := simpleService{}
successHandler := httpTransport.NewServer(
makeSuccessEndpoint(svc),
decodeSuccessRequest,
encodeSuccessResponse)
validateHandler := httpTransport.NewServer(
makeValidateEndpoint(svc),
decodeValidateRequest,
encodeValidateResponse)
reflectHandler := httpTransport.NewServer(
makeReflectEndpoint(svc),
decodeReflectRequest,
encodeReflectResponse)
reverseHandler := httpTransport.NewServer(
makeReverseEndpoint(svc),
decodeReverseRequest,
encodeResponse)
convertHandler := httpTransport.NewServer(
makeConvertEndpoint(svc),
decodeConvertRequest,
encodeResponse)
xml2JsonHandler := httpTransport.NewServer(
makeXml2JsonEndpoint(svc),
decodeXml2JsonRequest,
x2jEncodeResponse)
http.Handle("/success/", successHandler)
http.Handle("/reverse", reverseHandler)
http.Handle("/parsifal", convertHandler)
http.Handle("/convert", convertHandler)
http.Handle("/reflect", reflectHandler)
http.Handle("/validate", validateHandler)
http.Handle("/xml2json", xml2JsonHandler)
service := "127.0.0.1:" + FlagPort
srv := http.Server{
Addr: service,
Handler: nil,
DisableGeneralOptionsHandler: false,
TLSConfig: nil,
ReadTimeout: 2 * time.Minute,
ReadHeaderTimeout: 2 * time.Minute,
WriteTimeout: 2 * time.Minute,
IdleTimeout: 2 * time.Minute,
MaxHeaderBytes: 0,
TLSNextProto: nil,
ConnState: nil,
ErrorLog: nil,
BaseContext: nil,
ConnContext: nil,
}
if !misc.IsStringSet(&FlagCert) || !misc.IsStringSet(&FlagKey) {
xLog.Printf("reverting to HTTP\n\tCertification file is %s\n\tKey file is %s",
misc.Ternary(misc.IsStringSet(&FlagCert), FlagCert, "missing (use --certfile to set)"),
misc.Ternary(misc.IsStringSet(&FlagKey), FlagKey, "missing (use --keyfile to set)"))
flushLog()
err = srv.ListenAndServe()
} else {
xLog.Printf("using HTTPS\n\tCertification file is %s\n\tKey file is %s",
FlagCert, FlagKey)
flushLog()
err = srv.ListenAndServeTLS(FlagCert, FlagKey)
}
if nil != err {
xLog.Printf("http listener service failed because %s", err.Error())
myFatal()
}
}
func encodeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error {
return json.NewEncoder(w).Encode(response)
}