Docs and CORS

This commit is contained in:
Caleb Gardner
2024-06-17 19:25:10 -05:00
parent 197860b9d4
commit 11c9ec9242
4 changed files with 64 additions and 8 deletions
+25
View File
@@ -2,6 +2,7 @@ package backend
import ( import (
"crypto/ed25519" "crypto/ed25519"
"embed"
"encoding/json" "encoding/json"
"errors" "errors"
"log" "log"
@@ -10,17 +11,23 @@ import (
"time" "time"
) )
//go:embed embed/*
var robotEmbed embed.FS
// A simple backend that handles user authentication, user count, and crash reports.
type Backend struct { type Backend struct {
userTable Table[User] userTable Table[User]
keyTable Table[ApiKey] keyTable Table[ApiKey]
m *http.ServeMux m *http.ServeMux
apps map[string]App apps map[string]App
managementKeyID string managementKeyID string
corsAddr string
jwtPriv ed25519.PrivateKey jwtPriv ed25519.PrivateKey
jwtPub ed25519.PublicKey jwtPub ed25519.PublicKey
userMutex sync.RWMutex userMutex sync.RWMutex
} }
// Create a new Backend with the given apps. keyTable must be specified.
func NewBackend(keyTable Table[ApiKey], apps ...App) (*Backend, error) { func NewBackend(keyTable Table[ApiKey], apps ...App) (*Backend, error) {
b := &Backend{ b := &Backend{
keyTable: keyTable, keyTable: keyTable,
@@ -28,6 +35,7 @@ func NewBackend(keyTable Table[ApiKey], apps ...App) (*Backend, error) {
apps: make(map[string]App), apps: make(map[string]App),
userMutex: sync.RWMutex{}, userMutex: sync.RWMutex{},
} }
b.m.Handle("GET /robots.txt", http.FileServerFS(robotEmbed))
var hasLog, hasCrash bool var hasLog, hasCrash bool
for i := range apps { for i := range apps {
_, has := b.apps[apps[i].AppID()] _, has := b.apps[apps[i].AppID()]
@@ -76,7 +84,21 @@ func (b *Backend) cleanupLoop() {
} }
} }
// Enable CORS for with the given cors address
func (b *Backend) AddCorsAddress(corsAddr string) {
b.corsAddr = corsAddr
}
// http.Handler
func (b *Backend) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (b *Backend) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if b.corsAddr != "" {
w.Header().Set("Access-Control-Allow-Origin", b.corsAddr)
if r.Method == http.MethodOptions {
w.Header().Set("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Headers", "Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers")
}
}
b.m.ServeHTTP(w, r) b.m.ServeHTTP(w, r)
} }
@@ -84,6 +106,7 @@ func getDate(t time.Time) int {
return (t.Year() * 10000) + (int(t.Month()) * 100) + t.Day() return (t.Year() * 10000) + (int(t.Month()) * 100) + t.Day()
} }
// Enables the use of a management API key for crash and count.
func (b *Backend) EnableManagementKey(managementID string) { func (b *Backend) EnableManagementKey(managementID string) {
b.managementKeyID = managementID b.managementKeyID = managementID
b.m.HandleFunc("DELETE /{appID}/crash/{crashID}", b.managementDeleteCrash) b.m.HandleFunc("DELETE /{appID}/crash/{crashID}", b.managementDeleteCrash)
@@ -91,6 +114,7 @@ func (b *Backend) EnableManagementKey(managementID string) {
b.m.HandleFunc("GET /{appID}/count", b.getCount) b.m.HandleFunc("GET /{appID}/count", b.getCount)
} }
// Enables user creation and authentication.
func (b *Backend) AddUserAuth(userTable Table[User], privKey, pubKey []byte) { func (b *Backend) AddUserAuth(userTable Table[User], privKey, pubKey []byte) {
b.userTable = userTable b.userTable = userTable
b.jwtPriv = privKey b.jwtPriv = privKey
@@ -113,6 +137,7 @@ type retError struct {
ErrorMsg string `json:"errorMsg"` ErrorMsg string `json:"errorMsg"`
} }
// Return an error response with the given status code, code, and message.
func ReturnError(w http.ResponseWriter, status int, code, msg string) { func ReturnError(w http.ResponseWriter, status int, code, msg string) {
w.WriteHeader(status) w.WriteHeader(status)
json.NewEncoder(w).Encode(retError{ json.NewEncoder(w).Encode(retError{
+2
View File
@@ -0,0 +1,2 @@
User-agent: *
Disallow: /
+12
View File
@@ -28,3 +28,15 @@ func NewBlogApp(b *backend.Backend, db *mongo.Database, mux *http.ServeMux) *Blo
//TODO //TODO
return out return out
} }
func (b *BlogApp) AppID() string {
return "blog"
}
func (b *BlogApp) CountTable() backend.CountTable {
return nil
}
func (b *BlogApp) CrashTable() backend.CrashTable {
return nil
}
+25 -8
View File
@@ -8,6 +8,8 @@ import (
"net/http" "net/http"
"path/filepath" "path/filepath"
"github.com/CalebQ42/darkstorm-server/internal/backend"
"github.com/CalebQ42/darkstorm-server/internal/backend/db"
"github.com/CalebQ42/darkstorm-server/internal/blog" "github.com/CalebQ42/darkstorm-server/internal/blog"
"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"
@@ -15,6 +17,7 @@ import (
var ( var (
mongoClient *mongo.Client mongoClient *mongo.Client
back *backend.Backend
blogApp *blog.BlogApp blogApp *blog.BlogApp
) )
@@ -28,28 +31,42 @@ func main() {
if *mongoURL == "" || *webRoot == "" { if *mongoURL == "" || *webRoot == "" {
log.Fatal("SPECIFY MONGO AND WEB-ROOT OR I WILL DIE (Death noises).") log.Fatal("SPECIFY MONGO AND WEB-ROOT OR I WILL DIE (Death noises).")
} }
go func() {
http.ListenAndServe(":80", http.RedirectHandler("https://darkstorm.tech", http.StatusPermanentRedirect))
}()
mux := http.NewServeMux() mux := http.NewServeMux()
mongoClient = setupMongo(*mongoURL) setupMongo(*mongoURL)
setupBackend(mux) setupBackend(mux)
setupWebsite(mux, *webRoot) setupWebsite(mux, *webRoot)
http.ListenAndServeTLS(":443", filepath.Join(flag.Arg(0), "cert.pem"), filepath.Join(flag.Arg(0), "key.pem"), mux) serv := &http.Server{
Addr: ":443",
Handler: mux,
}
err := serv.ListenAndServeTLS(filepath.Join(flag.Arg(0), "cert.pem"), filepath.Join(flag.Arg(0), "key.pem"))
log.Println("webserver closed:", err)
} }
func setupMongo(uri string) *mongo.Client { func setupMongo(uri string) {
mongoCert, err := tls.LoadX509KeyPair(filepath.Join(flag.Arg(0), "mongo.pem"), filepath.Join(flag.Arg(0)+"key.pem")) mongoCert, err := tls.LoadX509KeyPair(filepath.Join(flag.Arg(0), "mongo.pem"), filepath.Join(flag.Arg(0)+"key.pem"))
if err != nil { if err != nil {
log.Fatal("error loading mongo keys:", err) log.Fatal("error loading mongo keys:", err)
} }
client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(uri).SetTLSConfig(&tls.Config{ mongoClient, err = mongo.Connect(context.Background(), options.Client().ApplyURI(uri).SetTLSConfig(&tls.Config{
Certificates: []tls.Certificate{mongoCert}, Certificates: []tls.Certificate{mongoCert},
})) }))
if err != nil { if err != nil {
log.Fatal("error connecting to mongo:", err) log.Fatal("error connecting to mongo:", err)
} }
return client }
func setupBackend(mux *http.ServeMux) {
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)
if err != nil {
log.Fatal("error setting up backend:", err)
}
} }
func setupWebsite(mux *http.ServeMux, root string) {} func setupWebsite(mux *http.ServeMux, root string) {}
func setupBackend(mux *http.ServeMux) {
}