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
212
213
214
215
216
217
|
# Go Web Server with Collaborative Editing
This is an expandable Go web server designed to serve a static website with HTML, CSS, and JavaScript files. It includes WebSocket support for collaborative real-time editing of table data.
## Setup Instructions
### Prerequisites
- Go 1.15 or higher
### Installation
1. Install the required dependencies:
```
go get github.com/gorilla/websocket
```
2. No other installation is required beyond having Go installed.
### Running the Server
1. Start the Go server:
```
go run main.go
```
Options:
- `--port=8080` - Change the port (default: 5000)
- `--debug=false` - Disable debug mode
2. Access the website at [http://localhost:5000](http://localhost:5000)
## Project Structure
- `main.go` - The main Go web server with WebSocket support
- `index.html` - Main HTML file with editable table
- `css/` - CSS stylesheets
- `main.css` - General styles
- `skeleton.css` - Grid and layout styles
- `table.css` - Styles for the collaborative table
- `js/` - JavaScript files
- `main.js` - Main application logic with table editing and WebSocket handling
- `jquery-3.7.1.min.js` - jQuery library
## Collaborative Editing Features
The server includes real-time collaborative editing functionality via WebSockets:
- Clients can edit cells in the table, and changes are broadcast to all connected clients
- Table state is maintained on the server and synchronized to new clients when they connect
- Edits made while offline are stored locally and can be synchronized when reconnected
- Visual indicators show when other users make changes to cells
### WebSocket Protocol
The server and clients communicate using a simple JSON-based protocol:
1. **Cell Update Message**: Sent when a user updates a cell
```json
{
"type": "cellUpdate",
"row": 0,
"col": 0,
"value": "New cell value"
}
```
2. **Full Update Message**: Sent by the server when a client connects
```json
{
"type": "fullUpdate",
"tableData": [["Cell 0,0", "Cell 0,1"], ["Cell 1,0", "Cell 1,1"]]
}
```
## How to Expand the Server
### Adding New Routes
To add a new route, add a handler function in the `main.go` file:
```go
http.HandleFunc("/new-route", func(w http.ResponseWriter, r *http.Request) {
// Your handler code here
fmt.Fprintf(w, "This is a new route")
})
```
### Adding API Endpoints
For JSON APIs, add a route that returns a JSON response:
```go
http.HandleFunc("/api/data", func(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{
"key": "value",
"items": []int{1, 2, 3},
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(data)
})
```
### Adding Database Support
To add a database, you can use one of Go's database packages:
1. Add the necessary imports:
```go
import (
"database/sql"
_ "github.com/mattn/go-sqlite3" // For SQLite
)
```
2. Create a database connection:
```go
db, err := sql.Open("sqlite3", "./database.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
```
3. Use the database in your handlers:
```go
http.HandleFunc("/api/users", func(w http.ResponseWriter, r *http.Request) {
rows, err := db.Query("SELECT id, name FROM users")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer rows.Close()
var users []map[string]interface{}
for rows.Next() {
var id int
var name string
rows.Scan(&id, &name)
users = append(users, map[string]interface{}{
"id": id,
"name": name,
})
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(users)
})
```
### Adding User Authentication
For user authentication, you can use middleware:
1. Create an authentication middleware:
```go
func authMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
// Validate token
if token == "" {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
// Call the next handler
next(w, r)
}
}
```
2. Use the middleware with your handlers:
```go
http.HandleFunc("/api/protected", authMiddleware(func(w http.ResponseWriter, r *http.Request) {
// Protected route logic
}))
```
### Environment Variables
The server passes environment variables to the frontend. To add new environment variables:
1. Update the `EnvVars` struct in `main.go`:
```go
type EnvVars struct {
Thing int `json:"thing"`
ApiUrl string `json:"apiUrl"`
NewVar string `json:"newVariable"`
}
```
2. Update the `getEnvVars` function:
```go
func getEnvVars() EnvVars {
return EnvVars{
Thing: 1,
ApiUrl: "http://api.example.com",
NewVar: "value",
}
}
```
## Production Deployment
For production deployment, consider:
1. Building a binary:
```
go build -o server main.go
./server --port=8080 --debug=false
```
2. Setting up a proper web server like Nginx as a reverse proxy
3. Using environment variables for configuration
4. Implementing proper logging and monitoring
5. Adding authentication for WebSocket connections
6. Implementing data persistence (database storage) for table data
7. Adding rate limiting and message size restrictions for WebSocket connections
8. Setting up TLS/SSL for secure WebSocket connections (wss://)
|