Store article content outside of DB and convert and serve on request from respective file

This commit is contained in:
2024-08-30 23:43:01 +02:00
parent 3f1b18c29f
commit 4663cedec5
9 changed files with 146 additions and 104 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt"
"log"
"net/http"
"os"
"strconv"
b "streifling.com/jason/cpolis/cmd/backend"
@ -30,6 +31,24 @@ func ServeArticle(c *b.Config, db *b.DB) http.HandlerFunc {
return
}
fmt.Fprint(w, article.Content)
if !article.Published {
return
}
contentBytes, err := os.ReadFile(article.Link)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
content, err := b.ConvertToHTML(string(contentBytes))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprint(w, content)
}
}