Skip to content

Commit 2ef0fc6

Browse files
author
Joshua O'Sullivan
committed
Merge branch 'dev'
2 parents 362aa9b + 52bc3dc commit 2ef0fc6

36 files changed

+2293
-460
lines changed

customResponseWriter.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"io/ioutil"
6+
"log"
7+
"net/http"
8+
"time"
9+
)
10+
11+
//CustomResponseWriter ...
12+
type CustomResponseWriter struct {
13+
http.ResponseWriter
14+
status int
15+
}
16+
17+
//WriteHeader ...
18+
func (w *CustomResponseWriter) WriteHeader(status int) {
19+
w.status = status
20+
if status != http.StatusNotFound {
21+
w.ResponseWriter.WriteHeader(status)
22+
}
23+
}
24+
25+
func (w *CustomResponseWriter) Write(data []byte) (int, error) {
26+
if w.status != http.StatusNotFound {
27+
return w.ResponseWriter.Write(data)
28+
}
29+
return len(data), nil
30+
}
31+
32+
func getFileHandler(h http.Handler) http.HandlerFunc {
33+
return func(w http.ResponseWriter, r *http.Request) {
34+
customResponseWriter := &CustomResponseWriter{ResponseWriter: w}
35+
36+
h.ServeHTTP(customResponseWriter, r)
37+
38+
if customResponseWriter.status == 404 {
39+
log.Println("[REDIRECT] - To index.html, from:", r.RequestURI)
40+
data, _ := ioutil.ReadFile("dist/index.html")
41+
w.Header().Set("Content-Type", "text/html")
42+
http.ServeContent(w, r, "index.html", time.Now(), bytes.NewReader(data))
43+
}
44+
}
45+
}

docs/api-private.md

Lines changed: 85 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,39 +32,91 @@ Returns the state of the `ROOM_ID` specified by marshalling the room object.
3232

3333
`/api/?token=API_TOKEN&method=get_static_rooms`
3434

35-
Returns a list of static rooms including if they're public, their population and capacity as follows:
35+
Returns a list of static rooms with their population and capacity as follows:
3636

3737
```json
3838
[
3939
{
40-
"Name":"Parlor",
41-
"Public":true,
42-
"Cap":64,
43-
"Pop":0
44-
},
45-
{
46-
"Name":"Library",
47-
"Public":true,
48-
"Cap":64,
40+
"Name":"StaticRoom",
41+
"Cap":16,
4942
"Pop":0
50-
},
43+
}
44+
]
45+
```
46+
47+
48+
49+
## get_submission_rooms
50+
51+
`/api/?token=API_TOKEN&method=get_submission_rooms`
52+
53+
Returns a list of submission rooms with their population, capacity and the number of (unpublished) submissions in that room, as follows:
54+
55+
```json
56+
[
5157
{
52-
"Name":"Garden",
53-
"Public":true,
58+
"Name":"SubmissionRoom",
59+
"Desc":"This is a submission room.",
5460
"Cap":64,
55-
"Pop":0
56-
},
57-
{
58-
"Name":"Hidden Static Room",
59-
"Public":false,
60-
"Cap":16,
61-
"Pop":0
61+
"Pop":5,
62+
"Published":10,
63+
"Unpublished":34
6264
}
6365
]
6466
```
6567

6668

6769

70+
## get_submissions
71+
72+
`api/?token=API_TOKEN&method=get_submissions&room_id=ROOM_ID`
73+
74+
Returns all the submissions in the submission cache of the `ROOM_ID` specified, as follows:
75+
76+
```json
77+
[
78+
{
79+
"ID":"127.0.0.1-8-April",
80+
"Addr":"127.0.0.1:36262",
81+
"Message":
82+
{
83+
"ColourIndex":0,
84+
"Sender":"Josh",
85+
"Data":"//////////gAABBBB/KAAB///rAABAAAB/JAAB///rAABAAABABBBBAABAAABABBBB///oAABBBBAABAAABABAAABABAAAB///nAABAAABAB/BAABAAABABAAAB///nAABAAABAB/BAABAAABABAAAB///nAABBBBAAB/CAABBBBABAAAB///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////wAA",
86+
"Span":192
87+
}
88+
}
89+
]
90+
```
91+
92+
The submissions are ordered such that the oldest submission is first.
93+
94+
95+
96+
## set_submission_state
97+
98+
`/api/?token=API_TOKEN&method=set_submission_state&room_id=ROOM_ID&submission_id=SUBMISSION_ID&state=STATE`
99+
100+
Sets the state of submission `SUBMISSION_ID` in `ROOM_ID` to `STATE`.
101+
102+
Valid `STATE` values are: "submitted" and "published".
103+
104+
If a submission's `STATE` is set to "published" and there are already `MaxPublishedSubmissions` published submissions, then the oldest one is discarded.
105+
106+
107+
108+
## reject_submission
109+
110+
`/api/?token=API_TOKEN&method=reject_submission&room_id=ROOM_ID&submission_id=SUBMISSION_ID&offensive=OFFENSIVE`
111+
112+
Rejects the submission `SUBMISSION_ID` in `ROOM_ID`.
113+
114+
`OFFENSIVE` must be `true` or `false` exactly (it is case sensitive).
115+
116+
If `OFFENSIVE` is `true`, the client who sent the submission will be ignored for `ClientIgnoreTime` (set in `config.go`) from when this endpoint is called. Otherwise, the submission is simply discarded.
117+
118+
119+
68120
## announce
69121

70122
`/api/?token=API_TOKEN&method=announce&message=MESSAGE&id=ROOM_ID`
@@ -91,10 +143,21 @@ This time can be overwritten by making another call to this endpoint, but you wi
91143

92144
## create_static_room
93145

94-
`/api/?token=API_TOKEN&method=create_static_room&name=ROOM_NAME&size=ROOM_SIZE&public=PUBLIC`
146+
`/api/?token=API_TOKEN&method=create_static_room&name=ROOM_NAME&size=ROOM_SIZE`
95147

96148
Creates a static room (continues to exist when there are no clients connected) with name `ROOM_NAME` and a max clients of `ROOM_SIZE`.
97149

98150
If `ROOM_SIZE` is not supplied it defaults to the `DefaultRoomSize` set in `config.go`.
99151

100-
If `PUBLIC` is "true" then it is a public room that will be displayed in the front page. It defaults to `false` and is case sensitive.
152+
153+
154+
## create_submission_room
155+
156+
`/api/?token=API_TOKEN&method=create_submission_room&name=ROOM_NAME&desc=DESCRIPTION&size=ROOM_SIZE`
157+
158+
Creates a submission room (appears on front page, is public and moderated) with name `ROOM_NAME` and description `DESCRIPTION` that can hold `ROOM_SIZE` clients.
159+
160+
Unlike `create_static_room`, a `ROOM_SIZE` must be specified.
161+
162+
163+

docs/api-public.md

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,15 @@ If `ROOM_ID` exists, returns `true`. Otherwise `false`.
1616

1717
`api/?method=get_public_rooms`
1818

19-
Returns a list of public rooms including their population and capacity, as follows:
19+
Returns a list of public rooms including their description, population and capacity as follows:
2020

2121
```json
2222
[
2323
{
24-
"Name":"Parlor",
24+
"Name":"Pets",
25+
"Desc":"This room is for drawing pictures of your pets! You can make 1 submission to this room per day.",
2526
"Cap":64,
2627
"Pop":12
27-
},
28-
{
29-
"Name":"Library",
30-
"Cap":64,
31-
"Pop":43
32-
},
33-
{
34-
"Name":"Garden",
35-
"Cap":64,
36-
"Pop":27
3728
}
3829
]
3930
```

docs/websocket.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ The server will be in charge of dating events as it receives them.
5050
{
5151
"RoomID": "id",
5252
"RoomName": "default",
53-
"Static": true,
53+
"Public": true,
5454
"UserIndex": 1,
5555
"Users": ["Eddie", null, "Josho", null, null, "Martin", "Elle", null],
5656
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ go 1.13
55
require (
66
github.com/gorilla/websocket v1.4.1
77
github.com/mitchellh/mapstructure v1.1.2
8+
honnef.co/go/tools v0.0.1-2020.1.3 // indirect
89
)

go.sum

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
1+
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
2+
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
13
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
24
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
5+
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
6+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
7+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
8+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
39
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
410
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
11+
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
12+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
13+
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
14+
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
15+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
16+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
17+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
18+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
19+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
20+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
21+
golang.org/x/tools v0.0.0-20191130070609-6e064ea0cf2d/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
22+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
23+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
24+
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
25+
honnef.co/go/tools v0.0.1-2020.1.3 h1:sXmLre5bzIR6ypkjXCDI3jHPssRhc8KD/Ome589sc3U=
26+
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Picto",
3-
"version": "1.1.2",
3+
"version": "1.2.0",
44
"private": true,
55
"scripts": {
66
"serve": "vue-cli-service serve",

pictoServer.go

Lines changed: 13 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,18 @@
11
package main
22

33
import (
4-
"bytes"
54
"io"
6-
"io/ioutil"
75
"log"
86
"math/rand"
97
"net/http"
108
"os"
119
"strconv"
12-
"strings"
13-
"time"
1410

1511
"github.com/onfe/Picto/src/server"
1612
)
1713

1814
var roomManager server.RoomManager
1915

20-
//CustomResponseWriter ...
21-
type CustomResponseWriter struct {
22-
http.ResponseWriter
23-
status int
24-
}
25-
26-
//WriteHeader ...
27-
func (w *CustomResponseWriter) WriteHeader(status int) {
28-
w.status = status
29-
if status != http.StatusNotFound {
30-
w.ResponseWriter.WriteHeader(status)
31-
}
32-
}
33-
34-
func (w *CustomResponseWriter) Write(data []byte) (int, error) {
35-
if w.status != http.StatusNotFound {
36-
return w.ResponseWriter.Write(data)
37-
}
38-
return len(data), nil
39-
}
40-
41-
func getFileHandler(h http.Handler) http.HandlerFunc {
42-
return func(w http.ResponseWriter, r *http.Request) {
43-
customResponseWriter := &CustomResponseWriter{ResponseWriter: w}
44-
45-
h.ServeHTTP(customResponseWriter, r)
46-
47-
if customResponseWriter.status == 404 {
48-
log.Println("[REDIRECT] - To index.html, from:", r.RequestURI)
49-
data, _ := ioutil.ReadFile("dist/index.html")
50-
w.Header().Set("Content-Type", "text/html")
51-
http.ServeContent(w, r, "index.html", time.Now(), bytes.NewReader(data))
52-
}
53-
}
54-
}
55-
56-
func loadWordsList(fp string) []string {
57-
data, err := ioutil.ReadFile(fp)
58-
if err != nil {
59-
log.Println("[SYSTEM] - Couldn't open words list.")
60-
panic(err)
61-
}
62-
return strings.Split(strings.Replace(string(data), "\r\n", "\n", -1), "\n")
63-
}
64-
6516
func main() {
6617
//Logfile setup
6718
logFile, err := os.OpenFile("info.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
@@ -76,7 +27,7 @@ func main() {
7627
go NewMonitor(60)
7728

7829
//Loading words list
79-
wordsList := loadWordsList("words.txt")
30+
wordsList := server.LoadWordsList("words.txt")
8031

8132
//Getting env var values
8233
apiToken, tokenSet := os.LookupEnv("API_TOKEN")
@@ -92,10 +43,20 @@ func main() {
9243
//Creating room manager instance
9344
if tokenSet && portSet { //Only in prod if both API_TOKEN and PORT env variables are set.
9445
log.Println("[ROOM MANAGER] - Creating room manager in PROD mode")
95-
roomManager = server.NewRoomManager(server.MaxRooms, apiToken, "prod", wordsList, "PUBLIC_ROOMS")
46+
roomManager = server.NewRoomManager(server.MaxRooms, apiToken, "prod", wordsList)
9647
} else {
9748
log.Println("[ROOM MANAGER] - Creating room manager in DEV mode")
98-
roomManager = server.NewRoomManager(server.MaxRooms, "dev", "dev", wordsList, "PUBLIC_ROOMS")
49+
roomManager = server.NewRoomManager(server.MaxRooms, "dev", "dev", wordsList)
50+
}
51+
52+
err = roomManager.LoadStaticRoomConfig("STATIC_ROOMS")
53+
if err != nil {
54+
log.Println("Error loading STATIC_ROOMS:", err.Error())
55+
}
56+
57+
err = roomManager.LoadSubmissionRoomConfig("SUBMISSION_ROOMS")
58+
if err != nil {
59+
log.Println("Error loading SUBMISSION_ROOMS:", err.Error())
9960
}
10061

10162
//Seeing random number generator

0 commit comments

Comments
 (0)