forked from mercari/gaurun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
46 lines (40 loc) · 1.1 KB
/
server.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
package gaurun
import (
"log"
"net"
"net/http"
"os"
"strconv"
"strings"
statsGo "github.com/fukata/golang-stats-api-handler"
)
func RegisterHTTPHandlers() {
http.HandleFunc(ConfGaurun.Api.PushUri, PushNotificationHandler)
http.HandleFunc(ConfGaurun.Api.StatAppUri, StatsHandler)
http.HandleFunc(ConfGaurun.Api.ConfigAppUri, ConfigHandler)
statsGo.PrettyPrintEnabled()
http.HandleFunc(ConfGaurun.Api.StatGoUri, statsGo.Handler)
}
func RunHTTPServer() {
// Listen TCP Port
if _, err := strconv.Atoi(ConfGaurun.Core.Port); err == nil {
http.ListenAndServe(":"+ConfGaurun.Core.Port, nil)
}
// Listen UNIX Socket
if strings.HasPrefix(ConfGaurun.Core.Port, "unix:/") {
sockPath := ConfGaurun.Core.Port[5:]
fi, err := os.Lstat(sockPath)
if err == nil && (fi.Mode()&os.ModeSocket) == os.ModeSocket {
err := os.Remove(sockPath)
if err != nil {
log.Fatal("failed to remove " + sockPath)
}
}
l, err := net.Listen("unix", sockPath)
if err != nil {
log.Fatal("failed to listen: " + sockPath)
}
http.Serve(l, nil)
}
log.Fatal("port parameter is invalid: " + ConfGaurun.Core.Port)
}