Changed ExtendedApp to use ServeMux directly

More work on portfolio stuff
Added technologies to portfolio projects
This commit is contained in:
Caleb Gardner
2024-07-27 02:38:31 -05:00
parent f59a4207c4
commit cb53b7b831
7 changed files with 48 additions and 27 deletions
+6
View File
@@ -0,0 +1,6 @@
package main
const (
blogTitle = "<h2 class='blog-title'>%v</h2>"
blogAuthor = "<h4 class='blog-author'>%v</h4>"
)
+2 -3
View File
@@ -15,10 +15,9 @@ type CrashFilterApp interface {
AddCrash(IndividualCrash) bool AddCrash(IndividualCrash) bool
} }
// Allows an app more flexibility by directly interfacing with the backend's mux
type ExtendedApp interface { type ExtendedApp interface {
// Extension is called for any calls to /{appID}/ Extension(*http.ServeMux)
// Alternatively, use Backend.HandleFunc for more customizability
Extension(http.ResponseWriter, *http.Request)
} }
type simpleApp struct { type simpleApp struct {
+3 -1
View File
@@ -44,7 +44,7 @@ func NewBackend(keyTable Table[ApiKey], apps ...App) (*Backend, error) {
} }
b.apps[apps[i].AppID()] = apps[i] b.apps[apps[i].AppID()] = apps[i]
if ext, is := apps[i].(ExtendedApp); is { 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 { if !hasLog && apps[i].CountTable() != nil {
hasLog = true hasLog = true
@@ -124,10 +124,12 @@ func (b *Backend) AddUserAuth(userTable Table[User], privKey, pubKey []byte) {
b.m.HandleFunc("POST /user/login", b.login) 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) { func (b *Backend) HandleFunc(pattern string, h http.HandlerFunc) {
b.m.HandleFunc(pattern, h) 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 { func (b *Backend) GetApp(a *ApiKey) App {
return b.apps[a.AppID] return b.apps[a.AppID]
} }
+5 -1
View File
@@ -169,9 +169,13 @@ Return:
```json ```json
[ [
{ {
title: "title", title: "Darkstorm Server",
repository: "https://github.com/CalebQ42/darkstorm-server", repository: "https://github.com/CalebQ42/darkstorm-server",
description: "The backend that runs runs my website and APIs", description: "The backend that runs runs my website and APIs",
technologies: [ // May be empty
"MongoDB",
"RESTful API"
],
language: [ language: [
{ {
language: "go", language: "go",
+15 -13
View File
@@ -16,7 +16,7 @@ type BlogApp struct {
conv *bbConvert.HTMLConverter 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{ out := &BlogApp{
back: b, back: b,
blogCol: db.Collection("blog"), blogCol: db.Collection("blog"),
@@ -25,18 +25,6 @@ func NewBlogApp(b *backend.Backend, db *mongo.Database, mux *http.ServeMux) *Blo
conv: &bbConvert.HTMLConverter{}, conv: &bbConvert.HTMLConverter{},
} }
out.conv.ImplementDefaults() 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 return out
} }
@@ -51,3 +39,17 @@ func (b *BlogApp) CountTable() backend.CountTable {
func (b *BlogApp) CrashTable() backend.CrashTable { func (b *BlogApp) CrashTable() backend.CrashTable {
return nil 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)
}
+15 -7
View File
@@ -2,19 +2,22 @@ package blog
import ( import (
"context" "context"
"encoding/json"
"net/http" "net/http"
"github.com/CalebQ42/darkstorm-server/internal/backend"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
) )
type PortfolioProject struct { type PortfolioProject struct {
Title string `bson:"_id"` Title string `json:"_id" bson:"_id"`
Repository string `bson:"repository"` Repository string `json:"repository" bson:"repository"`
Description string `bson:"description"` Description string `json:"description" bson:"description"`
Technologies []string `json:"technologies" bson:"technologies"`
Languages []struct { Languages []struct {
Language string `bson:"language"` Language string `json:"language" bson:"language"`
Dates string `bson:"dates"` Dates string `json:"dates" bson:"dates"`
} `bson:"language"` } `json:"language" bson:"language"`
} }
func (b *BlogApp) Projects(languageFilter string) ([]PortfolioProject, error) { 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) { 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)
} }
+1 -1
View File
@@ -67,7 +67,7 @@ func setupBackend(mux *http.ServeMux) {
mongoClient.Database("testing").Collection("crash"), mongoClient.Database("testing").Collection("crash"),
mongoClient.Database("testing").Collection("archive"), 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 //TODO: SWAssistant and CDR backends
var err error var err error
back, err = backend.NewBackend(db.NewMongoTable[backend.ApiKey]( back, err = backend.NewBackend(db.NewMongoTable[backend.ApiKey](