diff options
author | Runxi Yu <me@runxiyu.org> | 2025-02-12 18:26:01 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-02-12 18:34:46 +0800 |
commit | 542510aa484ecbc6c11c513289e95a151e59783d (patch) | |
tree | 162e94b97621dfbf84594fe2d9945273bee84cc6 /router_http.go | |
parent | ssh.go: Add anonymous SSH cloning (diff) | |
download | forge-542510aa484ecbc6c11c513289e95a151e59783d.tar.gz forge-542510aa484ecbc6c11c513289e95a151e59783d.tar.zst forge-542510aa484ecbc6c11c513289e95a151e59783d.zip |
http_router.go: Move from router.go and fix conditional placement bug
Diffstat (limited to 'router_http.go')
-rw-r--r-- | router_http.go | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/router_http.go b/router_http.go new file mode 100644 index 0000000..abf47c2 --- /dev/null +++ b/router_http.go @@ -0,0 +1,117 @@ +package main + +import ( + "errors" + "fmt" + "net/http" + "strings" +) + +type http_router_t struct{} + +func (router *http_router_t) ServeHTTP(w http.ResponseWriter, r *http.Request) { + segments, _, err := parse_request_uri(r.RequestURI) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + + if segments[0] == ":" { + if len(segments) < 2 { + http.Error(w, "Blank system endpoint", http.StatusNotFound) + return + } + + switch segments[1] { + case "static": + static_handler.ServeHTTP(w, r) + case "source": + source_handler.ServeHTTP(w, r) + default: + fmt.Fprintln(w, "Unknown system module type:", segments[1]) + } + return + } + + separator_index := -1 + for i, part := range segments { + if part == ":" { + separator_index = i + 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) + case separator_index == -1: + fmt.Fprintln(w, "Group indexing hasn't been implemented yet") + case non_empty_last_segments_len == separator_index+1: + fmt.Fprintln(w, "Group root hasn't been implemented yet") + case non_empty_last_segments_len == separator_index+2: + module_type := segments[separator_index+1] + params["group_name"] = segments[0] + switch module_type { + case "repos": + handle_group_repos(w, r, params) + default: + fmt.Fprintln(w, "Unknown module type:", module_type) + } + 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 + // 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 "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 { + fmt.Fprintln(w, "Too many parameters") + return + } + if dir_mode { + http.Redirect(w, r, strings.TrimSuffix(r.URL.Path, "/"), http.StatusSeeOther) + return + } + params["ref"] = 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: + fmt.Fprintln(w, "Unknown module type:", module_type) + } + } +} + +var err_bad_request = errors.New("Bad Request") |