diff options
Diffstat (limited to '')
-rw-r--r-- | http_auth.go | 16 | ||||
-rw-r--r-- | http_handle_group_index.go | 9 | ||||
-rw-r--r-- | http_handle_index.go | 9 | ||||
-rw-r--r-- | http_handle_repo_commit.go | 19 | ||||
-rw-r--r-- | http_handle_repo_index.go | 19 | ||||
-rw-r--r-- | http_handle_repo_info.go | 4 | ||||
-rw-r--r-- | http_handle_repo_log.go | 11 | ||||
-rw-r--r-- | http_handle_repo_raw.go | 14 | ||||
-rw-r--r-- | http_handle_repo_tree.go | 20 | ||||
-rw-r--r-- | http_server.go | 31 | ||||
-rw-r--r-- | schema.sql | 6 | ||||
-rw-r--r-- | templates/_header.html.tmpl | 12 | ||||
-rw-r--r-- | templates/_repo_header.html.tmpl | 1 | ||||
-rw-r--r-- | templates/group_repos.html.tmpl | 3 | ||||
-rw-r--r-- | templates/index.html.tmpl | 3 | ||||
-rw-r--r-- | templates/repo_commit.html.tmpl | 3 | ||||
-rw-r--r-- | templates/repo_index.html.tmpl | 3 | ||||
-rw-r--r-- | templates/repo_log.html.tmpl | 3 | ||||
-rw-r--r-- | templates/repo_raw_dir.html.tmpl | 3 | ||||
-rw-r--r-- | templates/repo_tree_dir.html.tmpl | 3 | ||||
-rw-r--r-- | templates/repo_tree_file.html.tmpl | 3 |
21 files changed, 123 insertions, 72 deletions
diff --git a/http_auth.go b/http_auth.go new file mode 100644 index 0000000..370e38b --- /dev/null +++ b/http_auth.go @@ -0,0 +1,16 @@ +package main + +import ( + "net/http" +) + +func get_user_info_from_request(r *http.Request) (id int, username string, err error) { + session_cookie, err := r.Cookie("session") + if err != nil { + return + } + + err = database.QueryRow(r.Context(), "SELECT user_id, username FROM users u JOIN sessions s ON u.id = s.user_id WHERE s.session_id = $1;", session_cookie.Value).Scan(&id, &username) + + return +} diff --git a/http_handle_group_index.go b/http_handle_group_index.go index 2a8e1ca..d342277 100644 --- a/http_handle_group_index.go +++ b/http_handle_group_index.go @@ -4,11 +4,8 @@ import ( "net/http" ) -func handle_group_repos(w http.ResponseWriter, r *http.Request, params map[string]string) { - data := make(map[string]any) - data["global"] = global_data +func handle_group_repos(w http.ResponseWriter, r *http.Request, params map[string]any) { group_name := params["group_name"] - data["group_name"] = group_name var names []string rows, err := database.Query(r.Context(), "SELECT r.name FROM repos r JOIN groups g ON r.group_id = g.id WHERE g.name = $1;", group_name) @@ -32,9 +29,9 @@ func handle_group_repos(w http.ResponseWriter, r *http.Request, params map[strin return } - data["repos"] = names + params["repos"] = names - err = templates.ExecuteTemplate(w, "group_repos", data) + err = templates.ExecuteTemplate(w, "group_repos", params) if err != nil { _, _ = w.Write([]byte("Error rendering template: " + err.Error())) return diff --git a/http_handle_index.go b/http_handle_index.go index bf36f98..4448272 100644 --- a/http_handle_index.go +++ b/http_handle_index.go @@ -4,10 +4,7 @@ import ( "net/http" ) -func handle_index(w http.ResponseWriter, r *http.Request) { - data := make(map[string]any) - data["global"] = global_data - +func handle_index(w http.ResponseWriter, r *http.Request, params map[string]any) { rows, err := database.Query(r.Context(), "SELECT name FROM groups") if err != nil { _, _ = w.Write([]byte("Error querying groups: " + err.Error())) @@ -30,9 +27,9 @@ func handle_index(w http.ResponseWriter, r *http.Request) { return } - data["groups"] = groups + params["groups"] = groups - err = templates.ExecuteTemplate(w, "index", data) + err = templates.ExecuteTemplate(w, "index", params) if err != nil { _, _ = w.Write([]byte("Error rendering template: " + err.Error())) return diff --git a/http_handle_repo_commit.go b/http_handle_repo_commit.go index d79b088..073bfa1 100644 --- a/http_handle_repo_commit.go +++ b/http_handle_repo_commit.go @@ -16,11 +16,8 @@ type usable_file_patch struct { Chunks []diff.Chunk } -func handle_repo_commit(w http.ResponseWriter, r *http.Request, params map[string]string) { - data := make(map[string]any) - data["global"] = global_data - group_name, repo_name, commit_id_specified_string := params["group_name"], params["repo_name"], params["commit_id"] - data["group_name"], data["repo_name"] = group_name, repo_name +func handle_repo_commit(w http.ResponseWriter, r *http.Request, params map[string]any) { + group_name, repo_name, commit_id_specified_string := params["group_name"].(string), params["repo_name"].(string), params["commit_id"].(string) repo, err := open_git_repo(r.Context(), group_name, repo_name) if err != nil { _, _ = w.Write([]byte("Error opening repo: " + err.Error())) @@ -49,16 +46,16 @@ func handle_repo_commit(w http.ResponseWriter, r *http.Request, params map[strin return } - data["commit_object"] = commit_object - data["commit_id"] = commit_id_string + params["commit_object"] = commit_object + params["commit_id"] = commit_id_string parent_commit_hash, patch, err := get_patch_from_commit(commit_object) if err != nil { _, _ = w.Write([]byte("Error getting patch from commit: " + err.Error())) return } - data["parent_commit_hash"] = parent_commit_hash.String() - data["patch"] = patch + params["parent_commit_hash"] = parent_commit_hash.String() + params["patch"] = patch // TODO: Remove unnecessary context // TODO: Prepend "+"/"-"/" " instead of solely distinguishing based on color @@ -78,9 +75,9 @@ func handle_repo_commit(w http.ResponseWriter, r *http.Request, params map[strin } usable_file_patches = append(usable_file_patches, usable_file_patch) } - data["file_patches"] = usable_file_patches + params["file_patches"] = usable_file_patches - err = templates.ExecuteTemplate(w, "repo_commit", data) + err = templates.ExecuteTemplate(w, "repo_commit", params) if err != nil { _, _ = w.Write([]byte("Error rendering template: " + err.Error())) return diff --git a/http_handle_repo_index.go b/http_handle_repo_index.go index 0c7b570..866463d 100644 --- a/http_handle_repo_index.go +++ b/http_handle_repo_index.go @@ -5,11 +5,8 @@ import ( "net/url" ) -func handle_repo_index(w http.ResponseWriter, r *http.Request, params map[string]string) { - data := make(map[string]any) - data["global"] = global_data - group_name, repo_name := params["group_name"], params["repo_name"] - data["group_name"], data["repo_name"] = group_name, repo_name +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, err := open_git_repo(r.Context(), group_name, repo_name) if err != nil { _, _ = w.Write([]byte("Error opening repo: " + err.Error())) @@ -20,14 +17,14 @@ func handle_repo_index(w http.ResponseWriter, r *http.Request, params map[string _, _ = w.Write([]byte("Error getting repo HEAD: " + err.Error())) return } - data["ref"] = head.Name().Short() + params["ref"] = head.Name().Short() head_hash := head.Hash() recent_commits, err := get_recent_commits(repo, head_hash, 3) if err != nil { _, _ = w.Write([]byte("Error getting recent commits: " + err.Error())) return } - data["commits"] = recent_commits + params["commits"] = recent_commits commit_object, err := repo.CommitObject(head_hash) if err != nil { _, _ = w.Write([]byte("Error getting commit object: " + err.Error())) @@ -39,12 +36,12 @@ func handle_repo_index(w http.ResponseWriter, r *http.Request, params map[string return } - data["readme_filename"], data["readme"] = render_readme_at_tree(tree) - data["files"] = build_display_git_tree(tree) + params["readme_filename"], params["readme"] = render_readme_at_tree(tree) + params["files"] = build_display_git_tree(tree) - data["clone_url"] = "ssh://" + r.Host + "/" + url.PathEscape(params["group_name"]) + "/:/repos/" + url.PathEscape(params["repo_name"]) + params["clone_url"] = "ssh://" + r.Host + "/" + url.PathEscape(group_name) + "/:/repos/" + url.PathEscape(repo_name) - err = templates.ExecuteTemplate(w, "repo_index", data) + err = templates.ExecuteTemplate(w, "repo_index", params) if err != nil { _, _ = w.Write([]byte("Error rendering template: " + err.Error())) return diff --git a/http_handle_repo_info.go b/http_handle_repo_info.go index 5dd92e9..e220f18 100644 --- a/http_handle_repo_info.go +++ b/http_handle_repo_info.go @@ -5,6 +5,6 @@ import ( "net/url" ) -func handle_repo_info(w http.ResponseWriter, r *http.Request, params map[string]string) { - http.Error(w, "\x1b[1;93mHi! We do not support Git operations over HTTP yet.\x1b[0m\n\x1b[1;93mMeanwhile, please use ssh by simply replacing the scheme with \"ssh://\":\x1b[0m\n\x1b[1;93mssh://"+r.Host+"/"+url.PathEscape(params["group_name"])+"/:/repos/"+url.PathEscape(params["repo_name"])+"\x1b[0m", http.StatusNotImplemented) +func handle_repo_info(w http.ResponseWriter, r *http.Request, params map[string]any) { + http.Error(w, "\x1b[1;93mHi! We do not support Git operations over HTTP yet.\x1b[0m\n\x1b[1;93mMeanwhile, please use ssh by simply replacing the scheme with \"ssh://\":\x1b[0m\n\x1b[1;93mssh://"+r.Host+"/"+url.PathEscape(params["group_name"].(string))+"/:/repos/"+url.PathEscape(params["repo_name"].(string))+"\x1b[0m", http.StatusNotImplemented) } diff --git a/http_handle_repo_log.go b/http_handle_repo_log.go index 6a3f446..f36fba2 100644 --- a/http_handle_repo_log.go +++ b/http_handle_repo_log.go @@ -7,11 +7,8 @@ import ( ) // TODO: I probably shouldn't include *all* commits here... -func handle_repo_log(w http.ResponseWriter, r *http.Request, params map[string]string) { - data := make(map[string]any) - data["global"] = global_data - group_name, repo_name, ref_name := params["group_name"], params["repo_name"], params["ref"] - data["group_name"], data["repo_name"], data["ref"] = group_name, repo_name, ref_name +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) repo, err := open_git_repo(r.Context(), group_name, repo_name) if err != nil { _, _ = w.Write([]byte("Error opening repo: " + err.Error())) @@ -28,9 +25,9 @@ func handle_repo_log(w http.ResponseWriter, r *http.Request, params map[string]s _, _ = w.Write([]byte("Error getting recent commits: " + err.Error())) return } - data["commits"] = commits + params["commits"] = commits - err = templates.ExecuteTemplate(w, "repo_log", data) + err = templates.ExecuteTemplate(w, "repo_log", params) if err != nil { _, _ = w.Write([]byte("Error rendering template: " + err.Error())) return diff --git a/http_handle_repo_raw.go b/http_handle_repo_raw.go index 24b5794..e647e32 100644 --- a/http_handle_repo_raw.go +++ b/http_handle_repo_raw.go @@ -9,11 +9,9 @@ import ( "github.com/go-git/go-git/v5/plumbing/object" ) -func handle_repo_raw(w http.ResponseWriter, r *http.Request, params map[string]string) { - data := make(map[string]any) - data["global"] = global_data - raw_path_spec := params["rest"] - group_name, repo_name, path_spec := params["group_name"], params["repo_name"], strings.TrimSuffix(raw_path_spec, "/") +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 { @@ -25,7 +23,7 @@ func handle_repo_raw(w http.ResponseWriter, r *http.Request, params map[string]s } } - data["ref_type"], data["ref"], data["group_name"], data["repo_name"], data["path_spec"] = ref_type, ref_name, group_name, repo_name, path_spec + params["ref_type"], params["ref"], params["path_spec"] = ref_type, ref_name, path_spec repo, err := open_git_repo(r.Context(), group_name, repo_name) if err != nil { @@ -80,9 +78,9 @@ func handle_repo_raw(w http.ResponseWriter, r *http.Request, params map[string]s return } - data["files"] = build_display_git_tree(target) + params["files"] = build_display_git_tree(target) - err = templates.ExecuteTemplate(w, "repo_raw_dir", data) + err = templates.ExecuteTemplate(w, "repo_raw_dir", params) if err != nil { _, _ = w.Write([]byte("Error rendering template: " + err.Error())) return diff --git a/http_handle_repo_tree.go b/http_handle_repo_tree.go index 1dc06a8..0132239 100644 --- a/http_handle_repo_tree.go +++ b/http_handle_repo_tree.go @@ -14,11 +14,9 @@ import ( "github.com/go-git/go-git/v5/plumbing/object" ) -func handle_repo_tree(w http.ResponseWriter, r *http.Request, params map[string]string) { - data := make(map[string]any) - data["global"] = global_data - raw_path_spec := params["rest"] - group_name, repo_name, path_spec := params["group_name"], params["repo_name"], strings.TrimSuffix(raw_path_spec, "/") +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) { @@ -28,7 +26,7 @@ func handle_repo_tree(w http.ResponseWriter, r *http.Request, params map[string] return } } - data["ref_type"], data["ref"], data["group_name"], data["repo_name"], data["path_spec"] = ref_type, ref_name, group_name, repo_name, path_spec + params["ref_type"], params["ref"], params["path_spec"] = ref_type, ref_name, path_spec repo, err := open_git_repo(r.Context(), group_name, repo_name) if err != nil { _, _ = w.Write([]byte("Error opening repo: " + err.Error())) @@ -88,9 +86,9 @@ func handle_repo_tree(w http.ResponseWriter, r *http.Request, params map[string] return } formatted_encapsulated := template.HTML(formatted_unencapsulated.Bytes()) - data["file_contents"] = formatted_encapsulated + params["file_contents"] = formatted_encapsulated - err = templates.ExecuteTemplate(w, "repo_tree_file", data) + err = templates.ExecuteTemplate(w, "repo_tree_file", params) if err != nil { _, _ = w.Write([]byte("Error rendering template: " + err.Error())) return @@ -104,10 +102,10 @@ func handle_repo_tree(w http.ResponseWriter, r *http.Request, params map[string] return } - data["readme_filename"], data["readme"] = render_readme_at_tree(target) - data["files"] = build_display_git_tree(target) + params["readme_filename"], params["readme"] = render_readme_at_tree(target) + params["files"] = build_display_git_tree(target) - err = templates.ExecuteTemplate(w, "repo_tree_dir", data) + err = templates.ExecuteTemplate(w, "repo_tree_dir", params) if err != nil { _, _ = w.Write([]byte("Error rendering template: " + err.Error())) return diff --git a/http_server.go b/http_server.go index fbf729b..4d2d77c 100644 --- a/http_server.go +++ b/http_server.go @@ -15,11 +15,20 @@ func (router *http_router_t) ServeHTTP(w http.ResponseWriter, r *http.Request) { 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] { @@ -33,6 +42,18 @@ func (router *http_router_t) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } + params := make(map[string]any) + params["global"] = global_data + var _user_id int + _user_id, params["username"], err = get_user_info_from_request(r) + if _user_id == 0 { + params["user_id"] = "" + } else { + params["user_id"] = string(_user_id) + } + + fmt.Printf("%#v\n", params) + separator_index := -1 for i, part := range segments { if part == ":" { @@ -40,18 +61,10 @@ func (router *http_router_t) ServeHTTP(w http.ResponseWriter, r *http.Request) { break } } - non_empty_last_segments_len := len(segments) - dir_mode := false - if segments[len(segments)-1] == "" { - non_empty_last_segments_len-- - dir_mode = true - } - params := make(map[string]string) - _ = params switch { case non_empty_last_segments_len == 0: - handle_index(w, r) + 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: @@ -51,6 +51,12 @@ CREATE TABLE users ( password TEXT NOT NULL ); +CREATE TABLE sessions ( + user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, + session_id TEXT NOT NULL, + PRIMARY KEY (user_id, session_id) +); + CREATE TABLE merge_requests ( id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY, repo_id INTEGER NOT NULL REFERENCES repos(id) ON DELETE CASCADE, diff --git a/templates/_header.html.tmpl b/templates/_header.html.tmpl new file mode 100644 index 0000000..bfe386a --- /dev/null +++ b/templates/_header.html.tmpl @@ -0,0 +1,12 @@ +{{- define "header" -}} +<div> + <a href="/">Lindenii Forge</a> +</div> +<div> + {{ if ne .user_id "" }} + <a href="/:/user/{{ .user_id }}">{{ .username }}</a> + {{ else }} + <a href="/:/login/">Login</a> + {{ end }} +</div> +{{- end -}} diff --git a/templates/_repo_header.html.tmpl b/templates/_repo_header.html.tmpl index 8e5baa8..8a7fd4b 100644 --- a/templates/_repo_header.html.tmpl +++ b/templates/_repo_header.html.tmpl @@ -1,5 +1,4 @@ {{- define "repo_header" -}} -<a href="/">Lindenii Forge</a> / <a href="/{{ .group_name }}/">{{ .group_name }}</a> / diff --git a/templates/group_repos.html.tmpl b/templates/group_repos.html.tmpl index f24ce97..1050ce4 100644 --- a/templates/group_repos.html.tmpl +++ b/templates/group_repos.html.tmpl @@ -6,6 +6,9 @@ <title>Repos in {{ .group_name }} – Lindenii Forge</title> </head> <body class="group-repos"> + <header> + {{ template "header" . }} + </header> <div class="padding-wrapper"> <h1> Repos in {{ .group_name }} diff --git a/templates/index.html.tmpl b/templates/index.html.tmpl index e88b568..a8ef445 100644 --- a/templates/index.html.tmpl +++ b/templates/index.html.tmpl @@ -6,6 +6,9 @@ <title>Index – Lindenii Forge</title> </head> <body class="index"> + <header> + {{ template "header" . }} + </header> <div class="padding-wrapper"> <h1>Lindenii Forge</h1> <h2> diff --git a/templates/repo_commit.html.tmpl b/templates/repo_commit.html.tmpl index f219d2e..6c064fc 100644 --- a/templates/repo_commit.html.tmpl +++ b/templates/repo_commit.html.tmpl @@ -7,6 +7,9 @@ </head> <body class="repo-commit"> <header> + {{ template "header" . }} + </header> + <header> {{ template "repo_header" . }} </header> <div class="padding-wrapper scroll"> diff --git a/templates/repo_index.html.tmpl b/templates/repo_index.html.tmpl index 6bc3253..f77e10b 100644 --- a/templates/repo_index.html.tmpl +++ b/templates/repo_index.html.tmpl @@ -7,6 +7,9 @@ </head> <body class="repo-index"> <header> + {{ template "header" . }} + </header> + <header> {{ template "repo_header" . }} </header> <div class="padding-wrapper"> diff --git a/templates/repo_log.html.tmpl b/templates/repo_log.html.tmpl index a24138c..3cc719e 100644 --- a/templates/repo_log.html.tmpl +++ b/templates/repo_log.html.tmpl @@ -7,6 +7,9 @@ </head> <body class="repo-log"> <header> + {{ template "header" . }} + </header> + <header> {{ template "repo_header" . }} </header> <table id="commits" class="wide"> diff --git a/templates/repo_raw_dir.html.tmpl b/templates/repo_raw_dir.html.tmpl index 055445d..a005a32 100644 --- a/templates/repo_raw_dir.html.tmpl +++ b/templates/repo_raw_dir.html.tmpl @@ -7,6 +7,9 @@ </head> <body class="repo-raw-dir"> <header> + {{ template "header" . }} + </header> + <header> {{ template "repo_header" . }} </header> <div class="padding-wrapper scroll"> diff --git a/templates/repo_tree_dir.html.tmpl b/templates/repo_tree_dir.html.tmpl index dd5ecaf..5d2f632 100644 --- a/templates/repo_tree_dir.html.tmpl +++ b/templates/repo_tree_dir.html.tmpl @@ -7,6 +7,9 @@ </head> <body class="repo-tree-dir"> <header> + {{ template "header" . }} + </header> + <header> {{ template "repo_header" . }} </header> <div class="padding-wrapper scroll"> diff --git a/templates/repo_tree_file.html.tmpl b/templates/repo_tree_file.html.tmpl index 00547d2..7b184e3 100644 --- a/templates/repo_tree_file.html.tmpl +++ b/templates/repo_tree_file.html.tmpl @@ -8,6 +8,9 @@ </head> <body class="repo-tree-file"> <header> + {{ template "header" . }} + </header> + <header> {{ template "repo_header" . }} </header> <p> |