13 Commits

11 changed files with 118 additions and 22 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,20 @@ dokku storage:mount teafolio /srv/teafolio-dokku/config.toml:/app/config.toml
## CHANGELOG
2024-03-19 v1.4.1
- Add cooldown for failed markdown API calls
- Fix missing extra html elements when using fallback markdown renderer
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
2022-12-31 v1.3.0
- Add `OverrideOrder` configuration option to insert repos at specific positions
- Cache target image URLs to improve homepage load performance

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

@@ -4,25 +4,32 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"sync/atomic"
"time"
"golang.org/x/sync/semaphore"
)
const MarkdownFailureCooldownSeconds = 3600 // 1 hour
type APIClient struct {
urlBase string
orgName string
token string
apiSem *semaphore.Weighted
lastMarkdownFailure atomic.Int64
}
// 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 +38,7 @@ func NewAPIClient(urlBase string, orgName string, maxConnections int64) *APIClie
ret := &APIClient{
urlBase: urlBase,
orgName: orgName,
token: token,
}
if maxConnections == 0 { // unlimited
@@ -98,6 +106,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
@@ -291,8 +303,22 @@ func (ac *APIClient) topicsForRepo(ctx context.Context, repo string) ([]string,
return tr.Topics, nil
}
var ErrMarkdownCooldown = errors.New("Markdown API was recently not functional")
func (ac *APIClient) checkMarkdownCooldown() error {
if ac.lastMarkdownFailure.Load()+MarkdownFailureCooldownSeconds > time.Now().Unix() {
return ErrMarkdownCooldown
}
return nil
}
// renderMarkdown calls the remote Gitea server's own markdown renderer.
func (ac *APIClient) RenderMarkdown(ctx context.Context, repoName string, body string) ([]byte, error) {
if err := ac.checkMarkdownCooldown(); err != nil {
return nil, err
}
err := ac.apiSem.Acquire(ctx, 1)
if err != nil {
return nil, err // e.g. ctx closed
@@ -322,6 +348,7 @@ func (ac *APIClient) RenderMarkdown(ctx context.Context, repoName string, body s
defer resp.Body.Close()
if resp.StatusCode != 200 {
ac.lastMarkdownFailure.Store(time.Now().Unix())
return nil, fmt.Errorf("HTTP %d", resp.StatusCode)
}
@@ -330,6 +357,10 @@ func (ac *APIClient) RenderMarkdown(ctx context.Context, repoName string, body s
// renderMarkdownRaw calls the remote Gitea server's own markdown renderer.
func (ac *APIClient) renderMarkdownRaw(ctx context.Context, body []byte) ([]byte, error) {
if err := ac.checkMarkdownCooldown(); err != nil {
return nil, err
}
err := ac.apiSem.Acquire(ctx, 1)
if err != nil {
return nil, err // e.g. ctx closed
@@ -350,6 +381,7 @@ func (ac *APIClient) renderMarkdownRaw(ctx context.Context, body []byte) ([]byte
defer resp.Body.Close()
if resp.StatusCode != 200 {
ac.lastMarkdownFailure.Store(time.Now().Unix())
return nil, fmt.Errorf("HTTP %d", resp.StatusCode)
}

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 {
@@ -36,6 +36,7 @@ type Application struct {
reposMut sync.RWMutex
reposCache []gitea.Repo // Sorted by recently-created-first
reposCacheByName map[string]int
reposAlphabeticalOrder map[string]int
}
@@ -54,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

@@ -9,7 +9,7 @@ func (this *Application) Bannerpage(w http.ResponseWriter, r *http.Request, repo
this.reposMut.RLock()
defer this.reposMut.RUnlock()
ridx, ok := this.reposAlphabeticalOrder[repoName]
ridx, ok := this.reposCacheByName[repoName]
if !ok {
w.Header().Set(`Location`, `/static/no_image.png`)
w.WriteHeader(301)

View File

@@ -2,11 +2,17 @@ package main
import (
"bytes"
"errors"
"fmt"
"html"
"log"
"net/http"
"net/url"
"strings"
"teafolio/gitea"
"github.com/yuin/goldmark"
)
func (this *Application) Repopage(w http.ResponseWriter, r *http.Request, repoName string) {
@@ -16,7 +22,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 +62,36 @@ 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
if !errors.Is(err, gitea.ErrMarkdownCooldown) {
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([]byte(strings.Join(lines, "\n")), &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

13
sync.go
View File

@@ -94,7 +94,7 @@ func (this *Application) sync(ctx context.Context) (bool, error) {
}
for i, rr := range repos {
if idx, ok := this.reposAlphabeticalOrder[rr.Name]; ok && this.reposCache[idx].GiteaUpdated == rr.GiteaUpdated {
if idx, ok := this.reposCacheByName[rr.Name]; ok && this.reposCache[idx].GiteaUpdated == rr.GiteaUpdated {
// Already exists in cache with same Gitea update time
// Copy timestamps
repos[i] = this.reposCache[idx]
@@ -117,6 +117,11 @@ func (this *Application) sync(ctx context.Context) (bool, error) {
log.Printf("loading topics for '%s': %s", rr.Name, err)
}
err = this.gitea.PopulateImages(ctx, &rr)
if err != nil {
log.Printf("loading images for '%s': %s", rr.Name, err)
}
// Save
repos[i] = rr
}
@@ -151,9 +156,15 @@ func (this *Application) sync(ctx context.Context) (bool, error) {
repos = reordered
}
reposCacheByName := make(map[string]int, len(repos))
for idx, repo := range repos {
reposCacheByName[repo.Name] = idx
}
// Commit our changes for the other threads to look at
this.reposMut.Lock()
this.reposCache = repos
this.reposCacheByName = reposCacheByName
this.reposAlphabeticalOrder = alphabeticalOrderIndexes
this.reposMut.Unlock()