From cb53b7b831bcd82726ab3ae4c7a12b3c1a92da08 Mon Sep 17 00:00:00 2001 From: Caleb Gardner Date: Sat, 27 Jul 2024 02:38:31 -0500 Subject: [PATCH] Changed ExtendedApp to use ServeMux directly More work on portfolio stuff Added technologies to portfolio projects --- blog.go | 6 ++++++ internal/backend/app.go | 5 ++--- internal/backend/darkstorm.go | 4 +++- internal/blog/README.md | 6 +++++- internal/blog/main.go | 28 +++++++++++++++------------- internal/blog/portfolio.go | 24 ++++++++++++++++-------- main.go | 2 +- 7 files changed, 48 insertions(+), 27 deletions(-) create mode 100644 blog.go diff --git a/blog.go b/blog.go new file mode 100644 index 0000000..6be51bf --- /dev/null +++ b/blog.go @@ -0,0 +1,6 @@ +package main + +const ( + blogTitle = "

%v

" + blogAuthor = "

%v

" +) diff --git a/internal/backend/app.go b/internal/backend/app.go index e305468..b46e609 100644 --- a/internal/backend/app.go +++ b/internal/backend/app.go @@ -15,10 +15,9 @@ type CrashFilterApp interface { AddCrash(IndividualCrash) bool } +// Allows an app more flexibility by directly interfacing with the backend's mux type ExtendedApp interface { - // Extension is called for any calls to /{appID}/ - // Alternatively, use Backend.HandleFunc for more customizability - Extension(http.ResponseWriter, *http.Request) + Extension(*http.ServeMux) } type simpleApp struct { diff --git a/internal/backend/darkstorm.go b/internal/backend/darkstorm.go index 72fc864..cf63beb 100644 --- a/internal/backend/darkstorm.go +++ b/internal/backend/darkstorm.go @@ -44,7 +44,7 @@ func NewBackend(keyTable Table[ApiKey], apps ...App) (*Backend, error) { } b.apps[apps[i].AppID()] = apps[i] if ext, is := apps[i].(ExtendedApp); is { - b.m.HandleFunc("/"+apps[i].AppID()+"/", ext.Extension) + ext.Extension(b.m) } if !hasLog && apps[i].CountTable() != nil { hasLog = true @@ -124,10 +124,12 @@ func (b *Backend) AddUserAuth(userTable Table[User], privKey, pubKey []byte) { b.m.HandleFunc("POST /user/login", b.login) } +// Add values to the Backend's underlying ServeMux func (b *Backend) HandleFunc(pattern string, h http.HandlerFunc) { b.m.HandleFunc(pattern, h) } +// Try to get the App associated with the given ApiKey. Returns nil if not found. func (b *Backend) GetApp(a *ApiKey) App { return b.apps[a.AppID] } diff --git a/internal/blog/README.md b/internal/blog/README.md index 1d3182c..7683dde 100644 --- a/internal/blog/README.md +++ b/internal/blog/README.md @@ -169,9 +169,13 @@ Return: ```json [ { - title: "title", + title: "Darkstorm Server", repository: "https://github.com/CalebQ42/darkstorm-server", description: "The backend that runs runs my website and APIs", + technologies: [ // May be empty + "MongoDB", + "RESTful API" + ], language: [ { language: "go", diff --git a/internal/blog/main.go b/internal/blog/main.go index 3889514..a591978 100644 --- a/internal/blog/main.go +++ b/internal/blog/main.go @@ -16,7 +16,7 @@ type BlogApp struct { conv *bbConvert.HTMLConverter } -func NewBlogApp(b *backend.Backend, db *mongo.Database, mux *http.ServeMux) *BlogApp { +func NewBlogApp(b *backend.Backend, db *mongo.Database) *BlogApp { out := &BlogApp{ back: b, blogCol: db.Collection("blog"), @@ -25,18 +25,6 @@ func NewBlogApp(b *backend.Backend, db *mongo.Database, mux *http.ServeMux) *Blo conv: &bbConvert.HTMLConverter{}, } out.conv.ImplementDefaults() - // setup mux - mux.HandleFunc("GET /blog", out.reqLatestBlogs) - mux.HandleFunc("GET /blog/list", out.reqBlogList) - mux.HandleFunc("GET /blog/{blogID}", out.reqBlog) - mux.HandleFunc("POST /blog", out.createBlog) - mux.HandleFunc("POST /blog/{blogID}", out.updateBlog) - - mux.HandleFunc("GET /author/{authorID}", out.reqAuthorInfo) - mux.HandleFunc("POST /author", out.addAuthorInfo) - mux.HandleFunc("POST /author/{authorID}", out.updateAuthorInfo) - - mux.HandleFunc("GET /portfolio", out.reqPortfolio) return out } @@ -51,3 +39,17 @@ func (b *BlogApp) CountTable() backend.CountTable { func (b *BlogApp) CrashTable() backend.CrashTable { return nil } + +func (b *BlogApp) Extension(mux *http.ServeMux) { + mux.HandleFunc("GET /blog", b.reqLatestBlogs) + mux.HandleFunc("GET /blog/list", b.reqBlogList) + mux.HandleFunc("GET /blog/{blogID}", b.reqBlog) + mux.HandleFunc("POST /blog", b.createBlog) + mux.HandleFunc("POST /blog/{blogID}", b.updateBlog) + + mux.HandleFunc("GET /author/{authorID}", b.reqAuthorInfo) + mux.HandleFunc("POST /author", b.addAuthorInfo) + mux.HandleFunc("POST /author/{authorID}", b.updateAuthorInfo) + + mux.HandleFunc("GET /portfolio", b.reqPortfolio) +} diff --git a/internal/blog/portfolio.go b/internal/blog/portfolio.go index 25d75ca..6d083ae 100644 --- a/internal/blog/portfolio.go +++ b/internal/blog/portfolio.go @@ -2,19 +2,22 @@ package blog import ( "context" + "encoding/json" "net/http" + "github.com/CalebQ42/darkstorm-server/internal/backend" "go.mongodb.org/mongo-driver/bson" ) type PortfolioProject struct { - Title string `bson:"_id"` - Repository string `bson:"repository"` - Description string `bson:"description"` - Languages []struct { - Language string `bson:"language"` - Dates string `bson:"dates"` - } `bson:"language"` + Title string `json:"_id" bson:"_id"` + Repository string `json:"repository" bson:"repository"` + Description string `json:"description" bson:"description"` + Technologies []string `json:"technologies" bson:"technologies"` + Languages []struct { + Language string `json:"language" bson:"language"` + Dates string `json:"dates" bson:"dates"` + } `json:"language" bson:"language"` } func (b *BlogApp) Projects(languageFilter string) ([]PortfolioProject, error) { @@ -32,5 +35,10 @@ func (b *BlogApp) Projects(languageFilter string) ([]PortfolioProject, error) { } func (b *BlogApp) reqPortfolio(w http.ResponseWriter, r *http.Request) { - //TODO + folio, err := b.Projects(r.URL.Query().Get("lang")) + if err != nil { + backend.ReturnError(w, http.StatusInternalServerError, "internal", "Server Error") + return + } + json.NewEncoder(w).Encode(folio) } diff --git a/main.go b/main.go index c9fce61..dfbf8a1 100644 --- a/main.go +++ b/main.go @@ -67,7 +67,7 @@ func setupBackend(mux *http.ServeMux) { mongoClient.Database("testing").Collection("crash"), mongoClient.Database("testing").Collection("archive"), )) - blogApp = blog.NewBlogApp(back, mongoClient.Database("blog"), mux) + blogApp = blog.NewBlogApp(back, mongoClient.Database("blog")) //TODO: SWAssistant and CDR backends var err error back, err = backend.NewBackend(db.NewMongoTable[backend.ApiKey](