Skip to content

Commit 0e49fc1

Browse files
authored
feat: Workspaces and File API (#2)
* feat: Add websocket connection * feat: Socket Workspaces * fix: workflow * update: move to gorilla websockets * feat: File API and LSP Instances
1 parent 2027796 commit 0e49fc1

File tree

17 files changed

+280
-5
lines changed

17 files changed

+280
-5
lines changed

.githooks/pre-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ echo "\033[36m
55
Running golangci-lint
66
======================================================================"
77

8-
golangci-lint run .
8+
golangci-lint run . --config .golangci.yaml
99

1010
if [ $? != 0 ]; then
1111
echo "\033[31m

.github/workflows/golangci-lint.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on:
99

1010
permissions:
1111
contents: read
12-
12+
1313
jobs:
1414
golangci:
1515
name: Lint
@@ -23,4 +23,4 @@ jobs:
2323
uses: golangci/golangci-lint-action@v3
2424
with:
2525
version: latest
26-
args: "--out-${NO_FUTURE}format colored-line-number --print-issued-lines --print-linter-name"
26+
args: "--config .golangci.yaml --out-${NO_FUTURE}format colored-line-number --print-issued-lines --print-linter-name"

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.env
22
server
3-
.vscode
3+
.vscode
4+
workspaces

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ FROM build AS dev
1515

1616
WORKDIR /app
1717

18-
RUN apk add --no-cache make
18+
RUN apk add --no-cache make ccls
1919

2020
RUN go install github.com/cespare/reflex@latest
2121

controllers/.gitkeep

Whitespace-only changes.

controllers/file.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package controllers
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
7+
"github.com/delta/codecharacter-lsp-2023/models"
8+
)
9+
10+
func handleFileUpdate(message map[string]interface{}, ws *models.WebsocketConnection) error {
11+
fmt.Println("Processing File Update Request")
12+
var filename string
13+
switch ws.Language {
14+
case "cpp":
15+
filename = "run.cpp"
16+
case "java":
17+
filename = "run.java"
18+
case "python":
19+
filename = "run.py"
20+
}
21+
err := ioutil.WriteFile(ws.WorkspacePath+"/"+filename, []byte(message["code"].(string)), 0644)
22+
if err != nil {
23+
fmt.Println(err)
24+
return err
25+
}
26+
return nil
27+
}

controllers/message.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package controllers
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/delta/codecharacter-lsp-2023/models"
8+
"github.com/gorilla/websocket"
9+
)
10+
11+
func HandleMessage(ws *models.WebsocketConnection, messageBytes []byte) error {
12+
var message map[string]interface{}
13+
err := json.Unmarshal(messageBytes, &message)
14+
if err != nil {
15+
return err
16+
}
17+
_, isPresent := message["jsonrpc"]
18+
fmt.Println("Is JSONRPC? : ", isPresent)
19+
if isPresent {
20+
return handleJSONRPCRequest(ws, messageBytes)
21+
}
22+
return handleWebSocketRequest(ws, message)
23+
}
24+
25+
func SendMessage(ws *models.WebsocketConnection, message map[string]interface{}) error {
26+
messageBytes, err := json.Marshal(message)
27+
if err != nil {
28+
return err
29+
}
30+
err = ws.Connection.WriteMessage(websocket.TextMessage, messageBytes)
31+
if err != nil {
32+
return err
33+
}
34+
return nil
35+
}
36+
37+
func handleJSONRPCRequest(ws *models.WebsocketConnection, messageBytes []byte) error {
38+
fmt.Println("JSONRPC Request : ", string(messageBytes), " with ID : ", ws.ID)
39+
return nil
40+
}
41+
42+
func handleWebSocketRequest(ws *models.WebsocketConnection, message map[string]interface{}) error {
43+
fmt.Println("Websocket Request : ", message, " with ID : ", ws.ID)
44+
if message["operation"] == "fileUpdate" {
45+
return handleFileUpdate(message, ws)
46+
}
47+
return nil
48+
}

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ services:
1111
- ${SERVER_PORT}:8000
1212
volumes:
1313
- .:/app
14+
- ./workspaces:/app/workspaces

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ require (
88
github.com/labstack/echo/v4 v4.9.0
99
)
1010

11+
require github.com/gorilla/websocket v1.5.0
12+
1113
require (
1214
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
15+
github.com/google/uuid v1.3.0
1316
github.com/labstack/gommon v0.4.0 // indirect
1417
github.com/mattn/go-colorable v0.1.13 // indirect
1518
github.com/mattn/go-isatty v0.0.16 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
55
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
66
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
77
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
8+
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
9+
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
10+
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
11+
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
812
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
913
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
1014
github.com/labstack/echo/v4 v4.9.0 h1:wPOF1CE6gvt/kmbMR4dGzWvHMPT+sAEUJOwOTtvITVY=

0 commit comments

Comments
 (0)