2022-10-30 22:46:54 +01:00
|
|
|
package gitea
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2024-06-23 13:55:53 +02:00
|
|
|
"gitea.djmil.dev/goland/gitea-pages/pkg/gitea"
|
2022-10-30 22:46:54 +01:00
|
|
|
"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)
|
2022-11-02 00:00:02 +01:00
|
|
|
|
2022-10-30 22:46:54 +01:00
|
|
|
return m, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Middleware implements gitea plugin.
|
|
|
|
type Middleware struct {
|
2022-11-02 18:53:32 +01:00
|
|
|
Client *gitea.Client `json:"-"`
|
|
|
|
Server string `json:"server,omitempty"`
|
|
|
|
Token string `json:"token,omitempty"`
|
|
|
|
GiteaPages string `json:"gitea_pages,omitempty"`
|
|
|
|
GiteaPagesAllowAll string `json:"gitea_pages_allowall,omitempty"`
|
|
|
|
Domain string `json:"domain,omitempty"`
|
2022-10-30 22:46:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2022-11-01 21:50:49 +01:00
|
|
|
var err error
|
2022-11-02 18:53:32 +01:00
|
|
|
m.Client, err = gitea.NewClient(m.Server, m.Token, m.GiteaPages, m.GiteaPagesAllowAll)
|
2022-10-30 22:46:54 +01:00
|
|
|
|
2022-11-01 21:50:49 +01:00
|
|
|
return err
|
2022-10-30 22:46:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate implements caddy.Validator.
|
|
|
|
func (m *Middleware) Validate() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalCaddyfile unmarshals a Caddyfile.
|
2022-11-02 00:00:02 +01:00
|
|
|
func (m *Middleware) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
2022-10-30 22:46:54 +01:00
|
|
|
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)
|
2022-11-01 21:22:12 +01:00
|
|
|
case "gitea_pages":
|
|
|
|
d.Args(&m.GiteaPages)
|
2022-11-02 18:53:32 +01:00
|
|
|
case "gitea_pages_allowall":
|
|
|
|
d.Args(&m.GiteaPagesAllowAll)
|
2022-11-02 00:00:02 +01:00
|
|
|
case "domain":
|
|
|
|
d.Args(&m.Domain)
|
2022-10-30 22:46:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP performs gitea content fetcher.
|
|
|
|
func (m Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request, _ caddyhttp.Handler) error {
|
2022-11-02 00:00:02 +01:00
|
|
|
// remove the domain if it's set (works fine if it's empty)
|
|
|
|
host := strings.TrimRight(strings.TrimSuffix(r.Host, m.Domain), ".")
|
|
|
|
h := strings.Split(host, ".")
|
|
|
|
|
2022-10-30 22:46:54 +01:00
|
|
|
fp := h[0] + r.URL.Path
|
2022-11-02 00:00:02 +01:00
|
|
|
ref := r.URL.Query().Get("ref")
|
|
|
|
|
|
|
|
// if we haven't specified a domain, do not support repo.username and branch.repo.username
|
|
|
|
if m.Domain != "" {
|
|
|
|
switch {
|
|
|
|
case len(h) == 2:
|
|
|
|
fp = h[1] + "/" + h[0] + r.URL.Path
|
|
|
|
case len(h) == 3:
|
|
|
|
fp = h[2] + "/" + h[1] + r.URL.Path
|
|
|
|
ref = h[0]
|
|
|
|
}
|
|
|
|
}
|
2022-10-30 22:46:54 +01:00
|
|
|
|
2022-11-02 00:00:02 +01:00
|
|
|
f, err := m.Client.Open(fp, ref)
|
2022-10-30 22:46:54 +01:00
|
|
|
if err != nil {
|
2022-11-01 21:21:46 +01:00
|
|
|
return caddyhttp.Error(http.StatusNotFound, err)
|
2022-10-30 22:46:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(w, f)
|
2022-11-02 00:00:02 +01:00
|
|
|
|
2022-10-30 22:46:54 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interface guards
|
|
|
|
var (
|
|
|
|
_ caddy.Provisioner = (*Middleware)(nil)
|
|
|
|
_ caddy.Validator = (*Middleware)(nil)
|
|
|
|
_ caddyhttp.MiddlewareHandler = (*Middleware)(nil)
|
|
|
|
_ caddyfile.Unmarshaler = (*Middleware)(nil)
|
|
|
|
)
|