Lindenii Project Forge
Login
Commit info
ID0a5f821f78425d97c0797803e57aa2c2d6f982da
AuthorRunxi Yu<me@runxiyu.org>
Author dateMon, 10 Feb 2025 13:46:50 +0800
CommitterRunxi Yu<me@runxiyu.org>
Committer dateMon, 10 Feb 2025 13:46:50 +0800
Actions
Get patch
category_index: Add a repo index for each category
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
	}
}
package main

import (
	"flag"
	"net"
	"net/http"

	"go.lindenii.runxiyu.org/lindenii-common/clog"
)

func main() {
	config_path := flag.String(
		"config",
		"/etc/lindenii/forge.scfg",
		"path to configuration file",
	)
	flag.Parse()

	err := load_config(*config_path)
	if err != nil {
		clog.Fatal(1, "Loading configuration: "+err.Error())
	}

	err = load_templates()
	if err != nil {
		clog.Fatal(1, "Loading templates: "+err.Error())
	}

	err = serve_static()
	if err != nil {
		clog.Fatal(1, "Serving static: "+err.Error())
	}

	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)

	listener, err := net.Listen(config.HTTP.Net, config.HTTP.Addr)
	if err != nil {
		clog.Fatal(1, "Listening: "+err.Error())
	}

	err = http.Serve(listener, nil)
	if err != nil {
		clog.Fatal(1, "Serving: "+err.Error())
	}
}
{{- 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 -}}