Add import template for minimal response, add topics format

Signed-off-by: jolheiser <john.olheiser@gmail.com>
pull/8/head
jolheiser 2021-02-21 14:03:23 -06:00
parent 46071014b9
commit f233cfead2
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
7 changed files with 50 additions and 7 deletions

View File

@ -74,6 +74,8 @@ To run Vanity in config-only mode for packages, set `--service` to `off`.
To run Vanity without automatic updating, use `--manual`.
When running with manual-mode, the provided button or `/_/update` endpoint can be used once every `--interval`.
## Topic Lists
By setting `--topics`, anyone visiting the index page will see packages grouped by their topics.

View File

@ -29,6 +29,13 @@ func (p packageList) Names() []string {
func (p packageList) Topics() map[string][]*api.Package {
topics := make(map[string][]*api.Package, 0)
for _, pkg := range p {
if len(pkg.Topics) == 0 {
if tt, ok := topics["other"]; ok {
topics["other"] = append(tt, pkg)
} else {
topics["other"] = []*api.Package{pkg}
}
}
for _, t := range pkg.Topics {
if tt, ok := topics[t]; ok {
topics[t] = append(tt, pkg)

View File

@ -27,7 +27,7 @@ func Init() (*chi.Mux, error) {
r := chi.NewRouter()
r.Use(middleware.RedirectSlashes)
r.Use(middleware.Recoverer)
r.Use(middleware.Timeout(30 * time.Second))
r.Use(middleware.Timeout(60 * time.Second))
r.Get("/", doIndex)
r.Get("/*", doVanity)
@ -71,12 +71,22 @@ func doVanity(res http.ResponseWriter, req *http.Request) {
return
}
if err := tmpl.Lookup("vanity.tmpl").Execute(res, map[string]interface{}{
ctx := map[string]interface{}{
"Package": pkg,
"Module": pkg.Module(flags.Domain),
"GoSource": fmt.Sprintf("%s %s %s %s", pkg.Module(flags.Domain), pkg.CloneHTTP, svc.GoDir(pkg), svc.GoFile(pkg)),
"Index": false,
}); err != nil {
}
q := req.URL.Query()
if q.Get("go-get") != "" || q.Get("git-import") != "" {
if err := tmpl.Lookup("import.tmpl").Execute(res, ctx); err != nil {
beaver.Errorf("could not write response: %v", err)
}
return
}
if err := tmpl.Lookup("vanity.tmpl").Execute(res, ctx); err != nil {
beaver.Errorf("could not write response: %v", err)
}
}

View File

@ -12,7 +12,7 @@
updateImports.addEventListener('click', () => {
updateImports.disabled = true;
updateImports.innerHTML = 'Updating...';
fetch('{{if .Index}}.{{else}}../{{end}}_/update', {
fetch('{{if .Index}}{{else}}../{{end}}_/update', {
method: 'GET'
}).then(() => {
location.reload();

View File

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!-- Go -->
<meta name="go-import" content="{{.Module}} git {{.Package.CloneHTTP}}"/>
<meta name="go-source" content="{{.GoSource}}">
<!-- Git Import -->
<meta name="git-import" content="{{.Package.Name}} {{.Package.CloneHTTP}} {{.Package.CloneSSH}}"/>
<title>{{.Module}}</title>
</head>
<body>
<code>go get {{.Module}}</code>
<br/>
<code>git-import {{.Module}}</code>
</body>
</html>

View File

@ -1,5 +1,5 @@
{{template "head.tmpl" .}}
<h3>Imports:</h3>
<h3>{{if eq .Format "list"}}Imports{{else}}Topics{{end}}:</h3>
{{if eq .Format "list"}}
<ul>
{{range $path, $package := .Packages}}

View File

@ -59,12 +59,16 @@ func Check(pkg *api.Package) error {
// Inclusions
if len(flags.Include) > 0 {
var included bool
for _, include := range flags.Include {
if include.MatchString(pkg.Name) {
return fmt.Errorf("%s was included by the rule %s", pkg.Name, include.String())
included = true
break
}
}
return fmt.Errorf("%s wasn't included by any existing inclusion rule", pkg.Name)
if !included {
return fmt.Errorf("%s wasn't included by any existing inclusion rule", pkg.Name)
}
}
return nil