From 3a35a851001ea64c8e1dc00f987ae8216919f515 Mon Sep 17 00:00:00 2001 From: Caleb Gardner Date: Sun, 23 Jun 2024 06:48:40 -0500 Subject: [PATCH] Logging --- internal/backend/crash.go | 1 + internal/backend/db/mongo.go | 8 ++++++-- internal/backend/header.go | 2 +- internal/backend/log.go | 2 ++ internal/backend/user.go | 7 +++++++ internal/blog/blog.go | 2 ++ main.go | 19 ++++++++++++++++--- 7 files changed, 35 insertions(+), 6 deletions(-) diff --git a/internal/backend/crash.go b/internal/backend/crash.go index 9ee767b..9dd49db 100644 --- a/internal/backend/crash.go +++ b/internal/backend/crash.go @@ -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 } diff --git a/internal/backend/db/mongo.go b/internal/backend/db/mongo.go index c49d718..5579a39 100644 --- a/internal/backend/db/mongo.go +++ b/internal/backend/db/mongo.go @@ -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 diff --git a/internal/backend/header.go b/internal/backend/header.go index 6c47ab5..02868d6 100644 --- a/internal/backend/header.go +++ b/internal/backend/header.go @@ -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 } diff --git a/internal/backend/log.go b/internal/backend/log.go index c84fa3d..cce29d2 100644 --- a/internal/backend/log.go +++ b/internal/backend/log.go @@ -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 } diff --git a/internal/backend/user.go b/internal/backend/user.go index 120712c..cbe6257 100644 --- a/internal/backend/user.go +++ b/internal/backend/user.go @@ -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 } diff --git a/internal/blog/blog.go b/internal/blog/blog.go index e4369cd..1d4ff85 100644 --- a/internal/blog/blog.go +++ b/internal/blog/blog.go @@ -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 } diff --git a/main.go b/main.go index 96daef7..801bbb6 100644 --- a/main.go +++ b/main.go @@ -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 +}