From 2afbd64dc2e0e2dc099ae5a68a53ad42ee1ef42d Mon Sep 17 00:00:00 2001 From: Caleb Gardner Date: Wed, 7 Aug 2024 02:24:54 -0500 Subject: [PATCH] Added CallbackApp to better access backend.Backend from with in an App Fixed issues with CORS not being able to do an OPTIONS request Fixed folders not being processed properly. Fixed some other issues. --- internal/backend/app.go | 7 +++++++ internal/backend/darkstorm.go | 8 ++++++-- internal/blog/main.go | 7 +++++-- internal/cdr/backend.go | 9 ++++++--- internal/swassistant/main.go | 9 ++++++--- main.go | 21 +++++++++------------ 6 files changed, 39 insertions(+), 22 deletions(-) diff --git a/internal/backend/app.go b/internal/backend/app.go index b46e609..be31929 100644 --- a/internal/backend/app.go +++ b/internal/backend/app.go @@ -9,6 +9,12 @@ type App interface { CrashTable() CrashTable } +// Provides an App access to it's parent *Backend. This is called only once, while setting up the Backend. +type CallbackApp interface { + App + AddBackend(*Backend) +} + // Allows for an App to filter crashes before they get added to the DB, such as making sure the crash is from the correct version. type CrashFilterApp interface { App @@ -17,6 +23,7 @@ type CrashFilterApp interface { // Allows an app more flexibility by directly interfacing with the backend's mux type ExtendedApp interface { + App Extension(*http.ServeMux) } diff --git a/internal/backend/darkstorm.go b/internal/backend/darkstorm.go index a6255ea..4aef5c4 100644 --- a/internal/backend/darkstorm.go +++ b/internal/backend/darkstorm.go @@ -46,6 +46,9 @@ func NewBackend(keyTable Table[ApiKey], apps ...App) (*Backend, error) { if ext, is := apps[i].(ExtendedApp); is { ext.Extension(b.m) } + if back, is := apps[i].(CallbackApp); is { + back.AddBackend(b) + } if !hasLog && apps[i].CountTable() != nil { hasLog = true } @@ -65,6 +68,7 @@ func NewBackend(keyTable Table[ApiKey], apps ...App) (*Backend, error) { b.m.HandleFunc("DELETE /crash/{crashID}", b.deleteCrash) b.m.HandleFunc("POST /crash/archive", b.archiveCrash) } + b.m.HandleFunc("OPTIONS /", func(_ http.ResponseWriter, _ *http.Request) {}) //Here to send just CORS data. go b.cleanupLoop() return b, nil } @@ -97,9 +101,9 @@ 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-Methods", "*") 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") + w.Header().Set("Access-Control-Allow-Headers", "Access-Control-Allow-Headers, Authorization, X-API-Key, Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers") } } b.m.ServeHTTP(w, r) diff --git a/internal/blog/main.go b/internal/blog/main.go index 04e6588..7f72e16 100644 --- a/internal/blog/main.go +++ b/internal/blog/main.go @@ -16,9 +16,8 @@ type BlogApp struct { conv *bbConvert.HTMLConverter } -func NewBlogApp(b *backend.Backend, db *mongo.Database) *BlogApp { +func NewBlogApp(db *mongo.Database) *BlogApp { out := &BlogApp{ - back: b, blogCol: db.Collection("blog"), authCol: db.Collection("author"), portfolioCol: db.Collection("portfolio"), @@ -40,6 +39,10 @@ func (b *BlogApp) CrashTable() backend.CrashTable { return nil } +func (b *BlogApp) AddBackend(back *backend.Backend) { + b.back = back +} + func (b *BlogApp) Extension(mux *http.ServeMux) { mux.HandleFunc("GET /blog", b.reqLatestBlogs) mux.HandleFunc("GET /blog/list", b.reqBlogList) diff --git a/internal/cdr/backend.go b/internal/cdr/backend.go index 654699a..0ff7990 100644 --- a/internal/cdr/backend.go +++ b/internal/cdr/backend.go @@ -17,7 +17,7 @@ type CDRBackend struct { db *mongo.Database } -func NewBackend(back *backend.Backend, db *mongo.Database) *CDRBackend { +func NewBackend(db *mongo.Database) *CDRBackend { go func() { for range time.Tick(time.Hour) { log.Println("CDR: Deleting expired dice") @@ -29,8 +29,7 @@ func NewBackend(back *backend.Backend, db *mongo.Database) *CDRBackend { } }() return &CDRBackend{ - back: back, - db: db, + db: db, } } @@ -46,6 +45,10 @@ func (b CDRBackend) CrashTable() backend.CrashTable { return db.NewMongoCrashTable(b.db.Collection("crashes"), b.db.Collection("crashArchive")) } +func (b *CDRBackend) AddBackend(back *backend.Backend) { + b.back = back +} + func (s CDRBackend) AddCrash(cr backend.IndividualCrash) bool { res := s.db.Collection("versions").FindOne(context.Background(), bson.M{"version": cr.Version}) return res.Err() != mongo.ErrNoDocuments diff --git a/internal/swassistant/main.go b/internal/swassistant/main.go index 0cc8a47..4de7646 100644 --- a/internal/swassistant/main.go +++ b/internal/swassistant/main.go @@ -17,7 +17,7 @@ type SWBackend struct { db *mongo.Database } -func NewSWBackend(back *backend.Backend, db *mongo.Database) *SWBackend { +func NewSWBackend(db *mongo.Database) *SWBackend { go func() { for range time.Tick(time.Hour) { log.Println("SWAssistant: Deleting expired profiles") @@ -29,8 +29,7 @@ func NewSWBackend(back *backend.Backend, db *mongo.Database) *SWBackend { } }() return &SWBackend{ - back: back, - db: db, + db: db, } } @@ -46,6 +45,10 @@ func (s *SWBackend) CrashTable() backend.CrashTable { return db.NewMongoCrashTable(s.db.Collection("crashes"), s.db.Collection("crashArchive")) } +func (s *SWBackend) AddBackend(b *backend.Backend) { + s.back = b +} + func (s *SWBackend) AddCrash(cr backend.IndividualCrash) bool { res := s.db.Collection("versions").FindOne(context.Background(), bson.M{"version": cr.Version}) return res.Err() != mongo.ErrNoDocuments diff --git a/main.go b/main.go index 73dc460..0d3e787 100644 --- a/main.go +++ b/main.go @@ -52,7 +52,7 @@ func main() { Addr: *addr, Handler: mux, } - err := serv.ListenAndServeTLS(filepath.Join(flag.Arg(0), "cert.pem"), filepath.Join(flag.Arg(0), "key.pem")) + err := serv.ListenAndServeTLS(filepath.Join(flag.Arg(0), "fullchain.pem"), filepath.Join(flag.Arg(0), "key.pem")) log.Println("webserver closed:", err) } @@ -70,15 +70,13 @@ func setupMongo(uri string) { } func setupBackend(mux *http.ServeMux) { - blogApp = blog.NewBlogApp(back, mongoClient.Database("blog")) - swApp := swassistant.NewSWBackend(back, mongoClient.Database("swassistant")) - cdrApp := cdr.NewBackend(back, mongoClient.Database("cdr")) + blogApp = blog.NewBlogApp(mongoClient.Database("blog")) var err error back, err = backend.NewBackend(db.NewMongoTable[backend.ApiKey]( mongoClient.Database("darkstorm").Collection("keys")), blogApp, - swApp, - cdrApp, + swassistant.NewSWBackend(mongoClient.Database("swassistant")), + cdr.NewBackend(mongoClient.Database("cdr")), ) back.AddCorsAddress("https://darkstorm.tech") if err != nil { @@ -105,12 +103,11 @@ func mainHandle(w http.ResponseWriter, r *http.Request) { if err == nil && !stat.IsDir() { http.ServeFile(w, r, filepath.Join(*webRoot, path)) return - } - spl := strings.Split(path, "/") - if len(spl) > 1 { - stat, err = os.Stat(filepath.Join(*webRoot, spl[0])) - if err == nil && stat.IsDir() { - http.ServeFile(w, r, filepath.Join(*webRoot, spl[0], "index.html")) + } else if stat.IsDir() { + ind := filepath.Join(*webRoot, path, "index.html") + stat, err = os.Stat(ind) + if err == nil { + http.ServeFile(w, r, ind) return } }