Updated to BunRouter
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
@ -9,7 +8,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/uptrace/bunrouter"
|
||||
"goth.stack/lib"
|
||||
)
|
||||
|
||||
@ -17,18 +16,20 @@ type BlogProps struct {
|
||||
Posts []lib.CardLink
|
||||
}
|
||||
|
||||
func Blog(c echo.Context) error {
|
||||
func Blog(w http.ResponseWriter, req bunrouter.Request) error {
|
||||
var posts []lib.CardLink
|
||||
|
||||
files, err := os.ReadDir("./content/")
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "There was an finding posts!")
|
||||
http.Error(w, "There was an issue finding posts!", http.StatusInternalServerError)
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
frontMatter, err := lib.ExtractFrontMatter(file, "./content/")
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "There was an issue rendering the posts!")
|
||||
http.Error(w, "There was an issue rendering the posts!", http.StatusInternalServerError)
|
||||
return nil
|
||||
}
|
||||
|
||||
frontMatter.Href = "post/" + strings.TrimSuffix(file.Name(), ".md")
|
||||
@ -57,19 +58,9 @@ func Blog(c echo.Context) error {
|
||||
Posts: posts,
|
||||
}
|
||||
|
||||
templates := []string{
|
||||
"./pages/templates/layouts/base.html",
|
||||
"./pages/templates/partials/header.html",
|
||||
"./pages/templates/partials/navitems.html",
|
||||
"./pages/templates/partials/cardlinks.html",
|
||||
"./pages/templates/blog.html",
|
||||
}
|
||||
// Specify the partials used by this page
|
||||
partials := []string{"header", "navitems", "cardlinks"}
|
||||
|
||||
ts, err := template.ParseFiles(templates...)
|
||||
if err != nil {
|
||||
log.Print(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
return ts.ExecuteTemplate(c.Response().Writer, "base", props)
|
||||
// Render the template
|
||||
return lib.RenderTemplate(w, "base", partials, props)
|
||||
}
|
||||
|
114
pages/home.go
114
pages/home.go
File diff suppressed because one or more lines are too long
@ -7,7 +7,7 @@ import (
|
||||
"os"
|
||||
|
||||
chromahtml "github.com/alecthomas/chroma/v2/formatters/html"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/uptrace/bunrouter"
|
||||
"github.com/yuin/goldmark"
|
||||
highlighting "github.com/yuin/goldmark-highlighting/v2"
|
||||
"gopkg.in/yaml.v2"
|
||||
@ -21,24 +21,27 @@ type PostProps struct {
|
||||
Tags []string
|
||||
}
|
||||
|
||||
func Post(c echo.Context) error {
|
||||
postName := c.ParamValues()[0]
|
||||
func Post(w http.ResponseWriter, req bunrouter.Request) error {
|
||||
postName := req.Param("post")
|
||||
|
||||
filePath := "content/" + postName + ".md"
|
||||
|
||||
md, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusNotFound, "This post does not exist!")
|
||||
http.Error(w, "This post does not exist!", http.StatusNotFound)
|
||||
return nil
|
||||
}
|
||||
|
||||
frontmatterBytes, content, err := lib.SplitFrontmatter(md)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "There was an issue rendering this post!")
|
||||
http.Error(w, "There was an issue rendering this post!", http.StatusInternalServerError)
|
||||
return nil
|
||||
}
|
||||
|
||||
var frontmatter lib.FrontMatter
|
||||
if err := yaml.Unmarshal(frontmatterBytes, &frontmatter); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "There was an issue rendering this post!")
|
||||
http.Error(w, "There was an issue rendering this post!", http.StatusInternalServerError)
|
||||
return nil
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
@ -54,7 +57,8 @@ func Post(c echo.Context) error {
|
||||
)
|
||||
|
||||
if err := markdown.Convert(content, &buf); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "There was an issue rendering this post!")
|
||||
http.Error(w, "There was an issue rendering this post!", http.StatusInternalServerError)
|
||||
return nil
|
||||
}
|
||||
|
||||
props := PostProps{
|
||||
@ -64,17 +68,9 @@ func Post(c echo.Context) error {
|
||||
Tags: frontmatter.Tags,
|
||||
}
|
||||
|
||||
templates := []string{
|
||||
"./pages/templates/layouts/post.html",
|
||||
"./pages/templates/partials/header.html",
|
||||
"./pages/templates/partials/navitems.html",
|
||||
"./pages/templates/post.html",
|
||||
}
|
||||
// Specify the partials used by this page
|
||||
partials := []string{"header", "navitems"}
|
||||
|
||||
ts, err := template.ParseFiles(templates...)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "There was an issue rendering this post!")
|
||||
}
|
||||
|
||||
return ts.ExecuteTemplate(c.Response().Writer, "post", props)
|
||||
// Render the template
|
||||
return lib.RenderTemplate(w, "post", partials, props)
|
||||
}
|
||||
|
@ -1,25 +1,16 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/uptrace/bunrouter"
|
||||
"goth.stack/lib"
|
||||
)
|
||||
|
||||
func SSEDemo(c echo.Context) error {
|
||||
templates := []string{
|
||||
"./pages/templates/layouts/base.html",
|
||||
"./pages/templates/partials/header.html",
|
||||
"./pages/templates/partials/navitems.html",
|
||||
"./pages/templates/ssedemo.html",
|
||||
}
|
||||
func SSEDemo(w http.ResponseWriter, req bunrouter.Request) error {
|
||||
// Specify the partials used by this page
|
||||
partials := []string{"header", "navitems"}
|
||||
|
||||
ts, err := template.ParseFiles(templates...)
|
||||
if err != nil {
|
||||
log.Print(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
return ts.ExecuteTemplate(c.Response().Writer, "base", nil)
|
||||
// Render the template
|
||||
return lib.RenderTemplate(w, "base", partials, nil)
|
||||
}
|
||||
|
@ -5,7 +5,7 @@
|
||||
</a>
|
||||
</li>
|
||||
<li>
|
||||
<a class="no-underline" href="/ssedemo">
|
||||
<a class="no-underline" href="/sse">
|
||||
SSE Demo
|
||||
</a>
|
||||
</li>
|
||||
|
@ -13,7 +13,7 @@ GOTH // SSE <div class="badge badge-accent">DEMO</div>
|
||||
<h1 class="text-4xl">Server Sent Events</h1>
|
||||
<h2 class="text-xl">This page demonstrates the use of the <a href="https://htmx.org/extensions/sse/">HTMX SSE Extention</a> to receive Server Sent Events on the "default" channel.</h2>
|
||||
<p class="text-lg">Any events received on the "default" channel will appear below:</p>
|
||||
<div hx-ext="sse" sse-connect="/api/ssedemo" sse-swap="message">
|
||||
<div hx-ext="sse" sse-connect="/api/sse" sse-swap="message">
|
||||
Waiting for SSE Message...
|
||||
</div>
|
||||
|
||||
|
Reference in New Issue
Block a user