/
handler.go
211 lines (192 loc) · 5.3 KB
/
handler.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package server
import (
"errors"
"fmt"
"io"
"mime"
"net"
"net/http"
"path"
"strings"
"sync"
"github.com/elazarl/go-bindata-assetfs"
"github.com/golang/glog"
//"github.com/gorilla/websocket"
"golang.org/x/net/context"
"golang.org/x/net/websocket"
"github.com/tangfeixiong/go-to-bigdata/nps-wss/pkg/httpfs"
"github.com/tangfeixiong/go-to-kubernetes/hadoop-operator/pb"
"github.com/tangfeixiong/go-to-kubernetes/hadoop-operator/pkg/ui/data/webapp"
)
func (ctl *controller) CreateCrd(ctx context.Context, req *pb.CrdReqResp) (*pb.CrdReqResp, error) {
fmt.Println("Request to create CRD:", req)
resp := &pb.CrdReqResp{
Recipe: new(pb.CrdRecipient),
}
if req == nil || req.Recipe == nil {
resp.StateCode = 100
resp.StateMessage = "CRD recipe is required"
return resp, errors.New(resp.StateMessage)
}
if req.Recipe.Plural == "" || req.Recipe.Group == "" {
resp.StateCode = 101
resp.StateMessage = "Empty CRD field is not allowed"
return resp, errors.New(resp.StateMessage)
}
resp.Recipe.Group = req.Recipe.Group
resp.Recipe.Version = req.Recipe.Version
resp.Recipe.Scope = req.Recipe.Scope
resp.Recipe.Plural = req.Recipe.Plural
resp.Recipe.Singular = req.Recipe.Singular
resp.Recipe.Kind = req.Recipe.Kind
err := ctl.ops["rabbitmq-operator"].CreateCRD(req.Recipe)
if err != nil {
glog.Infof("Create CRD failed: %s", err.Error())
resp.StateCode = 10
resp.StateMessage = err.Error()
return resp, err
}
return resp, nil
}
func (ctl *controller) ReapCrd(ctx context.Context, req *pb.CrdReqResp) (*pb.CrdReqResp, error) {
fmt.Println("Requesting:", req)
return new(pb.CrdReqResp), nil
}
/*
https://github.com/golang/go/blob/master/net/http/fs.go
*/
// FileServer returns a handler that serves HTTP requests
// with the contents of the file system rooted at root.
//
// To use the operating system's file system implementation,
// use http.Dir:
//
// http.Handle("/", http.FileServer(http.Dir("/tmp")))
//
// As a special case, the returned file server redirects any request
// ending in "/index.html" to the same path, without the final
// "index.html".
/*
func FileServer(root http.FileSystem) http.Handler {
return &fileHandler{root}
}
*/
func (ctl *controller) serveWebPages(mux *http.ServeMux) {
mime.AddExtensionType(".svg", "image/svg+xml")
// Expose files in third_party/swagger-ui/ on <host>/swagger-ui
// fileserver := FileServer(&assetfs.AssetFS{
// Asset: webapp.Asset,
// AssetDir: webapp.AssetDir,
// Prefix: "static",
// })
// prefix := "/html/"
// mux.Handle(prefix, http.StripPrefix(prefix, fileserver))
ctl.root = &assetfs.AssetFS{
Asset: webapp.Asset,
AssetDir: webapp.AssetDir,
Prefix: "static",
}
prefix := "/test/"
mux.Handle(prefix, http.StripPrefix(prefix, ctl))
// ws := websocket.Server{
// Handshake: ctl.bootHandshake,
// Handler: ctl.handleWss,
// }
// mux.Handle("/ws", ws)
prefix += "ws"
mux.HandleFunc(prefix, ctl.WebsocketHandler)
}
func (f *controller) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if token := r.URL.Query().Get("token"); token != "" {
fmt.Printf("request: %+v\n", r)
http.SetCookie(w, &http.Cookie{
Name: "nps_token",
Value: token,
Path: "/",
})
} else {
fmt.Printf("header referer: %+v\n", r.Header.Get("Referer"))
}
upath := r.URL.Path
if !strings.HasPrefix(upath, "/") {
upath = "/" + upath
r.URL.Path = upath
}
httpfs.ServeFileSystem(w, r, f.root, path.Clean(upath), true)
}
func (ctl *controller) handleWss(wsconn *websocket.Conn) {
fmt.Printf("handle wss: %+v\n", wsconn.Config())
var cookie *http.Cookie
var err error
cookie, err = wsconn.Request().Cookie("novnc_token")
if cookie == nil || err != nil {
fmt.Println(wsconn.Request().URL, wsconn.Request().Form, wsconn.Request().Header)
return
}
token := cookie.Value
if token == "" {
fmt.Println("Unexpected, token does not exist")
return
}
var conn net.Conn
var wsc *websocket.Conn
var targetAddr string
conn, wsc, targetAddr = nil, nil, "127.0.0.1:5670"
if targetAddr == "" {
fmt.Println("API not invoked")
return
}
if wsc != nil {
if wsc == wsconn {
fmt.Println("Already connected")
return
}
fmt.Print("Disconnect old streaming")
if err := wsc.Close(); err != nil {
fmt.Println(err.Error())
} else {
fmt.Println()
}
if conn != nil {
if err := conn.Close(); err != nil {
fmt.Println(err.Error())
} else {
fmt.Println()
}
}
}
if conn == nil {
fmt.Println("Connecting", targetAddr)
conn, err = net.Dial("tcp", targetAddr)
if err != nil {
fmt.Println("Could not connect VNC, error:", err.Error())
wsconn.Close()
return
}
}
wsconn.PayloadType = websocket.BinaryFrame
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
defer wsconn.Close()
l, e := io.Copy(conn, wsconn)
fmt.Println("Client streaming terminated (ws -> vnc), ", l, e)
}()
wg.Add(1)
go func() {
defer wg.Done()
defer conn.Close()
l, e := io.Copy(wsconn, conn)
fmt.Println("Server streaming terminated (vnc -> ws), ", l, e)
}()
//select {}
wg.Wait()
}
func (ctl *controller) bootHandshake(config *websocket.Config, r *http.Request) error {
fmt.Printf("handshake: %+v\nrequest: %+v\n", config, r)
config.Protocol = []string{"binary"}
r.Header.Set("Access-Control-Allow-Origin", "*")
r.Header.Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE")
return nil
}