Added API
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
@@ -11,10 +13,14 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/CalebQ42/stupid-backend"
|
||||
"github.com/CalebQ42/stupid-backend/pkg/db"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
func webserver() {
|
||||
flag.Parse()
|
||||
func webserver(mongoStr string) {
|
||||
path := flag.Arg(0)
|
||||
keyPath := flag.Arg(1)
|
||||
if path == "" {
|
||||
@@ -26,6 +32,59 @@ func webserver() {
|
||||
quitChan <- "web arg"
|
||||
return
|
||||
}
|
||||
if mongoStr != "" {
|
||||
client, err := mongo.NewClient(options.Client().ApplyURI(mongoStr))
|
||||
if err != nil {
|
||||
log.Println("Issues connecting to mongo:", err)
|
||||
quitChan <- "web arg"
|
||||
return
|
||||
}
|
||||
err = client.Connect(context.TODO())
|
||||
if err != nil {
|
||||
log.Println("Issues connecting to mongo:", err)
|
||||
quitChan <- "web arg"
|
||||
return
|
||||
}
|
||||
stupid := stupid.NewStupidBackend(db.NewMongoTable(client.Database("stupid").Collection("keys")))
|
||||
users := true
|
||||
var pub, priv []byte
|
||||
stupidPubFil, err := os.Open(keyPath + "/stupid-pub.key")
|
||||
if err != nil {
|
||||
log.Println("Disabling API users:", err)
|
||||
users = false
|
||||
} else {
|
||||
pub, err = io.ReadAll(stupidPubFil)
|
||||
if err != nil {
|
||||
log.Println("Disabling API users:", err)
|
||||
users = false
|
||||
}
|
||||
}
|
||||
stupidPrivFil, err := os.Open(keyPath + "/stupid-pub.key")
|
||||
if err != nil {
|
||||
log.Println("Disabling API users:", err)
|
||||
users = false
|
||||
} else {
|
||||
priv, err = io.ReadAll(stupidPrivFil)
|
||||
if err != nil {
|
||||
log.Println("Disabling API users:", err)
|
||||
users = false
|
||||
}
|
||||
}
|
||||
if users {
|
||||
stupid.EnableUserAuth(db.NewMongoTable(client.Database("stupid").Collection("keys")), pub, priv)
|
||||
}
|
||||
stupid.SetApps(map[string]db.App{
|
||||
"swassistant": {
|
||||
Logs: db.NewMongoTable(client.Database("swassistant").Collection("log")),
|
||||
Crashes: db.NewMongoTable(client.Database("swassistant").Collection("crash")),
|
||||
},
|
||||
"cdr": {
|
||||
Logs: db.NewMongoTable(client.Database("cdr").Collection("log")),
|
||||
Crashes: db.NewMongoTable(client.Database("cdr").Collection("crash")),
|
||||
},
|
||||
})
|
||||
http.Handle("api.darkstorm.tech/", stupid)
|
||||
}
|
||||
http.Handle("/", http.FileServer(http.Dir(path)))
|
||||
http.Handle("/SWAssistant/", swaHandler{})
|
||||
http.Handle("/CDR/", cdrHandler{})
|
||||
|
||||
Reference in New Issue
Block a user