Author | Jakob Wakeling <[email protected]> |
Date | 2023-07-24 03:52:57 |
Commit | 583a0660cb480978c9a329575d7ee8f78504f647 |
Parent | 52b258d5f7839961411fef3021609a9a3c4c9c9b |
Cleanup repo pages
Diffstat
M | res/index.html | | | 1 | + |
A | res/repo/create.html | | | 28 | ++++++++++++++++++++++++++++ |
D | res/repo_create.html | | | 21 | --------------------- |
R | res/repo_log.html -> res/repo/log.html | | | 0 | |
R | res/repo_refs.html -> res/repo/refs.html | | | 0 | |
R | res/repo_tree.html -> res/repo/tree.html | | | 0 | |
M | res/res.go | | | 8 | ++++---- |
M | res/user/header.html | | | 2 | +- |
M | src/http.go | | | 8 | ++++---- |
M | src/repo.go | | | 59 | +++++++++++++++++++++++++++++++++++------------------------ |
10 files changed, 73 insertions, 54 deletions
diff --git a/res/index.html b/res/index.html index 44b60d1..a482024 100644 --- a/res/index.html +++ b/res/index.html @@ -13,6 +13,7 @@ <td> <a href="/">Repositories</a> {{if .Auth}} + | <a href="/repo/create">Create</a> | <a href="/user/sessions">User</a> {{end}} {{if .Admin}} diff --git a/res/repo_create.html b/res/repo/create.html similarity index 50% rename from res/repo_create.html rename to res/repo/create.html index 7fd3740..c3b3035 100644 --- a/res/repo_create.html +++ b/res/repo/create.html @@ -1,15 +1,22 @@ <!DOCTYPE html> -<head> - <meta charset="UTF-8"> - <title>Create Repository</title> - <meta name="viewport" content="width=device-width, initial-scale=1.0"> - <link rel="stylesheet" type="text/css" href="/static/style.css"> - <link rel="icon" type="image/png" href="/static/favicon.png"> -</head> +<head>{{template "base/head" .}}</head> <body> + <header> + <table> + <tr> + <td rowspan="2"> + <a href="/"><img src="/static/favicon.png" style="max-height: 24px"></a> + </td> + <td><h1>{{.Title}}</h1></td> + </tr> + <tr><td></td></tr> + </table> + </header> <form action="/repo/create" method="post"> <label for="reponame">Name:</label> <input type="text" name="reponame"><br> + <label for="description">Description</label> + <input type="text" name="description"><br> <label for="visibility">Visibility:</label> <select name="visibility"> <option value="public">Public</option> @@ -17,5 +24,5 @@ </select><br> <input type="submit" value="Create"> </form> - <p>{{.Msg}}</p> + <p>{{.Message}}</p> </body> diff --git a/res/repo_log.html b/res/repo/log.html similarity index 100% rename from res/repo_log.html rename to res/repo/log.html diff --git a/res/repo_refs.html b/res/repo/refs.html similarity index 100% rename from res/repo_refs.html rename to res/repo/refs.html diff --git a/res/repo_tree.html b/res/repo/tree.html similarity index 100% rename from res/repo_tree.html rename to res/repo/tree.html diff --git a/res/res.go b/res/res.go index ee395c2..94c8dfc 100644 --- a/res/res.go +++ b/res/res.go @@ -41,16 +41,16 @@ var UserSessions string //go:embed repo/header.html var RepoHeader string -//go:embed repo_create.html +//go:embed repo/create.html var RepoCreate string -//go:embed repo_log.html +//go:embed repo/log.html var RepoLog string -//go:embed repo_tree.html +//go:embed repo/tree.html var RepoTree string -//go:embed repo_refs.html +//go:embed repo/refs.html var RepoRefs string //go:embed style.css diff --git a/res/user/header.html b/res/user/header.html index 54e63d1..1b86f45 100644 --- a/res/user/header.html +++ b/res/user/header.html @@ -2,7 +2,7 @@ <table> <tr> <td rowspan="2"> - <a href="/"><img style="max-height: 24px;" src="/static/favicon.png"></a> + <a href="/"><img src="/static/favicon.png" style="max-height: 24px;"></a> </td> <td><h1>{{.Title}}</h1></td> </tr> diff --git a/src/http.go b/src/http.go index cb083cb..4fc6e78 100644 --- a/src/http.go +++ b/src/http.go @@ -30,11 +30,11 @@ func init() { template.Must(tmpl.New("user/sessions").Parse(res.UserSessions)) template.Must(tmpl.New("repo/header").Parse(res.RepoHeader)) - template.Must(tmpl.New("repo_create").Parse(res.RepoCreate)) + template.Must(tmpl.New("repo/create").Parse(res.RepoCreate)) - template.Must(tmpl.New("repo_log").Parse(res.RepoLog)) - template.Must(tmpl.New("repo_tree").Parse(res.RepoTree)) - template.Must(tmpl.New("repo_refs").Parse(res.RepoRefs)) + template.Must(tmpl.New("repo/log").Parse(res.RepoLog)) + template.Must(tmpl.New("repo/tree").Parse(res.RepoTree)) + template.Must(tmpl.New("repo/refs").Parse(res.RepoRefs)) } func HttpError(w http.ResponseWriter, code int) { diff --git a/src/repo.go b/src/repo.go index 83fc769..67b5a03 100644 --- a/src/repo.go +++ b/src/repo.go @@ -86,34 +86,45 @@ func HandleIndex(w http.ResponseWriter, r *http.Request) { } func HandleRepoCreate(w http.ResponseWriter, r *http.Request) { - if ok, uid := AuthCookie(w, r, true); !ok { + ok, uid := AuthCookie(w, r, true) + if !ok { HttpError(w, http.StatusUnauthorized) - } else if r.Method == http.MethodPost { - name := r.FormValue("reponame") + return + } + + data := struct{ Title, Message string }{Title: "Repository - Create"} + + if r.Method == http.MethodPost { + reponame := r.FormValue("reponame") + description := r.FormValue("description") private := r.FormValue("visibility") == "private" - if taken, err := RepoExists(name); err != nil { - log.Println("[RepoCreate:RepoExists]", err.Error()) + if reponame == "" { + data.Message = "Name cannot be empty" + } else if util.SliceContains(reserved, reponame) { + data.Message = "Name \"" + reponame + "\" is reserved" + } else if exists, err := RepoExists(reponame); err != nil { + log.Println("[/repo/create]", err.Error()) + HttpError(w, http.StatusInternalServerError) + return + } else if exists { + data.Message = "Name \"" + reponame + "\" already exists" + } else if _, err := db.Exec( + `INSERT INTO repos (owner_id, name, name_lower, description, default_branch, is_private) + VALUES (?, ?, ?, ?, ?, ?)`, + uid, reponame, strings.ToLower(reponame), description, "master", private, + ); err != nil { + log.Println("[/repo/create]", err.Error()) HttpError(w, http.StatusInternalServerError) - } else if taken { - tmpl.ExecuteTemplate(w, "repo_create", struct{ Msg string }{"Reponame is taken"}) - } else if util.SliceContains[string](reserved, name) { - tmpl.ExecuteTemplate(w, "repo_create", struct{ Msg string }{"Reponame is reserved"}) + return } else { - if _, err := db.Exec( - `INSERT INTO repos ( - owner_id, name, name_lower, description, default_branch, is_private - ) VALUES (?, ?, ?, ?, ?, ?)`, - uid, name, strings.ToLower(name), "", "master", private, - ); err != nil { - log.Println("[RepoCreate:INSERT]", err.Error()) - HttpError(w, http.StatusInternalServerError) - } else { - http.Redirect(w, r, "/"+name+"/", http.StatusFound) - } + http.Redirect(w, r, "/"+reponame, http.StatusFound) + return } - } else /* GET */ { - tmpl.ExecuteTemplate(w, "repo_create", nil) + } + + if err := tmpl.ExecuteTemplate(w, "repo/create", data); err != nil { + log.Println("[/repo/create]", err.Error()) } } @@ -155,7 +166,7 @@ func HandleRepoLog(w http.ResponseWriter, r *http.Request) { return } - if err := tmpl.ExecuteTemplate(w, "repo_log", struct { + if err := tmpl.ExecuteTemplate(w, "repo/log", struct { Title, Name, Description, Url string Readme, Licence string Commits []row @@ -215,7 +226,7 @@ func HandleRepoRefs(w http.ResponseWriter, r *http.Request) { return } - if err := tmpl.ExecuteTemplate(w, "repo_refs", struct { + if err := tmpl.ExecuteTemplate(w, "repo/refs", struct { Title, Name, Description, Url string Readme, Licence string Branches []bra