2024-08-30 15:17:14 +02:00
|
|
|
package calls
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-08-30 23:43:01 +02:00
|
|
|
"os"
|
2024-08-30 15:17:14 +02:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
b "streifling.com/jason/cpolis/cmd/backend"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ServeArticle(c *b.Config, db *b.DB) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-08-30 21:20:29 +02:00
|
|
|
if !tokenIsVerified(w, r, c) {
|
|
|
|
return
|
|
|
|
}
|
2024-08-30 15:17:14 +02:00
|
|
|
|
2024-08-30 21:20:29 +02:00
|
|
|
idString := r.PathValue("id")
|
|
|
|
id, err := strconv.ParseInt(idString, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-08-30 15:17:14 +02:00
|
|
|
|
2024-08-30 21:20:29 +02:00
|
|
|
article, err := db.GetArticle(id)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
2024-08-30 15:17:14 +02:00
|
|
|
}
|
2024-08-30 21:20:29 +02:00
|
|
|
|
2024-08-30 23:43:01 +02:00
|
|
|
if !article.Published {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-31 01:00:39 +02:00
|
|
|
articleAbsName := fmt.Sprint(c.ArticleDir, "/", article.ID, ".md")
|
|
|
|
contentBytes, err := os.ReadFile(articleAbsName)
|
2024-08-30 23:43:01 +02:00
|
|
|
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)
|
2024-08-30 15:17:14 +02:00
|
|
|
}
|
|
|
|
}
|