Move to new stupid defalt apps
This commit is contained in:
@@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -10,7 +9,7 @@ import (
|
|||||||
|
|
||||||
"github.com/CalebQ42/stupid-backend"
|
"github.com/CalebQ42/stupid-backend"
|
||||||
"github.com/CalebQ42/stupid-backend/pkg/db"
|
"github.com/CalebQ42/stupid-backend/pkg/db"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"github.com/CalebQ42/stupid-backend/pkg/defaultapp"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
)
|
)
|
||||||
@@ -26,19 +25,10 @@ func setupStupid(keyPath, mongoStr string) error {
|
|||||||
log.Println("Issues connecting to mongo:", err)
|
log.Println("Issues connecting to mongo:", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
swApp := NewDefaultApp("swassistant", client)
|
stupid := stupid.NewStupidBackend(db.NewMongoTable(client.Database("stupid").Collection("keys")), map[string]stupid.App{
|
||||||
cdrApp := NewDefaultApp("cdr", client)
|
"swassistant": defaultapp.NewDefaultApp(client.Database("swassistant")),
|
||||||
webApp := NewDarkstormtech(client)
|
"cdr": defaultapp.NewDefaultApp(client.Database("cdr")),
|
||||||
stupid := stupid.NewStupidBackend(db.NewMongoTable(client.Database("stupid").Collection("keys")), func(app string) stupid.App {
|
"darkstormtech": defaultapp.NewUnauthorizedDataApp(client.Database("darkstormtech")),
|
||||||
switch app {
|
|
||||||
case "swassistant":
|
|
||||||
return swApp
|
|
||||||
case "cdr":
|
|
||||||
return cdrApp
|
|
||||||
case "darkstormtech":
|
|
||||||
return webApp
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
})
|
||||||
users := true
|
users := true
|
||||||
var pub, priv []byte
|
var pub, priv []byte
|
||||||
@@ -71,70 +61,3 @@ func setupStupid(keyPath, mongoStr string) error {
|
|||||||
http.Handle("api.darkstorm.tech/", stupid)
|
http.Handle("api.darkstorm.tech/", stupid)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type defaultApp struct {
|
|
||||||
d *mongo.Database
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewDefaultApp(name string, c *mongo.Client) *defaultApp {
|
|
||||||
return &defaultApp{
|
|
||||||
d: c.Database(name),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *defaultApp) Logs() db.Table {
|
|
||||||
return db.NewMongoTable(d.d.Collection("logs"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *defaultApp) Crashes() db.CrashTable {
|
|
||||||
return db.NewMongoTable(d.d.Collection("crashes"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *defaultApp) Extension(*stupid.Request) bool {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
type darkstormtech struct {
|
|
||||||
*defaultApp
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewDarkstormtech(c *mongo.Client) *darkstormtech {
|
|
||||||
return &darkstormtech{
|
|
||||||
defaultApp: NewDefaultApp("darkstormtech", c),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *darkstormtech) Extension(r *stupid.Request) bool {
|
|
||||||
if len(r.Path) != 2 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if r.Path[0] != "page" {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
res := d.d.Collection("pages").FindOne(context.TODO(), bson.M{"page": r.Path[1]})
|
|
||||||
if res.Err() == mongo.ErrNoDocuments {
|
|
||||||
r.Resp.WriteHeader(http.StatusNotFound)
|
|
||||||
return true
|
|
||||||
} else if res.Err() != nil {
|
|
||||||
log.Println("Error while finding darkstorm.tech page:", res.Err())
|
|
||||||
r.Resp.WriteHeader(http.StatusInternalServerError)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
darkstormPage := struct {
|
|
||||||
Content string
|
|
||||||
}{}
|
|
||||||
err := res.Decode(&darkstormPage)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Error while decoding darkstorm.tech page:", err)
|
|
||||||
r.Resp.WriteHeader(http.StatusInternalServerError)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
fmt.Println(darkstormPage)
|
|
||||||
_, err = r.Resp.Write([]byte(darkstormPage.Content))
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Error while sending darkstorm.tech page:", err)
|
|
||||||
r.Resp.WriteHeader(http.StatusInternalServerError)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user