chore: fix commited file

This commit is contained in:
Guz
2024-12-18 15:51:51 -03:00
parent 23062bd02a
commit 8559f57473
2 changed files with 36 additions and 9 deletions

View File

@@ -4,13 +4,13 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"path"
"forge.capytal.company/capytalcode/project-comicverse/lib/router"
"forge.capytal.company/capytalcode/project-comicverse/lib/router/rerrors"
"forge.capytal.company/capytal/www/templates/layouts"
"forge.capytal.company/loreddev/x/groute/router/rerrors"
)
type Blog struct {
@@ -27,10 +27,17 @@ func NewBlog(owner, repo, endpoint string) *Blog {
return &Blog{repo: repo, owner: owner, endpoint: u.String()}
}
func (p *Blog) Routes() router.Router {
r := router.NewRouter()
func (p *Blog) Routes() http.Handler {
r := http.NewServeMux()
r.HandleFunc("/", p.listPosts)
r.HandleFunc("/{entry...}", func(w http.ResponseWriter, r *http.Request) {
pv := r.PathValue("entry")
if pv == "" {
p.listPosts(w, r)
} else {
p.blogEntry(w, r)
}
})
return r
}
@@ -50,13 +57,32 @@ func (p *Blog) listPosts(w http.ResponseWriter, r *http.Request) {
return
}
w.WriteHeader(http.StatusOK)
_, err = w.Write([]byte(fmt.Sprintf("%v", list)))
err = p.blogEntryList(list).Render(r.Context(), w)
if err != nil {
rerrors.InternalError(err).ServeHTTP(w, r)
}
}
templ (p *Blog) blogEntryList(entries []forgejoFile) {
@layouts.Page() {
<ul>
for _, e := range entries {
<li><a href={ templ.SafeURL(path.Join(".", e.Path)) }>{ e.Name }</a></li>
}
</ul>
}
}
func (p *Blog) blogEntry(w http.ResponseWriter, r *http.Request) {
_, body, rerr := p.get(fmt.Sprintf("/repos/%s/%s/raw/%s", p.owner, p.repo, r.PathValue("entry")))
if rerr != nil {
rerr.ServeHTTP(w, r)
return
}
w.Write(body)
}
func (p *Blog) get(endpoint string) (http.Header, []byte, *rerrors.RouteError) {
u, _ := url.Parse(p.endpoint)
u.Path = path.Join(u.Path, endpoint)

View File

@@ -2,6 +2,7 @@ package pages
import (
"log/slog"
"net/http"
"forge.capytal.company/loreddev/x/groute/router"
"forge.capytal.company/loreddev/x/groute/router/rerrors"
@@ -16,7 +17,7 @@ func Routes(log *slog.Logger) router.Router {
r.Handle("/about", &AboutPage{})
b := NewBlog("dot013", "blog", "https://forge.capytal.company/api/v1")
r.Handle("/blog/", b.Routes())
r.Handle("/blog", http.StripPrefix("/blog/", b.Routes()))
return r
}