forked from codetainerapp/codetainer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-server.go
139 lines (110 loc) · 3.27 KB
/
http-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
package codetainer
import (
"fmt"
"net/http"
"strings"
"github.com/elazarl/go-bindata-assetfs"
"github.com/gorilla/context"
"github.com/gorilla/mux"
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
"github.com/gorilla/websocket"
)
// handlerFunc adapts a function to an http.Handler.
type handlerFunc func(ctx *Context) error
var (
Store *sessions.CookieStore
)
func StartServer() {
Log.Infof("Initializing %s (%s)", Name, Version)
r := mux.NewRouter()
r.StrictSlash(true)
if DevMode {
// dev
Log.Debugf("Loading assets from disk.")
r.PathPrefix("/public/").Handler(http.FileServer(http.Dir("./web/")))
} else {
Log.Debugf("Loading assets from memory.")
r.PathPrefix("/public/").Handler(http.FileServer(
&assetfs.AssetFS{
Asset: Asset,
AssetDir: AssetDir,
Prefix: "web",
},
))
}
r.Handle("/", handlerFunc(RouteIndex))
// API v1
r.Handle("/api/v1/codetainer/", handlerFunc(RouteApiV1Codetainer))
r.Handle("/api/v1/codetainer/{id}", handlerFunc(RouteApiV1CodetainerId))
r.Handle("/api/v1/codetainer/{id}/view", handlerFunc(RouteApiV1CodetainerView))
r.Handle("/api/v1/codetainer/{id}/tty", handlerFunc(RouteApiV1CodetainerTTY))
r.Handle("/api/v1/codetainer/{id}/send", handlerFunc(RouteApiV1CodetainerSend))
r.Handle("/api/v1/codetainer/{id}/attach", handlerFunc(RouteApiV1CodetainerAttach))
r.Handle("/api/v1/codetainer/{id}/start", handlerFunc(RouteApiV1CodetainerStart))
r.Handle("/api/v1/codetainer/{id}/stop", handlerFunc(RouteApiV1CodetainerStop))
r.Handle("/api/v1/codetainer/{id}/file", handlerFunc(RouteApiV1CodetainerFile))
r.Handle("/api/v1/image", handlerFunc(RouteApiV1CodetainerImage))
r.Handle("/api/v1/codetainer/{id}/file/download", handlerFunc(RouteApiV1CodetainerFileDownload))
http.Handle("/", r)
Store = sessions.NewCookieStore(securecookie.GenerateRandomKey(32))
Store.Options = &sessions.Options{
//Domain: "localhost", // Chrome doesn't work with localhost domain
Path: "/",
MaxAge: 3600 * 8, // 8 hours
HttpOnly: true,
Secure: *appSSL,
}
var port string = fmt.Sprintf(":%d", 3000)
var proto string = "http"
if *appSSL {
proto = "http"
}
Log.Infof("URL: %s://%s%s", proto, "127.0.0.1", port)
var err error
if *appSSL {
// err = http.ListenAndServeTLS(port, KittyConfig.file.PublicKeyPath,
// KittyConfig.file.PrivateKeyPath, context.ClearHandler(http.DefaultServeMux))
} else {
err = http.ListenAndServe(port, context.ClearHandler(http.DefaultServeMux))
}
if err != nil {
Log.Error(err.Error())
}
}
func (f handlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
source, err := GetRemoteAddr(r)
var ws *websocket.Conn
if strings.Contains(r.URL.String(), "/attach") {
ws, err = upgrader.Upgrade(w, r, nil)
if err != nil {
Log.Error("Unable to upgrade websocket connection:", err)
}
}
if err != nil {
source = "N/A"
}
Log.Debugf("HTTP: %s %s %s", source, r.Method, r.URL)
session, err := Store.Get(r, "codetainer")
if err != nil {
Log.Debug(err)
}
if session.IsNew {
// Log.Debug("Create New Session")
if err := session.Save(r, w); err != nil {
Log.Fatal(err)
}
}
//
context := &Context{
W: w,
R: r,
Session: session,
WS: ws,
// current user stuff
}
err = f(context)
if err != nil {
Log.Error(err)
}
}