diff options
-rw-r--r-- | handle_category_index.go | 34 | ||||
-rw-r--r-- | main.go | 1 | ||||
-rw-r--r-- | templates/category_index.html | 20 |
3 files changed, 55 insertions, 0 deletions
diff --git a/handle_category_index.go b/handle_category_index.go new file mode 100644 index 0000000..407d0e7 --- /dev/null +++ b/handle_category_index.go @@ -0,0 +1,34 @@ +package main + +import ( + "net/http" + "path/filepath" + "os" + "strings" +) + +func handle_category_index(w http.ResponseWriter, r *http.Request) { + data := make(map[string]any) + project_name := r.PathValue("project_name") + data["category_name"] = project_name + entries, err := os.ReadDir(filepath.Join(config.Git.Root, project_name)) + if err != nil { + _, _ = w.Write([]byte("Error listing repos: " + err.Error())) + return + } + + repos := []string{} + for _, entry := range entries { + this_name := entry.Name() + if strings.HasSuffix(this_name, ".git") { + repos = append(repos, strings.TrimSuffix(this_name, ".git")) + } + } + data["repos"] = repos + + err = templates.ExecuteTemplate(w, "category_index", data) + if err != nil { + _, _ = w.Write([]byte("Error rendering template: " + err.Error())) + return + } +} @@ -32,6 +32,7 @@ func main() { } http.HandleFunc("/{$}", handle_index) + http.HandleFunc("/{project_name}/{$}", handle_category_index) http.HandleFunc("/{project_name}/repos/{repo_name}/{$}", handle_repo_index) http.HandleFunc("/{project_name}/repos/{repo_name}/tree/{ref}/{rest...}", handle_repo_tree) diff --git a/templates/category_index.html b/templates/category_index.html new file mode 100644 index 0000000..1ae0c64 --- /dev/null +++ b/templates/category_index.html @@ -0,0 +1,20 @@ +{{- define "category_index" -}} +<!DOCTYPE html> +<html> +<head> +<link rel="stylesheet" href="/static/style.css" /> +<title>{{ .category_name }}</title> +</head> +<body class="index"> +<div class="padding-wrapper"> +<ul> +{{- range .repos }} +<li> +<a href="repos/{{ . }}/">{{ . }}</a> +</li> +{{- end }} +</ul> +</div> +</body> +</html> +{{- end -}} |