chore: fix commited file
This commit is contained in:
@@ -4,13 +4,13 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
"forge.capytal.company/capytalcode/project-comicverse/lib/router"
|
"forge.capytal.company/capytal/www/templates/layouts"
|
||||||
"forge.capytal.company/capytalcode/project-comicverse/lib/router/rerrors"
|
|
||||||
|
"forge.capytal.company/loreddev/x/groute/router/rerrors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Blog struct {
|
type Blog struct {
|
||||||
@@ -27,10 +27,17 @@ func NewBlog(owner, repo, endpoint string) *Blog {
|
|||||||
return &Blog{repo: repo, owner: owner, endpoint: u.String()}
|
return &Blog{repo: repo, owner: owner, endpoint: u.String()}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Blog) Routes() router.Router {
|
func (p *Blog) Routes() http.Handler {
|
||||||
r := router.NewRouter()
|
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
|
return r
|
||||||
}
|
}
|
||||||
@@ -50,13 +57,32 @@ func (p *Blog) listPosts(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
err = p.blogEntryList(list).Render(r.Context(), w)
|
||||||
_, err = w.Write([]byte(fmt.Sprintf("%v", list)))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rerrors.InternalError(err).ServeHTTP(w, r)
|
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) {
|
func (p *Blog) get(endpoint string) (http.Header, []byte, *rerrors.RouteError) {
|
||||||
u, _ := url.Parse(p.endpoint)
|
u, _ := url.Parse(p.endpoint)
|
||||||
u.Path = path.Join(u.Path, endpoint)
|
u.Path = path.Join(u.Path, endpoint)
|
||||||
@@ -2,6 +2,7 @@ package pages
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"forge.capytal.company/loreddev/x/groute/router"
|
"forge.capytal.company/loreddev/x/groute/router"
|
||||||
"forge.capytal.company/loreddev/x/groute/router/rerrors"
|
"forge.capytal.company/loreddev/x/groute/router/rerrors"
|
||||||
@@ -16,7 +17,7 @@ func Routes(log *slog.Logger) router.Router {
|
|||||||
r.Handle("/about", &AboutPage{})
|
r.Handle("/about", &AboutPage{})
|
||||||
|
|
||||||
b := NewBlog("dot013", "blog", "https://forge.capytal.company/api/v1")
|
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
|
return r
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user