-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
70 lines (59 loc) · 1.96 KB
/
main.go
File metadata and controls
70 lines (59 loc) · 1.96 KB
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
package main
import (
"flag"
"log"
"net/http"
"github.com/docker/docker/client"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"github.com/unklearn/notebook-backend/channels"
"github.com/unklearn/notebook-backend/connection"
containerservices "github.com/unklearn/notebook-backend/container-services"
"github.com/unklearn/notebook-backend/notebooks"
)
var addr = flag.String("addr", "localhost:8080", "http service address")
func CheckOrigin(r *http.Request) bool {
return true
}
var dcs *containerservices.DockerContainerService
var upgrader = websocket.Upgrader{
CheckOrigin: CheckOrigin,
} // use default options
func HandleWS(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
// Use the path for registering channels to conn
vars := mux.Vars(r)
notebookId := vars["notebookId"]
// Maps execId to a multiplexed connection
mx := connection.NewMxedWebsocketConn(c, notebookId)
mx.RegisterChannel(notebookId, channels.NewRootChannel(notebookId))
if err != nil {
log.Print("upgrade error:", err)
return
}
defer c.Close()
executor := NewCommandExecutor(dcs, mx)
// Run connector handler
executor.ConnectionHandler()
}
// Main serve function that runs the HTTP handler routes as well as the websocker handler.
// The routes will handle notebook related API calls, and the websocket will relay container
// outputs and execution status of a cell.
func main() {
// Create new docker client
cli, err := client.NewClientWithOpts()
router := mux.NewRouter()
if err != nil {
panic(err)
}
dcs = containerservices.NewDockerContainerService(cli)
// Register websocket handler
router.HandleFunc("/websocket/{notebookId}", HandleWS)
router.HandleFunc("/api/v1/notebooks", notebooks.NotebooksHandler)
router.HandleFunc("/api/v1/notebooks/{notebookId}", notebooks.NotebookHandler)
// Root router route
http.Handle("/", router)
log.Printf("Listening on %v\n", *addr)
// Listen and serve
log.Fatal(http.ListenAndServe(*addr, nil))
}