diff --git a/go.mod b/go.mod index 63bee28..f2f28f2 100644 --- a/go.mod +++ b/go.mod @@ -4,16 +4,16 @@ go 1.21.5 require ( github.com/1lann/udp-forward v0.0.0-20191015034046-6b774a53ea39 - github.com/CalebQ42/cdr-backend v0.1.0 - github.com/CalebQ42/stupid-backend/v2 v2.0.1 - github.com/CalebQ42/swassistant-backend v0.2.0 - go.mongodb.org/mongo-driver v1.13.0 github.com/CalebQ42/bbConvert v1.0.0 + github.com/CalebQ42/cdr-backend v0.1.0 + github.com/CalebQ42/stupid-backend/v2 v2.0.2 + github.com/CalebQ42/swassistant-backend v0.2.0 + go.mongodb.org/mongo-driver v1.13.1 ) require ( github.com/golang/snappy v0.0.4 // indirect - github.com/google/uuid v1.4.0 // indirect + github.com/google/uuid v1.5.0 // indirect github.com/klauspost/compress v1.17.4 // indirect github.com/lithammer/shortuuid/v3 v3.0.7 // indirect github.com/montanaflynn/stats v0.7.1 // indirect @@ -22,7 +22,7 @@ require ( github.com/xdg-go/scram v1.1.2 // indirect github.com/xdg-go/stringprep v1.0.4 // indirect github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect - golang.org/x/crypto v0.16.0 // indirect + golang.org/x/crypto v0.17.0 // indirect golang.org/x/sync v0.5.0 // indirect golang.org/x/sys v0.15.0 // indirect golang.org/x/text v0.14.0 // indirect diff --git a/go.sum b/go.sum index e296258..5adf25b 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ github.com/CalebQ42/cdr-backend v0.1.0 h1:9245aooAyLxAFO5gfXPgpqOHaYr6NAkMI75v2I github.com/CalebQ42/cdr-backend v0.1.0/go.mod h1:N7A+ia+4GDsDMZ3gb5IRZ6CY07gdFfJECtR9csKh5nI= github.com/CalebQ42/stupid-backend/v2 v2.0.1 h1:BACdi/3BEDE/vZsKvhZl1OAA74QnP/Zd5Hb8uNQCjrg= github.com/CalebQ42/stupid-backend/v2 v2.0.1/go.mod h1:skBYIF77NzxYcqZ34V1eSD2/MIZCAGyyINWIkCBrpx8= +github.com/CalebQ42/stupid-backend/v2 v2.0.2 h1:FB4tbu0opV4kTQfO+56dTHuMPnmgbo8F1joDN1fSU0Q= +github.com/CalebQ42/stupid-backend/v2 v2.0.2/go.mod h1:skBYIF77NzxYcqZ34V1eSD2/MIZCAGyyINWIkCBrpx8= github.com/CalebQ42/swassistant-backend v0.2.0 h1:pXUG7+uHP5/lHaqqD6Hc64hOjfPzCGO8TkOpvtDB238= github.com/CalebQ42/swassistant-backend v0.2.0/go.mod h1:m67UAzh552+puEc0LngTHzOL3b/Y2B19NzbaVdVeabI= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -18,6 +20,8 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4= github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= +github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4= github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM= @@ -40,12 +44,16 @@ github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a/go.mod h1:ul22v+Nro/ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.mongodb.org/mongo-driver v1.13.0 h1:67DgFFjYOCMWdtTEmKFpV3ffWlFnh+CYZ8ZS/tXWUfY= go.mongodb.org/mongo-driver v1.13.0/go.mod h1:/rGBTebI3XYboVmgz+Wv3Bcbl3aD0QF9zl6kDDw18rQ= +go.mongodb.org/mongo-driver v1.13.1 h1:YIc7HTYsKndGK4RFzJ3covLz1byri52x0IoMB0Pt/vk= +go.mongodb.org/mongo-driver v1.13.1/go.mod h1:wcDf1JBCXy2mOW0bWHwO/IOYqdca1MPCwDtFu/Z9+eo= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY= golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= +golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= diff --git a/internal/darkstormtech/app.go b/internal/darkstormtech/app.go index 566e9c6..afa5cf9 100644 --- a/internal/darkstormtech/app.go +++ b/internal/darkstormtech/app.go @@ -2,6 +2,7 @@ package darkstormtech import ( "context" + "encoding/json" "io/fs" "log" "net/http" @@ -34,22 +35,54 @@ func NewDarkstormTech(c *mongo.Client, filesFolder string) *DarkstormTech { } } +func (d *DarkstormTech) AlternateName() string { + return "page" +} + +type pageOut struct { + Content string `json:"content"` + Title string `json:"title"` + Favicon string `json:"favicon"` +} + +func notFoundPage() pageOut { + return pageOut{ + Content: "404 Page Not Found 😥", + Title: "Darkstorm.Tech", + Favicon: "https://darkstorm.tech/favicon.png", + } +} + +func (p pageOut) json() []byte { + out, _ := json.Marshal(p) + return out +} + +func (p *pageOut) addDefaults() { + if p.Title == "" { + p.Title = "Darkstorm.Tech" + } + if p.Favicon == "" { + p.Favicon = "https://darkstorm.tech/favicon.png" + } +} + func (d *DarkstormTech) HandleReqest(req *stupid.Request) bool { - if req.Path[1] != "page" { + if req.Path[0] != "page" { return false } if len(req.Path) == 1 { req.Resp.WriteHeader(http.StatusBadRequest) return true } - if req.Path[2] == "files" { + if req.Path[1] == "files" { return d.handleFiles(req) - } else if req.Path[2] == "portfolio" { + } else if req.Path[1] == "portfolio" { return d.handlePortfolio(req) } - res := d.DB.Collection("pages").FindOne(context.TODO(), bson.M{"_id": strings.Join(req.Path[2:], "/")}, options.FindOne().SetProjection(bson.M{"_id": 0, "content": 1})) + res := d.DB.Collection("pages").FindOne(context.TODO(), bson.M{"_id": strings.Join(req.Path[1:], "/")}, options.FindOne().SetProjection(bson.M{"_id": 0})) if res.Err() == mongo.ErrNoDocuments { - req.Resp.Write([]byte("Page not found 😥")) + req.Resp.Write(notFoundPage().json()) req.Resp.WriteHeader(http.StatusNotFound) //TODO: Give some sort of default page. return true } else if res.Err() != nil { @@ -57,16 +90,16 @@ func (d *DarkstormTech) HandleReqest(req *stupid.Request) bool { req.Resp.WriteHeader(http.StatusInternalServerError) return true } - pag := struct { //TODO: Add favicon and title support. - Content string - }{} + var pag pageOut err := res.Decode(&pag) if err != nil { log.Println("Error while decoding page:", err) req.Resp.WriteHeader(http.StatusInternalServerError) return true } - _, err = req.Resp.Write([]byte(d.bb.Convert(pag.Content))) + pag.Content = d.bb.Convert(pag.Content) + (&pag).addDefaults() + _, err = req.Resp.Write(pag.json()) if err != nil { log.Println("Error while writing response:", err) req.Resp.WriteHeader(http.StatusInternalServerError) @@ -76,8 +109,8 @@ func (d *DarkstormTech) HandleReqest(req *stupid.Request) bool { func (d *DarkstormTech) handleFiles(req *stupid.Request) bool { foldPath := "" - if len(req.Path) > 3 { - foldPath = filepath.Join(req.Path[3:]...) + if len(req.Path) > 1 { + foldPath = filepath.Join(req.Path[2:]...) } fils, err := os.ReadDir(filepath.Join(d.filesFolder, foldPath)) if err != nil {