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.
This commit is contained in:
@@ -9,6 +9,12 @@ type App interface {
|
|||||||
CrashTable() CrashTable
|
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.
|
// 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 {
|
type CrashFilterApp interface {
|
||||||
App
|
App
|
||||||
@@ -17,6 +23,7 @@ type CrashFilterApp interface {
|
|||||||
|
|
||||||
// Allows an app more flexibility by directly interfacing with the backend's mux
|
// Allows an app more flexibility by directly interfacing with the backend's mux
|
||||||
type ExtendedApp interface {
|
type ExtendedApp interface {
|
||||||
|
App
|
||||||
Extension(*http.ServeMux)
|
Extension(*http.ServeMux)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -46,6 +46,9 @@ func NewBackend(keyTable Table[ApiKey], apps ...App) (*Backend, error) {
|
|||||||
if ext, is := apps[i].(ExtendedApp); is {
|
if ext, is := apps[i].(ExtendedApp); is {
|
||||||
ext.Extension(b.m)
|
ext.Extension(b.m)
|
||||||
}
|
}
|
||||||
|
if back, is := apps[i].(CallbackApp); is {
|
||||||
|
back.AddBackend(b)
|
||||||
|
}
|
||||||
if !hasLog && apps[i].CountTable() != nil {
|
if !hasLog && apps[i].CountTable() != nil {
|
||||||
hasLog = true
|
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("DELETE /crash/{crashID}", b.deleteCrash)
|
||||||
b.m.HandleFunc("POST /crash/archive", b.archiveCrash)
|
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()
|
go b.cleanupLoop()
|
||||||
return b, nil
|
return b, nil
|
||||||
}
|
}
|
||||||
@@ -97,9 +101,9 @@ func (b *Backend) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
if b.corsAddr != "" {
|
if b.corsAddr != "" {
|
||||||
w.Header().Set("Access-Control-Allow-Origin", b.corsAddr)
|
w.Header().Set("Access-Control-Allow-Origin", b.corsAddr)
|
||||||
if r.Method == http.MethodOptions {
|
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-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)
|
b.m.ServeHTTP(w, r)
|
||||||
|
|||||||
@@ -16,9 +16,8 @@ type BlogApp struct {
|
|||||||
conv *bbConvert.HTMLConverter
|
conv *bbConvert.HTMLConverter
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBlogApp(b *backend.Backend, db *mongo.Database) *BlogApp {
|
func NewBlogApp(db *mongo.Database) *BlogApp {
|
||||||
out := &BlogApp{
|
out := &BlogApp{
|
||||||
back: b,
|
|
||||||
blogCol: db.Collection("blog"),
|
blogCol: db.Collection("blog"),
|
||||||
authCol: db.Collection("author"),
|
authCol: db.Collection("author"),
|
||||||
portfolioCol: db.Collection("portfolio"),
|
portfolioCol: db.Collection("portfolio"),
|
||||||
@@ -40,6 +39,10 @@ func (b *BlogApp) CrashTable() backend.CrashTable {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *BlogApp) AddBackend(back *backend.Backend) {
|
||||||
|
b.back = back
|
||||||
|
}
|
||||||
|
|
||||||
func (b *BlogApp) Extension(mux *http.ServeMux) {
|
func (b *BlogApp) Extension(mux *http.ServeMux) {
|
||||||
mux.HandleFunc("GET /blog", b.reqLatestBlogs)
|
mux.HandleFunc("GET /blog", b.reqLatestBlogs)
|
||||||
mux.HandleFunc("GET /blog/list", b.reqBlogList)
|
mux.HandleFunc("GET /blog/list", b.reqBlogList)
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ type CDRBackend struct {
|
|||||||
db *mongo.Database
|
db *mongo.Database
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBackend(back *backend.Backend, db *mongo.Database) *CDRBackend {
|
func NewBackend(db *mongo.Database) *CDRBackend {
|
||||||
go func() {
|
go func() {
|
||||||
for range time.Tick(time.Hour) {
|
for range time.Tick(time.Hour) {
|
||||||
log.Println("CDR: Deleting expired dice")
|
log.Println("CDR: Deleting expired dice")
|
||||||
@@ -29,7 +29,6 @@ func NewBackend(back *backend.Backend, db *mongo.Database) *CDRBackend {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
return &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"))
|
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 {
|
func (s CDRBackend) AddCrash(cr backend.IndividualCrash) bool {
|
||||||
res := s.db.Collection("versions").FindOne(context.Background(), bson.M{"version": cr.Version})
|
res := s.db.Collection("versions").FindOne(context.Background(), bson.M{"version": cr.Version})
|
||||||
return res.Err() != mongo.ErrNoDocuments
|
return res.Err() != mongo.ErrNoDocuments
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ type SWBackend struct {
|
|||||||
db *mongo.Database
|
db *mongo.Database
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSWBackend(back *backend.Backend, db *mongo.Database) *SWBackend {
|
func NewSWBackend(db *mongo.Database) *SWBackend {
|
||||||
go func() {
|
go func() {
|
||||||
for range time.Tick(time.Hour) {
|
for range time.Tick(time.Hour) {
|
||||||
log.Println("SWAssistant: Deleting expired profiles")
|
log.Println("SWAssistant: Deleting expired profiles")
|
||||||
@@ -29,7 +29,6 @@ func NewSWBackend(back *backend.Backend, db *mongo.Database) *SWBackend {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
return &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"))
|
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 {
|
func (s *SWBackend) AddCrash(cr backend.IndividualCrash) bool {
|
||||||
res := s.db.Collection("versions").FindOne(context.Background(), bson.M{"version": cr.Version})
|
res := s.db.Collection("versions").FindOne(context.Background(), bson.M{"version": cr.Version})
|
||||||
return res.Err() != mongo.ErrNoDocuments
|
return res.Err() != mongo.ErrNoDocuments
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ func main() {
|
|||||||
Addr: *addr,
|
Addr: *addr,
|
||||||
Handler: mux,
|
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)
|
log.Println("webserver closed:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,15 +70,13 @@ func setupMongo(uri string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func setupBackend(mux *http.ServeMux) {
|
func setupBackend(mux *http.ServeMux) {
|
||||||
blogApp = blog.NewBlogApp(back, mongoClient.Database("blog"))
|
blogApp = blog.NewBlogApp(mongoClient.Database("blog"))
|
||||||
swApp := swassistant.NewSWBackend(back, mongoClient.Database("swassistant"))
|
|
||||||
cdrApp := cdr.NewBackend(back, mongoClient.Database("cdr"))
|
|
||||||
var err error
|
var err error
|
||||||
back, err = backend.NewBackend(db.NewMongoTable[backend.ApiKey](
|
back, err = backend.NewBackend(db.NewMongoTable[backend.ApiKey](
|
||||||
mongoClient.Database("darkstorm").Collection("keys")),
|
mongoClient.Database("darkstorm").Collection("keys")),
|
||||||
blogApp,
|
blogApp,
|
||||||
swApp,
|
swassistant.NewSWBackend(mongoClient.Database("swassistant")),
|
||||||
cdrApp,
|
cdr.NewBackend(mongoClient.Database("cdr")),
|
||||||
)
|
)
|
||||||
back.AddCorsAddress("https://darkstorm.tech")
|
back.AddCorsAddress("https://darkstorm.tech")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -105,12 +103,11 @@ func mainHandle(w http.ResponseWriter, r *http.Request) {
|
|||||||
if err == nil && !stat.IsDir() {
|
if err == nil && !stat.IsDir() {
|
||||||
http.ServeFile(w, r, filepath.Join(*webRoot, path))
|
http.ServeFile(w, r, filepath.Join(*webRoot, path))
|
||||||
return
|
return
|
||||||
}
|
} else if stat.IsDir() {
|
||||||
spl := strings.Split(path, "/")
|
ind := filepath.Join(*webRoot, path, "index.html")
|
||||||
if len(spl) > 1 {
|
stat, err = os.Stat(ind)
|
||||||
stat, err = os.Stat(filepath.Join(*webRoot, spl[0]))
|
if err == nil {
|
||||||
if err == nil && stat.IsDir() {
|
http.ServeFile(w, r, ind)
|
||||||
http.ServeFile(w, r, filepath.Join(*webRoot, spl[0], "index.html"))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user