/
server.go
86 lines (73 loc) · 1.96 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
// Package rest provides the REST API to create highly-available iSCSI targets.
package rest
import (
"encoding/json"
"fmt"
"net/http"
"sync"
"github.com/LINBIT/linstor-gateway/pkg/iscsi"
"github.com/LINBIT/linstor-gateway/pkg/nfs"
"github.com/LINBIT/linstor-gateway/pkg/nvmeof"
log "github.com/sirupsen/logrus"
"github.com/gorilla/mux"
"github.com/rs/cors"
)
type server struct {
router *mux.Router
iscsi *iscsi.ISCSI
nfs *nfs.NFS
nvmeof *nvmeof.NVMeoF
sync.Mutex
}
// Error is the type that is returned in case of an error.
type Error struct {
Code string `json:"code"`
Message string `json:"message"`
}
func (e Error) Error() string {
return e.Message
}
// Errorf takes a StatusCode, a ResponseWriter and a format string.
// It sets up the REST response and writes it to the ResponseWriter
// It also sets the according error code.
func Errorf(code int, w http.ResponseWriter, format string, a ...interface{}) (n int, err error) {
e := Error{
Code: http.StatusText(code),
Message: fmt.Sprintf(format, a...),
}
b, err := json.Marshal(&e)
if err != nil {
return 0, err
}
w.WriteHeader(code)
return fmt.Fprint(w, string(b))
}
func MustError(code int, w http.ResponseWriter, format string, a ...interface{}) {
_, err := Errorf(code, w, format, a...)
if err != nil {
log.WithError(err).Warn("failed to write error response")
}
}
// ListenAndServe is the entry point for the REST API
func ListenAndServe(addr string, controllers []string) {
iscsi, err := iscsi.New(controllers)
if err != nil {
log.Fatalf("Failed to initialize ISCSI: %v", err)
}
nfs, err := nfs.New(controllers)
if err != nil {
log.Fatalf("Failed to initialize NFS: %v", err)
}
nvmeof, err := nvmeof.New(controllers)
if err != nil {
log.Fatalf("Failed to initialize NVMeoF: %v", err)
}
s := &server{
router: mux.NewRouter(),
iscsi: iscsi,
nfs: nfs,
nvmeof: nvmeof,
}
s.routes()
log.Fatal(http.ListenAndServe(addr, cors.Default().Handler(s.router)))
}