Skip to content

Commit 7422edd

Browse files
author
Mathis Van Eetvelde
authored
Merge pull request #6 from Coding-Web-Community/development
First merge from dev into main
2 parents d322993 + 371df97 commit 7422edd

22 files changed

+1032
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
store.json

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: go
2+
3+
go:
4+
- 1.x
5+
- 1.15
6+
7+
script:
8+
- cd api/src
9+
- go test -v .
10+
- go build

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,23 @@
1+
[![Build Status](https://travis-ci.org/Coding-Web-Community/CodingBump.svg?branch=master)](https://travis-ci.org/Coding-Web-Community/CodingBump)
12
# CodingBump
23
Disboard clone (for now)
4+
5+
# Architecture
6+
7+
![](https://media.discordapp.net/attachments/760959186745163806/761885595857190972/unknown.png)
8+
## API
9+
- Keeps track of server list, bump times, etc
10+
11+
## Discord Bot
12+
- Discord bot POSTS guild ID's to the API when bumped
13+
- other things about the Discord bot
14+
15+
## Website
16+
- Website GETS recently bumped guilds
17+
- Things about the website
18+
19+
# Contributing
20+
21+
Please make a new branch `{name}/development/{thing you're working on}` and only PR into the `development` branch!
22+
23+
IE: `mathis/development/api` -> PR -> `development`

api/README.md

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
[![Build Status](https://travis-ci.org/Coding-Web-Community/CodingBump.svg?branch=master)](https://travis-ci.org/Coding-Web-Community/CodingBump)
2+
# Central API
3+
4+
## Usage
5+
Launch the API the following way:
6+
```
7+
cd src
8+
go build -o main .
9+
./main
10+
```
11+
or
12+
```
13+
cd src
14+
go run .
15+
```
16+
17+
# Endpoints
18+
This is where we describe the API endpoints and how they react to certain data.
19+
20+
21+
# bump
22+
23+
**URL** Structure:
24+
```
25+
http://localhost:8080/V1/bump
26+
```
27+
28+
Method: **POST**
29+
30+
**JSON** Body:
31+
```json
32+
{"guildId": 636145886279237652}
33+
```
34+
35+
## **Responses**:
36+
37+
38+
### **200**
39+
- *200 - Ok* | **guildId** successfully bumped!
40+
```json
41+
{
42+
"code": 200,
43+
"message": "Guild bumped",
44+
"payload": {
45+
"guildId": 636145886279237652,
46+
"timestamp": 1601832221
47+
}
48+
}
49+
```
50+
51+
- *200 - Added* | **guildId** successfully bumped!
52+
```json
53+
{
54+
"code": 200,
55+
"message": "Guild added and bumped",
56+
"payload": {
57+
"guildId": 636145886279237152,
58+
"timestamp": 1601832254
59+
}
60+
}
61+
```
62+
63+
### **400**
64+
- *400 - BadRequest* | Request body contains invalid character, most likely a string or a non number
65+
allowed characters: 0-9
66+
```json
67+
{
68+
"code": 400,
69+
"message": "Request body contains invalid character",
70+
"payload": {
71+
"guildId": 0,
72+
"timestamp": 0
73+
}
74+
}
75+
```
76+
- *400 - BadRequest* | **guildId** needs to be 18 characters long
77+
```json
78+
{
79+
"code": 400,
80+
"message": "GuildId does not conform to 18 character long integer requirement",
81+
"payload": {
82+
"guildId": 636149237152,
83+
"timestamp": 0
84+
}
85+
}
86+
```
87+
88+
### **425**
89+
- *425 - TooEarly* | **guildId** bump delta hasn't exceeded the bumping interval. Try again later
90+
```json
91+
{
92+
"code": 425,
93+
"message": "Guild bumped too early",
94+
"payload": {
95+
"guildId": 636145886279237152,
96+
"timestamp": 1601832360
97+
}
98+
}
99+
```
100+
101+
### **500**
102+
- *500 - InternalServerError* | Unrecoverable internal server error. Try again later
103+
```json
104+
{
105+
"code": 425,
106+
"message": "",
107+
"payload": {
108+
"guildId": 0,
109+
"timestamp": 0
110+
}
111+
}
112+
```
113+
114+
*Additional note*:
115+
The **payload** is always a direct and latest representation of the stored guild in the database. That means when getting a `200` or `425` status code, the `timestamp` attribute represents the time that guild was last bumped in a UNIX timestamp.
116+
117+
# fetch
118+
119+
**URL** Structure:
120+
```
121+
http://localhost:8080/V1/fetch
122+
```
123+
124+
Method: **GET**
125+
126+
## **Responses**:
127+
128+
129+
### **200**
130+
- *200 - Ok* | **guildId** successfully bumped!
131+
```json
132+
{
133+
"code":200,
134+
"message":"Ok",
135+
"paypload":[
136+
{"guildId":636145886279237699,"timestamp":1602394289},
137+
{"guildId":636123886245557612,"timestamp":1602394230}
138+
]
139+
}
140+
```
141+
142+
### **400**
143+
- *400 - BadRequest*
144+
```json
145+
{
146+
"code":400,
147+
"message":"BadRequest",
148+
"paypload":[
149+
{}
150+
]
151+
}
152+
```

api/src/bump.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"io/ioutil"
6+
"log"
7+
"net/http"
8+
"strconv"
9+
"strings"
10+
"time"
11+
)
12+
13+
func BumpGuild(w http.ResponseWriter, r *http.Request) {
14+
var guild Guild
15+
16+
// Read request body
17+
body, err := ioutil.ReadAll(r.Body)
18+
if err != nil {
19+
log.Print(err)
20+
WriteBumpResponse(w, http.StatusInternalServerError, "Unable to read request body", guild)
21+
return
22+
}
23+
24+
// Unmarshal body into guild object
25+
err = json.Unmarshal(body, &guild)
26+
if err != nil {
27+
log.Print(err)
28+
if strings.Contains(err.Error(), "invalid character") {
29+
WriteBumpResponse(w, http.StatusBadRequest, "Request body contains invalid character", guild)
30+
return
31+
}
32+
WriteBumpResponse(w, http.StatusBadRequest, "Unable to process request body", guild)
33+
return
34+
}
35+
36+
// check if GuildId's length is 18
37+
if len(strconv.Itoa(guild.GuildId)) != 18 {
38+
errString := "GuildId does not conform to 18 character long integer requirement"
39+
log.Print(errString)
40+
WriteBumpResponse(w, http.StatusBadRequest, errString, guild)
41+
return
42+
}
43+
44+
// Adding timestamp to guild object
45+
ts := time.Now().Unix()
46+
guild.Timestamp = ts
47+
48+
if !gs.GuildInStore(guild) {
49+
// guild is not yet present in GuildStore
50+
gs.AddToStore(guild)
51+
WriteBumpResponse(w, http.StatusOK, "Guild added and bumped", guild)
52+
} else {
53+
// guild is present in GuildStore
54+
if gs.PastInterval(guild) {
55+
// guild.Timestamp has exceeded BUMP_INTERVAL, Timestamp has been updated (bumped)
56+
WriteBumpResponse(w, http.StatusOK, "Guild bumped", guild)
57+
} else {
58+
// guild.Timestamp has not exceeded BUMP_INTERVAL, not updated
59+
guild.Timestamp = gs.GetTimestamp(guild)
60+
WriteBumpResponse(w, http.StatusTooEarly, "Guild bumped too early", guild)
61+
}
62+
}
63+
64+
err = gs.WriteStore()
65+
if err != nil {
66+
log.Panic(err)
67+
WriteBumpResponse(w, http.StatusInternalServerError, "Could not store changes", guild)
68+
return
69+
}
70+
}

0 commit comments

Comments
 (0)