This repository has been archived by the owner on Apr 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
122 lines (99 loc) · 3.34 KB
/
config.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
// Copyright 2017, Project ArteMisc
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package gitter
import (
"bytes"
"crypto/tls"
"fmt"
"log"
"net/http"
"text/template"
)
// tmpl is the
const (
templateHtml = `<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="go-import" content="{{.Name}} git {{.Git.RepoUrl}}">
<meta name="go-source" content="{{.Name}} {{.Git.RepoUrl}}/ {{.Git.RepoUrl}}/tree/{{.Git.Branch}}{/dir} {{.Git.RepoUrl}}/blob/{{.Git.Branch}}{/dir}/{file}#L{line}">
<meta http-equiv="refresh" content="0; url=https://godoc.org/{{.Name}}">
</head>
<body>
Nothing to see here, <a href="https://godoc.org/{{.Name}}">move along</a>
</body>
</html>`
)
var (
tmplt = template.Must(template.New("git").Parse(templateHtml))
)
// Config holds all the relevant configuration values for the
type Config struct {
// Host holds the host on which the gitter Server should listen.
Host string `json:"host"`
// Post holds the port on which the gitter Server should listen.
Port uint16 `json:"port"`
// Tls configuration, including certificate path and key path for
// http.ListenAndServeTLS.
Tls *TlsConfig `json:"tls"`
// Packages holds a list of packages for which the gitter Server should
// serve redirects.
Packages []*Package `json:"packages"`
// TODO logger configuration
}
// TlsConfig specifies the server's TLS configuration. If the Config field is
// left empty, the package's defaults are used.
type TlsConfig struct {
CertPath string `json:"cert_path"`
KeyPath string `json:"key_path"`
*tls.Config
}
// Package contains a path (package name) match with a git repository.
type Package struct {
// Name holds the package name
Name string `json:"name"`
// Git describes a git repository.
Git Repo `json:"git"`
}
// Repo describes a git repository
type Repo struct {
// Host holds the host domain of the git repository, e.g. "github.com".
// GitHub is currently the only supported host.
Host string `json:"host"`
// Username is the username of the repository's owner on the git platform.
Username string `json:"username"`
// Package holds the name of the package/repo, e.g. "go-gitter"
Package string `json:"package"`
// Branch holds the name of the branch to refer to, e.g. "master".
Branch string `json:"branch"`
}
func (r *Repo) RepoUrl() string {
return fmt.Sprintf("https://%s/%s/%s", r.Host, r.Username, r.Package)
}
func (p *Package) HtmlBody() (result []byte) {
body := bytes.NewBuffer(make([]byte, 0, 1024))
err := tmplt.Execute(body, p)
if err != nil {
panic(err)
}
result = body.Bytes()
return
}
// HttpHandler returns a http.Handler that responds with the HTML needed to
// redirect a 'go get' command to the right git repository.
func (p *Package) HttpHandler() http.Handler {
data := p.HtmlBody()
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write(data)
log.Printf("Served %s%s\n", r.Host, r.RequestURI)
})
}
// Handle adds a Handler to the mux. The Package's full name (domain and path)
// are used as route. The result of p.HttpHandler() is used as the route's
// handler.
func (p *Package) Handle(mux *http.ServeMux) {
mux.Handle(p.Name, p.HttpHandler())
}