feat(blog,gitea): gitea api to blog reroute mock
This commit is contained in:
@@ -2,7 +2,9 @@ package pages
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"forge.capytal.company/capytal/www/libs/blog"
|
||||
"forge.capytal.company/capytalcode/project-comicverse/lib/router"
|
||||
"forge.capytal.company/capytalcode/project-comicverse/lib/router/rerrors"
|
||||
)
|
||||
@@ -15,5 +17,9 @@ func Routes(log *slog.Logger) router.Router {
|
||||
r.Handle("/", &IndexPage{})
|
||||
r.Handle("/about", &AboutPage{})
|
||||
|
||||
b := blog.NewGiteaBlog("dot013", "blog", "https://forge.capytal.company/api/v1")
|
||||
|
||||
r.Handle("/blog/{path...}", http.StripPrefix("/blog/", b))
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
45
libs/blog/blog.go
Normal file
45
libs/blog/blog.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package blog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"path"
|
||||
"forge.capytal.company/capytalcode/project-comicverse/lib/router/rerrors"
|
||||
)
|
||||
|
||||
func NewGiteaBlog(owner, repo, endpoint string) *GiteaBlog {
|
||||
return &GiteaBlog{
|
||||
owner: owner,
|
||||
repo: repo,
|
||||
endpoint: endpoint,
|
||||
}
|
||||
}
|
||||
|
||||
func (b *GiteaBlog) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
p := path.Clean(r.URL.Path)
|
||||
p = fmt.Sprintf("%s/repos/%s/%s/contents/%s", b.endpoint, b.owner, b.repo, p)
|
||||
|
||||
log.Printf("PATH %s", p)
|
||||
|
||||
res, err := http.Get(p)
|
||||
if err != nil {
|
||||
rerrors.InternalError(err).ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
rerrors.InternalError(err).ServeHTTP(w, r)
|
||||
return
|
||||
} else if res.StatusCode != http.StatusOK {
|
||||
rerrors.InternalError(fmt.Errorf("Non-OK: %s", string(body))).ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := w.Write(body); err != nil {
|
||||
rerrors.InternalError(err).ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user