Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua O'Sullivan committed May 4, 2020
2 parents 362aa9b + 52bc3dc commit 2ef0fc6
Show file tree
Hide file tree
Showing 36 changed files with 2,293 additions and 460 deletions.
45 changes: 45 additions & 0 deletions customResponseWriter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"bytes"
"io/ioutil"
"log"
"net/http"
"time"
)

//CustomResponseWriter ...
type CustomResponseWriter struct {
http.ResponseWriter
status int
}

//WriteHeader ...
func (w *CustomResponseWriter) WriteHeader(status int) {
w.status = status
if status != http.StatusNotFound {
w.ResponseWriter.WriteHeader(status)
}
}

func (w *CustomResponseWriter) Write(data []byte) (int, error) {
if w.status != http.StatusNotFound {
return w.ResponseWriter.Write(data)
}
return len(data), nil
}

func getFileHandler(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
customResponseWriter := &CustomResponseWriter{ResponseWriter: w}

h.ServeHTTP(customResponseWriter, r)

if customResponseWriter.status == 404 {
log.Println("[REDIRECT] - To index.html, from:", r.RequestURI)
data, _ := ioutil.ReadFile("dist/index.html")
w.Header().Set("Content-Type", "text/html")
http.ServeContent(w, r, "index.html", time.Now(), bytes.NewReader(data))
}
}
}
107 changes: 85 additions & 22 deletions docs/api-private.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,91 @@ Returns the state of the `ROOM_ID` specified by marshalling the room object.

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

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

```json
[
{
"Name":"Parlor",
"Public":true,
"Cap":64,
"Pop":0
},
{
"Name":"Library",
"Public":true,
"Cap":64,
"Name":"StaticRoom",
"Cap":16,
"Pop":0
},
}
]
```



## get_submission_rooms

`/api/?token=API_TOKEN&method=get_submission_rooms`

Returns a list of submission rooms with their population, capacity and the number of (unpublished) submissions in that room, as follows:

```json
[
{
"Name":"Garden",
"Public":true,
"Name":"SubmissionRoom",
"Desc":"This is a submission room.",
"Cap":64,
"Pop":0
},
{
"Name":"Hidden Static Room",
"Public":false,
"Cap":16,
"Pop":0
"Pop":5,
"Published":10,
"Unpublished":34
}
]
```



## get_submissions

`api/?token=API_TOKEN&method=get_submissions&room_id=ROOM_ID`

Returns all the submissions in the submission cache of the `ROOM_ID` specified, as follows:

```json
[
{
"ID":"127.0.0.1-8-April",
"Addr":"127.0.0.1:36262",
"Message":
{
"ColourIndex":0,
"Sender":"Josh",
"Data":"//////////gAABBBB/KAAB///rAABAAAB/JAAB///rAABAAABABBBBAABAAABABBBB///oAABBBBAABAAABABAAABABAAAB///nAABAAABAB/BAABAAABABAAAB///nAABAAABAB/BAABAAABABAAAB///nAABBBBAAB/CAABBBBABAAAB///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////wAA",
"Span":192
}
}
]
```

The submissions are ordered such that the oldest submission is first.



## set_submission_state

`/api/?token=API_TOKEN&method=set_submission_state&room_id=ROOM_ID&submission_id=SUBMISSION_ID&state=STATE`

Sets the state of submission `SUBMISSION_ID` in `ROOM_ID` to `STATE`.

Valid `STATE` values are: "submitted" and "published".

If a submission's `STATE` is set to "published" and there are already `MaxPublishedSubmissions` published submissions, then the oldest one is discarded.



## reject_submission

`/api/?token=API_TOKEN&method=reject_submission&room_id=ROOM_ID&submission_id=SUBMISSION_ID&offensive=OFFENSIVE`

Rejects the submission `SUBMISSION_ID` in `ROOM_ID`.

`OFFENSIVE` must be `true` or `false` exactly (it is case sensitive).

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.



## announce

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

## create_static_room

`/api/?token=API_TOKEN&method=create_static_room&name=ROOM_NAME&size=ROOM_SIZE&public=PUBLIC`
`/api/?token=API_TOKEN&method=create_static_room&name=ROOM_NAME&size=ROOM_SIZE`

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

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

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.


## create_submission_room

`/api/?token=API_TOKEN&method=create_submission_room&name=ROOM_NAME&desc=DESCRIPTION&size=ROOM_SIZE`

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.

Unlike `create_static_room`, a `ROOM_SIZE` must be specified.



15 changes: 3 additions & 12 deletions docs/api-public.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,15 @@ If `ROOM_ID` exists, returns `true`. Otherwise `false`.

`api/?method=get_public_rooms`

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

```json
[
{
"Name":"Parlor",
"Name":"Pets",
"Desc":"This room is for drawing pictures of your pets! You can make 1 submission to this room per day.",
"Cap":64,
"Pop":12
},
{
"Name":"Library",
"Cap":64,
"Pop":43
},
{
"Name":"Garden",
"Cap":64,
"Pop":27
}
]
```
Expand Down
2 changes: 1 addition & 1 deletion docs/websocket.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ The server will be in charge of dating events as it receives them.
{
"RoomID": "id",
"RoomName": "default",
"Static": true,
"Public": true,
"UserIndex": 1,
"Users": ["Eddie", null, "Josho", null, null, "Martin", "Elle", null],
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ go 1.13
require (
github.com/gorilla/websocket v1.4.1
github.com/mitchellh/mapstructure v1.1.2
honnef.co/go/tools v0.0.1-2020.1.3 // indirect
)
22 changes: 22 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191130070609-6e064ea0cf2d/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
honnef.co/go/tools v0.0.1-2020.1.3 h1:sXmLre5bzIR6ypkjXCDI3jHPssRhc8KD/Ome589sc3U=
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Picto",
"version": "1.1.2",
"version": "1.2.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
Expand Down
65 changes: 13 additions & 52 deletions pictoServer.go
Original file line number Diff line number Diff line change
@@ -1,67 +1,18 @@
package main

import (
"bytes"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"strconv"
"strings"
"time"

"github.com/onfe/Picto/src/server"
)

var roomManager server.RoomManager

//CustomResponseWriter ...
type CustomResponseWriter struct {
http.ResponseWriter
status int
}

//WriteHeader ...
func (w *CustomResponseWriter) WriteHeader(status int) {
w.status = status
if status != http.StatusNotFound {
w.ResponseWriter.WriteHeader(status)
}
}

func (w *CustomResponseWriter) Write(data []byte) (int, error) {
if w.status != http.StatusNotFound {
return w.ResponseWriter.Write(data)
}
return len(data), nil
}

func getFileHandler(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
customResponseWriter := &CustomResponseWriter{ResponseWriter: w}

h.ServeHTTP(customResponseWriter, r)

if customResponseWriter.status == 404 {
log.Println("[REDIRECT] - To index.html, from:", r.RequestURI)
data, _ := ioutil.ReadFile("dist/index.html")
w.Header().Set("Content-Type", "text/html")
http.ServeContent(w, r, "index.html", time.Now(), bytes.NewReader(data))
}
}
}

func loadWordsList(fp string) []string {
data, err := ioutil.ReadFile(fp)
if err != nil {
log.Println("[SYSTEM] - Couldn't open words list.")
panic(err)
}
return strings.Split(strings.Replace(string(data), "\r\n", "\n", -1), "\n")
}

func main() {
//Logfile setup
logFile, err := os.OpenFile("info.log", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
Expand All @@ -76,7 +27,7 @@ func main() {
go NewMonitor(60)

//Loading words list
wordsList := loadWordsList("words.txt")
wordsList := server.LoadWordsList("words.txt")

//Getting env var values
apiToken, tokenSet := os.LookupEnv("API_TOKEN")
Expand All @@ -92,10 +43,20 @@ func main() {
//Creating room manager instance
if tokenSet && portSet { //Only in prod if both API_TOKEN and PORT env variables are set.
log.Println("[ROOM MANAGER] - Creating room manager in PROD mode")
roomManager = server.NewRoomManager(server.MaxRooms, apiToken, "prod", wordsList, "PUBLIC_ROOMS")
roomManager = server.NewRoomManager(server.MaxRooms, apiToken, "prod", wordsList)
} else {
log.Println("[ROOM MANAGER] - Creating room manager in DEV mode")
roomManager = server.NewRoomManager(server.MaxRooms, "dev", "dev", wordsList, "PUBLIC_ROOMS")
roomManager = server.NewRoomManager(server.MaxRooms, "dev", "dev", wordsList)
}

err = roomManager.LoadStaticRoomConfig("STATIC_ROOMS")
if err != nil {
log.Println("Error loading STATIC_ROOMS:", err.Error())
}

err = roomManager.LoadSubmissionRoomConfig("SUBMISSION_ROOMS")
if err != nil {
log.Println("Error loading SUBMISSION_ROOMS:", err.Error())
}

//Seeing random number generator
Expand Down
Loading

0 comments on commit 2ef0fc6

Please sign in to comment.