Add support for branch.repo.username.hostname

If we have a domain configured eg giteapages.io then besides the normal
user.giteapages.io and org.giteapages.io we also support

repo.user.giteapages.io / repo.org.giteapages.io
branch.repo.user.giteapages.io / branch.repo.org.giteapages.io
This commit is contained in:
Wim 2022-11-02 00:00:02 +01:00
parent f99aedb6a1
commit 1ed8d43714
No known key found for this signature in database
2 changed files with 25 additions and 4 deletions

View File

@ -20,6 +20,7 @@ func init() {
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) { func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
var m Middleware var m Middleware
err := m.UnmarshalCaddyfile(h.Dispenser) err := m.UnmarshalCaddyfile(h.Dispenser)
return m, err return m, err
} }
@ -29,6 +30,7 @@ type Middleware struct {
Server string `json:"server,omitempty"` Server string `json:"server,omitempty"`
Token string `json:"token,omitempty"` Token string `json:"token,omitempty"`
GiteaPages string `json:"gitea_pages,omitempty"` GiteaPages string `json:"gitea_pages,omitempty"`
Domain string `json:"domain,omitempty"`
} }
// CaddyModule returns the Caddy module information. // CaddyModule returns the Caddy module information.
@ -53,7 +55,7 @@ func (m *Middleware) Validate() error {
} }
// UnmarshalCaddyfile unmarshals a Caddyfile. // UnmarshalCaddyfile unmarshals a Caddyfile.
func (m *Middleware) UnmarshalCaddyfile(d *caddyfile.Dispenser) (err error) { func (m *Middleware) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() { for d.Next() {
for n := d.Nesting(); d.NextBlock(n); { for n := d.Nesting(); d.NextBlock(n); {
switch d.Val() { switch d.Val() {
@ -63,6 +65,8 @@ func (m *Middleware) UnmarshalCaddyfile(d *caddyfile.Dispenser) (err error) {
d.Args(&m.Token) d.Args(&m.Token)
case "gitea_pages": case "gitea_pages":
d.Args(&m.GiteaPages) d.Args(&m.GiteaPages)
case "domain":
d.Args(&m.Domain)
} }
} }
} }
@ -72,15 +76,31 @@ func (m *Middleware) UnmarshalCaddyfile(d *caddyfile.Dispenser) (err error) {
// ServeHTTP performs gitea content fetcher. // ServeHTTP performs gitea content fetcher.
func (m Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request, _ caddyhttp.Handler) error { func (m Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request, _ caddyhttp.Handler) error {
h := strings.Split(r.Host, ".") // remove the domain if it's set (works fine if it's empty)
fp := h[0] + r.URL.Path host := strings.TrimRight(strings.TrimSuffix(r.Host, m.Domain), ".")
h := strings.Split(host, ".")
f, err := m.Client.Open(fp, r.URL.Query().Get("ref")) fp := h[0] + r.URL.Path
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]
}
}
f, err := m.Client.Open(fp, ref)
if err != nil { if err != nil {
return caddyhttp.Error(http.StatusNotFound, err) return caddyhttp.Error(http.StatusNotFound, err)
} }
_, err = io.Copy(w, f) _, err = io.Copy(w, f)
return err return err
} }

View File

@ -45,6 +45,7 @@ func (c *Client) Open(name, ref string) (fs.File, error) {
} }
owner, repo, filepath := splitName(name) owner, repo, filepath := splitName(name)
// if repo is empty they want to have the gitea-pages repo // if repo is empty they want to have the gitea-pages repo
if repo == "" { if repo == "" {
repo = c.giteapages repo = c.giteapages