Logging
This commit is contained in:
@@ -172,6 +172,7 @@ func (b *Backend) actualCrashArchive(w http.ResponseWriter, ap App, toArchive Ar
|
|||||||
if err == ErrNotFound {
|
if err == ErrNotFound {
|
||||||
return
|
return
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
|
log.Println("error finding matching crashes:", err)
|
||||||
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,8 +68,12 @@ func (m *MongoTable[T]) PartUpdate(ID string, update map[string]any) error {
|
|||||||
return res.Err()
|
return res.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MongoTable[CountLog]) RemoveOldLogs(date int) {
|
func (m *MongoTable[CountLog]) RemoveOldLogs(date int) error {
|
||||||
m.col.DeleteMany(context.Background(), bson.M{"date": bson.M{"$lt": date}})
|
_, 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) {
|
func (m *MongoTable[CountLog]) Count(platform string) (int, error) {
|
||||||
var filter bson.M
|
var filter bson.M
|
||||||
|
|||||||
@@ -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) {
|
func (b *Backend) VerifyHeader(w http.ResponseWriter, r *http.Request, keyPerm string, allowManagementKey bool) (*ParsedHeader, error) {
|
||||||
hdr, err := b.ParseHeader(r)
|
hdr, err := b.ParseHeader(r)
|
||||||
if hdr == nil || hdr.Key == nil {
|
if hdr == nil || hdr.Key == nil {
|
||||||
if err != ErrApiKeyUnauthorized {
|
if err == ErrApiKeyUnauthorized {
|
||||||
ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized")
|
ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized")
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ func (b *Backend) countLog(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
err = count.PartUpdate(req.ID, map[string]any{"date": curDate})
|
err = count.PartUpdate(req.ID, map[string]any{"date": curDate})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Println("error updating count log:", err)
|
||||||
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -122,6 +123,7 @@ func (b *Backend) getCount(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
out, err := count.Count(r.URL.Query().Get("platform"))
|
out, err := count.Count(r.URL.Query().Get("platform"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Println("error getting count:", err)
|
||||||
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -141,6 +141,7 @@ func (b *Backend) createUser(w http.ResponseWriter, r *http.Request) {
|
|||||||
defer b.userMutex.Unlock()
|
defer b.userMutex.Unlock()
|
||||||
matchUsername, err := b.userTable.Find(map[string]any{"username": req.Username})
|
matchUsername, err := b.userTable.Find(map[string]any{"username": req.Username})
|
||||||
if err != nil && !errors.Is(err, ErrNotFound) {
|
if err != nil && !errors.Is(err, ErrNotFound) {
|
||||||
|
log.Println("error when checking for username collisions:", err)
|
||||||
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||||
return
|
return
|
||||||
} else if (err == nil || errors.Is(err, ErrNotFound)) && len(matchUsername) > 0 {
|
} 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})
|
matchEmail, err := b.userTable.Find(map[string]any{"email": req.Email})
|
||||||
if err != nil && !errors.Is(err, ErrNotFound) {
|
if err != nil && !errors.Is(err, ErrNotFound) {
|
||||||
|
log.Println("error when checking for email collisions:", err)
|
||||||
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||||
return
|
return
|
||||||
} else if (err == nil || errors.Is(err, ErrNotFound)) && len(matchEmail) > 0 {
|
} 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)
|
u, err := NewUser(req.Username, req.Password, req.Email)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Println("error creating new user:", err)
|
||||||
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = b.userTable.Insert(u)
|
err = b.userTable.Insert(u)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Println("error inserting new user:", err)
|
||||||
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -169,6 +173,7 @@ func (b *Backend) createUser(w http.ResponseWriter, r *http.Request) {
|
|||||||
ret.Username = u.Username
|
ret.Username = u.Username
|
||||||
ret.Token, err = b.GenerateJWT(u.toReqUser())
|
ret.Token, err = b.GenerateJWT(u.toReqUser())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Println("error generating token:", err)
|
||||||
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -243,12 +248,14 @@ func (b *Backend) login(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
hash, err := u.HashPassword(req.Password)
|
hash, err := u.HashPassword(req.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Println("error hashing request password:", err)
|
||||||
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if u.Password == hash {
|
if u.Password == hash {
|
||||||
ret.Token, err = b.GenerateJWT(u.toReqUser())
|
ret.Token, err = b.GenerateJWT(u.toReqUser())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Println("error generating token:", err)
|
||||||
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -119,6 +119,7 @@ func (b *BlogApp) LatestBlogs(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
blogs, err := b.GetLatestBlogs(int64(page))
|
blogs, err := b.GetLatestBlogs(int64(page))
|
||||||
if err != nil && err != backend.ErrNotFound {
|
if err != nil && err != backend.ErrNotFound {
|
||||||
|
log.Println("error getting latest blogs:", err)
|
||||||
backend.ReturnError(w, http.StatusInternalServerError, "internal", "internal error")
|
backend.ReturnError(w, http.StatusInternalServerError, "internal", "internal error")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -168,6 +169,7 @@ func (b *BlogApp) BlogList(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
blogList, err := b.GetBlogList(int64(page))
|
blogList, err := b.GetBlogList(int64(page))
|
||||||
if err != nil && err != backend.ErrNotFound {
|
if err != nil && err != backend.ErrNotFound {
|
||||||
|
log.Println("error getting blog list:", err)
|
||||||
backend.ReturnError(w, http.StatusInternalServerError, "internal", "internal error")
|
backend.ReturnError(w, http.StatusInternalServerError, "internal", "internal error")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ func main() {
|
|||||||
log.Fatal("You must specify key directory. ex: darkstorm-server /etc/web-keys")
|
log.Fatal("You must specify key directory. ex: darkstorm-server /etc/web-keys")
|
||||||
}
|
}
|
||||||
if *mongoURL == "" || *webRoot == "" {
|
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() {
|
go func() {
|
||||||
http.ListenAndServe(":80", http.RedirectHandler("https://darkstorm.tech", http.StatusPermanentRedirect))
|
http.ListenAndServe(":80", http.RedirectHandler("https://darkstorm.tech", http.StatusPermanentRedirect))
|
||||||
@@ -60,13 +60,26 @@ func setupMongo(uri string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func setupBackend(mux *http.ServeMux) {
|
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)
|
blogApp = blog.NewBlogApp(back, mongoClient.Database("blog"), mux)
|
||||||
//TODO: SWAssistant and CDR backends
|
//TODO: SWAssistant and CDR backends
|
||||||
var err error
|
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 {
|
if err != nil {
|
||||||
log.Fatal("error setting up backend:", err)
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user