Compare commits
16 Commits
blog-cleanup
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| eb825b3f85 | |||
| 673e9918d9 | |||
| 2a16aca6ae | |||
| 338eb84469 | |||
| 243fa03af0 | |||
| 3e1078efec | |||
| 24b99d7d1e | |||
| 11a39a53cb | |||
| cd871c7217 | |||
| fc71473177 | |||
| 1694169177 | |||
| b5d582e797 | |||
| 1229be1cee | |||
| aa180ae67e | |||
| 9a492497c8 | |||
| 198c4ff749 |
@@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
@@ -38,8 +37,7 @@ const (
|
||||
{{end}}
|
||||
</select>
|
||||
</p>
|
||||
<div id="editor" hx-on::after-settle="blogEditorResize()">{{.Editor}}</div>
|
||||
`
|
||||
<div id="editor" hx-on::after-settle="blogEditorResize()">{{.Editor}}</div>`
|
||||
editorForm = `
|
||||
<form id="editorForm" hx-post="/editor/post" hx-target="#formResult" hx-confirm="Save changes, overwritting previous values??">
|
||||
<input name="id" type="hidden" value="{{.Blog.ID}}"></input>
|
||||
@@ -52,8 +50,7 @@ const (
|
||||
<input id="titleInput" name="title" value="{{.Blog.Title}}" type="text" onkeydown="return event.key != 'Enter';"/>
|
||||
<textarea id="blogEditor" name="blog" oninput="blogEditorResize()">{{.Blog.RawBlog}}</textarea>
|
||||
<div id="formResult">{{.Result}}</div>
|
||||
<p style="margin-right:0px;">
|
||||
<button class="formButton" type="submit">{{if eq .Blog.ID ""}}Create{{else}}Update{{end}}</button>
|
||||
<p style="margin-right:0px;display:flex;">
|
||||
<button class="formButton"
|
||||
hx-get="/editor/edit"
|
||||
hx-include="#blogSelect"
|
||||
@@ -61,6 +58,16 @@ const (
|
||||
hx-confirm="Undo all your changes??">
|
||||
Cancel
|
||||
</button>
|
||||
<span style="flex-grow:1;"></span>
|
||||
<button class="formButton"
|
||||
hx-delete="/editor/edit"
|
||||
hx-include="#blogSelect"
|
||||
hx-target="#editor"
|
||||
hx-confirm="Delete Page????">
|
||||
Delete
|
||||
</button>
|
||||
<span style="flex-grow:1;"></span>
|
||||
<button class="formButton" type="submit">{{if eq .Blog.ID ""}}Create{{else}}Update{{end}}</button>
|
||||
<p>
|
||||
</form>`
|
||||
)
|
||||
@@ -81,11 +88,12 @@ func trueLoginRequest(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
u, err := back.TryLogin(r.Context(), r.FormValue("username"), r.FormValue("password"))
|
||||
if err != nil {
|
||||
if err == backend.ErrLoginTimeout {
|
||||
sendContent(w, r, fmt.Sprint("<p>Timed out for", time.Unix(u.Timeout, 0).Sub(time.Now()), "</p>"), "", "")
|
||||
} else if err == backend.ErrLoginIncorrect {
|
||||
switch err {
|
||||
case backend.ErrLoginTimeout:
|
||||
sendContent(w, r, fmt.Sprint("<p>Timed out for", time.Until(time.Unix(u.Timeout, 0)), "</p>"), "", "")
|
||||
case backend.ErrLoginIncorrect:
|
||||
sendContent(w, r, "<p>Username or password invalid</p>", "", "")
|
||||
} else {
|
||||
default:
|
||||
log.Println("error trying to login:", err)
|
||||
sendContent(w, r, "<p>Server error</p>", "", "")
|
||||
}
|
||||
@@ -211,7 +219,7 @@ func editorPost(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
if newBlog.ID == "" {
|
||||
newBlog.ID = strings.TrimSpace(strings.ToLower(strings.ReplaceAll(newBlog.ID, " ", "-")))
|
||||
newBlog.ID = newBlog.IDFromTitle()
|
||||
if blogApp.Contains(r.Context(), newBlog.ID) {
|
||||
sendContent(w, r, "<p>Title is not unique!</p>", "", "")
|
||||
return
|
||||
@@ -273,6 +281,43 @@ func editorPost(w http.ResponseWriter, r *http.Request) {
|
||||
pageTmpl.Execute(w, pageTmplStruct{Selected: newBlog.ID, Blogs: blogs, Editor: newForm.String()})
|
||||
}
|
||||
|
||||
func editorDelete(w http.ResponseWriter, r *http.Request) {
|
||||
usr := verifyEditorCookie(r)
|
||||
if usr == nil {
|
||||
editorRedirect(w, r, "/login")
|
||||
return
|
||||
}
|
||||
if usr.Perm["blog"] != "admin" {
|
||||
sendContent(w, r, "<p>You are not allowed to perform this action. Sorry, not sorry.</p>", "", "")
|
||||
return
|
||||
}
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
sendContent(w, r, "<p>Bad request</p>", "", "")
|
||||
return
|
||||
}
|
||||
id := r.FormValue("id")
|
||||
if id == "" {
|
||||
sendContent(w, r, "<p>Invalid ID</p>", "", "")
|
||||
return
|
||||
}
|
||||
err = blogApp.RemoveBlog(r.Context(), id)
|
||||
if err != nil {
|
||||
log.Println("error updating blog:", err)
|
||||
sendContent(w, r, "<p>Server error removing blog</p>", "", "")
|
||||
return
|
||||
}
|
||||
var blogs []blog.BlogListResult
|
||||
blogs, err = blogApp.AllBlogsList(r.Context())
|
||||
if err != nil {
|
||||
log.Println("error getting all blogs list:", err)
|
||||
sendContent(w, r, "<p>Updated!</p>", "", "")
|
||||
return
|
||||
}
|
||||
w.Header().Set("HX-Retarget", "#content")
|
||||
pageTmpl.Execute(w, pageTmplStruct{Selected: "", Blogs: blogs, Editor: "<p>Blog removed!</p>"})
|
||||
}
|
||||
|
||||
func verifyEditorCookie(r *http.Request) *backend.User {
|
||||
authCookie, err := r.Cookie("blogAuthToken")
|
||||
if err != nil {
|
||||
|
||||
@@ -1,27 +1,26 @@
|
||||
module github.com/CalebQ42/darkstorm-server
|
||||
|
||||
go 1.23.0
|
||||
go 1.23.4
|
||||
|
||||
require (
|
||||
github.com/CalebQ42/bbConvert v1.0.2
|
||||
github.com/CalebQ42/bbConvert v1.0.7
|
||||
github.com/golang-jwt/jwt/v5 v5.2.1
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/lithammer/shortuuid/v3 v3.0.7
|
||||
go.mongodb.org/mongo-driver v1.15.1
|
||||
golang.org/x/crypto v0.24.0
|
||||
go.mongodb.org/mongo-driver v1.17.1
|
||||
golang.org/x/crypto v0.31.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/dlclark/regexp2 v1.11.5-0.20240806004527-5bbbed8ea10b // indirect
|
||||
github.com/golang/snappy v0.0.1 // indirect
|
||||
github.com/google/go-cmp v0.6.0 // indirect
|
||||
github.com/klauspost/compress v1.13.6 // indirect
|
||||
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
|
||||
github.com/golang/snappy v0.0.4 // indirect
|
||||
github.com/klauspost/compress v1.17.11 // indirect
|
||||
github.com/montanaflynn/stats v0.7.1 // indirect
|
||||
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
|
||||
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-20181117223130-1be2e3e5546d // indirect
|
||||
golang.org/x/sync v0.7.0 // indirect
|
||||
golang.org/x/sys v0.21.0 // indirect
|
||||
golang.org/x/text v0.16.0 // indirect
|
||||
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
|
||||
golang.org/x/sync v0.10.0 // indirect
|
||||
golang.org/x/sys v0.28.0 // indirect
|
||||
golang.org/x/text v0.21.0 // indirect
|
||||
)
|
||||
|
||||
@@ -1,62 +1,62 @@
|
||||
github.com/CalebQ42/bbConvert v1.0.2 h1:N0+q7Kw3Ge2m117jbYp1DyrCa7zUuBOpyBfhcuphjz8=
|
||||
github.com/CalebQ42/bbConvert v1.0.2/go.mod h1:gV0gaDhzuIwWqX9O1F8qu7tFXz50DnGQOnq56qrtO3A=
|
||||
github.com/CalebQ42/bbConvert v1.0.7 h1:dJh6S7lliotdQvcXrMbtBo4p8Afwe295/XvnKP0oj7E=
|
||||
github.com/CalebQ42/bbConvert v1.0.7/go.mod h1:UBFqtgZWSm9v/2Kl4NJDmgdSzEBZDkgON3QKoVOBC6Q=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dlclark/regexp2 v1.11.5-0.20240806004527-5bbbed8ea10b h1:AJKOdc+1fRSJ0/75Jty1npvxUUD0y7hQDg15LMAHhyU=
|
||||
github.com/dlclark/regexp2 v1.11.5-0.20240806004527-5bbbed8ea10b/go.mod h1:YvCrhrh/qlds8EhFKPtJprdXn5fWBllSw1qo99dZyiQ=
|
||||
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
|
||||
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
|
||||
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
|
||||
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
|
||||
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
|
||||
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
|
||||
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
|
||||
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
|
||||
github.com/lithammer/shortuuid/v3 v3.0.7 h1:trX0KTHy4Pbwo/6ia8fscyHoGA+mf1jWbPJVuvyJQQ8=
|
||||
github.com/lithammer/shortuuid/v3 v3.0.7/go.mod h1:vMk8ke37EmiewwolSO1NLW8vP4ZaKlRuDIi8tWWmAts=
|
||||
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0=
|
||||
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
|
||||
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
|
||||
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
|
||||
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
|
||||
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
|
||||
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
|
||||
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
|
||||
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
|
||||
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
|
||||
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA=
|
||||
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
|
||||
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM=
|
||||
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
go.mongodb.org/mongo-driver v1.15.1 h1:l+RvoUOoMXFmADTLfYDm7On9dRm7p4T80/lEQM+r7HU=
|
||||
go.mongodb.org/mongo-driver v1.15.1/go.mod h1:Vzb0Mk/pa7e6cWw85R4F/endUC3u0U9jGcNU603k65c=
|
||||
go.mongodb.org/mongo-driver v1.17.1 h1:Wic5cJIwJgSpBhe3lx3+/RybR5PiYRMpVFgO7cOHyIM=
|
||||
go.mongodb.org/mongo-driver v1.17.1/go.mod h1:wwWm/+BuOddhcq3n68LKRmgk2wXzmF6s0SFOa0GINL4=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
|
||||
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
|
||||
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
|
||||
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
|
||||
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
|
||||
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
|
||||
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
|
||||
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
|
||||
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
|
||||
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
|
||||
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
|
||||
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
|
||||
@@ -49,6 +49,7 @@ func (b *Backend) countLog(w http.ResponseWriter, r *http.Request) {
|
||||
ap := b.GetApp(hdr.Key)
|
||||
count := ap.CountTable()
|
||||
if count == nil {
|
||||
log.Println(ap.AppID(), "misconfigured: count table is nil.")
|
||||
ReturnError(w, http.StatusInternalServerError, "misconfigured", "Server Misconfigured")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -112,6 +112,7 @@ func (b *Backend) managementDeleteCrash(w http.ResponseWriter, r *http.Request)
|
||||
func (b *Backend) actualCrashDelete(ctx context.Context, w http.ResponseWriter, ap App, crashID string) {
|
||||
crash := ap.CrashTable()
|
||||
if crash == nil {
|
||||
log.Println(ap.AppID(), "misconfigured: crash table is nil.")
|
||||
ReturnError(w, http.StatusInternalServerError, "misconfigured", "Server Misconfigured")
|
||||
return
|
||||
}
|
||||
@@ -166,6 +167,7 @@ func (b *Backend) managementArchiveCrash(w http.ResponseWriter, r *http.Request)
|
||||
func (b *Backend) actualCrashArchive(ctx context.Context, w http.ResponseWriter, ap App, toArchive ArchivedCrash) {
|
||||
crash := ap.CrashTable()
|
||||
if crash == nil {
|
||||
log.Println(ap.AppID(), "misconfigured: crash table is nil.")
|
||||
ReturnError(w, http.StatusInternalServerError, "misconfigured", "Server Misconfigured")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ var robotEmbed embed.FS
|
||||
// A simple backend that handles user authentication, user count, and crash reports.
|
||||
type Backend struct {
|
||||
userTable Table[User]
|
||||
keyTable Table[ApiKey]
|
||||
keyTable Table[APIKey]
|
||||
m *http.ServeMux
|
||||
apps map[string]App
|
||||
managementKeyID string
|
||||
@@ -29,7 +29,7 @@ type Backend struct {
|
||||
}
|
||||
|
||||
// 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{
|
||||
keyTable: keyTable,
|
||||
m: &http.ServeMux{},
|
||||
@@ -75,19 +75,24 @@ func NewBackend(keyTable Table[ApiKey], apps ...App) (*Backend, error) {
|
||||
}
|
||||
|
||||
func (b *Backend) cleanupLoop() {
|
||||
b.cleanup()
|
||||
for range time.Tick(24 * time.Hour) {
|
||||
old := getDate(time.Now().Add(-30 * 24 * time.Hour))
|
||||
var err error
|
||||
for _, a := range b.apps {
|
||||
log.Printf("Removing logs for %v", a.AppID())
|
||||
tab := a.CountTable()
|
||||
if tab == nil {
|
||||
continue
|
||||
}
|
||||
err = tab.RemoveOldLogs(context.Background(), old)
|
||||
if err != nil {
|
||||
log.Printf("error removing old logs for %v: %v\n", a.AppID(), err)
|
||||
}
|
||||
b.cleanup()
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Backend) cleanup() {
|
||||
old := getDate(time.Now().Add(-30 * 24 * time.Hour))
|
||||
var err error
|
||||
for _, a := range b.apps {
|
||||
log.Printf("Removing logs for %v", a.AppID())
|
||||
tab := a.CountTable()
|
||||
if tab == nil {
|
||||
continue
|
||||
}
|
||||
err = tab.RemoveOldLogs(context.Background(), old)
|
||||
if err != nil {
|
||||
log.Printf("error removing old logs for %v: %v\n", a.AppID(), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -138,7 +143,7 @@ func (b *Backend) HandleFunc(pattern string, h http.HandlerFunc) {
|
||||
}
|
||||
|
||||
// Try to get the App associated with the given ApiKey. Returns nil if not found.
|
||||
func (b *Backend) GetApp(a *ApiKey) App {
|
||||
func (b *Backend) GetApp(a *APIKey) App {
|
||||
return b.apps[a.AppID]
|
||||
}
|
||||
|
||||
|
||||
@@ -10,11 +10,11 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
ErrApiKeyUnauthorized = errors.New("api key present but invalid")
|
||||
ErrAPIKeyUnauthorized = errors.New("api key present but invalid")
|
||||
ErrTokenUnauthorized = errors.New("token present but invalid")
|
||||
)
|
||||
|
||||
type ApiKey struct {
|
||||
type APIKey struct {
|
||||
Perm map[string]bool `json:"perm" bson:"perm"`
|
||||
ID string `json:"id" bson:"_id" valkey:",key"`
|
||||
AppID string `json:"appID" bson:"appID"`
|
||||
@@ -22,13 +22,13 @@ type ApiKey struct {
|
||||
AllowedOrigins []string `json:"allowedOrigins" bson:"allowedOrigins"`
|
||||
}
|
||||
|
||||
func (k ApiKey) GetID() string {
|
||||
func (k APIKey) GetID() string {
|
||||
return k.ID
|
||||
}
|
||||
|
||||
type ParsedHeader struct {
|
||||
User *ReqestUser
|
||||
Key *ApiKey
|
||||
Key *APIKey
|
||||
}
|
||||
|
||||
// Parses the X-API-Key and Authorization headers. If the API Key provided but invalid (either due to expiring or isn't found), ErrApiKeyUnauthorized is returned.
|
||||
@@ -41,24 +41,24 @@ func (b *Backend) ParseHeader(r *http.Request) (*ParsedHeader, error) {
|
||||
if key != "" {
|
||||
apiKey, err := b.keyTable.Get(r.Context(), key)
|
||||
if err == ErrNotFound {
|
||||
return nil, ErrApiKeyUnauthorized
|
||||
return nil, ErrAPIKeyUnauthorized
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if apiKey.Death > 0 && time.Unix(apiKey.Death, 0).Before(time.Now()) {
|
||||
return nil, ErrApiKeyUnauthorized
|
||||
return nil, ErrAPIKeyUnauthorized
|
||||
}
|
||||
out.Key = &apiKey
|
||||
} else {
|
||||
fmt.Println("origin:", r.Header.Get("origin"))
|
||||
keys, err := b.keyTable.Find(r.Context(), map[string]any{"allowedOrigins": r.Header.Get("origin")})
|
||||
if err == ErrNotFound {
|
||||
return nil, ErrApiKeyUnauthorized
|
||||
return nil, ErrAPIKeyUnauthorized
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if keys[0].Death > 0 && time.Unix(keys[0].Death, 0).Before(time.Now()) {
|
||||
return nil, ErrApiKeyUnauthorized
|
||||
return nil, ErrAPIKeyUnauthorized
|
||||
}
|
||||
out.Key = &keys[0]
|
||||
}
|
||||
@@ -87,7 +87,7 @@ func (b *Backend) ParseHeader(r *http.Request) (*ParsedHeader, error) {
|
||||
func (b *Backend) VerifyHeader(w http.ResponseWriter, r *http.Request, keyPerm string, allowManagementKey bool) (*ParsedHeader, error) {
|
||||
hdr, err := b.ParseHeader(r)
|
||||
if hdr == nil || hdr.Key == nil {
|
||||
if err == ErrApiKeyUnauthorized {
|
||||
if err == ErrAPIKeyUnauthorized {
|
||||
ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized")
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -301,13 +301,14 @@ func (b *Backend) login(w http.ResponseWriter, r *http.Request) {
|
||||
if err == nil {
|
||||
ret.Token, err = b.GenerateJWT(u.ToReqUser())
|
||||
if err != nil {
|
||||
log.Println("error generating JWT token:", err)
|
||||
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if err == ErrLoginTimeout {
|
||||
ret.Error = "timeout"
|
||||
ret.ErrorMsg = fmt.Sprint("Timed out for", time.Unix(u.Timeout, 0).Sub(time.Now()), "seconds")
|
||||
ret.ErrorMsg = fmt.Sprint("Timed out for", time.Until(time.Unix(u.Timeout, 0)), "seconds")
|
||||
ret.Timeout = u.Timeout
|
||||
} else {
|
||||
ret.Error = "incorrect"
|
||||
|
||||
@@ -15,10 +15,11 @@ import (
|
||||
)
|
||||
|
||||
const authorInfo = `
|
||||
<table><tr>
|
||||
<td><img src="%v" alt="%v" class='author-pic'></td>
|
||||
<td><h3 class="author-title">%v</h3>%v</td>
|
||||
</tr></table>`
|
||||
<div>
|
||||
<img src="%v" alt="%v" class='author-pic'>
|
||||
<h3 class="author-title">%v</h3>
|
||||
<p>%v<p>
|
||||
</div>`
|
||||
|
||||
type Author struct {
|
||||
ID string `json:"id" bson:"_id"`
|
||||
@@ -32,7 +33,7 @@ func (a Author) HTML() string {
|
||||
}
|
||||
|
||||
func (b *BlogApp) AboutMe(ctx context.Context) (*Author, error) {
|
||||
res := b.authCol.FindOne(ctx, bson.M{"_id": "caleb_gardner"})
|
||||
res := b.authCol.FindOne(ctx, bson.M{"_id": "BelacDarkstorm"})
|
||||
if res.Err() != nil {
|
||||
log.Println("error getting about me:", res.Err())
|
||||
if res.Err() == mongo.ErrNoDocuments {
|
||||
@@ -150,14 +151,15 @@ func (b *BlogApp) updateAuthorInfo(w http.ResponseWriter, r *http.Request) {
|
||||
res, err := b.authCol.UpdateByID(r.Context(), r.PathValue("authorID"), actlUpd)
|
||||
if err != nil {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
backend.ReturnError(w, http.StatusNotFound, "notFound", "Blog with ID "+r.PathValue("blogID")+" not found")
|
||||
backend.ReturnError(w, http.StatusNotFound, "notFound", "Author with ID "+r.PathValue("authorID")+" not found")
|
||||
} else {
|
||||
log.Println("error updating author", r.PathValue("authorID")+":", err)
|
||||
backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server Error")
|
||||
}
|
||||
return
|
||||
}
|
||||
if res.MatchedCount == 0 {
|
||||
backend.ReturnError(w, http.StatusNotFound, "notFound", "Blog with ID "+r.PathValue("blogID")+" not found")
|
||||
backend.ReturnError(w, http.StatusNotFound, "notFound", "Author with ID "+r.PathValue("authorID")+" not found")
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
|
||||
+17
-4
@@ -6,7 +6,9 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/CalebQ42/darkstorm-server/internal/backend"
|
||||
@@ -20,6 +22,7 @@ const (
|
||||
blogTitle = "<h1 class='blog-title'><a hx-push-url='true' hx-get='/%v' hx-target='#content' href='/%v' style='text-decoration:none'>%v</a></h1>"
|
||||
blogAuthor = "<h4 class='blog-author'><i><b>By %v</b></i></h4>"
|
||||
blogCreate = "<h5 class='blog-time'><i>Written on: %v</i></h5>"
|
||||
blogUpdate = "<h5 class='blog-time'><i>Updated on: %v</i></h5>"
|
||||
blogMain = "<div class='blog'>%v</div>"
|
||||
)
|
||||
|
||||
@@ -47,11 +50,9 @@ func (b *Blog) HTMX(blogApp *BlogApp, ctx context.Context) string {
|
||||
} else {
|
||||
out += fmt.Sprintf(blogAuthor, "unknown")
|
||||
}
|
||||
cTime := time.Unix(b.CreateTime, 0).Format(time.DateOnly)
|
||||
out += fmt.Sprintf(blogCreate, time.Unix(b.CreateTime, 0).Format(time.DateOnly))
|
||||
if b.UpdateTime > b.CreateTime {
|
||||
out += fmt.Sprintf(blogCreate, cTime+"; Last updated on: "+time.Unix(b.UpdateTime, 0).Format(time.DateOnly))
|
||||
} else {
|
||||
out += fmt.Sprintf(blogCreate, cTime)
|
||||
out += fmt.Sprintf(blogUpdate, time.Unix(b.UpdateTime, 0).Format(time.DateOnly))
|
||||
}
|
||||
out += fmt.Sprintf(blogMain, b.HTMLBlog)
|
||||
if err == nil {
|
||||
@@ -60,6 +61,11 @@ func (b *Blog) HTMX(blogApp *BlogApp, ctx context.Context) string {
|
||||
return out
|
||||
}
|
||||
|
||||
func (b Blog) IDFromTitle() string {
|
||||
id := strings.Join(regexp.MustCompile("([A-z]| |[0-9])*").FindAllString(b.Title, -1), "")
|
||||
return strings.ToLower(strings.ReplaceAll(id, " ", "-"))
|
||||
}
|
||||
|
||||
func (b *BlogApp) ConvertBlog(blog *Blog) {
|
||||
if !blog.StaticPage {
|
||||
blog.HTMLBlog = b.conv.HTMLConvert(blog.RawBlog)
|
||||
@@ -181,6 +187,7 @@ func (b *BlogApp) createBlog(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
id, err := uuid.NewV7()
|
||||
if err != nil {
|
||||
log.Println("error generating UUID:", err)
|
||||
backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server Error")
|
||||
return
|
||||
}
|
||||
@@ -241,6 +248,7 @@ func (b *BlogApp) updateBlog(w http.ResponseWriter, r *http.Request) {
|
||||
if err == mongo.ErrNoDocuments {
|
||||
backend.ReturnError(w, http.StatusNotFound, "notFound", "Blog with ID "+r.PathValue("blogID")+" not found")
|
||||
} else {
|
||||
log.Println("error updating blog", r.PathValue("blogID")+":", err)
|
||||
backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server Error")
|
||||
}
|
||||
return
|
||||
@@ -262,6 +270,11 @@ func (b *BlogApp) UpdateBlog(ctx context.Context, ID string, updates bson.M) err
|
||||
return err
|
||||
}
|
||||
|
||||
func (b *BlogApp) RemoveBlog(ctx context.Context, ID string) error {
|
||||
_, err := b.blogCol.DeleteOne(ctx, bson.M{"_id": ID})
|
||||
return err
|
||||
}
|
||||
|
||||
func (b *BlogApp) LatestBlogs(ctx context.Context, page int64) ([]*Blog, error) {
|
||||
res, err := b.blogCol.Find(ctx, bson.M{"staticPage": false, "draft": false}, options.Find().
|
||||
SetSort(bson.M{"createTime": -1}).
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
@@ -120,6 +121,7 @@ func (b *BlogApp) Projects(ctx context.Context, techFilter string) (Portfolio, e
|
||||
func (b *BlogApp) reqPortfolio(w http.ResponseWriter, r *http.Request) {
|
||||
folio, err := b.Projects(r.Context(), r.URL.Query().Get("tech"))
|
||||
if err != nil {
|
||||
log.Println("error getting projects with filter", r.URL.Query().Get("tech")+":", err)
|
||||
backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server Error")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"flag"
|
||||
"io"
|
||||
"log"
|
||||
@@ -13,6 +12,7 @@ import (
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/CalebQ42/darkstorm-server/internal/backend"
|
||||
"github.com/CalebQ42/darkstorm-server/internal/backend/db"
|
||||
@@ -71,13 +71,8 @@ func main() {
|
||||
|
||||
func setupMongo(uri string) {
|
||||
if !*testing {
|
||||
mongoCert, err := tls.LoadX509KeyPair(filepath.Join(flag.Arg(0), "mongo.pem"), filepath.Join(flag.Arg(0), "key.pem"))
|
||||
if err != nil {
|
||||
log.Fatal("error loading mongo keys:", err)
|
||||
}
|
||||
mongoClient, err = mongo.Connect(context.Background(), options.Client().ApplyURI(uri).SetTLSConfig(&tls.Config{
|
||||
Certificates: []tls.Certificate{mongoCert},
|
||||
}))
|
||||
var err error
|
||||
mongoClient, err = mongo.Connect(context.Background(), options.Client().ApplyURI(uri).SetTimeout(5*time.Second))
|
||||
if err != nil {
|
||||
log.Fatal("error connecting to mongo:", err)
|
||||
}
|
||||
@@ -93,7 +88,7 @@ func setupMongo(uri string) {
|
||||
func setupBackend(mux *http.ServeMux) {
|
||||
blogApp = blog.NewBlogApp(mongoClient.Database("blog"))
|
||||
var err error
|
||||
back, err = backend.NewBackend(db.NewMongoTable[backend.ApiKey](
|
||||
back, err = backend.NewBackend(db.NewMongoTable[backend.APIKey](
|
||||
mongoClient.Database("darkstorm").Collection("keys")),
|
||||
blogApp,
|
||||
swassistant.NewSWBackend(mongoClient.Database("swassistant")),
|
||||
@@ -144,8 +139,11 @@ here:
|
||||
|
||||
func setupWebsite(mux *http.ServeMux) {
|
||||
if !*testing {
|
||||
url, _ := url.Parse("https://localhost:30000")
|
||||
mux.Handle("rpg.darkstorm.tech/", httputil.NewSingleHostReverseProxy(url))
|
||||
rpgUrl, _ := url.Parse("https://localhost:30000")
|
||||
mux.Handle("rpg.darkstorm.tech/", httputil.NewSingleHostReverseProxy(rpgUrl))
|
||||
|
||||
gitUrl, _ := url.Parse("https://darkstorm.tech:3000")
|
||||
mux.Handle("git.darkstorm.tech/", httputil.NewSingleHostReverseProxy(gitUrl))
|
||||
}
|
||||
mux.HandleFunc("/", mainHandle)
|
||||
mux.HandleFunc("GET /files/{w...}", filesRequest)
|
||||
@@ -160,6 +158,7 @@ func setupWebsite(mux *http.ServeMux) {
|
||||
// Editor stuff
|
||||
mux.HandleFunc("GET /login", loginPageRequest)
|
||||
mux.HandleFunc("GET /editor/", editorRequest)
|
||||
mux.HandleFunc("DELETE /editor/edit", editorDelete)
|
||||
mux.HandleFunc("GET /editor/edit", editorEdit)
|
||||
mux.HandleFunc("POST /editor/post", editorPost)
|
||||
mux.HandleFunc("POST /login", trueLoginRequest)
|
||||
|
||||
@@ -6,6 +6,12 @@ import (
|
||||
)
|
||||
|
||||
func portfolioRequest(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "https://darkstorm.tech")
|
||||
if r.Method == http.MethodOptions {
|
||||
w.Header().Set("Access-Control-Allow-Methods", "*")
|
||||
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||
w.Header().Set("Access-Control-Allow-Headers", "*, Authorization")
|
||||
}
|
||||
selectedTech := r.URL.Query().Get("tech")
|
||||
proj, err := blogApp.Projects(r.Context(), selectedTech)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user