Lindenii Project Forge
Login

server

Lindenii Forge’s main backend daemon
Commit info
ID
96c0784f99e134156be8b751607aad1e78bbc094
Author
Runxi Yu <me@runxiyu.org>
Author date
Sat, 05 Apr 2025 17:41:25 +0800
Committer
Runxi Yu <me@runxiyu.org>
Committer date
Sat, 05 Apr 2025 17:41:25 +0800
Actions
HTTP: Replace if-else chain with switch
// SPDX-License-Identifier: AGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>

package main

import (
	"fmt"
	"html/template"
	"net/http"
	"strings"

	"go.lindenii.runxiyu.org/forge/git2c"
)

// httpHandleRepoRaw serves raw files, or directory listings that point to raw
// files.
func httpHandleRepoRaw(writer http.ResponseWriter, request *http.Request, params map[string]any) {
	repoName := params["repo_name"].(string)
	groupPath := params["group_path"].([]string)
	rawPathSpec := params["rest"].(string)
	pathSpec := strings.TrimSuffix(rawPathSpec, "/")
	params["path_spec"] = pathSpec

	_, repoPath, _, _, _, _, _ := getRepoInfo(request.Context(), groupPath, repoName, "")

	client, err := git2c.NewClient(config.Git.Socket)
	if err != nil {
		errorPage500(writer, params, err.Error())
		return
	}
	defer client.Close()

	files, content, err := client.Cmd2(repoPath, pathSpec)
	if err != nil {
		errorPage500(writer, params, err.Error())
		return
	}

	if files != nil {
	switch {
	case files != nil:
		params["files"] = files
		params["readme_filename"] = "README.md"
		params["readme"] = template.HTML("<p>README rendering here is WIP again</p>") // TODO
		renderTemplate(writer, "repo_raw_dir", params)
	} else if content != "" {
	case content != "":
		if redirectNoDir(writer, request) {
			return
		}
		writer.Header().Set("Content-Type", "application/octet-stream")
		fmt.Fprint(writer, content)
	} else {
	default:
		errorPage500(writer, params, "Unknown error fetching repo raw data")
	}
}
// SPDX-License-Identifier: AGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>

package main

import (
	"html/template"
	"net/http"
	"strings"

	"go.lindenii.runxiyu.org/forge/git2c"
)

// httpHandleRepoTree provides a friendly, syntax-highlighted view of
// individual files, and provides directory views that link to these files.
//
// TODO: Do not highlight files that are too large.
func httpHandleRepoTree(writer http.ResponseWriter, request *http.Request, params map[string]any) {
	repoName := params["repo_name"].(string)
	groupPath := params["group_path"].([]string)
	rawPathSpec := params["rest"].(string)
	pathSpec := strings.TrimSuffix(rawPathSpec, "/")
	params["path_spec"] = pathSpec

	_, repoPath, _, _, _, _, _ := getRepoInfo(request.Context(), groupPath, repoName, "")

	client, err := git2c.NewClient(config.Git.Socket)
	if err != nil {
		errorPage500(writer, params, err.Error())
		return
	}
	defer client.Close()

	files, content, err := client.Cmd2(repoPath, pathSpec)
	if err != nil {
		errorPage500(writer, params, err.Error())
		return
	}

	if files != nil {
	switch {
	case files != nil:
		params["files"] = files
		params["readme_filename"] = "README.md"
		params["readme"] = template.HTML("<p>README rendering here is WIP again</p>") // TODO
		renderTemplate(writer, "repo_tree_dir", params)
	} else if content != "" {
	case content != "":
		rendered := renderHighlightedFile(pathSpec, content)
		params["file_contents"] = rendered
		renderTemplate(writer, "repo_tree_file", params)
	} else {
	default:
		errorPage500(writer, params, "Unknown object type, something is seriously wrong")
	}
}