diff options
Diffstat (limited to 'forged/internal/incoming/web/handlers')
-rw-r--r-- | forged/internal/incoming/web/handlers/group.go | 72 | ||||
-rw-r--r-- | forged/internal/incoming/web/handlers/index.go | 7 | ||||
-rw-r--r-- | forged/internal/incoming/web/handlers/repo/index.go | 126 | ||||
-rw-r--r-- | forged/internal/incoming/web/handlers/repo/raw.go | 2 | ||||
-rw-r--r-- | forged/internal/incoming/web/handlers/repo/tree.go | 2 | ||||
-rw-r--r-- | forged/internal/incoming/web/handlers/special/login.go | 14 |
6 files changed, 201 insertions, 22 deletions
diff --git a/forged/internal/incoming/web/handlers/group.go b/forged/internal/incoming/web/handlers/group.go index 3201491..4823cb7 100644 --- a/forged/internal/incoming/web/handlers/group.go +++ b/forged/internal/incoming/web/handlers/group.go @@ -1,13 +1,17 @@ package handlers import ( + "fmt" "log/slog" "net/http" + "path/filepath" "strconv" + "github.com/jackc/pgx/v5" "go.lindenii.runxiyu.org/forge/forged/internal/database/queries" "go.lindenii.runxiyu.org/forge/forged/internal/incoming/web/templates" wtypes "go.lindenii.runxiyu.org/forge/forged/internal/incoming/web/types" + "go.lindenii.runxiyu.org/forge/forged/internal/ipc/git2c" ) type GroupHTTP struct { @@ -31,19 +35,19 @@ func (h *GroupHTTP) Index(w http.ResponseWriter, r *http.Request, _ wtypes.Vars) Column1: base.URLSegments, UserID: userID, } - p, err := base.Queries.GetGroupByPath(r.Context(), queryParams) + p, err := base.Global.Queries.GetGroupByPath(r.Context(), queryParams) if err != nil { slog.Error("failed to get group ID by path", "error", err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } - subgroups, err := base.Queries.GetSubgroups(r.Context(), &p.ID) + subgroups, err := base.Global.Queries.GetSubgroups(r.Context(), &p.ID) if err != nil { slog.Error("failed to get subgroups", "error", err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) // TODO: gracefully fail this part of the page } - repos, err := base.Queries.GetReposInGroup(r.Context(), p.ID) + repos, err := base.Global.Queries.GetReposInGroup(r.Context(), p.ID) if err != nil { slog.Error("failed to get repos in group", "error", err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) @@ -78,7 +82,7 @@ func (h *GroupHTTP) Post(w http.ResponseWriter, r *http.Request, _ wtypes.Vars) Column1: base.URLSegments, UserID: userID, } - p, err := base.Queries.GetGroupByPath(r.Context(), queryParams) + p, err := base.Global.Queries.GetGroupByPath(r.Context(), queryParams) if err != nil { slog.Error("failed to get group ID by path", "error", err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) @@ -89,4 +93,64 @@ func (h *GroupHTTP) Post(w http.ResponseWriter, r *http.Request, _ wtypes.Vars) http.Error(w, "You do not have the necessary permissions to create repositories in this group.", http.StatusForbidden) return } + + name := r.PostFormValue("repo_name") + desc := r.PostFormValue("repo_desc") + contrib := r.PostFormValue("repo_contrib") + if name == "" { + http.Error(w, "Repo name is required", http.StatusBadRequest) + return + } + + if contrib == "" || contrib == "public" { + contrib = "open" + } + + tx, err := base.Global.DB.BeginTx(r.Context(), pgx.TxOptions{}) + if err != nil { + slog.Error("begin tx failed", "error", err) + http.Error(w, "Internal Server Error", http.StatusInternalServerError) + return + } + defer func() { _ = tx.Rollback(r.Context()) }() + + txq := base.Global.Queries.WithTx(tx) + var descPtr *string + if desc != "" { + descPtr = &desc + } + repoID, err := txq.InsertRepo(r.Context(), queries.InsertRepoParams{ + GroupID: p.ID, + Name: name, + Description: descPtr, + ContribRequirements: contrib, + }) + if err != nil { + slog.Error("insert repo failed", "error", err) + http.Error(w, "Failed to create repository", http.StatusInternalServerError) + return + } + + repoPath := filepath.Join(base.Global.Config.Git.RepoDir, fmt.Sprintf("%d.git", repoID)) + + gitc, err := git2c.NewClient(r.Context(), base.Global.Config.Git.Socket) + if err != nil { + slog.Error("git2d connect failed", "error", err) + http.Error(w, "Failed to initialize repository (backend)", http.StatusInternalServerError) + return + } + defer func() { _ = gitc.Close() }() + if err = gitc.InitRepo(repoPath, base.Global.Config.Hooks.Execs); err != nil { + slog.Error("git2d init failed", "error", err) + http.Error(w, "Failed to initialize repository", http.StatusInternalServerError) + return + } + + if err = tx.Commit(r.Context()); err != nil { + slog.Error("commit tx failed", "error", err) + http.Error(w, "Failed to finalize repository creation", http.StatusInternalServerError) + return + } + + http.Redirect(w, r, r.URL.Path, http.StatusSeeOther) } diff --git a/forged/internal/incoming/web/handlers/index.go b/forged/internal/incoming/web/handlers/index.go index 22e6201..a758b07 100644 --- a/forged/internal/incoming/web/handlers/index.go +++ b/forged/internal/incoming/web/handlers/index.go @@ -6,7 +6,6 @@ import ( "go.lindenii.runxiyu.org/forge/forged/internal/database/queries" "go.lindenii.runxiyu.org/forge/forged/internal/incoming/web/templates" - "go.lindenii.runxiyu.org/forge/forged/internal/incoming/web/types" wtypes "go.lindenii.runxiyu.org/forge/forged/internal/incoming/web/types" ) @@ -21,17 +20,17 @@ func NewIndexHTTP(r templates.Renderer) *IndexHTTP { } func (h *IndexHTTP) Index(w http.ResponseWriter, r *http.Request, _ wtypes.Vars) { - groups, err := types.Base(r).Queries.GetRootGroups(r.Context()) + groups, err := wtypes.Base(r).Global.Queries.GetRootGroups(r.Context()) if err != nil { http.Error(w, "failed to get root groups", http.StatusInternalServerError) log.Println("failed to get root groups", "error", err) return } err = h.r.Render(w, "index", struct { - BaseData *types.BaseData + BaseData *wtypes.BaseData Groups []queries.GetRootGroupsRow }{ - BaseData: types.Base(r), + BaseData: wtypes.Base(r), Groups: groups, }) if err != nil { diff --git a/forged/internal/incoming/web/handlers/repo/index.go b/forged/internal/incoming/web/handlers/repo/index.go index 1a804b2..c2cb24a 100644 --- a/forged/internal/incoming/web/handlers/repo/index.go +++ b/forged/internal/incoming/web/handlers/repo/index.go @@ -1,20 +1,132 @@ package repo import ( + "bytes" + "fmt" + "html/template" + "log/slog" "net/http" + "net/url" + "path/filepath" "strings" + "github.com/yuin/goldmark" + "github.com/yuin/goldmark/extension" + "go.lindenii.runxiyu.org/forge/forged/internal/common/misc" + "go.lindenii.runxiyu.org/forge/forged/internal/database/queries" wtypes "go.lindenii.runxiyu.org/forge/forged/internal/incoming/web/types" + "go.lindenii.runxiyu.org/forge/forged/internal/ipc/git2c" ) func (h *HTTP) Index(w http.ResponseWriter, r *http.Request, v wtypes.Vars) { base := wtypes.Base(r) - repo := v["repo"] - _ = h.r.Render(w, "repo/index.html", struct { - Group string - Repo string - }{ - Group: "/" + strings.Join(base.GroupPath, "/") + "/", - Repo: repo, + repoName := v["repo"] + slog.Info("repo index", "group_path", base.GroupPath, "repo", repoName) + + var userID int64 + if base.UserID != "" { + _, _ = fmt.Sscan(base.UserID, &userID) + } + grp, err := base.Global.Queries.GetGroupByPath(r.Context(), queries.GetGroupByPathParams{ + Column1: base.GroupPath, + UserID: userID, + }) + if err != nil { + slog.Error("get group by path", "error", err) + http.Error(w, "Group not found", http.StatusNotFound) + return + } + + repoRow, err := base.Global.Queries.GetRepoByGroupAndName(r.Context(), queries.GetRepoByGroupAndNameParams{ + GroupID: grp.ID, + Name: repoName, }) + if err != nil { + slog.Error("get repo by name", "error", err) + http.Error(w, "Repository not found", http.StatusNotFound) + return + } + + repoPath := filepath.Join(base.Global.Config.Git.RepoDir, fmt.Sprintf("%d.git", repoRow.ID)) + + var commits []git2c.Commit + var readme template.HTML + var commitsErr error + var readmeFile *git2c.FilenameContents + var cerr error + client, err := git2c.NewClient(r.Context(), base.Global.Config.Git.Socket) + if err == nil { + defer func() { _ = client.Close() }() + commits, readmeFile, cerr = client.CmdIndex(repoPath) + if cerr != nil { + commitsErr = cerr + slog.Error("git2d CmdIndex failed", "error", cerr, "path", repoPath) + } else if readmeFile != nil { + nameLower := strings.ToLower(readmeFile.Filename) + if strings.HasSuffix(nameLower, ".md") || strings.HasSuffix(nameLower, ".markdown") || nameLower == "readme" { + md := goldmark.New( + goldmark.WithExtensions(extension.GFM), + ) + var buf bytes.Buffer + if err := md.Convert(readmeFile.Content, &buf); err == nil { + readme = template.HTML(buf.String()) + } else { + readme = template.HTML(template.HTMLEscapeString(string(readmeFile.Content))) + } + } else { + readme = template.HTML(template.HTMLEscapeString(string(readmeFile.Content))) + } + } + } else { + commitsErr = err + slog.Error("git2d connect failed", "error", err) + } + + sshRoot := strings.TrimSuffix(base.Global.Config.SSH.Root, "/") + httpRoot := strings.TrimSuffix(base.Global.Config.Web.Root, "/") + pathPart := misc.SegmentsToURL(base.GroupPath) + "/-/repos/" + url.PathEscape(repoRow.Name) + sshURL := "" + httpURL := "" + if sshRoot != "" { + sshURL = sshRoot + "/" + pathPart + } + if httpRoot != "" { + httpURL = httpRoot + "/" + pathPart + } + + var notes []string + if len(commits) == 0 && commitsErr == nil { + notes = append(notes, "This repository has no commits yet.") + } + if readme == template.HTML("") { + notes = append(notes, "No README found in the default branch.") + } + if sshURL == "" && httpURL == "" { + notes = append(notes, "Clone URLs not configured (missing SSH root and HTTP root).") + } + + cloneURL := sshURL + if cloneURL == "" { + cloneURL = httpURL + } + + data := map[string]any{ + "BaseData": base, + "group_path": base.GroupPath, + "repo_name": repoRow.Name, + "repo_description": repoRow.Description, + "ssh_clone_url": cloneURL, + "ref_name": base.RefName, + "commits": commits, + "commits_err": &commitsErr, + "readme": readme, + "notes": notes, + "global": map[string]any{ + "forge_title": base.Global.ForgeTitle, + }, + } + if err := h.r.Render(w, "repo_index", data); err != nil { + slog.Error("render repo index", "error", err) + http.Error(w, "Internal Server Error", http.StatusInternalServerError) + } } diff --git a/forged/internal/incoming/web/handlers/repo/raw.go b/forged/internal/incoming/web/handlers/repo/raw.go index e421f45..8bdfae3 100644 --- a/forged/internal/incoming/web/handlers/repo/raw.go +++ b/forged/internal/incoming/web/handlers/repo/raw.go @@ -15,5 +15,5 @@ func (h *HTTP) Raw(w http.ResponseWriter, r *http.Request, v wtypes.Vars) { if base.DirMode && rest != "" && !strings.HasSuffix(rest, "/") { rest += "/" } - _, _ = w.Write([]byte(fmt.Sprintf("raw: repo=%q path=%q", repo, rest))) + _, _ = fmt.Fprintf(w, "raw: repo=%q path=%q", repo, rest) } diff --git a/forged/internal/incoming/web/handlers/repo/tree.go b/forged/internal/incoming/web/handlers/repo/tree.go index 3432244..236dd48 100644 --- a/forged/internal/incoming/web/handlers/repo/tree.go +++ b/forged/internal/incoming/web/handlers/repo/tree.go @@ -15,5 +15,5 @@ func (h *HTTP) Tree(w http.ResponseWriter, r *http.Request, v wtypes.Vars) { if base.DirMode && rest != "" && !strings.HasSuffix(rest, "/") { rest += "/" } - _, _ = w.Write([]byte(fmt.Sprintf("tree: repo=%q path=%q", repo, rest))) + _, _ = fmt.Fprintf(w, "tree: repo=%q path=%q", repo, rest) } diff --git a/forged/internal/incoming/web/handlers/special/login.go b/forged/internal/incoming/web/handlers/special/login.go index 0287c47..5672f1f 100644 --- a/forged/internal/incoming/web/handlers/special/login.go +++ b/forged/internal/incoming/web/handlers/special/login.go @@ -14,7 +14,6 @@ import ( "go.lindenii.runxiyu.org/forge/forged/internal/common/misc" "go.lindenii.runxiyu.org/forge/forged/internal/database/queries" "go.lindenii.runxiyu.org/forge/forged/internal/incoming/web/templates" - "go.lindenii.runxiyu.org/forge/forged/internal/incoming/web/types" wtypes "go.lindenii.runxiyu.org/forge/forged/internal/incoming/web/types" ) @@ -33,10 +32,10 @@ func NewLoginHTTP(r templates.Renderer, cookieExpiry int) *LoginHTTP { func (h *LoginHTTP) Login(w http.ResponseWriter, r *http.Request, _ wtypes.Vars) { renderLoginPage := func(loginError string) bool { err := h.r.Render(w, "login", struct { - BaseData *types.BaseData + BaseData *wtypes.BaseData LoginError string }{ - BaseData: types.Base(r), + BaseData: wtypes.Base(r), LoginError: loginError, }) if err != nil { @@ -55,7 +54,7 @@ func (h *LoginHTTP) Login(w http.ResponseWriter, r *http.Request, _ wtypes.Vars) username := r.PostFormValue("username") password := r.PostFormValue("password") - userCreds, err := types.Base(r).Queries.GetUserCreds(r.Context(), &username) + userCreds, err := wtypes.Base(r).Global.Queries.GetUserCreds(r.Context(), &username) if err != nil { if errors.Is(err, pgx.ErrNoRows) { renderLoginPage("User not found") @@ -102,7 +101,7 @@ func (h *LoginHTTP) Login(w http.ResponseWriter, r *http.Request, _ wtypes.Vars) tokenHash := sha256.Sum256(misc.StringToBytes(cookieValue)) - err = types.Base(r).Queries.InsertSession(r.Context(), queries.InsertSessionParams{ + err = wtypes.Base(r).Global.Queries.InsertSession(r.Context(), queries.InsertSessionParams{ UserID: userCreds.ID, TokenHash: tokenHash[:], ExpiresAt: pgtype.Timestamptz{ @@ -110,6 +109,11 @@ func (h *LoginHTTP) Login(w http.ResponseWriter, r *http.Request, _ wtypes.Vars) Valid: true, }, }) + if err != nil { + log.Println("failed to insert session", "error", err) + http.Error(w, "Failed to create session", http.StatusInternalServerError) + return + } http.Redirect(w, r, "/", http.StatusSeeOther) } |