6 Commits

9 changed files with 65 additions and 20 deletions

View File

@@ -1,6 +1,6 @@
# Dockerfile for production Teafolio deployments
FROM golang:1.19-alpine AS builder
FROM golang:1.22-alpine AS builder
WORKDIR /app
COPY . .
@@ -10,6 +10,5 @@ FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/teafolio /app/teafolio
COPY /static /app/static
ENTRYPOINT [ "/app/teafolio" ]

View File

@@ -31,6 +31,12 @@ dokku storage:mount teafolio /srv/teafolio-dokku/config.toml:/app/config.toml
## CHANGELOG
2024-03-19 v1.4.0
- Support using application token for Gitea API
- Add fallback to internal markdown renderer if Gitea's API is unavailable
- Add fallback to Gitea page redirect if there are any errors building repo page
- Embed static web assets in binary (requires Go 1.16+)
2022-12-31 v1.3.1
- Fix missing images on homepage
- Fix an issue with wrong mtime comparisons for updated repositories

View File

@@ -5,6 +5,7 @@ BindTo="0.0.0.0:5656"
[Gitea]
URL="https://gitea.com/"
Org="gitea"
Token="" # Must have misc:write, org:read, repo:read permission
MaxConnections=2 # Use zero for unlimited
[Redirect]

View File

@@ -17,12 +17,13 @@ import (
type APIClient struct {
urlBase string
orgName string
token string
apiSem *semaphore.Weighted
}
// NewAPIClient creates a new Gitea API client for a single Gitea organization.
// Set maxConnections to 0 for unlimited concurrent API calls.
func NewAPIClient(urlBase string, orgName string, maxConnections int64) *APIClient {
func NewAPIClient(urlBase string, orgName string, token string, maxConnections int64) *APIClient {
if !strings.HasSuffix(urlBase, `/`) {
urlBase += `/`
@@ -31,6 +32,7 @@ func NewAPIClient(urlBase string, orgName string, maxConnections int64) *APIClie
ret := &APIClient{
urlBase: urlBase,
orgName: orgName,
token: token,
}
if maxConnections == 0 { // unlimited
@@ -98,6 +100,10 @@ func (ac *APIClient) apiRequest(ctx context.Context, endpoint string, target int
return err
}
if ac.token != "" {
req.Header.Set(`Authorization`, `token `+ac.token)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err

2
go.mod
View File

@@ -6,3 +6,5 @@ require (
github.com/BurntSushi/toml v0.3.1
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a
)
require github.com/yuin/goldmark v1.7.0 // indirect

2
go.sum
View File

@@ -1,4 +1,6 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/yuin/goldmark v1.7.0 h1:EfOIvIMZIzHdB/R/zVrikYLPPwJlfMcNczJFMs1m6sA=
github.com/yuin/goldmark v1.7.0/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a h1:WXEvlFVvvGxCJLG6REjsT03iWnKLEWinaScsxF2Vm2o=
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

View File

@@ -15,8 +15,8 @@ import (
type Config struct {
BindTo string
Gitea struct {
URL, Org string
MaxConnections int64
URL, Org, Token string
MaxConnections int64
}
Redirect map[string]string
Template struct {
@@ -55,7 +55,7 @@ func main() {
}
// Create Gitea API client
app.gitea = gitea.NewAPIClient(app.cfg.Gitea.URL, app.cfg.Gitea.Org, app.cfg.Gitea.MaxConnections)
app.gitea = gitea.NewAPIClient(app.cfg.Gitea.URL, app.cfg.Gitea.Org, app.cfg.Gitea.Token, app.cfg.Gitea.MaxConnections)
// Sync worker
go app.syncWorker(context.Background())

View File

@@ -4,9 +4,12 @@ import (
"bytes"
"fmt"
"html"
"log"
"net/http"
"net/url"
"strings"
"github.com/yuin/goldmark"
)
func (this *Application) Repopage(w http.ResponseWriter, r *http.Request, repoName string) {
@@ -16,7 +19,8 @@ func (this *Application) Repopage(w http.ResponseWriter, r *http.Request, repoNa
readme, err := this.gitea.RepoFile(ctx, repoName, `README.md`)
if err != nil {
this.internalError(w, r, fmt.Errorf("loading README.md: %w", err))
log.Printf("%s %s: %s", r.Method, r.URL.Path, err)
http.Redirect(w, r, repoURL, http.StatusTemporaryRedirect)
return
}
@@ -55,15 +59,34 @@ func (this *Application) Repopage(w http.ResponseWriter, r *http.Request, repoNa
readmeHtml, err := this.gitea.RenderMarkdown(ctx, repoName, strings.Join(lines, "\n"))
if err != nil {
this.internalError(w, r, fmt.Errorf("rendering markdown: %w", err))
return
log.Printf("%s %s: %s", r.Method, r.URL.Path, fmt.Errorf("rendering markdown: %w", err))
// Failed to use Gitea's markdown renderer
// HTTP 401 (Unauthorized) started happening with this API sometime
// between Gitea 1.18 -> 1.21, and no authorization token seems
// sufficient to make it work again
// Use our own one instead as a fallback
buff := bytes.Buffer{}
err = goldmark.Convert(readme, &buff)
if err != nil {
// Built-in markdown renderer didn't work either
log.Printf("%s %s: %s", r.Method, r.URL.Path, fmt.Errorf("rendering markdown: %w", err))
http.Redirect(w, r, repoURL, http.StatusTemporaryRedirect)
return
}
// OK
readmeHtml = buff.Bytes()
err = nil
}
readmeHtml = []byte(strings.Replace(string(readmeHtml), `%%REPLACEME__BADGE%%`, `<img src="/static/build_success_brightgreen.svg" style="width:90px;height:20px;">`, 1))
images, err := this.gitea.ImageFilesForRepo(ctx, repoName)
if err != nil {
this.internalError(w, r, fmt.Errorf("listing images: %w", err))
log.Printf("%s %s: %s", r.Method, r.URL.Path, fmt.Errorf("listing images: %w", err))
http.Redirect(w, r, repoURL, http.StatusTemporaryRedirect)
return
}

View File

@@ -1,6 +1,7 @@
package main
import (
"embed"
"encoding/base64"
"fmt"
"net/http"
@@ -8,6 +9,14 @@ import (
"strings"
)
//go:embed static/*
var StaticFiles embed.FS
func (this *Application) ServeStatic(w http.ResponseWriter, r *http.Request) {
http.FileServer(http.FS(StaticFiles)).ServeHTTP(w, r)
// http.StripPrefix(`/static/`, http.FileServer(http.FS(StaticFiles))).ServeHTTP(w, r)
}
func (this *Application) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method == `GET` {
if r.URL.Path == `/` {
@@ -56,8 +65,9 @@ func (this *Application) ServeHTTP(w http.ResponseWriter, r *http.Request) {
this.Repopage(w, r, repoName)
} else if r.URL.Path == `/static/logo.png` {
if this.cfg.Template.CustomLogoPngBase64 != "" {
} else if strings.HasPrefix(r.URL.Path, `/static/`) {
if r.URL.Path == `/static/logo.png` && this.cfg.Template.CustomLogoPngBase64 != "" {
logoPng, err := base64.StdEncoding.DecodeString(this.cfg.Template.CustomLogoPngBase64)
if err != nil {
@@ -69,16 +79,12 @@ func (this *Application) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set(`Content-Type`, `image/png`)
w.WriteHeader(200)
w.Write(logoPng)
} else {
r.URL.Path = r.URL.Path[8:]
http.FileServer(http.Dir(`static`)).ServeHTTP(w, r)
return
}
} else if strings.HasPrefix(r.URL.Path, `/static/`) {
r.URL.Path = r.URL.Path[8:]
http.FileServer(http.Dir(`static`)).ServeHTTP(w, r)
// Embedded resource
// r.URL.Path = r.URL.Path[8:]
this.ServeStatic(w, r)
} else if r.URL.Query().Get("go-get") == "1" {
// This wasn't one of our standard `/repo` paths, but there is the ?go-get=1 parameter