49 Commits

Author SHA1 Message Date
749e8bcab0 doc/CHANGELOG: move to separate file, add v1.5.0 changelog 2026-03-03 19:27:35 +13:00
34fdb3f383 improve homepage image rendering on Chrome 2026-03-03 19:24:42 +13:00
26907ae1c8 changelog support 2026-03-03 19:24:36 +13:00
de32b9c587 refactor: move methods repository->util, move template to page_template 2026-03-03 19:14:55 +13:00
d1d51c220c repository: refactor and extract methods 2026-03-03 19:09:28 +13:00
987f605bed router: simplify image URLs 2026-03-03 19:09:24 +13:00
be8f978170 template: add content-security-policy 2026-03-03 18:41:48 +13:00
316b5697e0 dockerfile: build production binary with latest go1.26 2026-03-03 18:41:48 +13:00
869c6ca87e router: replace with go1.22 stdlib mux 2026-03-03 18:41:48 +13:00
ee3e81f880 images: use inline disposition 2026-03-03 18:41:48 +13:00
5c75376910 images: svg images should always be served with SVG content type 2026-03-03 18:41:48 +13:00
fd3cc11693 support embedded image links in main README 2026-03-01 19:55:13 +13:00
add2f4cca5 image proxy: require that content type sniffs as image mime 2026-03-01 19:55:01 +13:00
53728d5472 consider svg images to count as images 2026-03-01 19:54:51 +13:00
9f88643196 image links: clicking the link should also show proxied image 2026-03-01 19:54:37 +13:00
7f5980dc8e increase refresh time 30m->60m 2026-03-01 19:54:21 +13:00
129d5a2a31 doc/README: update for v1.4.2 2025-06-01 17:06:01 +12:00
b15fe13f77 router: clean up some dead code 2025-06-01 17:06:01 +12:00
f256383ac9 css: fix very wide codeblock styling 2025-06-01 17:06:01 +12:00
ecdb8304dc proxy images from gitea to make use of token 2025-06-01 17:05:58 +12:00
9dac5b808e gitea: replace semaphore dependency with simple channel version 2025-06-01 17:02:17 +12:00
3c3098a15c vendor: update goldmark v1.7.0->1.7.8 2025-06-01 17:02:17 +12:00
c42f0a75ef markdown: always use goldmark, never remote rendering 2025-06-01 17:02:13 +12:00
0407a52fb6 doc/README: update for v1.4.1 2024-03-19 19:08:46 +13:00
c3b3e19a42 repo: ensure we render massaged html when in fallback mode 2024-03-19 19:08:12 +13:00
0d806965ab gitea/api: add cooldown to debounce markdown api failures 2024-03-19 19:08:03 +13:00
52166fcf30 doc/README: add changelog for v1.4.0 2024-03-19 18:54:33 +13:00
fda3899a23 markdown: fall back to internal renderer if gitea's is unavailable 2024-03-19 18:52:16 +13:00
1adacc0d49 production: build with go1.22 2024-03-19 18:42:18 +13:00
8de7d565a1 gitea: optionally support authenticated api 2024-03-19 18:42:13 +13:00
1b3a720323 use go1.16+ embed for static assets 2024-03-19 18:42:02 +13:00
eda45221cd repo: on any failure to build repo page, redirect to gitea page 2024-03-19 18:25:42 +13:00
bd04f5c117 doc/README: update for v1.3.1 bugfix 2022-12-31 14:55:21 +13:00
cb16a667cc banner: use reposCacheByName 2022-12-31 14:55:15 +13:00
b84b7bdb4e sync: idx values from reposAlphabeticalOrder do not match reposCache indexes 2022-12-31 14:55:00 +13:00
8321129ac8 sync: populate image cache 2022-12-31 14:33:51 +13:00
45da798791 doc/README: add changelog for v1.3.0 2022-12-31 14:29:52 +13:00
75f5ff35c9 chore: bump dockerfile, go.mod definitions to go1.19 2022-12-31 14:29:45 +13:00
92853a495c sync: cache image URLs for main homepage performance 2022-12-31 14:28:12 +13:00
5430f503e0 sync/reorder: add error cases, remove debug messaging 2022-12-31 14:27:55 +13:00
1e2ad32d54 config: add OverrideOrder option 2022-12-31 14:18:47 +13:00
cb454938cc api: move gitea api to subpackage 2022-12-31 14:06:56 +13:00
45ed36b327 split pages into multiple source files 2022-12-31 13:47:07 +13:00
65b520daa6 doc/changelog: v1.2.1 2021-04-12 13:49:01 +12:00
f7833f8dde also search the root directory for repo images 2021-04-12 13:47:07 +12:00
3c54a6eff4 self-host the build_success_brightgreen.svg image 2021-04-12 13:46:46 +12:00
78c7f82ef8 exclude 'build: success' badge from repos with 'article' topic 2021-04-12 13:32:58 +12:00
4f6814f3da html: add basic table styling 2021-04-12 13:22:24 +12:00
d21998a9fd html: use a free viewport 2021-04-12 13:16:30 +12:00
21 changed files with 1096 additions and 733 deletions

62
CHANGELOG.md Normal file
View File

@@ -0,0 +1,62 @@
# Changelog
2026-03-03 v1.5.0
- Reduce Gitea refresh interval to 60m
- Support SVG images
- Support embedded images in markdown doc
- Add `CHANGELOG.md` support
- Add Content-Security-Policy
- Change image URLs to be clearer and simpler
- Fix broken image links if proxy is used
- Fix pixellated image scaling in Chrome browser
- Use Go1.26, use new net/http router
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
- 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
2021-04-12 v1.2.1
- Exclude 'build:success' tag from article-only repositories, and re-host the image locally
- Also search root repo directory for images
- Enhance mobile viewport and table styling
2020-11-19 v1.2.0
- Cache homepage repositories, sync changes in the background
- Consider the updated time to be the most recent commit, not the Gitea repository metadata update field
- Add extra logging to startup
2020-11-08 v1.1.1
- Fix an issue with newer versions of Gitea that paginate repoistory list responses
- Fix an issue with blocking semaphores for a cancelled network request
2020-05-24 v1.1.0
- Support limiting the number of concurrent API requests to Gitea
- Display total number of projects
- Fix cosmetic issues with page background image, page height, and margins around thumbnails
2020-05-24 v1.0.1
- Remove image dependency from static files
- Fix a cosmetic issue with `h2`/`h3` margins
2020-05-05 v1.0.0
- Initial release

View File

@@ -1,6 +1,6 @@
# Dockerfile for production Teafolio deployments # Dockerfile for production Teafolio deployments
FROM golang:1.14-alpine AS builder FROM golang:1.26-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" ]

View File

@@ -28,26 +28,3 @@ By default, Dokku will proxy HTTP on port 5000.
dokku apps:create teafolio dokku apps:create teafolio
dokku storage:mount teafolio /srv/teafolio-dokku/config.toml:/app/config.toml dokku storage:mount teafolio /srv/teafolio-dokku/config.toml:/app/config.toml
``` ```
## CHANGELOG
2020-11-19 v1.2.0
- Cache homepage repositories, sync changes in the background
- Consider the updated time to be the most recent commit, not the Gitea repository metadata update field
- Add extra logging to startup
2020-11-08 v1.1.1
- Fix an issue with newer versions of Gitea that paginate repoistory list responses
- Fix an issue with blocking semaphores for a cancelled network request
2020-05-24 v1.1.0
- Support limiting the number of concurrent API requests to Gitea
- Display total number of projects
- Fix cosmetic issues with page background image, page height, and margins around thumbnails
2020-05-24 v1.0.1
- Remove image dependency from static files
- Fix a cosmetic issue with `h2`/`h3` margins
2020-05-05 v1.0.0
- Initial release

372
api.go
View File

@@ -1,372 +0,0 @@
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"time"
)
type Repo struct {
Name string `json:"name"`
Description string `json:"description"`
GiteaCreated time.Time `json:"created_at"`
GiteaUpdated time.Time `json:"updated_at"`
newestCommit time.Time
topics []string
}
func (this *Application) populateCommitInfo(ctx context.Context, rr *Repo) {
// The most recent commit will be the head of one of the branches (easy to find)
brs, err := this.branches(ctx, rr.Name)
if err != nil {
log.Printf("loading branches for '%s': %s", rr.Name, err)
rr.newestCommit = rr.GiteaUpdated // best guess
} else {
newestCommit := time.Unix(0, 0) // sentinel
for _, br := range brs {
if br.Commit.Timestamp.After(newestCommit) {
newestCommit = br.Commit.Timestamp
}
}
if !newestCommit.Equal(time.Unix(0, 0)) {
rr.newestCommit = newestCommit // replace it
}
}
}
type ContentsResponse struct {
Content []byte `json:"content"` // Assume base64 "encoding" parameter in Gitea response, and use Go's auto decode
}
type TopicsResponse struct {
Topics []string `json:"topics"`
}
type ReaddirEntry struct {
Name string `json:"name"`
Path string `json:"path"`
Size int `json:"size"`
RawURL string `json:"download_url"`
}
func (rde ReaddirEntry) isImage() bool {
return strings.HasSuffix(rde.Name, `.png`) || strings.HasSuffix(rde.Name, `.jpg`) || strings.HasSuffix(rde.Name, `.jpeg`)
}
type MarkdownRequest struct {
Context string
Mode string
Text string
Wiki bool
}
type BranchCommit struct {
ID string `json:"id"`
Message string `json:"message"`
Timestamp time.Time `json:"timestamp"`
}
type Branch struct {
Name string `json:"name"`
Commit BranchCommit `json:"commit"`
}
type AuthorInfo struct {
Name string `json:"name"`
Email string `json:"email"`
Date time.Time `json:"date"`
}
type CommitListEntryCommit struct {
Message string `json:"message"`
Author AuthorInfo `json:"author"`
Committer AuthorInfo `json:"committer"`
}
type CommitListEntry struct {
ID string `json:"sha"`
Commit CommitListEntryCommit `json:"commit"`
}
func (this *Application) apiRequest(ctx context.Context, endpoint string, target interface{}) error {
err := this.apiSem.Acquire(ctx, 1)
if err != nil {
return err // e.g. ctx closed
}
defer this.apiSem.Release(1)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, this.cfg.Gitea.URL+endpoint, nil)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("HTTP %d", resp.StatusCode)
}
err = json.NewDecoder(resp.Body).Decode(target)
if err != nil {
return err
}
return nil
}
func (this *Application) branches(ctx context.Context, repo string) ([]Branch, error) {
var branches []Branch
err := this.apiRequest(ctx, `api/v1/repos/`+url.PathEscape(this.cfg.Gitea.Org)+`/`+url.PathEscape(repo)+`/branches`, &branches)
if err != nil {
return nil, err // e.g. ctx closed
}
return branches, nil
}
func (this *Application) commitsPage(ctx context.Context, repo, ref string, page, limit int) ([]CommitListEntry, error) {
var ret []CommitListEntry
err := this.apiRequest(ctx, fmt.Sprintf(`api/v1/repos/%s/%s/commits?page=%d&limit=%d`, url.PathEscape(this.cfg.Gitea.Org), url.PathEscape(repo), page, limit), &ret)
if err != nil {
return nil, err // e.g. ctx closed
}
return ret, nil
}
func (this *Application) commits(ctx context.Context, repo, ref string) ([]CommitListEntry, error) {
var ret []CommitListEntry
nextPage := 1 // Counting starts at 1
for {
page, err := this.commitsPage(ctx, repo, ref, nextPage, 300)
if err != nil {
return nil, err
}
if len(page) == 0 && len(ret) > 0 {
break // Found enough already
}
ret = append(ret, page...)
nextPage += 1
}
if len(ret) == 0 {
return nil, fmt.Errorf("no commits found")
}
return ret, nil
}
// reposPage gets a single page of the list of Git repositories in this organisation.
func (this *Application) reposPage(ctx context.Context, page, limit int) ([]Repo, error) {
var repos []Repo
err := this.apiRequest(ctx, `api/v1/orgs/`+url.PathEscape(this.cfg.Gitea.Org)+fmt.Sprintf(`/repos?page=%d&limit=%d`, page, limit), &repos)
if err != nil {
return nil, err
}
return repos, nil
}
// repos gets a list of Git repositories in this organisation. It may have to
// make multiple network requests.
func (this *Application) repos(ctx context.Context) ([]Repo, error) {
// Seems like gitea-1.13.0-rc1 returns 30 results by default, and supports up to a limit of 100
// Make a much larger request
ret := make([]Repo, 0)
nextPage := 1 // Counting starts at 1
for {
page, err := this.reposPage(ctx, nextPage, 300)
if err != nil {
return nil, err
}
if len(page) == 0 && len(ret) > 0 {
break // Found enough already
}
ret = append(ret, page...)
nextPage += 1
}
return ret, nil
}
// repoFile gets a single file from the default branch of the git repository
// Usually the default branch is `master`.
func (this *Application) repoFile(ctx context.Context, repo, filename string) ([]byte, error) {
var cr ContentsResponse
err := this.apiRequest(ctx, `api/v1/repos/`+url.PathEscape(this.cfg.Gitea.Org)+`/`+url.PathEscape(repo)+`/contents/`+url.PathEscape(filename), &cr)
if err != nil {
return nil, err
}
return cr.Content, nil
}
func (this *Application) filesInDirectory(ctx context.Context, repo, dir string) ([]ReaddirEntry, error) {
err := this.apiSem.Acquire(ctx, 1)
if err != nil {
return nil, err // e.g. ctx closed
}
defer this.apiSem.Release(1)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, this.cfg.Gitea.URL+`api/v1/repos/`+url.PathEscape(this.cfg.Gitea.Org)+`/`+url.PathEscape(repo)+`/contents/`+dir, nil) // n.b. $dir param not escaped
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
// "No files found" happens with a HTTP 500/404 error depending on Gitea version. Catch this special case
if resp.StatusCode == 500 || resp.StatusCode == 404 {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if strings.Contains(string(b), `does not exist`) {
return []ReaddirEntry{}, nil // no files found
}
}
return nil, fmt.Errorf("HTTP %d", resp.StatusCode)
}
var ret []ReaddirEntry
err = json.NewDecoder(resp.Body).Decode(&ret)
if err != nil {
return nil, err
}
return ret, nil
}
// imageFilesForRepo finds documentation images for the repository.
// It searches the dist/ and doc/ subdirectories.
func (this *Application) imageFilesForRepo(ctx context.Context, repo string) ([]ReaddirEntry, error) {
ret := []ReaddirEntry{}
for _, dirName := range []string{`dist`, `doc`} {
files, err := this.filesInDirectory(ctx, repo, dirName)
if err != nil {
return nil, fmt.Errorf("readdir(%s): %w", dirName, err)
}
for _, f := range files {
if f.isImage() {
ret = append(ret, f)
}
}
}
return ret, nil
}
func (this *Application) topicsForRepo(ctx context.Context, repo string) ([]string, error) {
var tr TopicsResponse
err := this.apiRequest(ctx, `api/v1/repos/`+url.PathEscape(this.cfg.Gitea.Org)+`/`+url.PathEscape(repo)+`/topics`, &tr)
if err != nil {
return nil, err
}
return tr.Topics, nil
}
// renderMarkdown calls the remote Gitea server's own markdown renderer.
func (this *Application) renderMarkdown(ctx context.Context, repoName string, body string) ([]byte, error) {
err := this.apiSem.Acquire(ctx, 1)
if err != nil {
return nil, err // e.g. ctx closed
}
defer this.apiSem.Release(1)
jb, err := json.Marshal(MarkdownRequest{
Context: this.cfg.Gitea.URL + url.PathEscape(this.cfg.Gitea.Org) + `/` + 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, this.cfg.Gitea.URL+`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 (this *Application) renderMarkdownRaw(ctx context.Context, body []byte) ([]byte, error) {
err := this.apiSem.Acquire(ctx, 1)
if err != nil {
return nil, err // e.g. ctx closed
}
defer this.apiSem.Release(1)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, this.cfg.Gitea.URL+`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)
}

View File

@@ -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]
@@ -18,3 +19,8 @@ HomepageHeaderHTML="""
Teafolio is a web-based portfolio frontend for a Gitea server. Teafolio is a web-based portfolio frontend for a Gitea server.
</p> </p>
""" """
# OverrideOrder allows reordering target repositories (0 for oldest)
# This affects the default "Recent Projects" sort ordering only.
#[OverrideOrder]
#tea=0

306
gitea/apiclient.go Normal file
View File

@@ -0,0 +1,306 @@
package gitea
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"sync/atomic"
"time"
)
type APIClient struct {
urlBase string
orgName string
token string
apiSem chan struct{}
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, token string, maxConnections int64) *APIClient {
if !strings.HasSuffix(urlBase, `/`) {
urlBase += `/`
}
ret := &APIClient{
urlBase: urlBase,
orgName: orgName,
token: token,
}
if maxConnections == 0 { // unlimited
// no-op
} else {
ret.apiSem = make(chan struct{}, maxConnections)
}
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 {
// The most recent commit will be the head of one of the branches (easy to find)
brs, err := ac.Branches(ctx, rr.Name)
if err != nil {
return err
} else {
NewestCommit := time.Unix(0, 0) // sentinel
for _, br := range brs {
if br.Commit.Timestamp.After(NewestCommit) {
NewestCommit = br.Commit.Timestamp
}
}
if !NewestCommit.Equal(time.Unix(0, 0)) {
rr.NewestCommit = NewestCommit // replace it
}
}
return nil
}
func (ac *APIClient) PopulateTopics(ctx context.Context, rr *Repo) error {
t, err := ac.topicsForRepo(ctx, rr.Name)
if err != nil {
return err
}
rr.Topics = t
return nil
}
func (ac *APIClient) PopulateImages(ctx context.Context, rr *Repo) error {
img, err := ac.ImageFilesForRepo(ctx, rr.Name)
if err != nil {
return err
}
rr.Images = img
return nil
}
func (ac *APIClient) apiRequest(ctx context.Context, endpoint string, target interface{}) error {
release := ac.Sem()
defer release()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ac.urlBase+endpoint, nil)
if err != nil {
return err
}
if ac.token != "" {
req.Header.Set(`Authorization`, `token `+ac.token)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("HTTP %d", resp.StatusCode)
}
err = json.NewDecoder(resp.Body).Decode(target)
if err != nil {
return err
}
return nil
}
func (ac *APIClient) Branches(ctx context.Context, repo string) ([]Branch, error) {
var branches []Branch
err := ac.apiRequest(ctx, `api/v1/repos/`+url.PathEscape(ac.orgName)+`/`+url.PathEscape(repo)+`/branches`, &branches)
if err != nil {
return nil, err // e.g. ctx closed
}
return branches, nil
}
func (ac *APIClient) commitsPage(ctx context.Context, repo, ref string, page, limit int) ([]CommitListEntry, error) {
var ret []CommitListEntry
err := ac.apiRequest(ctx, fmt.Sprintf(`api/v1/repos/%s/%s/commits?page=%d&limit=%d`, url.PathEscape(ac.orgName), url.PathEscape(repo), page, limit), &ret)
if err != nil {
return nil, err // e.g. ctx closed
}
return ret, nil
}
func (ac *APIClient) Commits(ctx context.Context, repo, ref string) ([]CommitListEntry, error) {
var ret []CommitListEntry
nextPage := 1 // Counting starts at 1
for {
page, err := ac.commitsPage(ctx, repo, ref, nextPage, 300)
if err != nil {
return nil, err
}
if len(page) == 0 && len(ret) > 0 {
break // Found enough already
}
ret = append(ret, page...)
nextPage += 1
}
if len(ret) == 0 {
return nil, fmt.Errorf("no commits found")
}
return ret, nil
}
// reposPage gets a single page of the list of Git repositories in this organisation.
func (ac *APIClient) reposPage(ctx context.Context, page, limit int) ([]Repo, error) {
var repos []Repo
err := ac.apiRequest(ctx, `api/v1/orgs/`+url.PathEscape(ac.orgName)+fmt.Sprintf(`/repos?page=%d&limit=%d`, page, limit), &repos)
if err != nil {
return nil, err
}
return repos, nil
}
// repos gets a list of Git repositories in this organisation. It may have to
// make multiple network requests.
func (ac *APIClient) Repos(ctx context.Context) ([]Repo, error) {
// Seems like gitea-1.13.0-rc1 returns 30 results by default, and supports up to a limit of 100
// Make a much larger request
ret := make([]Repo, 0)
nextPage := 1 // Counting starts at 1
for {
page, err := ac.reposPage(ctx, nextPage, 300)
if err != nil {
return nil, err
}
if len(page) == 0 && len(ret) > 0 {
break // Found enough already
}
ret = append(ret, page...)
nextPage += 1
}
return ret, nil
}
// repoFile gets a single file from the default branch of the git repository
// Usually the default branch is `master`.
func (ac *APIClient) RepoFile(ctx context.Context, repo, filename string) ([]byte, error) {
var cr ContentsResponse
err := ac.apiRequest(ctx, `api/v1/repos/`+url.PathEscape(ac.orgName)+`/`+url.PathEscape(repo)+`/contents/`+url.PathEscape(filename), &cr)
if err != nil {
return nil, err
}
return cr.Content, nil
}
func (ac *APIClient) filesInDirectory(ctx context.Context, repo, dir string) ([]ReaddirEntry, error) {
release := ac.Sem()
defer release()
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 {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
// "No files found" happens with a HTTP 500/404 error depending on Gitea version. Catch this special case
if resp.StatusCode == 500 || resp.StatusCode == 404 {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if strings.Contains(string(b), `does not exist`) {
return []ReaddirEntry{}, nil // no files found
}
}
return nil, fmt.Errorf("HTTP %d", resp.StatusCode)
}
var ret []ReaddirEntry
err = json.NewDecoder(resp.Body).Decode(&ret)
if err != nil {
return nil, err
}
return ret, nil
}
// ImageFilesForRepo finds documentation images for the repository.
// It searches the top-level directory and the dist/ and doc/ subdirectories.
func (ac *APIClient) ImageFilesForRepo(ctx context.Context, repo string) ([]ReaddirEntry, error) {
ret := []ReaddirEntry{}
for _, dirName := range []string{``, `dist`, `doc`} {
files, err := ac.filesInDirectory(ctx, repo, dirName)
if err != nil {
return nil, fmt.Errorf("readdir(%s): %w", dirName, err)
}
for _, f := range files {
if f.isImage() {
ret = append(ret, f)
}
}
}
return ret, nil
}
func (ac *APIClient) topicsForRepo(ctx context.Context, repo string) ([]string, error) {
var tr TopicsResponse
err := ac.apiRequest(ctx, `api/v1/repos/`+url.PathEscape(ac.orgName)+`/`+url.PathEscape(repo)+`/topics`, &tr)
if err != nil {
return nil, err
}
return tr.Topics, nil
}

77
gitea/types.go Normal file
View File

@@ -0,0 +1,77 @@
package gitea
import (
"strings"
"time"
)
type Repo struct {
Name string `json:"name"`
Description string `json:"description"`
GiteaCreated time.Time `json:"created_at"`
GiteaUpdated time.Time `json:"updated_at"`
// NewestCommit is populated via PopulateCommitInfo().
NewestCommit time.Time
// Topics is populated via PopulateTopics().
Topics []string
// Images are populated via PopulateImages().
Images []ReaddirEntry
}
type ContentsResponse struct {
Content []byte `json:"content"` // Assume base64 "encoding" parameter in Gitea response, and use Go's auto decode
}
type TopicsResponse struct {
Topics []string `json:"topics"`
}
type ReaddirEntry struct {
Name string `json:"name"`
Path string `json:"path"`
Size int `json:"size"`
RawURL string `json:"download_url"`
}
func (rde ReaddirEntry) isImage() bool {
return strings.HasSuffix(rde.Name, `.png`) ||
strings.HasSuffix(rde.Name, `.jpg`) ||
strings.HasSuffix(rde.Name, `.jpeg`) ||
strings.HasSuffix(rde.Name, `.svg`)
}
type MarkdownRequest struct {
Context string
Mode string
Text string
Wiki bool
}
type BranchCommit struct {
ID string `json:"id"`
Message string `json:"message"`
Timestamp time.Time `json:"timestamp"`
}
type Branch struct {
Name string `json:"name"`
Commit BranchCommit `json:"commit"`
}
type AuthorInfo struct {
Name string `json:"name"`
Email string `json:"email"`
Date time.Time `json:"date"`
}
type CommitListEntryCommit struct {
Message string `json:"message"`
Author AuthorInfo `json:"author"`
Committer AuthorInfo `json:"committer"`
}
type CommitListEntry struct {
ID string `json:"sha"`
Commit CommitListEntryCommit `json:"commit"`
}

5
go.mod
View File

@@ -1,8 +1,9 @@
module teafolio module teafolio
go 1.13 go 1.22
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
View File

@@ -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=

38
main.go
View File

@@ -5,19 +5,19 @@ import (
"flag" "flag"
"log" "log"
"net/http" "net/http"
"regexp"
"strings"
"sync" "sync"
"teafolio/gitea"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"golang.org/x/sync/semaphore" "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
Template struct { Template struct {
@@ -25,23 +25,26 @@ type Config struct {
HomepageHeaderHTML string HomepageHeaderHTML string
CustomLogoPngBase64 string CustomLogoPngBase64 string
} }
OverrideOrder map[string]int
} }
type Application struct { type Application struct {
cfg Config cfg Config
rxRepoPage, rxRepoImage *regexp.Regexp gitea *gitea.APIClient
apiSem *semaphore.Weighted md goldmark.Markdown
reposMut sync.RWMutex reposMut sync.RWMutex
reposCache []Repo // Sorted by recently-created-first reposCache []gitea.Repo // Sorted by recently-created-first
reposCacheByName map[string]int
reposAlphabeticalOrder map[string]int reposAlphabeticalOrder map[string]int
} }
func main() { func main() {
app := Application{ app := Application{
rxRepoPage: regexp.MustCompile(`^/([^/]+)/?$`), md: goldmark.New(
rxRepoImage: regexp.MustCompile(`^/:banner/([^/]+)/?$`), 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`)
@@ -52,21 +55,14 @@ func main() {
log.Fatalf("toml.DecodeFile: %s", err.Error()) log.Fatalf("toml.DecodeFile: %s", err.Error())
} }
// Assert Gitea URL always has trailing slash // Create Gitea API client
if !strings.HasSuffix(app.cfg.Gitea.URL, `/`) { app.gitea = gitea.NewAPIClient(app.cfg.Gitea.URL, app.cfg.Gitea.Org, app.cfg.Gitea.Token, app.cfg.Gitea.MaxConnections)
app.cfg.Gitea.URL += `/`
}
// Create semaphore mux := app.createRouter()
if app.cfg.Gitea.MaxConnections == 0 { // unlimited
app.apiSem = semaphore.NewWeighted(99999)
} else {
app.apiSem = semaphore.NewWeighted(app.cfg.Gitea.MaxConnections)
}
// Sync worker // Sync worker
go app.syncWorker(context.Background()) go app.syncWorker(context.Background())
log.Printf("Starting web server on [%s]...", app.cfg.BindTo) log.Printf("Starting web server on [%s]...", app.cfg.BindTo)
log.Fatal(http.ListenAndServe(app.cfg.BindTo, &app)) log.Fatal(http.ListenAndServe(app.cfg.BindTo, mux))
} }

45
page_changelog.go Normal file
View File

@@ -0,0 +1,45 @@
package main
import (
"fmt"
"html"
"log"
"net/http"
"net/url"
)
const (
ChangelogFilename = `CHANGELOG.md`
)
func (this *Application) ChangelogPage(w http.ResponseWriter, r *http.Request, repoName string) {
ctx := r.Context()
repoURL := this.repoURL(repoName)
changelog, err := this.gitea.RepoFile(ctx, repoName, ChangelogFilename)
if err != nil {
log.Printf("%s %s: loading changelog %q: %s", r.Method, r.URL.Path, ChangelogFilename, err)
http.Redirect(w, r, repoURL, http.StatusTemporaryRedirect)
return
}
// Convert to markdown
changelogHtml, _, err := this.renderMarkdown(repoName, changelog)
if err != nil {
log.Printf("%s %s: %s", r.Method, r.URL.Path, fmt.Errorf("rendering markdown: %w", err))
http.Redirect(w, r, repoURL, http.StatusTemporaryRedirect)
return
}
// Ready for template
this.Templatepage(w, r, repoName, "", func() {
fmt.Fprint(w, `<div class="projinfo"><div class="projbody">`)
fmt.Fprint(w, `<h2><a href="`+html.EscapeString(`/`+url.PathEscape(repoName))+`">`+html.EscapeString(repoName)+`</a> &gt; Changelog</h2>`)
fmt.Fprint(w, changelogHtml)
fmt.Fprint(w, `</div>`)
fmt.Fprint(w, `<div style="clear:both;"></div>`)
fmt.Fprint(w, `</div>`) // projbody
})
}

90
page_home.go Normal file
View File

@@ -0,0 +1,90 @@
package main
import (
"fmt"
"html"
"net/http"
"net/url"
"strings"
)
func (this *Application) Homepage(w http.ResponseWriter, r *http.Request) {
this.reposMut.RLock()
defer this.reposMut.RUnlock()
if len(this.reposCache) == 0 {
// We haven't loaded the repositories from Gitea yet
this.Delay(w, r)
return
}
// Ready for template
this.Templatepage(w, r, "", "", func() {
fmt.Fprint(w, `
`+this.cfg.Template.HomepageHeaderHTML+`
<select id="sortorder" style="float:right;">
<option value="data-sort-al">Alphabetical</option>
<option value="data-sort-ls">Lifespan</option>
<option value="data-sort-ct" selected>Recent projects</option>
<option value="data-sort-mt">Recent updates</option>
</select>
<h2>Projects <small>(`+fmt.Sprintf("%d", len(this.reposCache))+`)</small></h2>
<table id="projtable-main" class="projtable">
`)
for repoIdx, repo := range this.reposCache {
pageHref := html.EscapeString(`/` + url.PathEscape(repo.Name))
normalisedDesc := repo.Description
normalisedDesc = strings.TrimRight(repo.Description, `.`)
if len(normalisedDesc) > 0 {
// Lowercase the first letter of the description, unless it starts with an acronym (all letters uppercase first word) or CamelCase word
firstWord := strings.SplitN(normalisedDesc, " ", 2)[0]
isAcronymOrCamelCase := len(firstWord) > 1 && (firstWord[1:] != strings.ToLower(firstWord[1:]))
if !(isAcronymOrCamelCase || firstWord == `Go`) {
normalisedDesc = strings.ToLower(normalisedDesc[0:1]) + normalisedDesc[1:]
}
// Add leading `<COMMA><SPACE>` to separate from the repo title
normalisedDesc = `, ` + normalisedDesc
}
rowClass := ""
for _, topic := range repo.Topics {
rowClass += `taggedWith-` + topic + ` `
}
fmt.Fprint(w, `
<tr
class="`+html.EscapeString(rowClass)+`"
data-sort-al="`+fmt.Sprintf("-%d", this.reposAlphabeticalOrder[repo.Name])+`"
data-sort-ls="`+fmt.Sprintf("%.0f", repo.NewestCommit.Sub(repo.GiteaCreated).Seconds())+`"
data-sort-ct="`+fmt.Sprintf("-%d", repoIdx)+`"
data-sort-mt="`+fmt.Sprintf("%d", repo.NewestCommit.Unix())+`"
>
<td>
<a href="`+pageHref+`"><img class="homeimage" loading="lazy" src="`+html.EscapeString(`/`+url.PathEscape(repo.Name)+`/banner`)+`"></div></a>
</td>
<td>
<strong>`+html.EscapeString(repo.Name)+`</strong>`+html.EscapeString(normalisedDesc)+`
<a href="`+pageHref+`" class="article-read-more">more...</a>
<br>
<small>
`)
for _, topic := range repo.Topics {
fmt.Fprint(w, `<a class="tag tag-link" data-tag="`+html.EscapeString(topic)+`">`+html.EscapeString(topic)+`</a> `)
}
fmt.Fprint(w, `
</small>
</td>
</tr>
`)
}
fmt.Fprint(w, `
</table>
`)
})
}

53
page_imageredir.go Normal file
View File

@@ -0,0 +1,53 @@
package main
import (
"bytes"
"net/http"
"strings"
)
func (this *Application) Bannerpage(w http.ResponseWriter, r *http.Request, repoName string, imageIdx int) {
this.reposMut.RLock()
defer this.reposMut.RUnlock()
ridx, ok := this.reposCacheByName[repoName]
if !ok {
http.Redirect(w, r, `/static/no_image.png`, 301)
return
}
images := this.reposCache[ridx].Images
if imageIdx >= len(images) {
http.Redirect(w, r, `/static/no_image.png`, 301)
return
}
// Mirror serving
// (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
}
// Go will automatically add a Content-Type header, but, sniff it ourselves
// to ensure it's really an image and not arbitrary data
// This is important for SVG that might embed JS
contentType := http.DetectContentType(imgData)
if strings.HasPrefix(contentType, `text/xml`) && bytes.Contains(imgData, []byte(`<svg`)) {
contentType = `image/svg+xml`
}
if !strings.HasPrefix(contentType, `image/`) {
http.Error(w, "not an image", 500)
return
}
w.Header().Set(`Content-Type`, contentType)
w.Header().Set(`Content-Disposition`, `inline`)
w.WriteHeader(200)
w.Write(imgData)
}

102
page_repository.go Normal file
View File

@@ -0,0 +1,102 @@
package main
import (
"bytes"
"fmt"
"html"
"log"
"net/http"
"net/url"
"strconv"
"strings"
)
func (this *Application) Repopage(w http.ResponseWriter, r *http.Request, repoName string) {
ctx := r.Context()
repoURL := this.repoURL(repoName)
extraHead := ""
readme, err := this.gitea.RepoFile(ctx, repoName, `README.md`)
if err != nil {
log.Printf("%s %s: %s", r.Method, r.URL.Path, err)
http.Redirect(w, r, repoURL, http.StatusTemporaryRedirect)
return
}
// Inject more badges to 3rd line; or, create badges on 3rd line if there are none already
// We add some extra badges based on special text entries
extraBadgesMd := ``
if !this.repoHasTag(repoName, "article") {
extraBadgesMd += ` ![](/static/build_success_brightgreen.svg)`
}
extraBadgesMd += ` [![](https://img.shields.io/badge/vcs-git-green?logo=git)](` + repoURL + `)`
lines := strings.Split(string(readme), "\n")
if len(lines) >= 3 && strings.Contains(lines[2], `shields.io`) {
lines[2] += ` ` + extraBadgesMd
} else {
// Push other lines down
lines = append([]string{lines[0], lines[1], extraBadgesMd, ""}, lines[2:]...)
}
if _, err := this.gitea.RepoFile(ctx, repoName, ChangelogFilename); err == nil {
// Changelog exists
// Add link at footer
lines = append(lines,
"",
"## Changelog",
"",
`[➡️ View changelog](/`+url.PathEscape(repoName)+`/changelog)`,
)
}
// Convert to markdown
readmeHtml, images, err := this.renderMarkdown(repoName, []byte(strings.Join(lines, "\n")))
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
}
// 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 {
// Check the first line should be `module MODULENAME\n`
firstLine := bytes.SplitN(goMod, []byte("\n"), 2)[0]
if bytes.HasPrefix(firstLine, []byte("module ")) {
moduleName := firstLine[7:]
extraHead = `<meta name="go-import" content="` + html.EscapeString(string(moduleName)) + ` git ` + repoURL + `.git">`
}
}
// Ready for template
this.Templatepage(w, r, repoName, extraHead, func() {
projBodyclass := `projbody`
if len(images) > 0 {
projBodyclass += ` projbody_halfw`
}
fmt.Fprint(w, `<div class="projinfo"><div class="`+projBodyclass+`">`)
fmt.Fprint(w, readmeHtml)
fmt.Fprint(w, `</div>`)
if len(images) > 0 {
fmt.Fprint(w, `<div class="projimg">`)
for imgIdx, _ := range images {
// imageUrl := img.RawURL // Direct link
imageUrl := `/` + url.PathEscape(repoName) + `/img/` + strconv.Itoa(imgIdx) // Proxied image
fmt.Fprint(w, `<a href="`+html.EscapeString(imageUrl)+`"><img alt="" class="thumbimage" src="`+html.EscapeString(imageUrl)+`" /></a>`)
}
fmt.Fprint(w, `</div>`)
}
fmt.Fprint(w, `<div style="clear:both;"></div>`)
fmt.Fprint(w, `</div>`) // projbody
})
}

45
page_template.go Normal file
View File

@@ -0,0 +1,45 @@
package main
import (
"fmt"
"html"
"net/http"
)
func (this *Application) Templatepage(w http.ResponseWriter, r *http.Request, pageDesc, extraHead string, cb func()) {
pageTitle := this.cfg.Template.AppName
if pageDesc != "" {
pageTitle = pageDesc + ` | ` + pageTitle
}
w.Header().Set(`Content-Type`, `text/html; charset=UTF-8`)
w.WriteHeader(200)
fmt.Fprint(w, `<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'self' ; style-src 'unsafe-inline' 'self' ; img-src data: * ;">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>`+html.EscapeString(pageTitle)+`</title>
`+extraHead+`
<link rel="shortcut icon" href="/static/logo.png" type="image/png">
<link rel="apple-touch-icon" href="/static/logo.png" type="image/png">
<link type="text/css" rel="stylesheet" href="/static/style.css">
</head>
<body>
<div id="container">
<div id="content">
<h1><a href="/"><div id="logo"></div>`+html.EscapeString(this.cfg.Template.AppName)+`</a></h1>
`)
cb()
fmt.Fprint(w, `
</body>
<script type="text/javascript" src="/static/site.js"></script>
</html>
`)
}

86
page_util.go Normal file
View File

@@ -0,0 +1,86 @@
package main
import (
"fmt"
"html"
"log"
"net/http"
"net/url"
"slices"
"strconv"
"strings"
"teafolio/gitea"
)
func (this *Application) renderMarkdown(repoName string, markdown []byte) (string, []gitea.ReaddirEntry, error) {
rendered := strings.Builder{}
err := this.md.Convert(markdown, &rendered)
if err != nil {
return "", nil, err
}
readmeHtml := rendered.String()
// Replace image embed links
var images []gitea.ReaddirEntry
this.reposMut.RLock()
if cacheIdx, ok := this.reposCacheByName[repoName]; ok {
images = this.reposCache[cacheIdx].Images
}
this.reposMut.RUnlock()
// Replace simple image embeds with our version
for imgIdx, img := range images {
readmeHtml = strings.ReplaceAll(
readmeHtml,
`<img src="`+html.EscapeString(img.Path)+`"`,
`<img src="`+html.EscapeString(`/`+url.PathEscape(repoName)+`/img/`+strconv.Itoa(imgIdx))+`"`, // Proxied image
)
}
// De-escalate all headers in rendered markdown to match our style
readmeHtml = strings.NewReplacer(
`<h1`, `<h2`,
`<h2`, `<h3`,
`<h3`, `<h4`,
`</h1>`, `</h2>`,
`</h2>`, `</h3>`,
`</h3>`, `</h4>`,
).Replace(readmeHtml)
return readmeHtml, images, nil
}
func (this *Application) repoHasTag(repoName string, searchTag string) bool {
this.reposMut.RLock()
defer this.reposMut.RUnlock()
repoId, ok := this.reposCacheByName[repoName]
if !ok {
return false
}
rr := this.reposCache[repoId]
return slices.Contains(rr.Topics, searchTag)
}
func (this *Application) repoURL(repoName string) string {
return this.cfg.Gitea.URL + url.PathEscape(this.cfg.Gitea.Org) + `/` + url.PathEscape(repoName)
}
func (this *Application) internalError(w http.ResponseWriter, r *http.Request, err error) {
log.Printf("%s %s: %s", r.Method, r.URL.Path, err)
http.Error(w, "An internal error occurred.", 500)
}
func (this *Application) Delay(w http.ResponseWriter, r *http.Request) {
this.Templatepage(w, r, "Loading...", "", func() {
fmt.Fprintf(w, `
<h2>Loading, please wait...</h2>
<meta http-equiv="refresh" content="5">
`)
})
}

240
pages.go
View File

@@ -1,240 +0,0 @@
package main
import (
"bytes"
"fmt"
"html"
"log"
"net/http"
"net/url"
"strings"
)
func (this *Application) Templatepage(w http.ResponseWriter, r *http.Request, pageDesc, extraHead string, cb func()) {
pageTitle := this.cfg.Template.AppName
if pageDesc != "" {
pageTitle = pageDesc + ` | ` + pageTitle
}
w.Header().Set(`Content-Type`, `text/html; charset=UTF-8`)
w.WriteHeader(200)
fmt.Fprint(w, `<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=960">
<title>`+html.EscapeString(pageTitle)+`</title>
`+extraHead+`
<link rel="shortcut icon" href="/static/logo.png" type="image/png">
<link rel="apple-touch-icon" href="/static/logo.png" type="image/png">
<link type="text/css" rel="stylesheet" href="/static/style.css">
</head>
<body>
<div id="container">
<div id="content">
<h1><a href="/"><div id="logo"></div>`+html.EscapeString(this.cfg.Template.AppName)+`</a></h1>
`)
cb()
fmt.Fprint(w, `
</body>
<script type="text/javascript" src="/static/site.js"></script>
</html>
`)
}
func (this *Application) internalError(w http.ResponseWriter, r *http.Request, err error) {
log.Printf("%s %s: %s", r.Method, r.URL.Path, err)
http.Error(w, "An internal error occurred.", 500)
}
func (this *Application) Delay(w http.ResponseWriter, r *http.Request) {
this.Templatepage(w, r, "Loading...", "", func() {
fmt.Fprintf(w, `
<h2>Loading, please wait...</h2>
<meta http-equiv="refresh" content="5">
`)
})
}
func (this *Application) Homepage(w http.ResponseWriter, r *http.Request) {
this.reposMut.RLock()
defer this.reposMut.RUnlock()
if len(this.reposCache) == 0 {
// We haven't loaded the repositories from Gitea yet
this.Delay(w, r)
return
}
// Ready for template
this.Templatepage(w, r, "", "", func() {
fmt.Fprint(w, `
`+this.cfg.Template.HomepageHeaderHTML+`
<select id="sortorder" style="float:right;">
<option value="data-sort-al">Alphabetical</option>
<option value="data-sort-ls">Lifespan</option>
<option value="data-sort-ct" selected>Recent projects</option>
<option value="data-sort-mt">Recent updates</option>
</select>
<h2>Projects <small>(`+fmt.Sprintf("%d", len(this.reposCache))+`)</small></h2>
<table id="projtable-main" class="projtable">
`)
for _, repo := range this.reposCache {
pageHref := html.EscapeString(`/` + url.PathEscape(repo.Name))
normalisedDesc := repo.Description
normalisedDesc = strings.TrimRight(repo.Description, `.`)
if len(normalisedDesc) > 0 {
// Lowercase the first letter of the description, unless it starts with an acronym (all letters uppercase first word) or CamelCase word
firstWord := strings.SplitN(normalisedDesc, " ", 2)[0]
isAcronymOrCamelCase := len(firstWord) > 1 && (firstWord[1:] != strings.ToLower(firstWord[1:]))
if !(isAcronymOrCamelCase || firstWord == `Go`) {
normalisedDesc = strings.ToLower(normalisedDesc[0:1]) + normalisedDesc[1:]
}
// Add leading `<COMMA><SPACE>` to separate from the repo title
normalisedDesc = `, ` + normalisedDesc
}
rowClass := ""
for _, topic := range repo.topics {
rowClass += `taggedWith-` + topic + ` `
}
fmt.Fprint(w, `
<tr
class="`+html.EscapeString(rowClass)+`"
data-sort-al="`+fmt.Sprintf("-%d", this.reposAlphabeticalOrder[repo.Name])+`"
data-sort-ls="`+fmt.Sprintf("%.0f", repo.newestCommit.Sub(repo.GiteaCreated).Seconds())+`"
data-sort-ct="`+fmt.Sprintf("%d", repo.GiteaCreated.Unix())+`"
data-sort-mt="`+fmt.Sprintf("%d", repo.newestCommit.Unix())+`"
>
<td>
<a href="`+pageHref+`"><img class="homeimage" loading="lazy" src="`+html.EscapeString(`/:banner/`+url.PathEscape(repo.Name))+`"></div></a>
</td>
<td>
<strong>`+html.EscapeString(repo.Name)+`</strong>`+html.EscapeString(normalisedDesc)+`
<a href="`+pageHref+`" class="article-read-more">more...</a>
<br>
<small>
`)
for _, topic := range repo.topics {
fmt.Fprint(w, `<a class="tag tag-link" data-tag="`+html.EscapeString(topic)+`">`+html.EscapeString(topic)+`</a> `)
}
fmt.Fprint(w, `
</small>
</td>
</tr>
`)
}
fmt.Fprint(w, `
</table>
`)
})
}
func (this *Application) Bannerpage(w http.ResponseWriter, r *http.Request, repoName string) {
ctx := r.Context()
images, err := this.imageFilesForRepo(ctx, repoName)
if err != nil {
this.internalError(w, r, fmt.Errorf("listing images: %w", err))
return
}
if len(images) == 0 {
w.Header().Set(`Location`, `/static/no_image.png`)
w.WriteHeader(301)
return
}
w.Header().Set(`Location`, images[0].RawURL)
w.WriteHeader(301)
}
func (this *Application) Repopage(w http.ResponseWriter, r *http.Request, repoName string) {
ctx := r.Context()
repoURL := this.cfg.Gitea.URL + url.PathEscape(this.cfg.Gitea.Org) + `/` + url.PathEscape(repoName)
extraHead := ""
readme, err := this.repoFile(ctx, repoName, `README.md`)
if err != nil {
this.internalError(w, r, fmt.Errorf("loading README.md: %w", err))
return
}
lines := strings.Split(string(readme), "\n")
// We add some extra badges based on special text entries
extraBadgesMd := ` ![](https://img.shields.io/badge/build-success-brightgreen)`
extraBadgesMd += ` [![](https://img.shields.io/badge/vcs-git-green?logo=git)](` + repoURL + `)`
// Inject more badges to 3rd line; or, create badges on 3rd line if there are none already
if len(lines) >= 3 && strings.Contains(lines[2], `shields.io`) {
lines[2] += ` ` + extraBadgesMd
} else {
// Push other lines down
lines = append([]string{lines[0], lines[1], extraBadgesMd, ""}, lines[2:]...)
}
readmeHtml, err := this.renderMarkdown(ctx, repoName, strings.Join(lines, "\n"))
if err != nil {
this.internalError(w, r, fmt.Errorf("rendering markdown: %w", err))
return
}
images, err := this.imageFilesForRepo(ctx, repoName)
if err != nil {
this.internalError(w, r, fmt.Errorf("listing images: %w", err))
return
}
// If the Git repository contains a top-level go.mod file, allow vanity imports
if goMod, err := this.repoFile(ctx, repoName, `go.mod`); err == nil {
// Check the first line should be `module MODULENAME\n`
firstLine := bytes.SplitN(goMod, []byte("\n"), 2)[0]
if bytes.HasPrefix(firstLine, []byte("module ")) {
moduleName := firstLine[7:]
extraHead = `<meta name="go-import" content="` + html.EscapeString(string(moduleName)) + ` git ` + repoURL + `.git">`
}
}
// De-escalate all headers in rendered markdown to match our style
repl := strings.NewReplacer(`<h1`, `<h2`, `<h2`, `<h3`, `<h3`, `<h4`,
`</h1>`, `</h2>`, `</h2>`, `</h3>`, `</h3>`, `</h4>`)
// Ready for template
this.Templatepage(w, r, repoName, extraHead, func() {
projBodyclass := `projbody`
if len(images) > 0 {
projBodyclass += ` projbody_halfw`
}
fmt.Fprint(w, `<div class="projinfo"><div class="`+projBodyclass+`">`)
repl.WriteString(w, string(readmeHtml))
fmt.Fprint(w, `</div>`)
if len(images) > 0 {
fmt.Fprint(w, `<div class="projimg">`)
for _, img := range images {
fmt.Fprint(w, `<a href="`+html.EscapeString(img.RawURL)+`"><img alt="" class="thumbimage" src="`+html.EscapeString(img.RawURL)+`" /></a>`)
}
fmt.Fprint(w, `</div>`)
}
fmt.Fprint(w, `<div style="clear:both;"></div>`)
fmt.Fprint(w, `</div>`) // projbody
})
}

143
router.go
View File

@@ -1,84 +1,95 @@
package main package main
import ( import (
"embed"
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"net/http" "net/http"
"net/url" "net/url"
"strconv"
"strings" "strings"
) )
func (this *Application) ServeHTTP(w http.ResponseWriter, r *http.Request) { //go:embed static/*
if r.Method == `GET` { 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) createRouter() *http.ServeMux {
mux := http.NewServeMux()
// No trailing slash = not a recursive match
mux.HandleFunc(`GET /{project}`, func(w http.ResponseWriter, r *http.Request) {
repoName := r.PathValue("project")
// Support /repo.html URIs for backward compatibility
if strings.HasSuffix(repoName, `.html`) {
targetUrl := `/` + url.PathEscape(strings.TrimSuffix(repoName, `.html`))
http.Redirect(w, r, targetUrl, 301)
return
}
// Maybe it's a redirected project (alternative name)
if rename, ok := this.cfg.Redirect[repoName]; ok {
http.Redirect(w, r, `/`+url.PathEscape(rename), 301)
return
}
this.Repopage(w, r, repoName)
})
mux.HandleFunc(`GET /{project}/changelog`, func(w http.ResponseWriter, r *http.Request) {
this.ChangelogPage(w, r, r.PathValue("project"))
})
mux.HandleFunc(`GET /{project}/banner`, func(w http.ResponseWriter, r *http.Request) {
this.Bannerpage(w, r, r.PathValue("project"), 0) // 0th image
})
mux.HandleFunc(`GET /{project}/img/{id}`, func(w http.ResponseWriter, r *http.Request) {
imageIdx, err := strconv.Atoi(r.PathValue("id"))
if err != nil {
http.Error(w, "Invalid request", 400)
return
}
this.Bannerpage(w, r, r.PathValue("project"), imageIdx) // 0th image
})
mux.HandleFunc(`GET /favicon.ico`, func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, `/static/logo.png`, 301)
})
// Wildcard route (ends in /)
mux.HandleFunc(`GET /`, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == `/` { if r.URL.Path == `/` {
// homepage
this.Homepage(w, r) this.Homepage(w, r)
} else if r.URL.Path == `/favicon.ico` { } else if r.URL.Path == `/static/logo.png` && this.cfg.Template.CustomLogoPngBase64 != "" {
w.Header().Set(`Location`, `/static/logo.png`)
w.WriteHeader(301)
} else if r.URL.Path == `/robots.txt` { logoPng, err := base64.StdEncoding.DecodeString(this.cfg.Template.CustomLogoPngBase64)
http.Error(w, "not found", 404)
} else if parts := this.rxRepoImage.FindStringSubmatch(r.URL.Path); parts != nil {
this.Bannerpage(w, r, parts[1])
} else if parts := this.rxRepoPage.FindStringSubmatch(r.URL.Path); parts != nil {
// Support /repo.html URIs for backward compatibility
if strings.HasSuffix(parts[1], `.html`) {
w.Header().Set(`Location`, r.URL.Path[0:len(r.URL.Path)-5])
w.WriteHeader(301)
return
}
// The regexp supports an optional trailing slash
// Redirect to canonical no-trailing-slash
if strings.HasSuffix(r.URL.Path, `/`) {
w.Header().Set(`Location`, `/`+parts[1]) // n.b. parts[1] isn't urldecoded yet
w.WriteHeader(301)
return
}
// Proper decoding of special characters in repo path component
repoName, err := url.PathUnescape(parts[1])
if err != nil { if err != nil {
http.Error(w, "malformed url encoding in repository name", 400) this.internalError(w, r, fmt.Errorf("parsing base64 logo: %w", err))
return return
} }
// Maybe it's a redirected project (alternative name) w.Header().Set(`Content-Length`, fmt.Sprintf("%d", len(logoPng)))
if rename, ok := this.cfg.Redirect[repoName]; ok { w.Header().Set(`Content-Type`, `image/png`)
w.Header().Set(`Location`, `/`+url.PathEscape(rename)) w.WriteHeader(200)
w.WriteHeader(301) w.Write(logoPng)
return return
}
this.Repopage(w, r, repoName)
} else if r.URL.Path == `/static/logo.png` {
if this.cfg.Template.CustomLogoPngBase64 != "" {
logoPng, err := base64.StdEncoding.DecodeString(this.cfg.Template.CustomLogoPngBase64)
if err != nil {
this.internalError(w, r, fmt.Errorf("parsing base64 logo: %w", err))
return
}
w.Header().Set(`Content-Length`, fmt.Sprintf("%d", len(logoPng)))
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)
}
} else if strings.HasPrefix(r.URL.Path, `/static/`) { } else if strings.HasPrefix(r.URL.Path, `/static/`) {
r.URL.Path = r.URL.Path[8:] // Embedded resource
http.FileServer(http.Dir(`static`)).ServeHTTP(w, r) // Can't use a regular mux route for this, otherwise routes like
// e.g. `/static/changelog` would conflict with an actual repo
// project named 'static'
this.ServeStatic(w, r) // knows to look after /static/ prefix
} 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
@@ -89,16 +100,14 @@ func (this *Application) ServeHTTP(w http.ResponseWriter, r *http.Request) {
slashParts := strings.SplitN(r.URL.Path, `/`, 3) // len === 3 is guaranteed from earlier if cases slashParts := strings.SplitN(r.URL.Path, `/`, 3) // len === 3 is guaranteed from earlier if cases
w.Header().Set(`Location`, `/`+slashParts[1]) http.Redirect(w, r, `/`+slashParts[1], 301)
w.WriteHeader(301)
return
} else { } else {
// 404
http.Error(w, "not found", 404) http.Error(w, "not found", 404)
} }
})
} else { return mux
http.Error(w, "invalid method", 400)
}
} }

View File

@@ -0,0 +1,2 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="90" height="20" role="img" aria-label="build: success"><title>build: success</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="90" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="37" height="20" fill="#555"/><rect x="37" width="53" height="20" fill="#4c1"/><rect width="90" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="195" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="270">build</text><text x="195" y="140" transform="scale(.1)" fill="#fff" textLength="270">build</text><text aria-hidden="true" x="625" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="430">success</text><text x="625" y="140" transform="scale(.1)" fill="#fff" textLength="430">success</text></g></svg>

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@@ -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;
@@ -137,6 +141,9 @@ body {
} }
.projbody { .projbody {
} }
.projbody img {
max-width: 100%;
}
.projbody_halfw { .projbody_halfw {
float:left; float:left;
width: 860px; /* 740px full - 60px rhs column - 2px border */ width: 860px; /* 740px full - 60px rhs column - 2px border */
@@ -161,6 +168,25 @@ img[src*="shields.io"] {
/* */ /* */
.projbody table {
width: 100%;
}
.projbody table {
border-collapse: collapse;
}
.projbody table td, .projbody table th {
border: 1px solid #eee;
padding: 4px;
}
.projbody tr:hover td {
background: #f8f8f8;
}
/* */
@media screen and (max-width:960px) { @media screen and (max-width:960px) {
#container { #container {
@@ -196,6 +222,7 @@ img[src*="shields.io"] {
height:32px; height:32px;
object-fit: cover; object-fit: cover;
overflow-clip-margin: unset; /* higher quality image resampling on Chrome */
} }
.thumbimage { .thumbimage {

100
sync.go
View File

@@ -2,15 +2,81 @@ package main
import ( import (
"context" "context"
"fmt"
"log" "log"
"sort" "sort"
"teafolio/gitea"
"time" "time"
) )
// rearrangeOrder applies rearrangements from the OverrideOrder configuration.
func (this *Application) rearrangeOrder(repos []gitea.Repo) ([]gitea.Repo, error) {
if len(this.cfg.OverrideOrder) == 0 {
return repos, nil // nothing to do
}
// Collect pre-existing positions for repos
reposByName := make(map[string]gitea.Repo, len(repos))
for _, repo := range repos {
reposByName[repo.Name] = repo
}
// Sort target insertion positions by lowest-first
type insertionPosition struct {
pos int
repoName string
}
insertAt := make([]insertionPosition, 0, len(this.cfg.OverrideOrder))
for rn, rpos := range this.cfg.OverrideOrder {
insertAt = append(insertAt, insertionPosition{len(repos) - rpos - 1, rn})
}
sort.Slice(insertAt, func(i, j int) bool {
return insertAt[i].pos < insertAt[j].pos
})
// Walking-insertion loop
ret := make([]gitea.Repo, 0, len(repos))
nextRepo := 0
nextOverride := 0
for {
if nextOverride < len(insertAt) && insertAt[nextOverride].pos == len(ret) {
// We have an override to insert at this position
ret = append(ret, reposByName[insertAt[nextOverride].repoName])
nextOverride++
} else if nextRepo < len(repos) {
// There are still other repos to insert generally
if _, ok := this.cfg.OverrideOrder[repos[nextRepo].Name]; ok {
// This repo has an overridden position. Don't insert it generally here
nextRepo++
} else {
// This repo does not have an overriden position. Here is fine
ret = append(ret, repos[nextRepo])
nextRepo++
}
} else {
// There are no more overrides to insert and there are no remaining
// non-override repos
// That means we're done
break
}
}
if len(ret) != len(repos) {
return nil, fmt.Errorf("Failed to apply OverrideOrder (got %d of %d repositories)", len(ret), len(repos))
}
return ret, nil
}
func (this *Application) sync(ctx context.Context) (bool, error) { func (this *Application) sync(ctx context.Context) (bool, error) {
// List repositories on Gitea // List repositories on Gitea
repos, err := this.repos(ctx) repos, err := this.gitea.Repos(ctx)
if err != nil { if err != nil {
return false, err return false, err
} }
@@ -28,7 +94,7 @@ func (this *Application) sync(ctx context.Context) (bool, error) {
} }
for i, rr := range repos { 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 // Already exists in cache with same Gitea update time
// Copy timestamps // Copy timestamps
repos[i] = this.reposCache[idx] repos[i] = this.reposCache[idx]
@@ -39,11 +105,21 @@ func (this *Application) sync(ctx context.Context) (bool, error) {
anyChanges = true anyChanges = true
// Refresh timestamps // Refresh timestamps
this.populateCommitInfo(ctx, &rr) err := this.gitea.PopulateCommitInfo(ctx, &rr)
if err != nil {
log.Printf("loading branches for '%s': %s", rr.Name, err)
rr.NewestCommit = rr.GiteaUpdated // best guess
}
// Refresh topics // Refresh topics
if t, err := this.topicsForRepo(ctx, rr.Name); err == nil { err = this.gitea.PopulateTopics(ctx, &rr)
rr.topics = t if err != nil {
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 // Save
@@ -73,10 +149,22 @@ func (this *Application) sync(ctx context.Context) (bool, error) {
sort.Slice(repos, func(i, j int) bool { sort.Slice(repos, func(i, j int) bool {
return repos[i].GiteaCreated.After(repos[j].GiteaCreated) return repos[i].GiteaCreated.After(repos[j].GiteaCreated)
}) })
reordered, err := this.rearrangeOrder(repos)
if err != nil {
log.Printf("Reorder failure: %s", err.Error())
} else {
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 // Commit our changes for the other threads to look at
this.reposMut.Lock() this.reposMut.Lock()
this.reposCache = repos this.reposCache = repos
this.reposCacheByName = reposCacheByName
this.reposAlphabeticalOrder = alphabeticalOrderIndexes this.reposAlphabeticalOrder = alphabeticalOrderIndexes
this.reposMut.Unlock() this.reposMut.Unlock()
@@ -86,7 +174,7 @@ func (this *Application) sync(ctx context.Context) (bool, error) {
func (this *Application) syncWorker(ctx context.Context) { func (this *Application) syncWorker(ctx context.Context) {
t := time.NewTicker(30 * time.Minute) t := time.NewTicker(60 * time.Minute)
defer t.Stop() defer t.Stop()
for { for {