/
apps.go
227 lines (215 loc) · 6.49 KB
/
apps.go
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
218
219
220
221
222
223
224
225
226
227
package defaultapp
import (
"context"
"encoding/json"
"io"
"log"
"net/http"
"strings"
"github.com/CalebQ42/stupid-backend/v2"
"github.com/CalebQ42/stupid-backend/v2/db"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// A simple implementation of stupid.App using mongodb.
type App struct {
DB *mongo.Database
}
func NewDefaultApp(db *mongo.Database) *App {
return &App{
DB: db,
}
}
func (a *App) Logs() db.LogTable {
return db.NewMongoTable(a.DB.Collection("logs"))
}
func (a *App) Crashes() db.CrashTable {
return db.NewMongoTable(a.DB.Collection("crashes"))
}
// A simple implementation of stupid.App using mongodb.
// Can make requests to GET: /data/{data id}?key={API key} without any
// authorization checks other then the API Key.
// Pulls from the collection "data" and data must be in the form:
/* {
"_id": "data id",
"data": "data",
}
*/
// The data is sent raw without any processing.
type UnauthorizedDataApp struct {
*App
}
func NewUnauthorizedDataApp(db *mongo.Database) *UnauthorizedDataApp {
return &UnauthorizedDataApp{
App: NewDefaultApp(db),
}
}
func (u *UnauthorizedDataApp) Extension(req *stupid.Request) bool {
if len(req.Path) > 1 && req.Path[0] == "data" {
if req.Method != http.MethodGet {
req.Resp.WriteHeader(http.StatusBadRequest)
return true
}
res := u.DB.Collection("data").FindOne(context.TODO(), bson.M{"_id": strings.Join(req.Path[1:], "/")}, options.FindOne().SetProjection(bson.M{"data": 1, "_id": 0}))
if res.Err() == mongo.ErrNoDocuments {
req.Resp.WriteHeader(http.StatusNotFound)
return true
} else if res.Err() != nil {
log.Println("Error while finding data:", res.Err())
req.Resp.WriteHeader(http.StatusInternalServerError)
return true
}
dat := struct {
Data string
}{}
err := res.Decode(&dat)
if err != nil {
log.Println("Error decoding data:", err)
req.Resp.WriteHeader(http.StatusInternalServerError)
return true
}
_, err = req.Resp.Write([]byte(dat.Data))
if err != nil {
log.Println("Error sending data:", err)
req.Resp.WriteHeader(http.StatusInternalServerError)
}
return true
}
return false
}
// A simple implementation of stupid.App using mongodb.
// Pulls from the collection "data" and data must be in the form:
/* {
"_id": "data id",
"owner": "user uuid",
"data": "data",
}
*/
// The data is sent raw without any processing.
// Requests:
/*
GET: /data/{data id}?key={API key}&token={JWT Token}
DELETE: /data/{data id}?key={API key}&token={JWT Token}
POST: /data/{data id}?key={API key}&token={JWT Token}
Request body will be the data
GET: /data/list?key={API key}?token={JWT Token}
Body will be a list of id strings in JSON format
*/
type AuthorizedDataApp struct {
*App
}
func NewAuthorizedDataApp(db *mongo.Database) *AuthorizedDataApp {
return &AuthorizedDataApp{
App: NewDefaultApp(db),
}
}
func (a *AuthorizedDataApp) Extension(req *stupid.Request) bool {
if len(req.Path) > 1 && req.Path[0] == "data" {
if req.User == nil {
req.Resp.WriteHeader(http.StatusBadRequest)
return true
}
if req.Path[1] == "list" {
if req.Method != http.MethodGet {
req.Resp.WriteHeader(http.StatusBadRequest)
return true
}
res, err := a.DB.Collection("data").Find(context.TODO(), bson.M{"owner": req.User.ID}, options.Find().SetProjection(bson.M{"_id": 1}))
if err == mongo.ErrNoDocuments {
req.Resp.WriteHeader(http.StatusNotFound)
return true
} else if err != nil {
log.Println("Error while getting list of data:", err)
req.Resp.WriteHeader(http.StatusInternalServerError)
return true
}
ids := make([]struct {
ID string `bson:"_id"`
}, 0)
err = res.All(context.TODO(), &ids)
if err != nil {
log.Println("Error while decoding list:", err)
req.Resp.WriteHeader(http.StatusInternalServerError)
return true
}
out := make([]string, len(ids))
for i := range ids {
out[i] = ids[i].ID
}
outDat, err := json.Marshal(out)
if err != nil {
log.Println("Error while marshaling data:", err)
req.Resp.WriteHeader(http.StatusInternalServerError)
return true
}
_, err = req.Resp.Write(outDat)
if err != nil {
log.Println("Error while writing data:", err)
req.Resp.WriteHeader(http.StatusInternalServerError)
}
return true
}
id := strings.Join(req.Path[1:], "/")
switch req.Method {
case http.MethodGet:
res := a.DB.Collection("data").FindOne(context.TODO(), bson.M{"_id": id, "owner": req.User.ID}, options.FindOne().SetProjection(bson.M{"data": 1, "_id": 0}))
if res.Err() == mongo.ErrNoDocuments {
req.Resp.WriteHeader(http.StatusNotFound)
return true
} else if res.Err() != nil {
log.Println("Error while getting data:", res.Err())
req.Resp.WriteHeader(http.StatusInternalServerError)
return true
}
dat := struct {
Data string
}{}
err := res.Decode(&dat)
if err != nil {
log.Println("Error while decoding data:", err)
req.Resp.WriteHeader(http.StatusInternalServerError)
return true
}
_, err = req.Resp.Write([]byte(dat.Data))
if err != nil {
log.Println("Error while writing data:", err)
req.Resp.WriteHeader(http.StatusInternalServerError)
}
case http.MethodDelete:
res := a.DB.Collection("data").FindOneAndDelete(context.TODO(), bson.M{"_id": id, "owner": req.User.ID})
if res.Err() == mongo.ErrNoDocuments {
req.Resp.WriteHeader(http.StatusNotFound)
return true
} else if res.Err() != nil {
log.Println("Error while getting data:", res.Err())
req.Resp.WriteHeader(http.StatusInternalServerError)
}
case http.MethodPost:
dat, err := io.ReadAll(req.Body)
if err != nil {
log.Println("Error while reading body:", err)
req.Resp.WriteHeader(http.StatusInternalServerError)
return true
}
res := a.DB.Collection("data").FindOneAndUpdate(context.TODO(), bson.M{"_id": id, "owner": req.User.ID}, bson.M{"data": string(dat)})
if res.Err() == mongo.ErrNoDocuments {
_, err = a.DB.Collection("data").InsertOne(context.TODO(), bson.M{"_id": id, "owner": req.User.ID, "data": string(dat)})
if err != nil {
log.Println("Error while creating data:", err)
req.Resp.WriteHeader(http.StatusInternalServerError)
return true
}
} else if res.Err() != nil {
log.Println("Error while updating data:", res.Err())
req.Resp.WriteHeader(http.StatusInternalServerError)
return true
}
req.Resp.WriteHeader(http.StatusCreated)
default:
req.Resp.WriteHeader(http.StatusBadRequest)
}
return true
}
return false
}