Lindenii Project Forge
Login
Commit info
IDd0cf88cfc3d2c328f9e398ba94a770141f3a7be4
AuthorRunxi Yu<me@runxiyu.org>
Author dateMon, 10 Feb 2025 20:01:09 +0800
CommitterRunxi Yu<me@runxiyu.org>
Committer dateMon, 10 Feb 2025 20:01:09 +0800
Actions
Get patch
category_index -> category_repos
package main

import (
	"net/http"
	"os"
	"path/filepath"
	"strings"
)

func handle_category_index(w http.ResponseWriter, r *http.Request) {
func handle_category_repos(w http.ResponseWriter, r *http.Request) {
	data := make(map[string]any)
	category_name := r.PathValue("category_name")
	data["category_name"] = category_name
	entries, err := os.ReadDir(filepath.Join(config.Git.Root, category_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)
	err = templates.ExecuteTemplate(w, "category_repos", 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("/{category_name}/{$}", handle_category_index)
	http.HandleFunc("/{category_name}/repos/{$}", handle_category_repos)
	http.HandleFunc("/{category_name}/repos/{repo_name}/{$}", handle_repo_index)
	http.HandleFunc("/{category_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" -}}
{{- define "category_repos" -}}
<!DOCTYPE html>
<html>
	<head>
		{{ template "head_common" . }}
		<title>{{ .category_name }} &ndash; Lindenii Forge</title>
		<title>Repos in {{ .category_name }} &ndash; Lindenii Forge</title>
	</head>
	<body class="category-index">
		<div class="padding-wrapper">
			<h1>
			Repos in {{ .category_name }}
			</h1>
			<ul>
				{{- range .repos }}
					<li>
						<a href="repos/{{ . }}/">{{ . }}</a>
						<a href="{{ . }}/">{{ . }}</a>
					</li>
				{{- end }}
			</ul>
		</div>
	</body>
</html>
{{- end -}}
{{- define "index" -}}
<!DOCTYPE html>
<html>
	<head>
		{{ template "head_common" . }}
		<title>Categories &ndash; Lindenii Forge</title>
	</head>
	<body class="index">
		<div class="padding-wrapper">
			<h1>
				Categories
			</h1>
			<ul>
				{{- range .categories }}
					<li>
						<a href="{{ . }}/">{{ . }}</a>
						<a href="{{ . }}/repos/">{{ . }}</a>
					</li>
				{{- end }}
			</ul>
		</div>
	</body>
</html>
{{- end -}}