-
Notifications
You must be signed in to change notification settings - Fork 644
/
server.go
146 lines (119 loc) · 4.27 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
136
137
138
139
140
141
142
143
144
145
146
package console
import (
"encoding/json"
"fmt"
"io"
"net/http"
"regexp"
"strings"
"github.com/chubaofs/chubaofs/console/cutil"
"github.com/chubaofs/chubaofs/console/service"
"github.com/chubaofs/chubaofs/proto"
"github.com/chubaofs/chubaofs/sdk/graphql/client"
"github.com/chubaofs/chubaofs/util/config"
"github.com/chubaofs/chubaofs/util/log"
"github.com/gorilla/mux"
"github.com/samsarahq/thunder/graphql"
"github.com/samsarahq/thunder/graphql/introspection"
)
type ConsoleNode struct {
listen string
masters []string
objectNodeDomain string
server *mux.Router
}
func (c *ConsoleNode) Start(cfg *config.Config) error {
if err := c.loadConfig(cfg); err != nil {
return err
}
cli := client.NewMasterGClient(c.masters)
c.addProxy(proto.AdminUserAPI, cli)
c.addProxy(proto.AdminClusterAPI, cli)
c.addProxy(proto.AdminVolumeAPI, cli)
c.server.HandleFunc(proto.ConsoleIQL, cutil.IQLFun)
loginService := service.NewLoginService(cli)
c.addHandle(proto.ConsoleLoginAPI, loginService.Schema(), loginService)
monitorService := service.NewMonitorService(cfg.GetString("monitor_addr"), cfg.GetString("monitor_app"), cfg.GetString("monitor_cluster"), cfg.GetString("dashboard_addr"))
c.addHandle(proto.ConsoleMonitorAPI, monitorService.Schema(), monitorService)
c.addHandle("/jiankong", monitorService.Schema(), monitorService)
fileService := service.NewFileService(c.objectNodeDomain, c.masters, cli)
c.server.HandleFunc(proto.ConsoleFileDown, func(writer http.ResponseWriter, request *http.Request) {
if err := fileService.DownFile(writer, request); err != nil {
c.writeError(err, writer)
}
})
c.server.HandleFunc(proto.ConsoleFileUpload, func(writer http.ResponseWriter, request *http.Request) {
if err := fileService.UpLoadFile(writer, request); err != nil {
c.writeError(err, writer)
}
})
c.addHandle(proto.ConsoleFile, fileService.Schema(), fileService)
indexPaths := []string{"overview", "login", "overview", "userDetails", "servers",
"serverList", "dashboard", "volumeList", "volumeDetail", "fileList", "operations",
"alarm", "authorization"}
for _, path := range indexPaths {
c.server.HandleFunc("/"+path, c.indexer).Methods("GET")
}
c.server.PathPrefix("/").Handler(http.FileServer(Assets))
return nil
}
func (c *ConsoleNode) indexer(writer http.ResponseWriter, request *http.Request) {
if file, err := Assets.Open("/index.html"); err != nil {
c.writeError(err, writer)
} else {
defer file.Close()
if w, e := io.Copy(writer, file); w == 0 && e != nil {
c.writeError(e, writer)
}
}
}
func (c *ConsoleNode) writeError(err error, writer http.ResponseWriter) {
rep := &proto.GeneralResp{
Message: err.Error(),
Code: http.StatusInternalServerError,
}
value, err := json.Marshal(rep)
if err != nil {
value = []byte("marshal rep has err")
}
if _, err := writer.Write(value); err != nil {
log.LogErrorf("write has err:[%s]", err.Error())
}
}
func (c ConsoleNode) Shutdown() {
}
func (c *ConsoleNode) Sync() {
if err := http.ListenAndServe(fmt.Sprintf(":%s", c.listen), c.server); err != nil {
log.LogErrorf("sync console has err:[%s]", err.Error())
}
}
func NewServer() *ConsoleNode {
return &ConsoleNode{}
}
func (c *ConsoleNode) loadConfig(cfg *config.Config) (err error) {
// parse listen
c.listen = cfg.GetString(proto.ListenPort)
if len(c.listen) == 0 {
c.listen = "80"
}
if match := regexp.MustCompile("^(\\d)+$").MatchString(c.listen); !match {
return fmt.Errorf("invalid listen configuration:[%s]", c.listen)
}
log.LogInfof("console loadConfig: setup config: %v(%v)", proto.ListenPort, c.listen)
// parse master config
c.masters = cfg.GetStringSlice(proto.MasterAddr)
if len(c.masters) == 0 {
return config.NewIllegalConfigError(proto.MasterAddr)
}
log.LogInfof("loadConfig: setup config: %v(%v)", proto.MasterAddr, strings.Join(c.masters, ","))
c.objectNodeDomain = cfg.GetString(proto.ObjectNodeDomain)
c.server = mux.NewRouter()
return
}
func (c *ConsoleNode) addProxy(model string, client *client.MasterGClient) {
c.server.Handle(model, cutil.NewProxyHandler(client)).Methods("POST")
}
func (c *ConsoleNode) addHandle(model string, schema *graphql.Schema, service interface{}) {
introspection.AddIntrospectionToSchema(schema)
c.server.Handle(model, cutil.HTTPHandler(schema)).Methods("POST")
}