diff options
-rw-r--r-- | http_handle_branches.go | 2 | ||||
-rw-r--r-- | http_handle_group_index.go | 30 | ||||
-rw-r--r-- | http_handle_index.go | 4 | ||||
-rw-r--r-- | http_handle_login.go | 16 | ||||
-rw-r--r-- | http_handle_repo_commit.go | 10 | ||||
-rw-r--r-- | http_handle_repo_contrib_index.go | 8 | ||||
-rw-r--r-- | http_handle_repo_contrib_one.go | 20 | ||||
-rw-r--r-- | http_handle_repo_index.go | 6 | ||||
-rw-r--r-- | http_handle_repo_log.go | 8 | ||||
-rw-r--r-- | http_handle_repo_raw.go | 8 | ||||
-rw-r--r-- | http_handle_repo_tree.go | 10 | ||||
-rw-r--r-- | http_handle_users.go | 4 | ||||
-rw-r--r-- | http_server.go | 42 | ||||
-rw-r--r-- | http_template.go | 4 | ||||
-rw-r--r-- | resources.go | 8 | ||||
-rw-r--r-- | server.go | 5 |
16 files changed, 93 insertions, 92 deletions
diff --git a/http_handle_branches.go b/http_handle_branches.go index 659287f..7709d4e 100644 --- a/http_handle_branches.go +++ b/http_handle_branches.go @@ -42,5 +42,5 @@ func (s *Server) httpHandleRepoBranches(writer http.ResponseWriter, _ *http.Requ params["ssh_clone_url"] = s.genSSHRemoteURL(groupPath, repoName) params["notes"] = notes - renderTemplate(writer, "repo_branches", params) + s.renderTemplate(writer, "repo_branches", params) } diff --git a/http_handle_group_index.go b/http_handle_group_index.go index 63bb164..5d2edce 100644 --- a/http_handle_group_index.go +++ b/http_handle_group_index.go @@ -62,10 +62,10 @@ func (s *Server) httpHandleGroupIndex(writer http.ResponseWriter, request *http. ).Scan(&groupID, &groupDesc) if errors.Is(err, pgx.ErrNoRows) { - web.ErrorPage404(templates, writer, params) + web.ErrorPage404(s.templates, writer, params) return } else if err != nil { - web.ErrorPage500(templates, writer, params, "Error getting group: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error getting group: "+err.Error()) return } @@ -78,14 +78,14 @@ func (s *Server) httpHandleGroupIndex(writer http.ResponseWriter, request *http. AND group_id = $2 `, params["user_id"].(int), groupID).Scan(&count) if err != nil { - web.ErrorPage500(templates, writer, params, "Error checking access: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error checking access: "+err.Error()) return } directAccess := (count > 0) if request.Method == http.MethodPost { if !directAccess { - web.ErrorPage403(templates, writer, params, "You do not have direct access to this group") + web.ErrorPage403(s.templates, writer, params, "You do not have direct access to this group") return } @@ -93,7 +93,7 @@ func (s *Server) httpHandleGroupIndex(writer http.ResponseWriter, request *http. repoDesc := request.FormValue("repo_desc") contribReq := request.FormValue("repo_contrib") if repoName == "" { - web.ErrorPage400(templates, writer, params, "Repo name is required") + web.ErrorPage400(s.templates, writer, params, "Repo name is required") return } @@ -109,7 +109,7 @@ func (s *Server) httpHandleGroupIndex(writer http.ResponseWriter, request *http. contribReq, ).Scan(&newRepoID) if err != nil { - web.ErrorPage500(templates, writer, params, "Error creating repo: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error creating repo: "+err.Error()) return } @@ -124,12 +124,12 @@ func (s *Server) httpHandleGroupIndex(writer http.ResponseWriter, request *http. newRepoID, ) if err != nil { - web.ErrorPage500(templates, writer, params, "Error updating repo path: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error updating repo path: "+err.Error()) return } if err = s.gitInit(filePath); err != nil { - web.ErrorPage500(templates, writer, params, "Error initializing repo: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error initializing repo: "+err.Error()) return } @@ -145,7 +145,7 @@ func (s *Server) httpHandleGroupIndex(writer http.ResponseWriter, request *http. WHERE group_id = $1 `, groupID) if err != nil { - web.ErrorPage500(templates, writer, params, "Error getting repos: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error getting repos: "+err.Error()) return } defer rows.Close() @@ -153,13 +153,13 @@ func (s *Server) httpHandleGroupIndex(writer http.ResponseWriter, request *http. for rows.Next() { var name, description string if err = rows.Scan(&name, &description); err != nil { - web.ErrorPage500(templates, writer, params, "Error getting repos: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error getting repos: "+err.Error()) return } repos = append(repos, nameDesc{name, description}) } if err = rows.Err(); err != nil { - web.ErrorPage500(templates, writer, params, "Error getting repos: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error getting repos: "+err.Error()) return } @@ -170,7 +170,7 @@ func (s *Server) httpHandleGroupIndex(writer http.ResponseWriter, request *http. WHERE parent_group = $1 `, groupID) if err != nil { - web.ErrorPage500(templates, writer, params, "Error getting subgroups: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error getting subgroups: "+err.Error()) return } defer rows.Close() @@ -178,13 +178,13 @@ func (s *Server) httpHandleGroupIndex(writer http.ResponseWriter, request *http. for rows.Next() { var name, description string if err = rows.Scan(&name, &description); err != nil { - web.ErrorPage500(templates, writer, params, "Error getting subgroups: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error getting subgroups: "+err.Error()) return } subgroups = append(subgroups, nameDesc{name, description}) } if err = rows.Err(); err != nil { - web.ErrorPage500(templates, writer, params, "Error getting subgroups: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error getting subgroups: "+err.Error()) return } @@ -193,5 +193,5 @@ func (s *Server) httpHandleGroupIndex(writer http.ResponseWriter, request *http. params["description"] = groupDesc params["direct_access"] = directAccess - renderTemplate(writer, "group", params) + s.renderTemplate(writer, "group", params) } diff --git a/http_handle_index.go b/http_handle_index.go index a1ecfe4..c1efd98 100644 --- a/http_handle_index.go +++ b/http_handle_index.go @@ -19,7 +19,7 @@ func (s *Server) httpHandleIndex(writer http.ResponseWriter, request *http.Reque groups, err = s.queryNameDesc(request.Context(), "SELECT name, COALESCE(description, '') FROM groups WHERE parent_group IS NULL") if err != nil { - web.ErrorPage500(templates, writer, params, "Error querying groups: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error querying groups: "+err.Error()) return } params["groups"] = groups @@ -28,5 +28,5 @@ func (s *Server) httpHandleIndex(writer http.ResponseWriter, request *http.Reque memstats := runtime.MemStats{} //exhaustruct:ignore runtime.ReadMemStats(&memstats) params["mem"] = humanize.IBytes(memstats.Alloc) - renderTemplate(writer, "index", params) + s.renderTemplate(writer, "index", params) } diff --git a/http_handle_login.go b/http_handle_login.go index dfb74c8..4b7cacf 100644 --- a/http_handle_login.go +++ b/http_handle_login.go @@ -29,7 +29,7 @@ func (s *Server) httpHandleLogin(writer http.ResponseWriter, request *http.Reque var cookie http.Cookie if request.Method != http.MethodPost { - renderTemplate(writer, "login", params) + s.renderTemplate(writer, "login", params) return } @@ -43,31 +43,31 @@ func (s *Server) httpHandleLogin(writer http.ResponseWriter, request *http.Reque if err != nil { if errors.Is(err, pgx.ErrNoRows) { params["login_error"] = "Unknown username" - renderTemplate(writer, "login", params) + s.renderTemplate(writer, "login", params) return } - web.ErrorPage500(templates, writer, params, "Error querying user information: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error querying user information: "+err.Error()) return } if passwordHash == "" { params["login_error"] = "User has no password" - renderTemplate(writer, "login", params) + s.renderTemplate(writer, "login", params) return } if passwordMatches, err = argon2id.ComparePasswordAndHash(password, passwordHash); err != nil { - web.ErrorPage500(templates, writer, params, "Error comparing password and hash: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error comparing password and hash: "+err.Error()) return } if !passwordMatches { params["login_error"] = "Invalid password" - renderTemplate(writer, "login", params) + s.renderTemplate(writer, "login", params) return } if cookieValue, err = randomUrlsafeStr(16); err != nil { - web.ErrorPage500(templates, writer, params, "Error getting random string: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error getting random string: "+err.Error()) return } @@ -88,7 +88,7 @@ func (s *Server) httpHandleLogin(writer http.ResponseWriter, request *http.Reque _, err = s.database.Exec(request.Context(), "INSERT INTO sessions (user_id, session_id) VALUES ($1, $2)", userID, cookieValue) if err != nil { - web.ErrorPage500(templates, writer, params, "Error inserting session: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error inserting session: "+err.Error()) return } diff --git a/http_handle_repo_commit.go b/http_handle_repo_commit.go index c348dca..196489f 100644 --- a/http_handle_repo_commit.go +++ b/http_handle_repo_commit.go @@ -32,7 +32,7 @@ type usableChunk struct { Content string } -func httpHandleRepoCommit(writer http.ResponseWriter, request *http.Request, params map[string]any) { +func (s *Server) httpHandleRepoCommit(writer http.ResponseWriter, request *http.Request, params map[string]any) { var repo *git.Repository var commitIDStrSpec, commitIDStrSpecNoSuffix string var commitID plumbing.Hash @@ -47,13 +47,13 @@ func httpHandleRepoCommit(writer http.ResponseWriter, request *http.Request, par commitIDStrSpecNoSuffix = strings.TrimSuffix(commitIDStrSpec, ".patch") commitID = plumbing.NewHash(commitIDStrSpecNoSuffix) if commitObj, err = repo.CommitObject(commitID); err != nil { - web.ErrorPage500(templates, writer, params, "Error getting commit object: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error getting commit object: "+err.Error()) return } if commitIDStrSpecNoSuffix != commitIDStrSpec { var patchStr string if patchStr, err = fmtCommitPatch(commitObj); err != nil { - web.ErrorPage500(templates, writer, params, "Error formatting patch: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error formatting patch: "+err.Error()) return } fmt.Fprintln(writer, patchStr) @@ -71,7 +71,7 @@ func httpHandleRepoCommit(writer http.ResponseWriter, request *http.Request, par parentCommitHash, patch, err = commitToPatch(commitObj) if err != nil { - web.ErrorPage500(templates, writer, params, "Error getting patch from commit: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error getting patch from commit: "+err.Error()) return } params["parent_commit_hash"] = parentCommitHash.String() @@ -79,7 +79,7 @@ func httpHandleRepoCommit(writer http.ResponseWriter, request *http.Request, par params["file_patches"] = makeUsableFilePatches(patch) - renderTemplate(writer, "repo_commit", params) + s.renderTemplate(writer, "repo_commit", params) } type fakeDiffFile struct { diff --git a/http_handle_repo_contrib_index.go b/http_handle_repo_contrib_index.go index b83dfcb..7bdcb49 100644 --- a/http_handle_repo_contrib_index.go +++ b/http_handle_repo_contrib_index.go @@ -28,7 +28,7 @@ func (s *Server) httpHandleRepoContribIndex(writer http.ResponseWriter, request "SELECT repo_local_id, COALESCE(title, 'Untitled'), status FROM merge_requests WHERE repo_id = $1", params["repo_id"], ); err != nil { - web.ErrorPage500(templates, writer, params, "Error querying merge requests: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error querying merge requests: "+err.Error()) return } defer rows.Close() @@ -37,16 +37,16 @@ func (s *Server) httpHandleRepoContribIndex(writer http.ResponseWriter, request var mrID int var mrTitle, mrStatus string if err = rows.Scan(&mrID, &mrTitle, &mrStatus); err != nil { - web.ErrorPage500(templates, writer, params, "Error scanning merge request: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error scanning merge request: "+err.Error()) return } result = append(result, idTitleStatus{mrID, mrTitle, mrStatus}) } if err = rows.Err(); err != nil { - web.ErrorPage500(templates, writer, params, "Error ranging over merge requests: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error ranging over merge requests: "+err.Error()) return } params["merge_requests"] = result - renderTemplate(writer, "repo_contrib_index", params) + s.renderTemplate(writer, "repo_contrib_index", params) } diff --git a/http_handle_repo_contrib_one.go b/http_handle_repo_contrib_one.go index dac58d0..280dca8 100644 --- a/http_handle_repo_contrib_one.go +++ b/http_handle_repo_contrib_one.go @@ -29,7 +29,7 @@ func (s *Server) httpHandleRepoContribOne(writer http.ResponseWriter, request *h mrIDStr = params["mr_id"].(string) mrIDInt64, err := strconv.ParseInt(mrIDStr, 10, strconv.IntSize) if err != nil { - web.ErrorPage400(templates, writer, params, "Merge request ID not an integer") + web.ErrorPage400(s.templates, writer, params, "Merge request ID not an integer") return } mrIDInt = int(mrIDInt64) @@ -38,18 +38,18 @@ func (s *Server) httpHandleRepoContribOne(writer http.ResponseWriter, request *h "SELECT COALESCE(title, ''), status, source_ref, COALESCE(destination_branch, '') FROM merge_requests WHERE repo_id = $1 AND repo_local_id = $2", params["repo_id"], mrIDInt, ).Scan(&title, &status, &srcRefStr, &dstBranchStr); err != nil { - web.ErrorPage500(templates, writer, params, "Error querying merge request: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error querying merge request: "+err.Error()) return } repo = params["repo"].(*git.Repository) if srcRefHash, err = getRefHash(repo, "branch", srcRefStr); err != nil { - web.ErrorPage500(templates, writer, params, "Error getting source ref hash: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error getting source ref hash: "+err.Error()) return } if srcCommit, err = repo.CommitObject(srcRefHash); err != nil { - web.ErrorPage500(templates, writer, params, "Error getting source commit: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error getting source commit: "+err.Error()) return } params["source_commit"] = srcCommit @@ -61,23 +61,23 @@ func (s *Server) httpHandleRepoContribOne(writer http.ResponseWriter, request *h dstBranchHash, err = getRefHash(repo, "branch", dstBranchStr) } if err != nil { - web.ErrorPage500(templates, writer, params, "Error getting destination branch hash: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error getting destination branch hash: "+err.Error()) return } if dstCommit, err = repo.CommitObject(dstBranchHash); err != nil { - web.ErrorPage500(templates, writer, params, "Error getting destination commit: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error getting destination commit: "+err.Error()) return } params["destination_commit"] = dstCommit if mergeBases, err = srcCommit.MergeBase(dstCommit); err != nil { - web.ErrorPage500(templates, writer, params, "Error getting merge base: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error getting merge base: "+err.Error()) return } if len(mergeBases) < 1 { - web.ErrorPage500(templates, writer, params, "No merge base found for this merge request; these two branches do not share any common history") + web.ErrorPage500(s.templates, writer, params, "No merge base found for this merge request; these two branches do not share any common history") // TODO return } @@ -87,12 +87,12 @@ func (s *Server) httpHandleRepoContribOne(writer http.ResponseWriter, request *h patch, err := mergeBaseCommit.Patch(srcCommit) if err != nil { - web.ErrorPage500(templates, writer, params, "Error getting patch: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error getting patch: "+err.Error()) return } params["file_patches"] = makeUsableFilePatches(patch) params["mr_title"], params["mr_status"], params["mr_source_ref"], params["mr_destination_branch"] = title, status, srcRefStr, dstBranchStr - renderTemplate(writer, "repo_contrib_one", params) + s.renderTemplate(writer, "repo_contrib_one", params) } diff --git a/http_handle_repo_index.go b/http_handle_repo_index.go index bb5d57e..b564286 100644 --- a/http_handle_repo_index.go +++ b/http_handle_repo_index.go @@ -20,14 +20,14 @@ func (s *Server) httpHandleRepoIndex(w http.ResponseWriter, req *http.Request, p client, err := git2c.NewClient(s.config.Git.Socket) if err != nil { - web.ErrorPage500(templates, w, params, err.Error()) + web.ErrorPage500(s.templates, w, params, err.Error()) return } defer client.Close() commits, readme, err := client.Cmd1(repoPath) if err != nil { - web.ErrorPage500(templates, w, params, err.Error()) + web.ErrorPage500(s.templates, w, params, err.Error()) return } @@ -35,7 +35,7 @@ func (s *Server) httpHandleRepoIndex(w http.ResponseWriter, req *http.Request, p params["readme_filename"] = readme.Filename _, params["readme"] = render.Readme(readme.Content, readme.Filename) - renderTemplate(w, "repo_index", params) + s.renderTemplate(w, "repo_index", params) // TODO: Caching } diff --git a/http_handle_repo_log.go b/http_handle_repo_log.go index 14fe84c..fbc7478 100644 --- a/http_handle_repo_log.go +++ b/http_handle_repo_log.go @@ -15,7 +15,7 @@ import ( // // TODO: This currently provides all commits in the branch. It should be // paginated and cached instead. -func httpHandleRepoLog(writer http.ResponseWriter, _ *http.Request, params map[string]any) { +func (s *Server) httpHandleRepoLog(writer http.ResponseWriter, _ *http.Request, params map[string]any) { var repo *git.Repository var refHash plumbing.Hash var err error @@ -23,17 +23,17 @@ func httpHandleRepoLog(writer http.ResponseWriter, _ *http.Request, params map[s repo = params["repo"].(*git.Repository) if refHash, err = getRefHash(repo, params["ref_type"].(string), params["ref_name"].(string)); err != nil { - web.ErrorPage500(templates, writer, params, "Error getting ref hash: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error getting ref hash: "+err.Error()) return } logOptions := git.LogOptions{From: refHash} //exhaustruct:ignore commitIter, err := repo.Log(&logOptions) if err != nil { - web.ErrorPage500(templates, writer, params, "Error getting recent commits: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error getting recent commits: "+err.Error()) return } params["commits"], params["commits_err"] = commitIterSeqErr(commitIter) - renderTemplate(writer, "repo_log", params) + s.renderTemplate(writer, "repo_log", params) } diff --git a/http_handle_repo_raw.go b/http_handle_repo_raw.go index c7f5653..286cbdf 100644 --- a/http_handle_repo_raw.go +++ b/http_handle_repo_raw.go @@ -27,14 +27,14 @@ func (s *Server) httpHandleRepoRaw(writer http.ResponseWriter, request *http.Req client, err := git2c.NewClient(s.config.Git.Socket) if err != nil { - web.ErrorPage500(templates, writer, params, err.Error()) + web.ErrorPage500(s.templates, writer, params, err.Error()) return } defer client.Close() files, content, err := client.Cmd2(repoPath, pathSpec) if err != nil { - web.ErrorPage500(templates, writer, params, err.Error()) + web.ErrorPage500(s.templates, writer, params, err.Error()) return } @@ -43,7 +43,7 @@ func (s *Server) httpHandleRepoRaw(writer http.ResponseWriter, request *http.Req 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) + s.renderTemplate(writer, "repo_raw_dir", params) case content != "": if misc.RedirectNoDir(writer, request) { return @@ -51,6 +51,6 @@ func (s *Server) httpHandleRepoRaw(writer http.ResponseWriter, request *http.Req writer.Header().Set("Content-Type", "application/octet-stream") fmt.Fprint(writer, content) default: - web.ErrorPage500(templates, writer, params, "Unknown error fetching repo raw data") + web.ErrorPage500(s.templates, writer, params, "Unknown error fetching repo raw data") } } diff --git a/http_handle_repo_tree.go b/http_handle_repo_tree.go index b855fe4..d127e5d 100644 --- a/http_handle_repo_tree.go +++ b/http_handle_repo_tree.go @@ -28,14 +28,14 @@ func (s *Server) httpHandleRepoTree(writer http.ResponseWriter, request *http.Re client, err := git2c.NewClient(s.config.Git.Socket) if err != nil { - web.ErrorPage500(templates, writer, params, err.Error()) + web.ErrorPage500(s.templates, writer, params, err.Error()) return } defer client.Close() files, content, err := client.Cmd2(repoPath, pathSpec) if err != nil { - web.ErrorPage500(templates, writer, params, err.Error()) + web.ErrorPage500(s.templates, writer, params, err.Error()) return } @@ -44,12 +44,12 @@ func (s *Server) httpHandleRepoTree(writer http.ResponseWriter, request *http.Re 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) + s.renderTemplate(writer, "repo_tree_dir", params) case content != "": rendered := render.Highlight(pathSpec, content) params["file_contents"] = rendered - renderTemplate(writer, "repo_tree_file", params) + s.renderTemplate(writer, "repo_tree_file", params) default: - web.ErrorPage500(templates, writer, params, "Unknown object type, something is seriously wrong") + web.ErrorPage500(s.templates, writer, params, "Unknown object type, something is seriously wrong") } } diff --git a/http_handle_users.go b/http_handle_users.go index ce6c045..816d703 100644 --- a/http_handle_users.go +++ b/http_handle_users.go @@ -10,6 +10,6 @@ import ( ) // httpHandleUsers is a useless stub. -func httpHandleUsers(writer http.ResponseWriter, _ *http.Request, params map[string]any) { - web.ErrorPage501(templates, writer, params) +func (s *Server) httpHandleUsers(writer http.ResponseWriter, _ *http.Request, params map[string]any) { + web.ErrorPage501(s.templates, writer, params) } diff --git a/http_server.go b/http_server.go index b2c93b9..717f223 100644 --- a/http_server.go +++ b/http_server.go @@ -40,7 +40,7 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) { params := make(map[string]any) if segments, _, err = misc.ParseReqURI(request.RequestURI); err != nil { - web.ErrorPage400(templates, writer, params, "Error parsing request URI: "+err.Error()) + web.ErrorPage400(s.templates, writer, params, "Error parsing request URI: "+err.Error()) return } dirMode := false @@ -56,7 +56,7 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) { userID, params["username"], err = s.getUserFromRequest(request) params["user_id"] = userID if err != nil && !errors.Is(err, http.ErrNoCookie) && !errors.Is(err, pgx.ErrNoRows) { - web.ErrorPage500(templates, writer, params, "Error getting user info from request: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error getting user info from request: "+err.Error()) return } @@ -68,7 +68,7 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) { for _, v := range segments { if strings.Contains(v, ":") { - web.ErrorPage400Colon(templates, writer, params) + web.ErrorPage400Colon(s.templates, writer, params) return } } @@ -80,7 +80,7 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) { if segments[0] == "-" { if len(segments) < 2 { - web.ErrorPage404(templates, writer, params) + web.ErrorPage404(s.templates, writer, params) return } else if len(segments) == 2 && misc.RedirectDir(writer, request) { return @@ -102,10 +102,10 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) { s.httpHandleLogin(writer, request, params) return case "users": - httpHandleUsers(writer, request, params) + s.httpHandleUsers(writer, request, params) return default: - web.ErrorPage404(templates, writer, params) + web.ErrorPage404(s.templates, writer, params) return } } @@ -138,10 +138,10 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) { } s.httpHandleGroupIndex(writer, request, params) case len(segments) == sepIndex+1: - web.ErrorPage404(templates, writer, params) + web.ErrorPage404(s.templates, writer, params) return case len(segments) == sepIndex+2: - web.ErrorPage404(templates, writer, params) + web.ErrorPage404(s.templates, writer, params) return default: moduleType = segments[sepIndex+1] @@ -154,12 +154,12 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) { switch segments[sepIndex+3] { case "info": if err = s.httpHandleRepoInfo(writer, request, params); err != nil { - web.ErrorPage500(templates, writer, params, err.Error()) + web.ErrorPage500(s.templates, writer, params, err.Error()) } return case "git-upload-pack": if err = s.httpHandleUploadPack(writer, request, params); err != nil { - web.ErrorPage500(templates, writer, params, err.Error()) + web.ErrorPage500(s.templates, writer, params, err.Error()) } return } @@ -169,13 +169,13 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) { if errors.Is(err, misc.ErrNoRefSpec) { params["ref_type"] = "" } else { - web.ErrorPage400(templates, writer, params, "Error querying ref type: "+err.Error()) + web.ErrorPage400(s.templates, writer, params, "Error querying ref type: "+err.Error()) return } } if params["repo"], params["repo_description"], params["repo_id"], _, err = s.openRepo(request.Context(), groupPath, moduleName); err != nil { - web.ErrorPage500(templates, writer, params, "Error opening repo: "+err.Error()) + web.ErrorPage500(s.templates, writer, params, "Error opening repo: "+err.Error()) return } @@ -200,7 +200,7 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) { switch repoFeature { case "tree": if misc.AnyContain(segments[sepIndex+4:], "/") { - web.ErrorPage400(templates, writer, params, "Repo tree paths may not contain slashes in any segments") + web.ErrorPage400(s.templates, writer, params, "Repo tree paths may not contain slashes in any segments") return } if dirMode { @@ -220,7 +220,7 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) { return case "raw": if misc.AnyContain(segments[sepIndex+4:], "/") { - web.ErrorPage400(templates, writer, params, "Repo tree paths may not contain slashes in any segments") + web.ErrorPage400(s.templates, writer, params, "Repo tree paths may not contain slashes in any segments") return } if dirMode { @@ -234,23 +234,23 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) { s.httpHandleRepoRaw(writer, request, params) case "log": if len(segments) > sepIndex+4 { - web.ErrorPage400(templates, writer, params, "Too many parameters") + web.ErrorPage400(s.templates, writer, params, "Too many parameters") return } if misc.RedirectDir(writer, request) { return } - httpHandleRepoLog(writer, request, params) + s.httpHandleRepoLog(writer, request, params) case "commit": if len(segments) != sepIndex+5 { - web.ErrorPage400(templates, writer, params, "Incorrect number of parameters") + web.ErrorPage400(s.templates, writer, params, "Incorrect number of parameters") return } if misc.RedirectNoDir(writer, request) { return } params["commit_id"] = segments[sepIndex+4] - httpHandleRepoCommit(writer, request, params) + s.httpHandleRepoCommit(writer, request, params) case "contrib": if misc.RedirectDir(writer, request) { return @@ -262,14 +262,14 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) { params["mr_id"] = segments[sepIndex+4] s.httpHandleRepoContribOne(writer, request, params) default: - web.ErrorPage400(templates, writer, params, "Too many parameters") + web.ErrorPage400(s.templates, writer, params, "Too many parameters") } default: - web.ErrorPage404(templates, writer, params) + web.ErrorPage404(s.templates, writer, params) return } default: - web.ErrorPage404(templates, writer, params) + web.ErrorPage404(s.templates, writer, params) return } } diff --git a/http_template.go b/http_template.go index f60f026..d6af9fb 100644 --- a/http_template.go +++ b/http_template.go @@ -10,8 +10,8 @@ import ( // renderTemplate abstracts out the annoyances of reporting template rendering // errors. -func renderTemplate(w http.ResponseWriter, templateName string, params map[string]any) { - if err := templates.ExecuteTemplate(w, templateName, params); err != nil { +func (s *Server) renderTemplate(w http.ResponseWriter, templateName string, params map[string]any) { + if err := s.templates.ExecuteTemplate(w, templateName, params); err != nil { http.Error(w, "error rendering template: "+err.Error(), http.StatusInternalServerError) slog.Error("error rendering template", "error", err.Error()) } diff --git a/resources.go b/resources.go index e2168a3..514c0ae 100644 --- a/resources.go +++ b/resources.go @@ -20,10 +20,8 @@ var embeddedSourceFS embed.FS //go:embed hookc/hookc git2d/git2d var embeddedResourcesFS embed.FS -var templates *template.Template //nolint:gochecknoglobals - // loadTemplates minifies and loads HTML templates. -func loadTemplates() (err error) { +func (s *Server) loadTemplates() (err error) { minifier := minify.New() minifierOptions := html.Minifier{ TemplateDelims: [2]string{"{{", "}}"}, @@ -31,7 +29,7 @@ func loadTemplates() (err error) { } //exhaustruct:ignore minifier.Add("text/html", &minifierOptions) - templates = template.New("templates").Funcs(template.FuncMap{ + s.templates = template.New("templates").Funcs(template.FuncMap{ "first_line": misc.FirstLine, "path_escape": misc.PathEscape, "query_escape": misc.QueryEscape, @@ -54,7 +52,7 @@ func loadTemplates() (err error) { return err } - _, err = templates.Parse(misc.BytesToString(minified)) + _, err = s.templates.Parse(misc.BytesToString(minified)) if err != nil { return err } @@ -5,6 +5,7 @@ package forge import ( "errors" + "html/template" "io/fs" "log" "log/slog" @@ -40,6 +41,8 @@ type Server struct { // packPasses contains hook cookies mapped to their packPass. packPasses cmap.Map[string, packPass] + + templates *template.Template } func (s *Server) Setup() { @@ -65,7 +68,7 @@ func (s *Server) Run() { slog.Error("deploying hooks", "error", err) os.Exit(1) } - if err := loadTemplates(); err != nil { + if err := s.loadTemplates(); err != nil { slog.Error("loading templates", "error", err) os.Exit(1) } |