Initial commit
This commit is contained in:
		
							parent
							
								
									e6e5634d44
								
							
						
					
					
						commit
						4511c4c017
					
				
							
								
								
									
										93
									
								
								gitea.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										93
									
								
								gitea.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,93 @@
 | 
			
		||||
package gitea
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"io"
 | 
			
		||||
	"io/fs"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/42wim/caddy-gitea/pkg/gitea"
 | 
			
		||||
	"github.com/caddyserver/caddy/v2"
 | 
			
		||||
	"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
 | 
			
		||||
	"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
 | 
			
		||||
	"github.com/caddyserver/caddy/v2/modules/caddyhttp"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	caddy.RegisterModule(Middleware{})
 | 
			
		||||
	httpcaddyfile.RegisterHandlerDirective("gitea", parseCaddyfile)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
 | 
			
		||||
	var m Middleware
 | 
			
		||||
	err := m.UnmarshalCaddyfile(h.Dispenser)
 | 
			
		||||
	return m, err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Middleware implements gitea plugin.
 | 
			
		||||
type Middleware struct {
 | 
			
		||||
	Client *gitea.Client `json:"-"`
 | 
			
		||||
	Server string        `json:"server,omitempty"`
 | 
			
		||||
	Token  string        `json:"token,omitempty"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CaddyModule returns the Caddy module information.
 | 
			
		||||
func (Middleware) CaddyModule() caddy.ModuleInfo {
 | 
			
		||||
	return caddy.ModuleInfo{
 | 
			
		||||
		ID:  "http.handlers.gitea",
 | 
			
		||||
		New: func() caddy.Module { return new(Middleware) },
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Provision provisions gitea client.
 | 
			
		||||
func (m *Middleware) Provision(ctx caddy.Context) error {
 | 
			
		||||
	m.Client = gitea.NewClient(m.Server, m.Token)
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Validate implements caddy.Validator.
 | 
			
		||||
func (m *Middleware) Validate() error {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UnmarshalCaddyfile unmarshals a Caddyfile.
 | 
			
		||||
func (m *Middleware) UnmarshalCaddyfile(d *caddyfile.Dispenser) (err error) {
 | 
			
		||||
	for d.Next() {
 | 
			
		||||
		for n := d.Nesting(); d.NextBlock(n); {
 | 
			
		||||
			switch d.Val() {
 | 
			
		||||
			case "server":
 | 
			
		||||
				d.Args(&m.Server)
 | 
			
		||||
			case "token":
 | 
			
		||||
				d.Args(&m.Token)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ServeHTTP performs gitea content fetcher.
 | 
			
		||||
func (m Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request, _ caddyhttp.Handler) error {
 | 
			
		||||
	h := strings.Split(r.Host, ".")
 | 
			
		||||
	fp := h[0] + r.URL.Path
 | 
			
		||||
 | 
			
		||||
	f, err := m.Client.Open(fp, r.URL.Query().Get("ref"))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		if errors.Is(err, fs.ErrNotExist) {
 | 
			
		||||
			return caddyhttp.Error(http.StatusNotFound, err)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err = io.Copy(w, f)
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Interface guards
 | 
			
		||||
var (
 | 
			
		||||
	_ caddy.Provisioner           = (*Middleware)(nil)
 | 
			
		||||
	_ caddy.Validator             = (*Middleware)(nil)
 | 
			
		||||
	_ caddyhttp.MiddlewareHandler = (*Middleware)(nil)
 | 
			
		||||
	_ caddyfile.Unmarshaler       = (*Middleware)(nil)
 | 
			
		||||
)
 | 
			
		||||
							
								
								
									
										130
									
								
								go.mod
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										130
									
								
								go.mod
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,130 @@
 | 
			
		||||
module github.com/42wim/caddy-gitea
 | 
			
		||||
 | 
			
		||||
go 1.19
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	github.com/BurntSushi/toml v1.2.1
 | 
			
		||||
	github.com/alecthomas/chroma v0.10.0
 | 
			
		||||
	github.com/caddyserver/caddy/v2 v2.6.2
 | 
			
		||||
	github.com/spf13/viper v1.13.0
 | 
			
		||||
	github.com/yuin/goldmark v1.5.2
 | 
			
		||||
	github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594
 | 
			
		||||
	gopkg.in/yaml.v3 v3.0.1
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	filippo.io/edwards25519 v1.0.0 // indirect
 | 
			
		||||
	github.com/AndreasBriese/bbloom v0.0.0-20190825152654-46b345b51c96 // indirect
 | 
			
		||||
	github.com/Masterminds/goutils v1.1.1 // indirect
 | 
			
		||||
	github.com/Masterminds/semver/v3 v3.1.1 // indirect
 | 
			
		||||
	github.com/Masterminds/sprig/v3 v3.2.2 // indirect
 | 
			
		||||
	github.com/antlr/antlr4/runtime/Go/antlr v1.4.10 // indirect
 | 
			
		||||
	github.com/aryann/difflib v0.0.0-20210328193216-ff5ff6dc229b // indirect
 | 
			
		||||
	github.com/beorn7/perks v1.0.1 // indirect
 | 
			
		||||
	github.com/caddyserver/certmagic v0.17.2 // indirect
 | 
			
		||||
	github.com/cespare/xxhash v1.1.0 // indirect
 | 
			
		||||
	github.com/cespare/xxhash/v2 v2.1.2 // indirect
 | 
			
		||||
	github.com/chzyer/readline v1.5.1 // indirect
 | 
			
		||||
	github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
 | 
			
		||||
	github.com/dgraph-io/badger v1.6.2 // indirect
 | 
			
		||||
	github.com/dgraph-io/badger/v2 v2.2007.4 // indirect
 | 
			
		||||
	github.com/dgraph-io/ristretto v0.1.1 // indirect
 | 
			
		||||
	github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
 | 
			
		||||
	github.com/dlclark/regexp2 v1.7.0 // indirect
 | 
			
		||||
	github.com/dustin/go-humanize v1.0.1-0.20200219035652-afde56e7acac // indirect
 | 
			
		||||
	github.com/fsnotify/fsnotify v1.6.0 // indirect
 | 
			
		||||
	github.com/go-kit/kit v0.12.0 // indirect
 | 
			
		||||
	github.com/go-kit/log v0.2.1 // indirect
 | 
			
		||||
	github.com/go-logfmt/logfmt v0.5.1 // indirect
 | 
			
		||||
	github.com/go-sql-driver/mysql v1.6.0 // indirect
 | 
			
		||||
	github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
 | 
			
		||||
	github.com/golang/glog v1.0.0 // indirect
 | 
			
		||||
	github.com/golang/mock v1.6.0 // indirect
 | 
			
		||||
	github.com/golang/protobuf v1.5.2 // indirect
 | 
			
		||||
	github.com/golang/snappy v0.0.4 // indirect
 | 
			
		||||
	github.com/google/cel-go v0.12.5 // indirect
 | 
			
		||||
	github.com/google/pprof v0.0.0-20221010195024-131d412537ea // indirect
 | 
			
		||||
	github.com/google/uuid v1.3.0 // indirect
 | 
			
		||||
	github.com/hashicorp/hcl v1.0.0 // indirect
 | 
			
		||||
	github.com/huandu/xstrings v1.3.2 // indirect
 | 
			
		||||
	github.com/imdario/mergo v0.3.13 // indirect
 | 
			
		||||
	github.com/inconshreveable/mousetrap v1.0.1 // indirect
 | 
			
		||||
	github.com/jackc/chunkreader/v2 v2.0.1 // indirect
 | 
			
		||||
	github.com/jackc/pgconn v1.13.0 // indirect
 | 
			
		||||
	github.com/jackc/pgio v1.0.0 // indirect
 | 
			
		||||
	github.com/jackc/pgpassfile v1.0.0 // indirect
 | 
			
		||||
	github.com/jackc/pgproto3/v2 v2.3.1 // indirect
 | 
			
		||||
	github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
 | 
			
		||||
	github.com/jackc/pgtype v1.12.0 // indirect
 | 
			
		||||
	github.com/jackc/pgx/v4 v4.17.2 // indirect
 | 
			
		||||
	github.com/klauspost/compress v1.15.12 // indirect
 | 
			
		||||
	github.com/klauspost/cpuid/v2 v2.1.2 // indirect
 | 
			
		||||
	github.com/libdns/libdns v0.2.1 // indirect
 | 
			
		||||
	github.com/lucas-clemente/quic-go v0.30.0 // indirect
 | 
			
		||||
	github.com/magiconair/properties v1.8.6 // indirect
 | 
			
		||||
	github.com/manifoldco/promptui v0.9.0 // indirect
 | 
			
		||||
	github.com/marten-seemann/qpack v0.3.0 // indirect
 | 
			
		||||
	github.com/marten-seemann/qtls-go1-18 v0.1.3 // indirect
 | 
			
		||||
	github.com/marten-seemann/qtls-go1-19 v0.1.1 // indirect
 | 
			
		||||
	github.com/mattn/go-colorable v0.1.13 // indirect
 | 
			
		||||
	github.com/mattn/go-isatty v0.0.16 // indirect
 | 
			
		||||
	github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
 | 
			
		||||
	github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
 | 
			
		||||
	github.com/mholt/acmez v1.0.4 // indirect
 | 
			
		||||
	github.com/micromdm/scep/v2 v2.1.0 // indirect
 | 
			
		||||
	github.com/miekg/dns v1.1.50 // indirect
 | 
			
		||||
	github.com/mitchellh/copystructure v1.2.0 // indirect
 | 
			
		||||
	github.com/mitchellh/go-ps v1.0.0 // indirect
 | 
			
		||||
	github.com/mitchellh/mapstructure v1.5.0 // indirect
 | 
			
		||||
	github.com/mitchellh/reflectwalk v1.0.2 // indirect
 | 
			
		||||
	github.com/onsi/ginkgo/v2 v2.4.0 // indirect
 | 
			
		||||
	github.com/pelletier/go-toml v1.9.5 // indirect
 | 
			
		||||
	github.com/pelletier/go-toml/v2 v2.0.5 // indirect
 | 
			
		||||
	github.com/pkg/errors v0.9.1 // indirect
 | 
			
		||||
	github.com/prometheus/client_golang v1.13.0 // indirect
 | 
			
		||||
	github.com/prometheus/client_model v0.3.0 // indirect
 | 
			
		||||
	github.com/prometheus/common v0.37.0 // indirect
 | 
			
		||||
	github.com/prometheus/procfs v0.8.0 // indirect
 | 
			
		||||
	github.com/rs/xid v1.4.0 // indirect
 | 
			
		||||
	github.com/russross/blackfriday/v2 v2.1.0 // indirect
 | 
			
		||||
	github.com/shopspring/decimal v1.3.1 // indirect
 | 
			
		||||
	github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
 | 
			
		||||
	github.com/sirupsen/logrus v1.9.0 // indirect
 | 
			
		||||
	github.com/slackhq/nebula v1.6.1 // indirect
 | 
			
		||||
	github.com/smallstep/certificates v0.22.1 // indirect
 | 
			
		||||
	github.com/smallstep/cli v0.22.0 // indirect
 | 
			
		||||
	github.com/smallstep/nosql v0.5.0 // indirect
 | 
			
		||||
	github.com/smallstep/truststore v0.12.0 // indirect
 | 
			
		||||
	github.com/spf13/afero v1.9.2 // indirect
 | 
			
		||||
	github.com/spf13/cast v1.5.0 // indirect
 | 
			
		||||
	github.com/spf13/cobra v1.6.1 // indirect
 | 
			
		||||
	github.com/spf13/jwalterweatherman v1.1.0 // indirect
 | 
			
		||||
	github.com/spf13/pflag v1.0.5 // indirect
 | 
			
		||||
	github.com/stoewer/go-strcase v1.2.0 // indirect
 | 
			
		||||
	github.com/subosito/gotenv v1.4.1 // indirect
 | 
			
		||||
	github.com/tailscale/tscert v0.0.0-20220316030059-54bbcb9f74e2 // indirect
 | 
			
		||||
	github.com/urfave/cli v1.22.10 // indirect
 | 
			
		||||
	go.etcd.io/bbolt v1.3.6 // indirect
 | 
			
		||||
	go.mozilla.org/pkcs7 v0.0.0-20210826202110-33d05740a352 // indirect
 | 
			
		||||
	go.step.sm/cli-utils v0.7.5 // indirect
 | 
			
		||||
	go.step.sm/crypto v0.22.0 // indirect
 | 
			
		||||
	go.step.sm/linkedca v0.18.0 // indirect
 | 
			
		||||
	go.uber.org/atomic v1.10.0 // indirect
 | 
			
		||||
	go.uber.org/multierr v1.8.0 // indirect
 | 
			
		||||
	go.uber.org/zap v1.23.0 // indirect
 | 
			
		||||
	golang.org/x/crypto v0.1.0 // indirect
 | 
			
		||||
	golang.org/x/exp v0.0.0-20221028150844-83b7d23a625f // indirect
 | 
			
		||||
	golang.org/x/mod v0.6.0 // indirect
 | 
			
		||||
	golang.org/x/net v0.1.0 // indirect
 | 
			
		||||
	golang.org/x/sys v0.1.0 // indirect
 | 
			
		||||
	golang.org/x/term v0.1.0 // indirect
 | 
			
		||||
	golang.org/x/text v0.4.0 // indirect
 | 
			
		||||
	golang.org/x/tools v0.2.0 // indirect
 | 
			
		||||
	google.golang.org/genproto v0.0.0-20221018160656-63c7b68cfc55 // indirect
 | 
			
		||||
	google.golang.org/grpc v1.50.1 // indirect
 | 
			
		||||
	google.golang.org/protobuf v1.28.1 // indirect
 | 
			
		||||
	gopkg.in/ini.v1 v1.67.0 // indirect
 | 
			
		||||
	gopkg.in/square/go-jose.v2 v2.6.0 // indirect
 | 
			
		||||
	gopkg.in/yaml.v2 v2.4.0 // indirect
 | 
			
		||||
	howett.net/plist v1.0.0 // indirect
 | 
			
		||||
)
 | 
			
		||||
							
								
								
									
										181
									
								
								pkg/gitea/frontmatter.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										181
									
								
								pkg/gitea/frontmatter.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,181 @@
 | 
			
		||||
// Taken from caddy source code (https://github.com/mholt/caddy/)
 | 
			
		||||
// Copyright 2015 Matthew Holt and The Caddy Authors
 | 
			
		||||
//
 | 
			
		||||
// Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
// you may not use this file except in compliance with the License.
 | 
			
		||||
// You may obtain a copy of the License at
 | 
			
		||||
//
 | 
			
		||||
//	http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
//
 | 
			
		||||
// Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
// distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
// See the License for the specific language governing permissions and
 | 
			
		||||
// limitations under the License.
 | 
			
		||||
//
 | 
			
		||||
//nolint:all
 | 
			
		||||
package gitea
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"unicode"
 | 
			
		||||
 | 
			
		||||
	"github.com/BurntSushi/toml"
 | 
			
		||||
	"github.com/alecthomas/chroma/formatters/html"
 | 
			
		||||
	"github.com/yuin/goldmark"
 | 
			
		||||
	highlighting "github.com/yuin/goldmark-highlighting"
 | 
			
		||||
	"github.com/yuin/goldmark/extension"
 | 
			
		||||
	"github.com/yuin/goldmark/parser"
 | 
			
		||||
	gmhtml "github.com/yuin/goldmark/renderer/html"
 | 
			
		||||
	"gopkg.in/yaml.v3"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func extractFrontMatter(input string) (map[string]any, string, error) {
 | 
			
		||||
	// get the bounds of the first non-empty line
 | 
			
		||||
	var firstLineStart, firstLineEnd int
 | 
			
		||||
	lineEmpty := true
 | 
			
		||||
	for i, b := range input {
 | 
			
		||||
		if b == '\n' {
 | 
			
		||||
			firstLineStart = firstLineEnd
 | 
			
		||||
			if firstLineStart > 0 {
 | 
			
		||||
				firstLineStart++ // skip newline character
 | 
			
		||||
			}
 | 
			
		||||
			firstLineEnd = i
 | 
			
		||||
			if !lineEmpty {
 | 
			
		||||
				break
 | 
			
		||||
			}
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		lineEmpty = lineEmpty && unicode.IsSpace(b)
 | 
			
		||||
	}
 | 
			
		||||
	firstLine := input[firstLineStart:firstLineEnd]
 | 
			
		||||
 | 
			
		||||
	// ensure residue windows carriage return byte is removed
 | 
			
		||||
	firstLine = strings.TrimSpace(firstLine)
 | 
			
		||||
 | 
			
		||||
	// see what kind of front matter there is, if any
 | 
			
		||||
	var closingFence []string
 | 
			
		||||
	var fmParser func([]byte) (map[string]any, error)
 | 
			
		||||
	for _, fmType := range supportedFrontMatterTypes {
 | 
			
		||||
		if firstLine == fmType.FenceOpen {
 | 
			
		||||
			closingFence = fmType.FenceClose
 | 
			
		||||
			fmParser = fmType.ParseFunc
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if fmParser == nil {
 | 
			
		||||
		// no recognized front matter; whole document is body
 | 
			
		||||
		return nil, input, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// find end of front matter
 | 
			
		||||
	var fmEndFence string
 | 
			
		||||
	fmEndFenceStart := -1
 | 
			
		||||
	for _, fence := range closingFence {
 | 
			
		||||
		index := strings.Index(input[firstLineEnd:], "\n"+fence)
 | 
			
		||||
		if index >= 0 {
 | 
			
		||||
			fmEndFenceStart = index
 | 
			
		||||
			fmEndFence = fence
 | 
			
		||||
			break
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if fmEndFenceStart < 0 {
 | 
			
		||||
		return nil, "", fmt.Errorf("unterminated front matter")
 | 
			
		||||
	}
 | 
			
		||||
	fmEndFenceStart += firstLineEnd + 1 // add 1 to account for newline
 | 
			
		||||
 | 
			
		||||
	// extract and parse front matter
 | 
			
		||||
	frontMatter := input[firstLineEnd:fmEndFenceStart]
 | 
			
		||||
	fm, err := fmParser([]byte(frontMatter))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, "", err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// the rest is the body
 | 
			
		||||
	body := input[fmEndFenceStart+len(fmEndFence):]
 | 
			
		||||
 | 
			
		||||
	return fm, body, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func yamlFrontMatter(input []byte) (map[string]any, error) {
 | 
			
		||||
	m := make(map[string]any)
 | 
			
		||||
	err := yaml.Unmarshal(input, &m)
 | 
			
		||||
	return m, err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func tomlFrontMatter(input []byte) (map[string]any, error) {
 | 
			
		||||
	m := make(map[string]any)
 | 
			
		||||
	err := toml.Unmarshal(input, &m)
 | 
			
		||||
	return m, err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func jsonFrontMatter(input []byte) (map[string]any, error) {
 | 
			
		||||
	input = append([]byte{'{'}, input...)
 | 
			
		||||
	input = append(input, '}')
 | 
			
		||||
	m := make(map[string]any)
 | 
			
		||||
	err := json.Unmarshal(input, &m)
 | 
			
		||||
	return m, err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type parsedMarkdownDoc struct {
 | 
			
		||||
	Meta map[string]any `json:"meta,omitempty"`
 | 
			
		||||
	Body string         `json:"body,omitempty"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type frontMatterType struct {
 | 
			
		||||
	FenceOpen  string
 | 
			
		||||
	FenceClose []string
 | 
			
		||||
	ParseFunc  func(input []byte) (map[string]any, error)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var supportedFrontMatterTypes = []frontMatterType{
 | 
			
		||||
	{
 | 
			
		||||
		FenceOpen:  "---",
 | 
			
		||||
		FenceClose: []string{"---", "..."},
 | 
			
		||||
		ParseFunc:  yamlFrontMatter,
 | 
			
		||||
	},
 | 
			
		||||
	{
 | 
			
		||||
		FenceOpen:  "+++",
 | 
			
		||||
		FenceClose: []string{"+++"},
 | 
			
		||||
		ParseFunc:  tomlFrontMatter,
 | 
			
		||||
	},
 | 
			
		||||
	{
 | 
			
		||||
		FenceOpen:  "{",
 | 
			
		||||
		FenceClose: []string{"}"},
 | 
			
		||||
		ParseFunc:  jsonFrontMatter,
 | 
			
		||||
	},
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func markdown(input []byte) ([]byte, error) {
 | 
			
		||||
	md := goldmark.New(
 | 
			
		||||
		goldmark.WithExtensions(
 | 
			
		||||
			extension.GFM,
 | 
			
		||||
			extension.Footnote,
 | 
			
		||||
			highlighting.NewHighlighting(
 | 
			
		||||
				highlighting.WithFormatOptions(
 | 
			
		||||
					html.WithClasses(true),
 | 
			
		||||
				),
 | 
			
		||||
			),
 | 
			
		||||
		),
 | 
			
		||||
		goldmark.WithParserOptions(
 | 
			
		||||
			parser.WithAutoHeadingID(),
 | 
			
		||||
		),
 | 
			
		||||
		goldmark.WithRendererOptions(
 | 
			
		||||
			gmhtml.WithUnsafe(), // TODO: this is not awesome, maybe should be configurable?
 | 
			
		||||
		),
 | 
			
		||||
	)
 | 
			
		||||
 | 
			
		||||
	buf := bufPool.Get().(*bytes.Buffer)
 | 
			
		||||
	buf.Reset()
 | 
			
		||||
 | 
			
		||||
	defer bufPool.Put(buf)
 | 
			
		||||
 | 
			
		||||
	if err := md.Convert(input, buf); err != nil {
 | 
			
		||||
		return input, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return buf.Bytes(), nil
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										93
									
								
								pkg/gitea/fs.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										93
									
								
								pkg/gitea/fs.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,93 @@
 | 
			
		||||
package gitea
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"io"
 | 
			
		||||
	"io/fs"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type fileInfo struct {
 | 
			
		||||
	size  int64
 | 
			
		||||
	isdir bool
 | 
			
		||||
	name  string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type openFile struct {
 | 
			
		||||
	content []byte
 | 
			
		||||
	offset  int64
 | 
			
		||||
	name    string
 | 
			
		||||
	isdir   bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (g fileInfo) Name() string {
 | 
			
		||||
	return g.name
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (g fileInfo) Size() int64 {
 | 
			
		||||
	return g.size
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (g fileInfo) Mode() fs.FileMode {
 | 
			
		||||
	return 0o444
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (g fileInfo) ModTime() time.Time {
 | 
			
		||||
	return time.Time{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (g fileInfo) Sys() any {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (g fileInfo) IsDir() bool {
 | 
			
		||||
	return g.isdir
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var _ io.Seeker = (*openFile)(nil)
 | 
			
		||||
 | 
			
		||||
func (o *openFile) Close() error {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (o *openFile) Stat() (fs.FileInfo, error) {
 | 
			
		||||
	return fileInfo{
 | 
			
		||||
		size:  int64(len(o.content)),
 | 
			
		||||
		isdir: o.isdir,
 | 
			
		||||
		name:  o.name,
 | 
			
		||||
	}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (o *openFile) Read(b []byte) (int, error) {
 | 
			
		||||
	if o.offset >= int64(len(o.content)) {
 | 
			
		||||
		return 0, io.EOF
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if o.offset < 0 {
 | 
			
		||||
		return 0, &fs.PathError{Op: "read", Path: o.name, Err: fs.ErrInvalid}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	n := copy(b, o.content[o.offset:])
 | 
			
		||||
 | 
			
		||||
	o.offset += int64(n)
 | 
			
		||||
 | 
			
		||||
	return n, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (o *openFile) Seek(offset int64, whence int) (int64, error) {
 | 
			
		||||
	switch whence {
 | 
			
		||||
	case 0:
 | 
			
		||||
		offset += 0
 | 
			
		||||
	case 1:
 | 
			
		||||
		offset += o.offset
 | 
			
		||||
	case 2:
 | 
			
		||||
		offset += int64(len(o.content))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if offset < 0 || offset > int64(len(o.content)) {
 | 
			
		||||
		return 0, &fs.PathError{Op: "seek", Path: o.name, Err: fs.ErrInvalid}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	o.offset = offset
 | 
			
		||||
 | 
			
		||||
	return offset, nil
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										238
									
								
								pkg/gitea/gitea.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										238
									
								
								pkg/gitea/gitea.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,238 @@
 | 
			
		||||
package gitea
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"io/fs"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"net/url"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"sync"
 | 
			
		||||
 | 
			
		||||
	"github.com/spf13/viper"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Client struct {
 | 
			
		||||
	serverURL string
 | 
			
		||||
	token     string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type pagesConfig struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type topicsResponse struct {
 | 
			
		||||
	Topics []string `json:"topics"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewClient(serverURL, token string) *Client {
 | 
			
		||||
	return &Client{
 | 
			
		||||
		serverURL: serverURL,
 | 
			
		||||
		token:     token,
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Client) Open(name, ref string) (fs.File, error) {
 | 
			
		||||
	if ref == "" {
 | 
			
		||||
		ref = "gitea-pages"
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	owner, repo, filepath, err := splitName(name)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if !c.allowsPages(owner, repo) {
 | 
			
		||||
		return nil, fs.ErrNotExist
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := c.readConfig(owner, repo); err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if !validRefs(ref) {
 | 
			
		||||
		return nil, fs.ErrNotExist
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	res, err := c.getRawFileOrLFS(owner, repo, filepath, ref)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if strings.HasSuffix(filepath, ".md") {
 | 
			
		||||
		res, err = handleMD(res)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &openFile{
 | 
			
		||||
		content: res,
 | 
			
		||||
		name:    filepath,
 | 
			
		||||
	}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Client) getRawFileOrLFS(owner, repo, filepath, ref string) ([]byte, error) {
 | 
			
		||||
	var (
 | 
			
		||||
		giteaURL string
 | 
			
		||||
		err      error
 | 
			
		||||
	)
 | 
			
		||||
 | 
			
		||||
	giteaURL, err = url.JoinPath(c.serverURL+"/api/v1/repos/", owner, repo, "media", filepath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	giteaURL += "?ref=" + url.QueryEscape(ref)
 | 
			
		||||
 | 
			
		||||
	req, err := http.NewRequest(http.MethodGet, giteaURL, nil)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	req.Header.Add("Authorization", "token "+c.token)
 | 
			
		||||
 | 
			
		||||
	resp, err := http.DefaultClient.Do(req)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	switch resp.StatusCode {
 | 
			
		||||
	case http.StatusNotFound:
 | 
			
		||||
		return nil, fs.ErrNotExist
 | 
			
		||||
	case http.StatusOK:
 | 
			
		||||
	default:
 | 
			
		||||
		return nil, fmt.Errorf("unexpected status code '%d'", resp.StatusCode)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	res, err := io.ReadAll(resp.Body)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	defer resp.Body.Close()
 | 
			
		||||
 | 
			
		||||
	return res, nil
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var bufPool = sync.Pool{
 | 
			
		||||
	New: func() any {
 | 
			
		||||
		return new(bytes.Buffer)
 | 
			
		||||
	},
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func handleMD(res []byte) ([]byte, error) {
 | 
			
		||||
	meta, resbody, err := extractFrontMatter(string(res))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	resmd, err := markdown([]byte(resbody))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	res = append([]byte("<!DOCTYPE html>\n<html>\n<body>\n<h1>"), []byte(meta["title"].(string))...)
 | 
			
		||||
	res = append(res, []byte("</h1>")...)
 | 
			
		||||
	res = append(res, resmd...)
 | 
			
		||||
	res = append(res, []byte("</body></html>")...)
 | 
			
		||||
 | 
			
		||||
	return res, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Client) repoTopics(owner, repo string) ([]string, error) {
 | 
			
		||||
	var (
 | 
			
		||||
		giteaURL string
 | 
			
		||||
		err      error
 | 
			
		||||
	)
 | 
			
		||||
 | 
			
		||||
	giteaURL, err = url.JoinPath(c.serverURL+"/api/v1/repos/", owner, repo, "topics")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	req, err := http.NewRequest(http.MethodGet, giteaURL, nil)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	req.Header.Add("Authorization", "token "+c.token)
 | 
			
		||||
 | 
			
		||||
	resp, err := http.DefaultClient.Do(req)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	res, err := io.ReadAll(resp.Body)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	defer resp.Body.Close()
 | 
			
		||||
 | 
			
		||||
	t := topicsResponse{}
 | 
			
		||||
	json.Unmarshal(res, &t)
 | 
			
		||||
 | 
			
		||||
	return t.Topics, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Client) allowsPages(owner, repo string) bool {
 | 
			
		||||
	topics, err := c.repoTopics(owner, repo)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return false
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, topic := range topics {
 | 
			
		||||
		if topic == "gitea-pages" {
 | 
			
		||||
			return true
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *Client) readConfig(owner, repo string) error {
 | 
			
		||||
	cfg, err := c.getRawFileOrLFS(owner, repo, "gitea-pages.toml", "gitea-pages")
 | 
			
		||||
	if err != nil && !errors.Is(err, fs.ErrNotExist) {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	viper.SetConfigType("toml")
 | 
			
		||||
	viper.ReadConfig(bytes.NewBuffer(cfg))
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func splitName(name string) (string, string, string, error) {
 | 
			
		||||
	parts := strings.Split(name, "/")
 | 
			
		||||
 | 
			
		||||
	// parts contains: ["owner", "repo", "filepath"]
 | 
			
		||||
	// return invalid path if not enough parts
 | 
			
		||||
	if len(parts) < 3 {
 | 
			
		||||
		return "", "", "", fs.ErrInvalid
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	owner := parts[0]
 | 
			
		||||
	repo := parts[1]
 | 
			
		||||
	filepath := strings.Join(parts[2:], "/")
 | 
			
		||||
 | 
			
		||||
	return owner, repo, filepath, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func validRefs(ref string) bool {
 | 
			
		||||
	validrefs := viper.GetStringSlice("allowedrefs")
 | 
			
		||||
	for _, r := range validrefs {
 | 
			
		||||
		if r == ref {
 | 
			
		||||
			return true
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if r == "*" {
 | 
			
		||||
			return true
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return false
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user