forked from 42wim/caddy-gitea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitea.go
133 lines (110 loc) · 3.42 KB
/
gitea.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package gitea
import (
"io"
"mime"
"net/http"
"strings"
"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"
"github.com/simongregorebner/caddy-gitea/pkg/gitea"
)
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"`
GiteaPages string `json:"gitea_pages,omitempty"`
GiteaPagesAllowAll string `json:"gitea_pages_allowall,omitempty"`
Domain string `json:"domain,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 {
var err error
// retrieve logger from the caddy context
// https://caddyserver.com/docs/extending-caddy#logs
var logger = ctx.Logger() // get logger
m.Client, err = gitea.NewClient(logger, m.Server, m.Token, m.GiteaPages, m.GiteaPagesAllowAll)
return err
}
// Validate implements caddy.Validator.
func (m *Middleware) Validate() error {
return nil
}
// UnmarshalCaddyfile unmarshals a Caddyfile.
func (m *Middleware) UnmarshalCaddyfile(d *caddyfile.Dispenser) 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)
case "gitea_pages":
d.Args(&m.GiteaPages)
case "gitea_pages_allowall":
d.Args(&m.GiteaPagesAllowAll)
case "domain":
d.Args(&m.Domain)
}
}
}
return nil
}
// ServeHTTP performs gitea content fetcher.
func (m Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request, _ caddyhttp.Handler) error {
// 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, ".")
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 {
return caddyhttp.Error(http.StatusNotFound, err)
}
// try to determine mime type based on extenstion of file
parts := strings.Split(r.URL.Path, ".")
var ext string
if len(parts) > 1 {
ext = parts[len(parts)-1]
// fmt.Println(ext)
w.Header().Add("Content-Type", mime.TypeByExtension("."+ext))
}
// w.Header().Add("Content-Type", mime.TypeByExtension(".css"))
_, 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)
)