diff options
Diffstat (limited to 'roseh.moe.go')
| -rw-r--r-- | roseh.moe.go | 47 |
1 files changed, 45 insertions, 2 deletions
diff --git a/roseh.moe.go b/roseh.moe.go index 407d112..84f3abf 100644 --- a/roseh.moe.go +++ b/roseh.moe.go @@ -824,7 +824,7 @@ func cmd(w http.ResponseWriter, r *http.Request) { return } if r.FormValue("go-get") == "1" { - fmt.Fprintf(w, `<meta name="go-import" content="roseh.moe/cmd/%s %s">`, r.PathValue("cmd"), source) + fmt.Fprintf(w, `<meta name="go-import" content="roseh.moe/cmd/%s %s">`, r.PathValue("cmd"), source.url) } else { http.Redirect(w, r, "https://pkg.go.dev/roseh.moe/cmd/"+r.PathValue("cmd"), http.StatusFound) } @@ -869,6 +869,48 @@ func pkgIndex(w http.ResponseWriter, r *http.Request) { } } +var ( + //go:embed templates/cmd-index.html.template + cmdIndexString string + cmdIndexTemplate = template.Must(outlinedTemplate(cmdIndexString)) +) + +type cmdIndexTemplateCommand struct { + Name string + Description string + Multi bool +} + +type cmdIndexTemplateArgs struct { + Commands []cmdIndexTemplateCommand +} + +func cmdIndex(w http.ResponseWriter, r *http.Request) { + serviceConfig, err := loadConfig() + if err != nil { + log.Printf("Warning: failed to load config: %s", err) + http.Error(w, "Failed to load config", http.StatusInternalServerError) + return + } + commands := make([]string, 0, len(serviceConfig.commandRedirects)) + for cmd := range serviceConfig.commandRedirects { + commands = append(commands, cmd) + } + slices.Sort(commands) + templateCommands := make([]cmdIndexTemplateCommand, len(commands)) + for i, name := range commands { + cmd := serviceConfig.commandRedirects[name] + templateCommands[i] = cmdIndexTemplateCommand{ + Name: name, + Description: cmd.description, + Multi: cmd.multi, + } + } + if err := cmdIndexTemplate.Execute(w, cmdIndexTemplateArgs{Commands: templateCommands}); err != nil { + log.Printf("Warning: pkgIndex: %s", err) + } +} + func pkg(w http.ResponseWriter, r *http.Request) { serviceConfig, err := loadConfig() if err != nil { @@ -882,7 +924,7 @@ func pkg(w http.ResponseWriter, r *http.Request) { return } if r.FormValue("go-get") == "1" { - fmt.Fprintf(w, `<meta name="go-import" content="roseh.moe/pkg/%s %s">`, r.PathValue("pkg"), source) + fmt.Fprintf(w, `<meta name="go-import" content="roseh.moe/pkg/%s %s">`, r.PathValue("pkg"), source.url) } else { http.Redirect(w, r, "https://pkg.go.dev/roseh.moe/pkg/"+r.PathValue("pkg"), http.StatusFound) } @@ -926,6 +968,7 @@ func main() { mux.HandleFunc("POST /login", login) mux.HandleFunc("GET /notepad", notepad) mux.HandleFunc("POST /notepad", autosave) + mux.HandleFunc("GET /cmd", cmdIndex) mux.HandleFunc("GET /cmd/{cmd}", cmd) mux.HandleFunc("GET /pkg", pkgIndex) mux.HandleFunc("GET /pkg/{pkg}", pkg) |
