This commit is contained in:
Caleb Gardner
2024-06-23 06:48:40 -05:00
parent 6ed9df45fb
commit 3a35a85100
7 changed files with 35 additions and 6 deletions
+1
View File
@@ -172,6 +172,7 @@ func (b *Backend) actualCrashArchive(w http.ResponseWriter, ap App, toArchive Ar
if err == ErrNotFound {
return
} else if err != nil {
log.Println("error finding matching crashes:", err)
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
return
}
+6 -2
View File
@@ -68,8 +68,12 @@ func (m *MongoTable[T]) PartUpdate(ID string, update map[string]any) error {
return res.Err()
}
func (m *MongoTable[CountLog]) RemoveOldLogs(date int) {
m.col.DeleteMany(context.Background(), bson.M{"date": bson.M{"$lt": date}})
func (m *MongoTable[CountLog]) RemoveOldLogs(date int) error {
_, err := m.col.DeleteMany(context.Background(), bson.M{"date": bson.M{"$lt": date}})
if err == mongo.ErrNoDocuments {
return nil
}
return err
}
func (m *MongoTable[CountLog]) Count(platform string) (int, error) {
var filter bson.M
+1 -1
View File
@@ -85,7 +85,7 @@ func (b *Backend) ParseHeader(r *http.Request) (*ParsedHeader, error) {
func (b *Backend) VerifyHeader(w http.ResponseWriter, r *http.Request, keyPerm string, allowManagementKey bool) (*ParsedHeader, error) {
hdr, err := b.ParseHeader(r)
if hdr == nil || hdr.Key == nil {
if err != ErrApiKeyUnauthorized {
if err == ErrApiKeyUnauthorized {
ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized")
return nil, nil
}
+2
View File
@@ -68,6 +68,7 @@ func (b *Backend) countLog(w http.ResponseWriter, r *http.Request) {
}
err = count.PartUpdate(req.ID, map[string]any{"date": curDate})
if err != nil {
log.Println("error updating count log:", err)
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
return
}
@@ -122,6 +123,7 @@ func (b *Backend) getCount(w http.ResponseWriter, r *http.Request) {
}
out, err := count.Count(r.URL.Query().Get("platform"))
if err != nil {
log.Println("error getting count:", err)
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
return
}
+7
View File
@@ -141,6 +141,7 @@ func (b *Backend) createUser(w http.ResponseWriter, r *http.Request) {
defer b.userMutex.Unlock()
matchUsername, err := b.userTable.Find(map[string]any{"username": req.Username})
if err != nil && !errors.Is(err, ErrNotFound) {
log.Println("error when checking for username collisions:", err)
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
return
} else if (err == nil || errors.Is(err, ErrNotFound)) && len(matchUsername) > 0 {
@@ -149,6 +150,7 @@ func (b *Backend) createUser(w http.ResponseWriter, r *http.Request) {
}
matchEmail, err := b.userTable.Find(map[string]any{"email": req.Email})
if err != nil && !errors.Is(err, ErrNotFound) {
log.Println("error when checking for email collisions:", err)
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
return
} else if (err == nil || errors.Is(err, ErrNotFound)) && len(matchEmail) > 0 {
@@ -157,11 +159,13 @@ func (b *Backend) createUser(w http.ResponseWriter, r *http.Request) {
}
u, err := NewUser(req.Username, req.Password, req.Email)
if err != nil {
log.Println("error creating new user:", err)
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
return
}
err = b.userTable.Insert(u)
if err != nil {
log.Println("error inserting new user:", err)
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
return
}
@@ -169,6 +173,7 @@ func (b *Backend) createUser(w http.ResponseWriter, r *http.Request) {
ret.Username = u.Username
ret.Token, err = b.GenerateJWT(u.toReqUser())
if err != nil {
log.Println("error generating token:", err)
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
return
}
@@ -243,12 +248,14 @@ func (b *Backend) login(w http.ResponseWriter, r *http.Request) {
}
hash, err := u.HashPassword(req.Password)
if err != nil {
log.Println("error hashing request password:", err)
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
return
}
if u.Password == hash {
ret.Token, err = b.GenerateJWT(u.toReqUser())
if err != nil {
log.Println("error generating token:", err)
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
return
}
+2
View File
@@ -119,6 +119,7 @@ func (b *BlogApp) LatestBlogs(w http.ResponseWriter, r *http.Request) {
}
blogs, err := b.GetLatestBlogs(int64(page))
if err != nil && err != backend.ErrNotFound {
log.Println("error getting latest blogs:", err)
backend.ReturnError(w, http.StatusInternalServerError, "internal", "internal error")
return
}
@@ -168,6 +169,7 @@ func (b *BlogApp) BlogList(w http.ResponseWriter, r *http.Request) {
}
blogList, err := b.GetBlogList(int64(page))
if err != nil && err != backend.ErrNotFound {
log.Println("error getting blog list:", err)
backend.ReturnError(w, http.StatusInternalServerError, "internal", "internal error")
return
}
+16 -3
View File
@@ -29,7 +29,7 @@ func main() {
log.Fatal("You must specify key directory. ex: darkstorm-server /etc/web-keys")
}
if *mongoURL == "" || *webRoot == "" {
log.Fatal("SPECIFY MONGO AND WEB-ROOT OR I WILL DIE, OH NO, THEIR COMING FOR ME.... **DEATH NOISES**")
log.Fatal("SPECIFY MONGO AND WEB-ROOT OR I WILL DIE, OH NO, THEY'RE COMING FOR ME.... **DEATH NOISES**")
}
go func() {
http.ListenAndServe(":80", http.RedirectHandler("https://darkstorm.tech", http.StatusPermanentRedirect))
@@ -60,13 +60,26 @@ func setupMongo(uri string) {
}
func setupBackend(mux *http.ServeMux) {
testApp := backend.NewSimpleApp("testing",
db.NewMongoTable[backend.CountLog](mongoClient.Database("testing").Collection("count")),
db.NewMongoCrashTable(
mongoClient.Database("testing").Collection("crash"),
mongoClient.Database("testing").Collection("archive"),
))
blogApp = blog.NewBlogApp(back, mongoClient.Database("blog"), mux)
//TODO: SWAssistant and CDR backends
var err error
back, err = backend.NewBackend(db.NewMongoTable[backend.ApiKey](mongoClient.Database("darkstorm").Collection("keys")), blogApp)
back, err = backend.NewBackend(db.NewMongoTable[backend.ApiKey](
mongoClient.Database("darkstorm").Collection("keys")),
testApp,
blogApp,
)
if err != nil {
log.Fatal("error setting up backend:", err)
}
mux.Handle("/", back)
}
func setupWebsite(mux *http.ServeMux, root string) {}
func setupWebsite(mux *http.ServeMux, root string) {
//TODO
}