Skip to content

Commit 71b0866

Browse files
committed
Reorganized the placement of server and client files.
1 parent 7fe5df0 commit 71b0866

File tree

9 files changed

+72
-48
lines changed

9 files changed

+72
-48
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,5 @@ public/js/client.js
5656
public/js/xterm.js
5757
public/css/xterm.css
5858

59+
# output of all builds
60+
out/

Dockerfile

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
FROM node:6.11-alpine AS nodebuilder
1+
FROM node:12.19-alpine AS nodebuilder
22
RUN apk update && apk add git
3-
WORKDIR /root/vdc
3+
WORKDIR /vdc
44
COPY client/ client/
55
COPY public/ public/
6-
COPY package.json package.json
7-
RUN npm install && npm run build-public && npm run build-client-debug
6+
COPY package.json .
7+
COPY package-lock.json .
8+
RUN npm install && npm run build-public && npm run build-client-release
89

9-
FROM golang:1.8.3-alpine AS gobuilder
10+
FROM golang:1.16.4-alpine AS gobuilder
1011
RUN apk update && apk add git
11-
COPY server/ /go/src/github.com/rajch/voxel-dockerclient/server/
12-
WORKDIR /go/src/github.com/rajch/voxel-dockerclient/server/
13-
RUN go get golang.org/x/crypto/bcrypt
14-
RUN CGO_ENABLED='0' go build
12+
WORKDIR /vds
13+
COPY server/ ./server
14+
COPY go.* ./
15+
RUN go mod tidy
16+
RUN CGO_ENABLED='0' go build -o out/voxel-dockerserver server/*.go
1517

16-
FROM scratch
17-
COPY --from=gobuilder /go/src/github.com/rajch/voxel-dockerclient/server/server /server/server
18-
COPY --from=nodebuilder /root/vdc/public/ public/
19-
ENTRYPOINT ["/server/server"]
20-
EXPOSE 80
18+
FROM scratch AS final
19+
WORKDIR /app
20+
COPY --from=nodebuilder /vdc/out .
21+
COPY --from=gobuilder /vds/out/voxel-dockerserver .
22+
ENTRYPOINT ["/app/voxel-dockerserver"]
23+
EXPOSE 8080
24+
VOLUME [ "/app/data" ]

docker-compose.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
version: '3.4'
22

3+
volumes:
4+
vdcvol1:
5+
36
services:
47
vdcservice:
8+
image: rajchaudhuri/voxel-dockerclient:latest
59
build:
610
context: .
711
dockerfile: Dockerfile
812
volumes:
913
- /var/run/docker.sock:/var/run/docker.sock
14+
- vdcvol1:/app/data
1015
ports:
11-
- 5000:8080
16+
- 8080:8080

package.json

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,16 @@
55
"main": "",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1",
8-
"build-public": "copyfiles -f node_modules/xterm/dist/xterm.js public/js && copyfiles -f node_modules/xterm/dist/xterm.css public/css",
9-
"clean-public": "rimraf public/js/xterm.js public/css/xterm.css",
10-
"build-client-debug": "browserify --debug client/main.js -o public/js/client.js",
11-
"build-client-release": "browserify client/main.js -o public/js/client.js",
12-
"clean-client": "rimraf public/js/client.js",
13-
"build-server": "CGO_ENABLED='0' go build -o server/server server/*.go",
14-
"clean-server": "rimraf server/server",
15-
"build-docker": "docker build -f server/Dockerfile -t voxel-dockerclient:goserver .",
16-
"build-all": "npm run build-public && npm run build-client-debug && npm run build-server && npm run build-docker",
17-
"build-all-release": "npm run build-public && npm run build-client-release && npm run build-server && npm run build-docker",
18-
"clean-all": "npm run clean-public && npm run clean-client && npm run clean-server",
19-
"run-docker": "docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -p 8080:80 voxel-dockerclient:goserver",
8+
"build-public": "copyfiles --up 1 public/css/* public/fonts/* public/js/* public/textures/* public/*.html out && copyfiles -f node_modules/xterm/dist/xterm.js out/js && copyfiles -f node_modules/xterm/dist/xterm.css out/css",
9+
"clean-public": "rimraf out/css out/fonts out/js/*dialog.js out/js/xterm.js out/textures out/*.html",
10+
"build-client-debug": "browserify --debug client/main.js -o out/js/client.js",
11+
"build-client-release": "browserify client/main.js -o out/js/client.js",
12+
"clean-client": "rimraf out/js/client.js",
13+
"build-server": "CGO_ENABLED='0' go build -o out/voxel-dockerserver server/*.go",
14+
"clean-server": "rimraf out/server",
15+
"build-debug": "npm run build-public && npm run build-client-debug && npm run build-server",
16+
"build": "npm run build-public && npm run build-client-release && npm run build-server",
17+
"clean": "rimraf out/",
2018
"standard": "standard client/** public/js/**"
2119
},
2220
"repository": {

server/auth.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ import (
88
"time"
99
)
1010

11-
const timeOUT = 120
12-
const sessionCOOKIENAME = "vdc.sid"
11+
const timeOut = 120
12+
const sessionCookieName = "vdc.sid"
13+
14+
const loginDialogFile = "logindialog.html"
15+
const loggedInDialogFile = "loggedindialog.html"
16+
const signUpDialogFile = "signupdialog.html"
1317

1418
type session struct {
1519
SessionID string
@@ -31,9 +35,7 @@ func generateRandomString(n int) (string, error) {
3135

3236
func authorize(next http.Handler) http.Handler {
3337
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
34-
// log.Printf("Sessions:\n%v\n", currentSessions)
35-
36-
sessioncookie, _ := r.Cookie(sessionCOOKIENAME)
38+
sessioncookie, _ := r.Cookie(sessionCookieName)
3739
if sessioncookie == nil {
3840
http.Error(w, "Not logged in at all.", http.StatusUnauthorized)
3941
return
@@ -52,7 +54,7 @@ func authorize(next http.Handler) http.Handler {
5254
}
5355

5456
// Expire session on extra idle time
55-
if time.Since(currentsession.lastActionAt).Seconds() > timeOUT {
57+
if time.Since(currentsession.lastActionAt).Seconds() > timeOut {
5658

5759
// Expire cookie
5860
sessioncookie.Expires = time.Now().AddDate(-1, 0, 0)
@@ -82,27 +84,26 @@ func handleLogin(w http.ResponseWriter, r *http.Request) {
8284
password := r.PostFormValue("password")
8385

8486
// Validate user
85-
//if username == "admin" && password == "Pass@word1" {
8687
if validateUser(username, password) {
8788

8889
sessionid, _ := generateRandomString(12)
8990

90-
sessioncookie := http.Cookie{Name: sessionCOOKIENAME, Value: sessionid, Path: "/", HttpOnly: true}
91+
sessioncookie := http.Cookie{Name: sessionCookieName, Value: sessionid, Path: "/", HttpOnly: true}
9192
http.SetCookie(w, &sessioncookie)
9293

9394
sessionsLock.Lock()
9495
currentSessions[sessionid] = session{SessionID: sessionid, lastActionAt: time.Now()}
9596
sessionsLock.Unlock()
9697

97-
http.ServeFile(w, r, "../public/loggedindialog.html")
98+
http.ServeFile(w, r, loggedInDialogFile)
9899

99100
return
100101
}
101102

102103
}
103104
}
104105

105-
http.ServeFile(w, r, "../public/logindialog.html")
106+
http.ServeFile(w, r, loginDialogFile)
106107
}
107108

108109
func handleSignUp(w http.ResponseWriter, r *http.Request) {
@@ -119,7 +120,7 @@ func handleSignUp(w http.ResponseWriter, r *http.Request) {
119120
}
120121
}
121122

122-
http.ServeFile(w, r, "../public/signupdialog.html")
123+
http.ServeFile(w, r, signUpDialogFile)
123124
}
124125

125126
func signin() http.Handler {
@@ -134,7 +135,7 @@ func signin() http.Handler {
134135

135136
func signout() http.Handler {
136137
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
137-
sessioncookie, _ := r.Cookie(sessionCOOKIENAME)
138+
sessioncookie, _ := r.Cookie(sessionCookieName)
138139
if sessioncookie != nil {
139140
sessionid := sessioncookie.Value
140141

@@ -143,7 +144,7 @@ func signout() http.Handler {
143144
sessionsLock.Unlock()
144145
}
145146

146-
sessioncookie = &http.Cookie{Name: sessionCOOKIENAME, Path: "/", MaxAge: -1}
147+
sessioncookie = &http.Cookie{Name: sessionCookieName, Path: "/", MaxAge: -1}
147148
http.SetCookie(w, sessioncookie)
148149
})
149150
}
@@ -154,7 +155,7 @@ func cleanupSessions() {
154155
sessionsLock.Lock()
155156

156157
for key, value := range currentSessions {
157-
if time.Since(value.lastActionAt) > (time.Second * timeOUT) {
158+
if time.Since(value.lastActionAt) > (time.Second * timeOut) {
158159
sessionsToClean = append(sessionsToClean, key)
159160
}
160161
}

server/dockerproxy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func initDockerProxy(mux *http.ServeMux) {
6363
<-errc
6464
})
6565

66-
// Hook the patch /api to the docker api
66+
// Hook the path /api to the docker api
6767
// Authorize it
6868
mux.Handle("/api/", authorize(http.StripPrefix("/api/", revProxy)))
6969

server/server.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ func main() {
3636
// Set up docker proxy, routes /api (authorized) and /websocket
3737
initDockerProxy(serverMux)
3838

39-
serverMux.Handle("/", http.FileServer(http.Dir("../public")))
39+
// Set up static content from application directory
40+
serverMux.Handle("/", http.FileServer(http.Dir(".")))
4041

4142
// Set up a custom server object
4243
srv := http.Server{Addr: ":8080", Handler: serverMux}
@@ -83,7 +84,7 @@ func main() {
8384
}()
8485

8586
// Start listening using the server
86-
log.Println("Server starting...")
87+
log.Println("Server starting on port 8080...")
8788
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
8889
log.Fatalf("The server failed with the following error:%v\n", err)
8990
}

server/user.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ import (
99
"golang.org/x/crypto/bcrypt"
1010
)
1111

12-
var dataDIR = "/run/secrets/"
13-
var dataFILE = "/run/secrets/vdcpassword"
12+
var dataDIR = ""
13+
var dataFILE = "vdcpassword"
1414

1515
const cryptCOST = 12
1616

1717
var isUserInit bool
1818
var passwordhashstring string
1919

2020
func initUser() {
21-
dataDIR = filepath.Join(".", "../data")
21+
dataDIR = filepath.Join(".", "data")
2222
dataFILE = filepath.Join(dataDIR, "vdcpassword")
2323

2424
passwordhash, err := ioutil.ReadFile(dataFILE)
@@ -29,7 +29,7 @@ func initUser() {
2929
}
3030

3131
func validateUser(username string, password string) bool {
32-
if username == "admin" {
32+
if isUserInit && username == "admin" {
3333
err := bcrypt.CompareHashAndPassword([]byte(passwordhashstring), []byte(password))
3434
if err == nil {
3535
return true

stack.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: '3.4'
2+
3+
volumes:
4+
vdcvol1:
5+
6+
services:
7+
vdcservice:
8+
image: rajchaudhuri/voxel-dockerclient:latest
9+
volumes:
10+
- /var/run/docker.sock:/var/run/docker.sock
11+
- vdcvol1:/app/data
12+
ports:
13+
- 8080:8080

0 commit comments

Comments
 (0)