More work on stuff

This commit is contained in:
Caleb Gardner
2024-06-28 07:04:10 -05:00
parent 5db8692ec8
commit 5af5de7719
8 changed files with 34 additions and 24 deletions
+2 -3
View File
@@ -9,11 +9,10 @@ require (
golang.org/x/crypto v0.24.0 golang.org/x/crypto v0.24.0
) )
require github.com/google/go-cmp v0.6.0 // indirect
require ( require (
github.com/CalebQ42/bbConvert v1.0.0 // indirect
github.com/golang/snappy v0.0.1 // indirect github.com/golang/snappy v0.0.1 // indirect
github.com/joho/godotenv v1.5.1 // indirect github.com/google/go-cmp v0.6.0 // indirect
github.com/klauspost/compress v1.13.6 // indirect github.com/klauspost/compress v1.13.6 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect github.com/xdg-go/pbkdf2 v1.0.0 // indirect
+2 -2
View File
@@ -1,3 +1,5 @@
github.com/CalebQ42/bbConvert v1.0.0 h1:2WSAxVKhCCMReuU30r3ehLtL6m9aH8sY3wugp9yUdzg=
github.com/CalebQ42/bbConvert v1.0.0/go.mod h1:QJevnlhzUdL3EJB5Lgqoi7rdgtzt/UEamn/QGNoVgkM=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 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/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk= github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
@@ -8,8 +10,6 @@ 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/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= 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/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc= 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.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0= github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0=
+12 -1
View File
@@ -14,13 +14,24 @@ var (
ErrTokenUnauthorized = errors.New("token present but invalid") ErrTokenUnauthorized = errors.New("token present but invalid")
) )
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"`
Death int64 `json:"death" bson:"death"`
}
func (k ApiKey) GetID() string {
return k.ID
}
type ParsedHeader struct { type ParsedHeader struct {
User *ReqestUser 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. // 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.
// If the Authorization header is present but invalid, ErrTokenUnauthorized is part of the returned error (check with errors.Is). // If the Authorization header is present but invalid, ErrTokenUnauthorized is returned.
// NOTE: An invalid apiKey will cause a nil return, but a invalid token will not. Token parsing is only // NOTE: An invalid apiKey will cause a nil return, but a invalid token will not. Token parsing is only
func (b *Backend) ParseHeader(r *http.Request) (*ParsedHeader, error) { func (b *Backend) ParseHeader(r *http.Request) (*ParsedHeader, error) {
out := &ParsedHeader{} out := &ParsedHeader{}
-12
View File
@@ -1,12 +0,0 @@
package backend
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"`
Death int64 `json:"death" bson:"death"`
}
func (k ApiKey) GetID() string {
return k.ID
}
-1
View File
@@ -75,7 +75,6 @@ Must have a auth token for a user with the `"blog": "admin"` permission.
```json ```json
{ {
author: "authorID",
favicon: "favicon url", favicon: "favicon url",
title: "blog title", title: "blog title",
blog: "blog", // blog will have been converted to HTML blog: "blog", // blog will have been converted to HTML
+1
View File
@@ -12,6 +12,7 @@ import (
type Author struct { type Author struct {
ID string `json:"id" bson:"_id"` ID string `json:"id" bson:"_id"`
Name string `json:"name" bson:"name"`
About string `json:"about" bson:"about"` About string `json:"about" bson:"about"`
PicURL string `json:"picurl" bson:"picurl"` PicURL string `json:"picurl" bson:"picurl"`
} }
+13 -5
View File
@@ -23,9 +23,9 @@ type Blog struct {
UpdateTime int `json:"updateTime" bson:"updateTime"` UpdateTime int `json:"updateTime" bson:"updateTime"`
} }
func (b *Blog) ConvertBlog() { func (b *BlogApp) ConvertBlog(blog *Blog) {
//TODO: parse BBCode/Markdown from blog //TODO: parse BBCode/Markdown from blog
//b.Blog = bbCodeConvert(b.Blog) blog.Blog = b.conv.Convert(blog.Blog)
} }
func (b *BlogApp) GetAuthor(blog *Blog) (*Author, error) { func (b *BlogApp) GetAuthor(blog *Blog) (*Author, error) {
@@ -54,7 +54,7 @@ func (b *BlogApp) GetBlog(ID string) (*Blog, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
blog.ConvertBlog() b.ConvertBlog(&blog)
return &blog, nil return &blog, nil
} }
@@ -78,7 +78,15 @@ func (b *BlogApp) Blog(w http.ResponseWriter, r *http.Request) {
} }
func (b *BlogApp) CreateBlog(w http.ResponseWriter, r *http.Request) { func (b *BlogApp) CreateBlog(w http.ResponseWriter, r *http.Request) {
//TODO hdr, err := b.back.ParseHeader(r)
if err != nil{
if err == backend.ErrApiKeyUnauthorized{
backend.ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application unauthorized")
return
}else if err == backend.ErrTokenUnauthorized{
backend.ReturnError(w, http.StatusUnauthorized, "")
}
}
} }
func (b *BlogApp) UpdateBlog(w http.ResponseWriter, r *http.Request) { func (b *BlogApp) UpdateBlog(w http.ResponseWriter, r *http.Request) {
@@ -102,7 +110,7 @@ func (b *BlogApp) GetLatestBlogs(page int64) ([]Blog, error) {
return nil, err return nil, err
} }
for i := range out { for i := range out {
out[i].ConvertBlog() b.ConvertBlog(&out[i])
} }
return out, nil return out, nil
} }
+4
View File
@@ -3,6 +3,7 @@ package blog
import ( import (
"net/http" "net/http"
"github.com/CalebQ42/bbConvert"
"github.com/CalebQ42/darkstorm-server/internal/backend" "github.com/CalebQ42/darkstorm-server/internal/backend"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
) )
@@ -11,6 +12,7 @@ type BlogApp struct {
back *backend.Backend back *backend.Backend
blogCol *mongo.Collection blogCol *mongo.Collection
authCol *mongo.Collection authCol *mongo.Collection
conv *bbConvert.HTMLConverter
} }
func NewBlogApp(b *backend.Backend, db *mongo.Database, mux *http.ServeMux) *BlogApp { func NewBlogApp(b *backend.Backend, db *mongo.Database, mux *http.ServeMux) *BlogApp {
@@ -18,7 +20,9 @@ func NewBlogApp(b *backend.Backend, db *mongo.Database, mux *http.ServeMux) *Blo
back: b, back: b,
blogCol: db.Collection("blog"), blogCol: db.Collection("blog"),
authCol: db.Collection("author"), authCol: db.Collection("author"),
conv: &bbConvert.HTMLConverter{},
} }
out.conv.ImplementDefaults()
// setup mux // setup mux
mux.HandleFunc("GET /blog", out.LatestBlogs) mux.HandleFunc("GET /blog", out.LatestBlogs)
mux.HandleFunc("GET /blog/list", out.BlogList) mux.HandleFunc("GET /blog/list", out.BlogList)