-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
135 lines (121 loc) · 2.77 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
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
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"os"
"strings"
"time"
"github.com/tehmaze-labs/secrets/router"
)
type Handler struct {
Config *Config
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
/*
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("This is an example server.\n"))
*/
var reply interface{}
var err error
switch req.Method {
case "GET":
switch {
case req.URL.Path == "/group/":
var groups = []string{}
for key := range h.Config.Storage.Keys.Scan("group.") {
groups = append(groups, key[6:])
}
reply = groups
case strings.HasPrefix(req.URL.Path, "/group/"):
n := strings.SplitN(req.URL.Path, "/", 4)[2]
k := fmt.Sprintf("group.%s", n)
if h.Config.Storage.Keys.Has(k) {
err = h.Config.Storage.Keys.Get(k, &reply)
} else {
log.Printf("no group %q\n", n)
}
}
}
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
} else if reply == nil {
w.WriteHeader(404)
w.Write([]byte("Not found"))
} else {
out, err := json.MarshalIndent(&reply, "", " ")
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
} else {
w.Header().Set("Content-Type", "text/plain")
w.Write(out)
}
}
}
type Server struct {
http.Server
Config *Config
Router *router.Router
}
func ListenAndServeTLS(cfg *Config) error {
rt := router.NewRouter()
rt.GET("/", getIndex)
rt.GET("/group/", getGroups(cfg))
rt.GET("/group/:name/", getGroup(cfg))
rt.GET("/group/:name/data/", getGroupDatas(cfg))
rt.GET("/group/:name/data/:key/", getGroupData(cfg))
rt.PUT("/group/:name/data/:key/", putGroupData(cfg))
rt.GET("/node/", getNodes(cfg))
sv := &Server{
Config: cfg,
}
sv.Addr = cfg.Server.Bind
sv.Handler = rt
return sv.ListenAndServeTLS()
}
type tcpKeepAliveListener struct {
*net.TCPListener
}
func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
tc, err := ln.AcceptTCP()
if err != nil {
return nil, err
}
tc.SetKeepAlive(true)
tc.SetKeepAlivePeriod(3 * time.Minute)
return tc, nil
}
func (srv *Server) ListenAndServeTLS() error {
addr := srv.Addr
if addr == "" {
addr = ":https"
}
config := &tls.Config{
Certificates: []tls.Certificate{srv.Config.Server.Certificate},
NextProtos: []string{"http/1.1"},
ClientAuth: tls.VerifyClientCertIfGiven,
ClientCAs: srv.Config.Server.Root,
}
host, port, err := net.SplitHostPort(addr)
if err != nil {
return err
}
if host == "" {
host, err = os.Hostname()
if err != nil {
return err
}
}
ln, err := net.Listen("tcp", addr)
if err != nil {
return err
}
log.Printf("server: listening on https://%s:%s/\n", host, port)
tlsListener := tls.NewListener(tcpKeepAliveListener{ln.(*net.TCPListener)}, config)
return srv.Serve(tlsListener)
}