Compare commits
	
		
			16 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 129d5a2a31 | |||
| b15fe13f77 | |||
| f256383ac9 | |||
| ecdb8304dc | |||
| 9dac5b808e | |||
| 3c3098a15c | |||
| c42f0a75ef | |||
| 0407a52fb6 | |||
| c3b3e19a42 | |||
| 0d806965ab | |||
| 52166fcf30 | |||
| fda3899a23 | |||
| 1adacc0d49 | |||
| 8de7d565a1 | |||
| 1b3a720323 | |||
| eda45221cd | 
@@ -1,6 +1,6 @@
 | 
				
			|||||||
# Dockerfile for production Teafolio deployments
 | 
					# Dockerfile for production Teafolio deployments
 | 
				
			||||||
 | 
					
 | 
				
			||||||
FROM golang:1.19-alpine AS builder
 | 
					FROM golang:1.22-alpine AS builder
 | 
				
			||||||
 | 
					
 | 
				
			||||||
WORKDIR /app
 | 
					WORKDIR /app
 | 
				
			||||||
COPY . .
 | 
					COPY . .
 | 
				
			||||||
@@ -10,6 +10,5 @@ FROM alpine:latest
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
WORKDIR /app
 | 
					WORKDIR /app
 | 
				
			||||||
COPY --from=builder /app/teafolio /app/teafolio
 | 
					COPY --from=builder /app/teafolio /app/teafolio
 | 
				
			||||||
COPY /static /app/static
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
ENTRYPOINT [ "/app/teafolio" ]
 | 
					ENTRYPOINT [ "/app/teafolio" ]
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										16
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								README.md
									
									
									
									
									
								
							@@ -31,6 +31,22 @@ dokku storage:mount teafolio /srv/teafolio-dokku/config.toml:/app/config.toml
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
## CHANGELOG
 | 
					## CHANGELOG
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					2025-06-01 v1.4.2
 | 
				
			||||||
 | 
					- Proxy images from Gitea, for servers using `REQUIRE_SIGNIN_VIEW=expensive`
 | 
				
			||||||
 | 
					- Always use fallback markdown renderer
 | 
				
			||||||
 | 
					- Fix missing support for markdown tables in fallback renderer
 | 
				
			||||||
 | 
					- Fix very wide code blocks overflowing page width
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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
 | 
					2022-12-31 v1.3.1
 | 
				
			||||||
- Fix missing images on homepage
 | 
					- Fix missing images on homepage
 | 
				
			||||||
- Fix an issue with wrong mtime comparisons for updated repositories
 | 
					- Fix an issue with wrong mtime comparisons for updated repositories
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,6 +5,7 @@ BindTo="0.0.0.0:5656"
 | 
				
			|||||||
[Gitea]
 | 
					[Gitea]
 | 
				
			||||||
URL="https://gitea.com/"
 | 
					URL="https://gitea.com/"
 | 
				
			||||||
Org="gitea"
 | 
					Org="gitea"
 | 
				
			||||||
 | 
					Token="" # Must have misc:write, org:read, repo:read permission
 | 
				
			||||||
MaxConnections=2 # Use zero for unlimited
 | 
					MaxConnections=2 # Use zero for unlimited
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[Redirect]
 | 
					[Redirect]
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,7 +1,6 @@
 | 
				
			|||||||
package gitea
 | 
					package gitea
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"bytes"
 | 
					 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
@@ -9,20 +8,23 @@ import (
 | 
				
			|||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"net/url"
 | 
						"net/url"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
						"sync/atomic"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					 | 
				
			||||||
	"golang.org/x/sync/semaphore"
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type APIClient struct {
 | 
					type APIClient struct {
 | 
				
			||||||
	urlBase string
 | 
						urlBase string
 | 
				
			||||||
	orgName string
 | 
						orgName string
 | 
				
			||||||
	apiSem  *semaphore.Weighted
 | 
						token   string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						apiSem chan struct{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						lastMarkdownFailure atomic.Int64
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// NewAPIClient creates a new Gitea API client for a single Gitea organization.
 | 
					// NewAPIClient creates a new Gitea API client for a single Gitea organization.
 | 
				
			||||||
// Set maxConnections to 0 for unlimited concurrent API calls.
 | 
					// 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, `/`) {
 | 
						if !strings.HasSuffix(urlBase, `/`) {
 | 
				
			||||||
		urlBase += `/`
 | 
							urlBase += `/`
 | 
				
			||||||
@@ -31,17 +33,31 @@ func NewAPIClient(urlBase string, orgName string, maxConnections int64) *APIClie
 | 
				
			|||||||
	ret := &APIClient{
 | 
						ret := &APIClient{
 | 
				
			||||||
		urlBase: urlBase,
 | 
							urlBase: urlBase,
 | 
				
			||||||
		orgName: orgName,
 | 
							orgName: orgName,
 | 
				
			||||||
 | 
							token:   token,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if maxConnections == 0 { // unlimited
 | 
						if maxConnections == 0 { // unlimited
 | 
				
			||||||
		ret.apiSem = semaphore.NewWeighted(99999)
 | 
							// no-op
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		ret.apiSem = semaphore.NewWeighted(maxConnections)
 | 
							ret.apiSem = make(chan struct{}, maxConnections)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return ret
 | 
						return ret
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (ac *APIClient) Sem() func() {
 | 
				
			||||||
 | 
						if ac.apiSem == nil {
 | 
				
			||||||
 | 
							// No connection limit
 | 
				
			||||||
 | 
							return func() {}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Connection limit
 | 
				
			||||||
 | 
						ac.apiSem <- struct{}{}
 | 
				
			||||||
 | 
						return func() {
 | 
				
			||||||
 | 
							<-ac.apiSem
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (ac *APIClient) PopulateCommitInfo(ctx context.Context, rr *Repo) error {
 | 
					func (ac *APIClient) PopulateCommitInfo(ctx context.Context, rr *Repo) error {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// The most recent commit will be the head of one of the branches (easy to find)
 | 
						// The most recent commit will be the head of one of the branches (easy to find)
 | 
				
			||||||
@@ -87,17 +103,18 @@ func (ac *APIClient) PopulateImages(ctx context.Context, rr *Repo) error {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (ac *APIClient) apiRequest(ctx context.Context, endpoint string, target interface{}) error {
 | 
					func (ac *APIClient) apiRequest(ctx context.Context, endpoint string, target interface{}) error {
 | 
				
			||||||
	err := ac.apiSem.Acquire(ctx, 1)
 | 
						release := ac.Sem()
 | 
				
			||||||
	if err != nil {
 | 
						defer release()
 | 
				
			||||||
		return err // e.g. ctx closed
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	defer ac.apiSem.Release(1)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	req, err := http.NewRequestWithContext(ctx, http.MethodGet, ac.urlBase+endpoint, nil)
 | 
						req, err := http.NewRequestWithContext(ctx, http.MethodGet, ac.urlBase+endpoint, nil)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if ac.token != "" {
 | 
				
			||||||
 | 
							req.Header.Set(`Authorization`, `token `+ac.token)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	resp, err := http.DefaultClient.Do(req)
 | 
						resp, err := http.DefaultClient.Do(req)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
@@ -215,11 +232,8 @@ func (ac *APIClient) RepoFile(ctx context.Context, repo, filename string) ([]byt
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (ac *APIClient) filesInDirectory(ctx context.Context, repo, dir string) ([]ReaddirEntry, error) {
 | 
					func (ac *APIClient) filesInDirectory(ctx context.Context, repo, dir string) ([]ReaddirEntry, error) {
 | 
				
			||||||
	err := ac.apiSem.Acquire(ctx, 1)
 | 
						release := ac.Sem()
 | 
				
			||||||
	if err != nil {
 | 
						defer release()
 | 
				
			||||||
		return nil, err // e.g. ctx closed
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	defer ac.apiSem.Release(1)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	req, err := http.NewRequestWithContext(ctx, http.MethodGet, ac.urlBase+`api/v1/repos/`+url.PathEscape(ac.orgName)+`/`+url.PathEscape(repo)+`/contents/`+dir, nil) // n.b. $dir param not escaped
 | 
						req, err := http.NewRequestWithContext(ctx, http.MethodGet, ac.urlBase+`api/v1/repos/`+url.PathEscape(ac.orgName)+`/`+url.PathEscape(repo)+`/contents/`+dir, nil) // n.b. $dir param not escaped
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
@@ -290,68 +304,3 @@ func (ac *APIClient) topicsForRepo(ctx context.Context, repo string) ([]string,
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	return tr.Topics, nil
 | 
						return tr.Topics, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
// renderMarkdown calls the remote Gitea server's own markdown renderer.
 | 
					 | 
				
			||||||
func (ac *APIClient) RenderMarkdown(ctx context.Context, repoName string, body string) ([]byte, error) {
 | 
					 | 
				
			||||||
	err := ac.apiSem.Acquire(ctx, 1)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err // e.g. ctx closed
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	defer ac.apiSem.Release(1)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	jb, err := json.Marshal(MarkdownRequest{
 | 
					 | 
				
			||||||
		Context: ac.urlBase + url.PathEscape(ac.orgName) + `/` + url.PathEscape(repoName) + `/src/branch/master`,
 | 
					 | 
				
			||||||
		Mode:    "gfm", // magic constant - Github Flavoured Markdown
 | 
					 | 
				
			||||||
		Text:    body,
 | 
					 | 
				
			||||||
	})
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	req, err := http.NewRequestWithContext(ctx, http.MethodPost, ac.urlBase+`api/v1/markdown/`, bytes.NewReader(jb))
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	req.Header.Set(`Content-Type`, `application/json`)
 | 
					 | 
				
			||||||
	req.Header.Set(`Content-Length`, fmt.Sprintf("%d", len(jb)))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	resp, err := http.DefaultClient.Do(req)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	defer resp.Body.Close()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if resp.StatusCode != 200 {
 | 
					 | 
				
			||||||
		return nil, fmt.Errorf("HTTP %d", resp.StatusCode)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return ioutil.ReadAll(resp.Body)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// renderMarkdownRaw calls the remote Gitea server's own markdown renderer.
 | 
					 | 
				
			||||||
func (ac *APIClient) renderMarkdownRaw(ctx context.Context, body []byte) ([]byte, error) {
 | 
					 | 
				
			||||||
	err := ac.apiSem.Acquire(ctx, 1)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err // e.g. ctx closed
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	defer ac.apiSem.Release(1)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	req, err := http.NewRequestWithContext(ctx, http.MethodPost, ac.urlBase+`api/v1/markdown/raw`, bytes.NewReader(body))
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	req.Header.Set(`Content-Type`, `text/plain`)
 | 
					 | 
				
			||||||
	req.Header.Set(`Content-Length`, fmt.Sprintf("%d", len(body)))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	resp, err := http.DefaultClient.Do(req)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	defer resp.Body.Close()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if resp.StatusCode != 200 {
 | 
					 | 
				
			||||||
		return nil, fmt.Errorf("HTTP %d", resp.StatusCode)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return ioutil.ReadAll(resp.Body)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										3
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								go.mod
									
									
									
									
									
								
							@@ -4,5 +4,6 @@ go 1.19
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
require (
 | 
					require (
 | 
				
			||||||
	github.com/BurntSushi/toml v0.3.1
 | 
						github.com/BurntSushi/toml v0.3.1
 | 
				
			||||||
	golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					require github.com/yuin/goldmark v1.7.8 // indirect
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										4
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								go.sum
									
									
									
									
									
								
							@@ -1,4 +1,8 @@
 | 
				
			|||||||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
 | 
					github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
 | 
				
			||||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
 | 
					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=
 | 
				
			||||||
 | 
					github.com/yuin/goldmark v1.7.8 h1:iERMLn0/QJeHFhxSt3p6PeN9mGnvIKSpG9YYorDMnic=
 | 
				
			||||||
 | 
					github.com/yuin/goldmark v1.7.8/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 h1:WXEvlFVvvGxCJLG6REjsT03iWnKLEWinaScsxF2Vm2o=
 | 
				
			||||||
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 | 
					golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										16
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								main.go
									
									
									
									
									
								
							@@ -10,12 +10,14 @@ import (
 | 
				
			|||||||
	"teafolio/gitea"
 | 
						"teafolio/gitea"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/BurntSushi/toml"
 | 
						"github.com/BurntSushi/toml"
 | 
				
			||||||
 | 
						"github.com/yuin/goldmark"
 | 
				
			||||||
 | 
						"github.com/yuin/goldmark/extension"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Config struct {
 | 
					type Config struct {
 | 
				
			||||||
	BindTo string
 | 
						BindTo string
 | 
				
			||||||
	Gitea  struct {
 | 
						Gitea  struct {
 | 
				
			||||||
		URL, Org       string
 | 
							URL, Org, Token string
 | 
				
			||||||
		MaxConnections  int64
 | 
							MaxConnections  int64
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	Redirect map[string]string
 | 
						Redirect map[string]string
 | 
				
			||||||
@@ -30,9 +32,12 @@ type Config struct {
 | 
				
			|||||||
type Application struct {
 | 
					type Application struct {
 | 
				
			||||||
	cfg Config
 | 
						cfg Config
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	rxRepoPage, rxRepoImage *regexp.Regexp
 | 
						rxRepoPage   *regexp.Regexp
 | 
				
			||||||
 | 
						rxRepoImage  *regexp.Regexp
 | 
				
			||||||
 | 
						rxRepoImage2 *regexp.Regexp
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	gitea *gitea.APIClient
 | 
						gitea *gitea.APIClient
 | 
				
			||||||
 | 
						md    goldmark.Markdown
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	reposMut               sync.RWMutex
 | 
						reposMut               sync.RWMutex
 | 
				
			||||||
	reposCache             []gitea.Repo // Sorted by recently-created-first
 | 
						reposCache             []gitea.Repo // Sorted by recently-created-first
 | 
				
			||||||
@@ -44,6 +49,11 @@ func main() {
 | 
				
			|||||||
	app := Application{
 | 
						app := Application{
 | 
				
			||||||
		rxRepoPage:   regexp.MustCompile(`^/([^/]+)/?$`),
 | 
							rxRepoPage:   regexp.MustCompile(`^/([^/]+)/?$`),
 | 
				
			||||||
		rxRepoImage:  regexp.MustCompile(`^/:banner/([^/]+)/?$`),
 | 
							rxRepoImage:  regexp.MustCompile(`^/:banner/([^/]+)/?$`),
 | 
				
			||||||
 | 
							rxRepoImage2: regexp.MustCompile(`^/:repo-img/([0-9]+)/([^/]+)/?$`),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							md: goldmark.New(
 | 
				
			||||||
 | 
								goldmark.WithExtensions(extension.GFM), // required for table rendering support
 | 
				
			||||||
 | 
							),
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	configFile := flag.String(`ConfigFile`, `config.toml`, `Configuration file in TOML format`)
 | 
						configFile := flag.String(`ConfigFile`, `config.toml`, `Configuration file in TOML format`)
 | 
				
			||||||
@@ -55,7 +65,7 @@ func main() {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Create Gitea API client
 | 
						// 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
 | 
						// Sync worker
 | 
				
			||||||
	go app.syncWorker(context.Background())
 | 
						go app.syncWorker(context.Background())
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,7 +4,7 @@ import (
 | 
				
			|||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (this *Application) Bannerpage(w http.ResponseWriter, r *http.Request, repoName string) {
 | 
					func (this *Application) Bannerpage(w http.ResponseWriter, r *http.Request, repoName string, imageIdx int) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	this.reposMut.RLock()
 | 
						this.reposMut.RLock()
 | 
				
			||||||
	defer this.reposMut.RUnlock()
 | 
						defer this.reposMut.RUnlock()
 | 
				
			||||||
@@ -17,12 +17,20 @@ func (this *Application) Bannerpage(w http.ResponseWriter, r *http.Request, repo
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	images := this.reposCache[ridx].Images
 | 
						images := this.reposCache[ridx].Images
 | 
				
			||||||
	if len(images) == 0 {
 | 
						if imageIdx >= len(images) {
 | 
				
			||||||
		w.Header().Set(`Location`, `/static/no_image.png`)
 | 
							w.Header().Set(`Location`, `/static/no_image.png`)
 | 
				
			||||||
		w.WriteHeader(301)
 | 
							w.WriteHeader(301)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	w.Header().Set(`Location`, images[0].RawURL)
 | 
						// Mirror serving
 | 
				
			||||||
	w.WriteHeader(301)
 | 
						// (Direct 301 redirects are no longer available)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						imgData, err := this.gitea.RepoFile(r.Context(), repoName, images[imageIdx].Path)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							http.Error(w, err.Error(), 500)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						w.Write(imgData)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,9 +4,12 @@ import (
 | 
				
			|||||||
	"bytes"
 | 
						"bytes"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"html"
 | 
						"html"
 | 
				
			||||||
 | 
						"log"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"net/url"
 | 
						"net/url"
 | 
				
			||||||
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
						"teafolio/gitea"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (this *Application) Repopage(w http.ResponseWriter, r *http.Request, repoName string) {
 | 
					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`)
 | 
						readme, err := this.gitea.RepoFile(ctx, repoName, `README.md`)
 | 
				
			||||||
	if err != nil {
 | 
						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
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -53,19 +57,27 @@ func (this *Application) Repopage(w http.ResponseWriter, r *http.Request, repoNa
 | 
				
			|||||||
		lines = append([]string{lines[0], lines[1], extraBadgesMd, ""}, lines[2:]...)
 | 
							lines = append([]string{lines[0], lines[1], extraBadgesMd, ""}, lines[2:]...)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	readmeHtml, err := this.gitea.RenderMarkdown(ctx, repoName, strings.Join(lines, "\n"))
 | 
						buff := bytes.Buffer{}
 | 
				
			||||||
 | 
						err = this.md.Convert([]byte(strings.Join(lines, "\n")), &buff)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		this.internalError(w, r, fmt.Errorf("rendering markdown: %w", err))
 | 
							// 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
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						readmeHtml := buff.Bytes()
 | 
				
			||||||
	readmeHtml = []byte(strings.Replace(string(readmeHtml), `%%REPLACEME__BADGE%%`, `<img src="/static/build_success_brightgreen.svg" style="width:90px;height:20px;">`, 1))
 | 
						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)
 | 
						// Use cached image list, since the idx numbers need to match the :repo-img/##
 | 
				
			||||||
	if err != nil {
 | 
						// which only uses a cached lookup
 | 
				
			||||||
		this.internalError(w, r, fmt.Errorf("listing images: %w", err))
 | 
						var images []gitea.ReaddirEntry
 | 
				
			||||||
		return
 | 
						this.reposMut.RLock()
 | 
				
			||||||
 | 
						if cacheIdx, ok := this.reposCacheByName[repoName]; ok {
 | 
				
			||||||
 | 
							images = this.reposCache[cacheIdx].Images
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						this.reposMut.RUnlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// If the Git repository contains a top-level go.mod file, allow vanity imports
 | 
						// If the Git repository contains a top-level go.mod file, allow vanity imports
 | 
				
			||||||
	if goMod, err := this.gitea.RepoFile(ctx, repoName, `go.mod`); err == nil {
 | 
						if goMod, err := this.gitea.RepoFile(ctx, repoName, `go.mod`); err == nil {
 | 
				
			||||||
@@ -97,8 +109,12 @@ func (this *Application) Repopage(w http.ResponseWriter, r *http.Request, repoNa
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		if len(images) > 0 {
 | 
							if len(images) > 0 {
 | 
				
			||||||
			fmt.Fprint(w, `<div class="projimg">`)
 | 
								fmt.Fprint(w, `<div class="projimg">`)
 | 
				
			||||||
			for _, img := range images {
 | 
								for imgIdx, img := range images {
 | 
				
			||||||
				fmt.Fprint(w, `<a href="`+html.EscapeString(img.RawURL)+`"><img alt="" class="thumbimage" src="`+html.EscapeString(img.RawURL)+`" /></a>`)
 | 
									// Direct link
 | 
				
			||||||
 | 
									// fmt.Fprint(w, `<a href="`+html.EscapeString(img.RawURL)+`"><img alt="" class="thumbimage" src="`+html.EscapeString(img.RawURL)+`" /></a>`)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									// Proxied image
 | 
				
			||||||
 | 
									fmt.Fprint(w, `<a href="`+html.EscapeString(img.RawURL)+`"><img alt="" class="thumbimage" src="/:repo-img/`+strconv.Itoa(imgIdx)+`/`+html.EscapeString(repoName)+`" /></a>`)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			fmt.Fprint(w, `</div>`)
 | 
								fmt.Fprint(w, `</div>`)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										36
									
								
								router.go
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								router.go
									
									
									
									
									
								
							@@ -1,13 +1,22 @@
 | 
				
			|||||||
package main
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"embed"
 | 
				
			||||||
	"encoding/base64"
 | 
						"encoding/base64"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"net/url"
 | 
						"net/url"
 | 
				
			||||||
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"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)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (this *Application) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 | 
					func (this *Application) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
	if r.Method == `GET` {
 | 
						if r.Method == `GET` {
 | 
				
			||||||
		if r.URL.Path == `/` {
 | 
							if r.URL.Path == `/` {
 | 
				
			||||||
@@ -21,7 +30,15 @@ func (this *Application) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
			http.Error(w, "not found", 404)
 | 
								http.Error(w, "not found", 404)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		} else if parts := this.rxRepoImage.FindStringSubmatch(r.URL.Path); parts != nil {
 | 
							} else if parts := this.rxRepoImage.FindStringSubmatch(r.URL.Path); parts != nil {
 | 
				
			||||||
			this.Bannerpage(w, r, parts[1])
 | 
								this.Bannerpage(w, r, parts[1], 0) // 0th image
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							} else if parts := this.rxRepoImage2.FindStringSubmatch(r.URL.Path); parts != nil {
 | 
				
			||||||
 | 
								imageIdx, err := strconv.Atoi(parts[1])
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									http.Error(w, "Invalid request", 400)
 | 
				
			||||||
 | 
									return
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								this.Bannerpage(w, r, parts[2], imageIdx) // 0th image
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		} else if parts := this.rxRepoPage.FindStringSubmatch(r.URL.Path); parts != nil {
 | 
							} else if parts := this.rxRepoPage.FindStringSubmatch(r.URL.Path); parts != nil {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -56,8 +73,9 @@ func (this *Application) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
			this.Repopage(w, r, repoName)
 | 
								this.Repopage(w, r, repoName)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		} else if r.URL.Path == `/static/logo.png` {
 | 
							} else if strings.HasPrefix(r.URL.Path, `/static/`) {
 | 
				
			||||||
			if this.cfg.Template.CustomLogoPngBase64 != "" {
 | 
					
 | 
				
			||||||
 | 
								if r.URL.Path == `/static/logo.png` && this.cfg.Template.CustomLogoPngBase64 != "" {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				logoPng, err := base64.StdEncoding.DecodeString(this.cfg.Template.CustomLogoPngBase64)
 | 
									logoPng, err := base64.StdEncoding.DecodeString(this.cfg.Template.CustomLogoPngBase64)
 | 
				
			||||||
				if err != nil {
 | 
									if err != nil {
 | 
				
			||||||
@@ -69,16 +87,12 @@ func (this *Application) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
				w.Header().Set(`Content-Type`, `image/png`)
 | 
									w.Header().Set(`Content-Type`, `image/png`)
 | 
				
			||||||
				w.WriteHeader(200)
 | 
									w.WriteHeader(200)
 | 
				
			||||||
				w.Write(logoPng)
 | 
									w.Write(logoPng)
 | 
				
			||||||
 | 
									return
 | 
				
			||||||
			} else {
 | 
					 | 
				
			||||||
				r.URL.Path = r.URL.Path[8:]
 | 
					 | 
				
			||||||
				http.FileServer(http.Dir(`static`)).ServeHTTP(w, r)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		} else if strings.HasPrefix(r.URL.Path, `/static/`) {
 | 
								// Embedded resource
 | 
				
			||||||
			r.URL.Path = r.URL.Path[8:]
 | 
								// r.URL.Path = r.URL.Path[8:]
 | 
				
			||||||
			http.FileServer(http.Dir(`static`)).ServeHTTP(w, r)
 | 
								this.ServeStatic(w, r)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		} else if r.URL.Query().Get("go-get") == "1" {
 | 
							} 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
 | 
								// This wasn't one of our standard `/repo` paths, but there is the ?go-get=1 parameter
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -28,6 +28,10 @@ h1 a:hover {
 | 
				
			|||||||
h1 {
 | 
					h1 {
 | 
				
			||||||
	margin-top:0;
 | 
						margin-top:0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					pre {
 | 
				
			||||||
 | 
						/* wide codeblocks should scroll */
 | 
				
			||||||
 | 
						overflow-y: auto;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
.code {
 | 
					.code {
 | 
				
			||||||
	background: #F8F8F8;
 | 
						background: #F8F8F8;
 | 
				
			||||||
	font-family:Consolas,monospace;
 | 
						font-family:Consolas,monospace;
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user