Lindenii Project Forge
Commit info | |
---|---|
ID | e347064abe3ce4c90fbad23d36e5d61a149e2389 |
Author | Runxi Yu<me@runxiyu.org> |
Author date | Fri, 14 Feb 2025 08:48:07 +0800 |
Committer | Runxi Yu<me@runxiyu.org> |
Committer date | Fri, 14 Feb 2025 08:48:07 +0800 |
Actions | Get patch |
http_*: Refactor to reduce duplication
package main import ( "errors" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" "go.lindenii.runxiyu.org/lindenii-common/misc" ) var ( err_getting_tag_reference = errors.New("Error getting tag reference") err_getting_branch_reference = errors.New("Error getting branch reference") err_getting_head = errors.New("Error getting HEAD") ) func get_ref_hash_from_type_and_name(repo *git.Repository, ref_type, ref_name string) (ref_hash plumbing.Hash, ret_err error) { switch ref_type {
case "head":
case "":
head, err := repo.Head() if err != nil { ret_err = misc.Wrap_one_error(err_getting_head, err) return } ref_hash = head.Hash() case "commit": ref_hash = plumbing.NewHash(ref_name) case "branch": ref, err := repo.Reference(plumbing.NewBranchReferenceName(ref_name), true) if err != nil { ret_err = misc.Wrap_one_error(err_getting_branch_reference, err) return } ref_hash = ref.Hash() case "tag": ref, err := repo.Reference(plumbing.NewTagReferenceName(ref_name), true) if err != nil { ret_err = misc.Wrap_one_error(err_getting_tag_reference, err) return } ref_hash = ref.Hash() default: panic("Invalid ref type " + ref_type) } return }
package main import ( "net/http" ) func handle_repo_index(w http.ResponseWriter, r *http.Request, params map[string]any) { group_name, repo_name := params["group_name"].(string), params["repo_name"].(string) repo, description, err := open_git_repo(r.Context(), group_name, repo_name) if err != nil { http.Error(w, "Error opening repo: "+err.Error(), http.StatusInternalServerError) return } params["repo_description"] = description head, err := repo.Head() if err != nil { http.Error(w, "Error getting repo HEAD: "+err.Error(), http.StatusInternalServerError) return }
params["ref"] = head.Name().Short()
head_hash := head.Hash() recent_commits, err := get_recent_commits(repo, head_hash, 3) if err != nil { http.Error(w, "Error getting recent commits: "+err.Error(), http.StatusInternalServerError) return } params["commits"] = recent_commits commit_object, err := repo.CommitObject(head_hash) if err != nil { http.Error(w, "Error getting commit object: "+err.Error(), http.StatusInternalServerError) return } tree, err := commit_object.Tree() if err != nil { http.Error(w, "Error getting file tree: "+err.Error(), http.StatusInternalServerError) return } params["readme_filename"], params["readme"] = render_readme_at_tree(tree) params["files"] = build_display_git_tree(tree) params["clone_url"] = generate_ssh_remote_url(group_name, repo_name) err = templates.ExecuteTemplate(w, "repo_index", params) if err != nil { http.Error(w, "Error rendering template: "+err.Error(), http.StatusInternalServerError) return } }
package main import ( "net/http" "github.com/go-git/go-git/v5/plumbing" ) // TODO: I probably shouldn't include *all* commits here... func handle_repo_log(w http.ResponseWriter, r *http.Request, params map[string]any) {
group_name, repo_name, ref_name := params["group_name"].(string), params["repo_name"].(string), params["ref"].(string)
group_name, repo_name, ref_name := params["group_name"].(string), params["repo_name"].(string), params["ref_name"].(string)
repo, description, err := open_git_repo(r.Context(), group_name, repo_name) if err != nil { http.Error(w, "Error opening repo: "+err.Error(), http.StatusInternalServerError) return } params["repo_description"] = description ref, err := repo.Reference(plumbing.NewBranchReferenceName(ref_name), true) if err != nil { http.Error(w, "Error getting repo reference: "+err.Error(), http.StatusInternalServerError) return } ref_hash := ref.Hash() commits, err := get_recent_commits(repo, ref_hash, -1) if err != nil { http.Error(w, "Error getting recent commits: "+err.Error(), http.StatusInternalServerError) return } params["commits"] = commits err = templates.ExecuteTemplate(w, "repo_log", params) if err != nil { http.Error(w, "Error rendering template: "+err.Error(), http.StatusInternalServerError) return } }
package main import (
"errors"
"fmt" "net/http" "path" "strings" "github.com/go-git/go-git/v5/plumbing/object" ) func handle_repo_raw(w http.ResponseWriter, r *http.Request, params map[string]any) { raw_path_spec := params["rest"].(string) group_name, repo_name, path_spec := params["group_name"].(string), params["repo_name"].(string), strings.TrimSuffix(raw_path_spec, "/")
ref_type, ref_name, err := get_param_ref_and_type(r) if err != nil { if errors.Is(err, err_no_ref_spec) { ref_type = "head" } else { http.Error(w, "Error querying ref type: "+err.Error(), http.StatusInternalServerError) return } } params["ref_type"], params["ref"], params["path_spec"] = ref_type, ref_name, path_spec
params["path_spec"] = path_spec
repo, description, err := open_git_repo(r.Context(), group_name, repo_name) if err != nil { http.Error(w, "Error opening repo: "+err.Error(), http.StatusInternalServerError) return } params["repo_description"] = description
ref_hash, err := get_ref_hash_from_type_and_name(repo, ref_type, ref_name)
ref_hash, err := get_ref_hash_from_type_and_name(repo, params["ref_type"].(string), params["ref_name"].(string))
if err != nil { http.Error(w, "Error getting ref hash: "+err.Error(), http.StatusInternalServerError) return } commit_object, err := repo.CommitObject(ref_hash) if err != nil { http.Error(w, "Error getting commit object: "+err.Error(), http.StatusInternalServerError) return } tree, err := commit_object.Tree() if err != nil { http.Error(w, "Error getting file tree: "+err.Error(), http.StatusInternalServerError) return } var target *object.Tree if path_spec == "" { target = tree } else { target, err = tree.Tree(path_spec) if err != nil { file, err := tree.File(path_spec) if err != nil { http.Error(w, "Error retrieving path: "+err.Error(), http.StatusInternalServerError) return } if len(raw_path_spec) != 0 && raw_path_spec[len(raw_path_spec)-1] == '/' { http.Redirect(w, r, "../"+path_spec, http.StatusSeeOther) return } file_contents, err := file.Contents() if err != nil { http.Error(w, "Error reading file: "+err.Error(), http.StatusInternalServerError) return } fmt.Fprintln(w, file_contents) return } } if len(raw_path_spec) != 0 && raw_path_spec[len(raw_path_spec)-1] != '/' { http.Redirect(w, r, path.Base(path_spec)+"/", http.StatusSeeOther) return } params["files"] = build_display_git_tree(target) err = templates.ExecuteTemplate(w, "repo_raw_dir", params) if err != nil { http.Error(w, "Error rendering template: "+err.Error(), http.StatusInternalServerError) return } }
package main import ( "bytes"
"errors"
"html/template" "net/http" "path" "strings" chroma_formatters_html "github.com/alecthomas/chroma/v2/formatters/html" chroma_lexers "github.com/alecthomas/chroma/v2/lexers" chroma_styles "github.com/alecthomas/chroma/v2/styles" "github.com/go-git/go-git/v5/plumbing/object" ) func handle_repo_tree(w http.ResponseWriter, r *http.Request, params map[string]any) { raw_path_spec := params["rest"].(string) group_name, repo_name, path_spec := params["group_name"].(string), params["repo_name"].(string), strings.TrimSuffix(raw_path_spec, "/")
ref_type, ref_name, err := get_param_ref_and_type(r) if err != nil { if errors.Is(err, err_no_ref_spec) { ref_type = "head" } else { http.Error(w, "Error querying ref type: "+err.Error(), http.StatusInternalServerError) return } } params["ref_type"], params["ref"], params["path_spec"] = ref_type, ref_name, path_spec
params["path_spec"] = path_spec
repo, description, err := open_git_repo(r.Context(), group_name, repo_name) if err != nil { http.Error(w, "Error opening repo: "+err.Error(), http.StatusInternalServerError) return } params["repo_description"] = description
ref_hash, err := get_ref_hash_from_type_and_name(repo, ref_type, ref_name)
ref_hash, err := get_ref_hash_from_type_and_name(repo, params["ref_type"].(string), params["ref_name"].(string))
if err != nil { http.Error(w, "Error getting ref hash: "+err.Error(), http.StatusInternalServerError) return } commit_object, err := repo.CommitObject(ref_hash) if err != nil { http.Error(w, "Error getting commit object: "+err.Error(), http.StatusInternalServerError) return } tree, err := commit_object.Tree() if err != nil { http.Error(w, "Error getting file tree: "+err.Error(), http.StatusInternalServerError) return } var target *object.Tree if path_spec == "" { target = tree } else { target, err = tree.Tree(path_spec) if err != nil { file, err := tree.File(path_spec) if err != nil { http.Error(w, "Error retrieving path: "+err.Error(), http.StatusInternalServerError) return } if len(raw_path_spec) != 0 && raw_path_spec[len(raw_path_spec)-1] == '/' { http.Redirect(w, r, "../"+path_spec, http.StatusSeeOther) return } file_contents, err := file.Contents() if err != nil { http.Error(w, "Error reading file: "+err.Error(), http.StatusInternalServerError) return } lexer := chroma_lexers.Match(path_spec) if lexer == nil { lexer = chroma_lexers.Fallback } iterator, err := lexer.Tokenise(nil, file_contents) if err != nil { http.Error(w, "Error tokenizing code: "+err.Error(), http.StatusInternalServerError) return } var formatted_unencapsulated bytes.Buffer style := chroma_styles.Get("autumn") formatter := chroma_formatters_html.New(chroma_formatters_html.WithClasses(true), chroma_formatters_html.TabWidth(8)) err = formatter.Format(&formatted_unencapsulated, style, iterator) if err != nil { http.Error(w, "Error formatting code: "+err.Error(), http.StatusInternalServerError) return } formatted_encapsulated := template.HTML(formatted_unencapsulated.Bytes()) params["file_contents"] = formatted_encapsulated err = templates.ExecuteTemplate(w, "repo_tree_file", params) if err != nil { http.Error(w, "Error rendering template: "+err.Error(), http.StatusInternalServerError) return } return } } if len(raw_path_spec) != 0 && raw_path_spec[len(raw_path_spec)-1] != '/' { http.Redirect(w, r, path.Base(path_spec)+"/", http.StatusSeeOther) return } params["readme_filename"], params["readme"] = render_readme_at_tree(target) params["files"] = build_display_git_tree(target) err = templates.ExecuteTemplate(w, "repo_tree_dir", params) if err != nil { http.Error(w, "Error rendering template: "+err.Error(), http.StatusInternalServerError) return } }
package main import ( "errors" "fmt" "net/http" "strconv" "strings" "github.com/jackc/pgx/v5" "go.lindenii.runxiyu.org/lindenii-common/clog" ) type http_router_t struct{} func (router *http_router_t) ServeHTTP(w http.ResponseWriter, r *http.Request) { clog.Debug("Incoming HTTP: " + r.RemoteAddr + " " + r.Method + " " + r.RequestURI) segments, _, err := parse_request_uri(r.RequestURI) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } non_empty_last_segments_len := len(segments) dir_mode := false if segments[len(segments)-1] == "" { non_empty_last_segments_len-- dir_mode = true } if segments[0] == ":" { if len(segments) < 2 { http.Error(w, "Blank system endpoint", http.StatusNotFound) return } else if len(segments) == 2 && !dir_mode { http.Redirect(w, r, r.URL.Path+"/", http.StatusSeeOther) return } switch segments[1] { case "static": static_handler.ServeHTTP(w, r) return case "source": source_handler.ServeHTTP(w, r) return } } params := make(map[string]any) params["global"] = global_data var _user_id int // 0 for none _user_id, params["username"], err = get_user_info_from_request(r) if errors.Is(err, http.ErrNoCookie) { } else if errors.Is(err, pgx.ErrNoRows) { } else if err != nil { http.Error(w, "Error getting user info from request: "+err.Error(), http.StatusInternalServerError) return } if _user_id == 0 { params["user_id"] = "" } else { params["user_id"] = strconv.Itoa(_user_id) } if segments[0] == ":" { switch segments[1] { case "login": handle_login(w, r, params) return case "users": handle_users(w, r, params) return default: http.Error(w, fmt.Sprintf("Unknown system module type: %s", segments[1]), http.StatusNotFound) return } } separator_index := -1 for i, part := range segments { if part == ":" { separator_index = i break } } switch { case non_empty_last_segments_len == 0: handle_index(w, r, params) case separator_index == -1: http.Error(w, "Group indexing hasn't been implemented yet", http.StatusNotImplemented) case non_empty_last_segments_len == separator_index+1: http.Error(w, "Group root hasn't been implemented yet", http.StatusNotImplemented) case non_empty_last_segments_len == separator_index+2: if !dir_mode { http.Redirect(w, r, r.URL.Path+"/", http.StatusSeeOther) return } module_type := segments[separator_index+1] params["group_name"] = segments[0] switch module_type { case "repos": handle_group_repos(w, r, params) default: http.Error(w, fmt.Sprintf("Unknown module type: %s", module_type), http.StatusNotFound) } default: module_type := segments[separator_index+1] module_name := segments[separator_index+2] params["group_name"] = segments[0] switch module_type { case "repos": params["repo_name"] = module_name
params["ref_type"], params["ref_name"], err = get_param_ref_and_type(r) if err != nil { if errors.Is(err, err_no_ref_spec) { params["ref_type"] = "" } else { http.Error(w, "Error querying ref type: "+err.Error(), http.StatusInternalServerError) return } }
// TODO: subgroups if non_empty_last_segments_len == separator_index+3 { if !dir_mode { http.Redirect(w, r, r.URL.Path+"/", http.StatusSeeOther) return } handle_repo_index(w, r, params) return } repo_feature := segments[separator_index+3] switch repo_feature { case "info": handle_repo_info(w, r, params) case "tree": params["rest"] = strings.Join(segments[separator_index+4:], "/") handle_repo_tree(w, r, params) case "raw": params["rest"] = strings.Join(segments[separator_index+4:], "/") handle_repo_raw(w, r, params) case "log":
if non_empty_last_segments_len != separator_index+5 {
if non_empty_last_segments_len > separator_index+5 {
http.Error(w, "Too many parameters", http.StatusBadRequest)
return } else if non_empty_last_segments_len < separator_index+5 { http.Error(w, "Insufficient parameters", http.StatusBadRequest)
return } if dir_mode { http.Redirect(w, r, strings.TrimSuffix(r.URL.Path, "/"), http.StatusSeeOther) return }
params["ref"] = segments[separator_index+4]
params["ref_name"] = segments[separator_index+4]
handle_repo_log(w, r, params) case "commit": if dir_mode { http.Redirect(w, r, strings.TrimSuffix(r.URL.Path, "/"), http.StatusSeeOther) return } params["commit_id"] = segments[separator_index+4] handle_repo_commit(w, r, params) default: http.Error(w, fmt.Sprintf("Unknown repo feature: %s", repo_feature), http.StatusNotFound) } default: http.Error(w, fmt.Sprintf("Unknown module type: %s", module_type), http.StatusNotFound) } } } var err_bad_request = errors.New("Bad Request")
{{- define "repo_index" -}} <!DOCTYPE html> <html lang="en"> <head> {{ template "head_common" . }} <title>{{ .group_name }}/repos/{{ .repo_name }} – Lindenii Forge</title> </head> <body class="repo-index"> {{ template "header" . }} <div class="padding-wrapper"> <table id="repo-info-table"> <thead> <tr class="title-row"> <th colspan="2">Repo Info</th> </tr> </thead> <tbody> <tr> <th scope="row">Name</th> <td>{{ .repo_name }}</td> </tr> <tr> <th scope="row">Clone</th> <td><code>git clone {{ .clone_url }}</code></td> </tr> {{ if .repo_description }} <tr> <th scope="row">Description</th> <td>{{ .repo_description }}</td> </tr> {{ end }} </tbody> </table> </div> <div class="padding-wrapper scroll"> <input id="toggle-table-recent-commits" type="checkbox" class="toggle-table-off" /> <table id="recent-commits" class="wide"> <thead> <tr class="title-row">
<th colspan="3"><label for="toggle-table-recent-commits">Recent Commits (<a href="log/{{ .ref }}/">see all</a>)</label></th>
<th colspan="3"><label for="toggle-table-recent-commits">Recent Commits (<a href="log/{{ .ref_name }}/">see all</a>)</label></th>
</tr> </thead> <tbody> {{- range .commits }} <tr> <td class="commit-title"><a href="commit/{{ .ID }}">{{ .Message | first_line }}</a></td> <td class="commit-author"> <a class="email-name" href="mailto:{{ .Author.Email }}">{{ .Author.Name }}</a> </td> <td class="commit-time"> {{ .Author.When.Format "2006-01-02 15:04:05 -0700" }} </td> </tr> {{- end }} </tbody> </table> </div> <div class="padding-wrapper scroll"> <input id="toggle-table-file-tree" type="checkbox" class="toggle-table-off" /> <table id="file-tree" class="wide"> <thead> <tr class="title-row">
<th colspan="3"><label for="toggle-table-file-tree">/ on {{ .ref }}</label></th>
<th colspan="3"><label for="toggle-table-file-tree">/ on {{ .ref_name }}</label></th>
</tr> </thead> <tbody>
{{- $ref := .ref }}
{{- $ref := .ref_name }}
{{- range .files }} <tr> <td class="file-mode">{{ .Mode }}</td> <td class="file-name"><a href="tree/{{ .Name }}">{{ .Name }}</a>{{ if not .Is_file }}/{{ end }}</td> <td class="file-size">{{ .Size }}</td> </tr> {{- end }} </tbody> </table> </div> <div class="padding-wrapper"> <div id="refs"> </div> </div> <div class="padding-wrapper"> {{ if .readme }} <input id="toggle-table-readme" type="checkbox" class="toggle-table-off" /> <table class="wide"> <thead> <tr class="title-row"> <th><label for="toggle-table-readme">{{ .readme_filename }}</label></th> </tr> </thead> <tbody> <tr> <td id="readme"> {{ .readme -}} </td> </tr> </tbody> </table> {{ end }} </div> <footer> {{ template "footer" . }} </footer> </body> </html> {{- end -}}
{{- define "repo_log" -}} <!DOCTYPE html> <html lang="en"> <head> {{ template "head_common" . }} <title>Log of {{ .group_name }}/repos/{{ .repo_name }} – Lindenii Forge</title> </head> <body class="repo-log"> {{ template "header" . }} <div class="scroll"> <table id="commits" class="wide"> <thead> <tr class="title-row">
<th colspan="4">Commits on {{ .ref }}</th>
<th colspan="4">Commits on {{ .ref_name }}</th>
</tr> <tr> <th scope="col">ID</th> <th scope="col">Title</th> <th scope="col">Author</th> <th scope="col">Time</th> </tr> </thead> <tbody> {{- range .commits }} <tr> <td class="commit-id"><a href="../commit/{{ .ID }}">{{ .ID }}</a></td> <td class="commit-title">{{ .Message | first_line }}</td> <td class="commit-author"> <a class="email-name" href="mailto:{{ .Author.Email }}">{{ .Author.Name }}</a> </td> <td class="commit-time"> {{ .Author.When.Format "2006-01-02 15:04:05 -0700" }} </td> </tr> {{- end }} </tbody> </table> </div> <footer> {{ template "footer" . }} </footer> </body> </html> {{- end -}}
{{- define "repo_raw_dir" -}} <!DOCTYPE html> <html lang="en"> <head> {{ template "head_common" . }} <title>{{ .group_name }}/repos/{{ .repo_name }}/{{ .path_spec }}{{ if ne .path_spec "" }}/{{ end }} – Lindenii Forge</title> </head> <body class="repo-raw-dir"> {{ template "header" . }} <div class="padding-wrapper scroll"> <table id="file-tree" class="wide"> <thead> <tr class="title-row"> <th colspan="3">
(Raw) /{{ .path_spec }}{{ if ne .path_spec "" }}/{{ end }}{{ if .ref }} on {{ .ref }}{{ end }}
(Raw) /{{ .path_spec }}{{ if ne .path_spec "" }}/{{ end }}{{ if .ref_name }} on {{ .ref_name }}{{ end }}
</th> </tr> </thead> <tbody> {{- $path_spec := .path_spec }}
{{- $ref := .ref }}
{{- $ref := .ref_name }}
{{- $ref_type := .ref_type }} {{- range .files }} <tr> <td class="file-mode">{{ .Mode }}</td>
<td class="file-name"><a href="{{ .Name }}{{ if not .Is_file }}/{{ end }}{{ if $ref }}?{{ $ref_type }}={{ $ref }}{{ end }}">{{ .Name }}</a>{{ if not .Is_file }}/{{ end }}</td>
<td class="file-name"><a href="{{ .Name }}{{ if not .Is_file }}/{{ end }}{{ if $ref_type }}?{{ $ref_type }}={{ $ref }}{{ end }}">{{ .Name }}</a>{{ if not .Is_file }}/{{ end }}</td>
<td class="file-size">{{ .Size }}</td> </tr> {{- end }} </tbody> </table> </div> <div class="padding-wrapper"> <div id="refs"> </div> </div> <footer> {{ template "footer" . }} </footer> </body> </html> {{- end -}}
{{- define "repo_tree_dir" -}} <!DOCTYPE html> <html lang="en"> <head> {{ template "head_common" . }} <title>{{ .group_name }}/repos/{{ .repo_name }}/{{ .path_spec }}{{ if ne .path_spec "" }}/{{ end }} – Lindenii Forge</title> </head> <body class="repo-tree-dir"> {{ template "header" . }} <div class="padding-wrapper scroll"> <table id="file-tree" class="wide"> <thead> <tr class="title-row"> <th colspan="3">
/{{ .path_spec }}{{ if ne .path_spec "" }}/{{ end }}{{ if .ref }} on {{ .ref }}{{ end }}
/{{ .path_spec }}{{ if ne .path_spec "" }}/{{ end }}{{ if .ref_name }} on {{ .ref_name }}{{ end }}
</th> </tr> </thead> <tbody> {{- $path_spec := .path_spec }}
{{- $ref := .ref }}
{{- $ref := .ref_name }}
{{- $ref_type := .ref_type }} {{- range .files }} <tr> <td class="file-mode">{{ .Mode }}</td>
<td class="file-name"><a href="{{ .Name }}{{ if not .Is_file }}/{{ end }}{{ if $ref }}?{{ $ref_type }}={{ $ref }}{{ end }}">{{ .Name }}</a>{{ if not .Is_file }}/{{ end }}</td>
<td class="file-name"><a href="{{ .Name }}{{ if not .Is_file }}/{{ end }}{{ if $ref_type }}?{{ $ref_type }}={{ $ref }}{{ end }}">{{ .Name }}</a>{{ if not .Is_file }}/{{ end }}</td>
<td class="file-size">{{ .Size }}</td> </tr> {{- end }} </tbody> </table> </div> <div class="padding-wrapper"> <div id="refs"> </div> </div> <div class="padding-wrapper"> {{ if .readme }} <table class="wide"> <thead> <tr class="title-row"> <th>{{ .readme_filename }}</th> </tr> </thead> <tbody> <tr> <td id="readme"> {{ .readme -}} </td> </tr> </tbody> </table> {{ end }} </div> <footer> {{ template "footer" . }} </footer> </body> </html> {{- end -}}
{{- define "repo_tree_file" -}} <!DOCTYPE html> <html lang="en"> <head> {{ template "head_common" . }} <link rel="stylesheet" href="/:/static/chroma.css" /> <title>{{ .group_name }}/repos/{{ .repo_name }}/{{ .path_spec }} – Lindenii Forge</title> </head> <body class="repo-tree-file"> {{ template "header" . }} <div class="padding"> <p>
/{{ .path_spec }} (<a href="/{{ .group_name }}/:/repos/{{ .repo_name }}/raw/{{ .path_spec }}{{ if .ref }}?{{ .ref_type }}={{ .ref }}{{ end }}">raw</a>)
/{{ .path_spec }} (<a href="/{{ .group_name }}/:/repos/{{ .repo_name }}/raw/{{ .path_spec }}{{ if .ref_type }}?{{ .ref_type }}={{ .ref_name }}{{ end }}">raw</a>)
</p> {{ .file_contents }} </div> <footer> {{ template "footer" . }} </footer> </body> </html> {{- end -}}