-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
68 lines (61 loc) · 1.71 KB
/
template.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
// Copyright 2019 Nirenjan Krishnan. All rights reserved.
package vanity
import (
"html/template"
"io"
"strings"
)
// buildTemplate builds the template structure and saves it
// in the *Server object
func (s *Server) buildTemplate() {
const tpl = `<!DOCTYPE html>
<html>
<head>
{{- $base := printf "%s/%s" .Base .Pkg -}}
{{- $host := printf "%s%s" .VcsHost .Pkg -}}
{{- $import := printf "%s %s %s" $base .VcsType $host -}}
{{- $redirect := .Pkg -}}
{{- if ne .Redirect .VcsHost -}}
{{- $redirect = printf "%s%s" .Redirect .Request -}}
{{- else -}}
{{- $redirect = printf "%s%s" .Redirect .Pkg -}}
{{- end }}
<meta charset="UTF-8">
<meta name="go-import" content="{{ $import }}">
{{- if or .Dir .File -}}
{{- $dir := (printf "%s/%s" $host .Dir) -}}{{- if not .Dir -}}{{- $dir = "_" -}}{{- end -}}
{{- $file := (printf "%s/%s" $host .File) -}}{{- if not .File -}}{{- $file = "_" -}}{{- end -}}
{{- $source := (printf "%s %s %s %s" $base $host $dir $file) }}
<meta name="go-source" content="{{ $source }}">
{{- end }}
<meta http-equiv="refresh" content="0;url={{$redirect}}">
</head>
<body>
<p>Redirecting to <a href="{{$redirect}}">{{$redirect}}</a></p>
</body>
</html>`
s.template = template.Must(template.New("vanity").Parse(tpl))
}
func (s *Server) serveMeta(w io.Writer, req string) {
pkg := repoBase(req)
tplData := struct {
Base string
Pkg string
VcsHost string
VcsType string
Redirect string
Request string
Dir string
File string
}{
Base: s.base,
Pkg: pkg,
VcsHost: s.repo.root,
VcsType: s.repo.vcsType,
Redirect: s.redirect,
Request: strings.TrimPrefix(req, "/"),
Dir: s.repo.dirFormat,
File: s.repo.fileFormat,
}
s.template.Execute(w, tplData)
}