diff options
author | Runxi Yu <me@runxiyu.org> | 2025-04-06 01:55:21 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-04-06 02:08:58 +0800 |
commit | faa5ca8fab23176d390e9522f1485d467851545b (patch) | |
tree | d3b1d081e0ea5e7f71a94dc1d301e2540a8abcc8 /internal/unsorted/http_handle_repo_tree.go | |
parent | Slight refactor on NewServer (diff) | |
download | forge-faa5ca8fab23176d390e9522f1485d467851545b.tar.gz forge-faa5ca8fab23176d390e9522f1485d467851545b.tar.zst forge-faa5ca8fab23176d390e9522f1485d467851545b.zip |
Move stuff into internal/unsortedv0.1.28
Diffstat (limited to 'internal/unsorted/http_handle_repo_tree.go')
-rw-r--r-- | internal/unsorted/http_handle_repo_tree.go | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/internal/unsorted/http_handle_repo_tree.go b/internal/unsorted/http_handle_repo_tree.go new file mode 100644 index 0000000..cbbba98 --- /dev/null +++ b/internal/unsorted/http_handle_repo_tree.go @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org> + +package unsorted + +import ( + "html/template" + "net/http" + "strings" + + "go.lindenii.runxiyu.org/forge/internal/git2c" + "go.lindenii.runxiyu.org/forge/internal/render" + "go.lindenii.runxiyu.org/forge/internal/web" +) + +// 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 (s *Server) 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, _, _, _, _, _ := s.getRepoInfo(request.Context(), groupPath, repoName, "") + + client, err := git2c.NewClient(s.config.Git.Socket) + if err != nil { + web.ErrorPage500(s.templates, writer, params, err.Error()) + return + } + defer client.Close() + + files, content, err := client.Cmd2(repoPath, pathSpec) + if err != nil { + web.ErrorPage500(s.templates, writer, params, err.Error()) + return + } + + 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 + s.renderTemplate(writer, "repo_tree_dir", params) + case content != "": + rendered := render.Highlight(pathSpec, content) + params["file_contents"] = rendered + s.renderTemplate(writer, "repo_tree_file", params) + default: + web.ErrorPage500(s.templates, writer, params, "Unknown object type, something is seriously wrong") + } +} |