From 248a157779d3ef8199a53b246a33e3040fdca3ce Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Thu, 7 May 2020 01:24:58 -0400 Subject: stubbing out endpoints --- http.go | 38 ++++++++++++++++++++++++++++++++++---- internal/endpoints/osversion.go | 10 ++++++++++ internal/endpoints/pkgs.go | 10 ++++++++++ internal/endpoints/query.go | 11 +++++++++++ internal/endpoints/uptime.go | 10 ++++++++++ internal/endpoints/usercount.go | 10 ++++++++++ internal/endpoints/users.go | 10 ++++++++++ 7 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 internal/endpoints/osversion.go create mode 100644 internal/endpoints/pkgs.go create mode 100644 internal/endpoints/query.go create mode 100644 internal/endpoints/uptime.go create mode 100644 internal/endpoints/usercount.go create mode 100644 internal/endpoints/users.go diff --git a/http.go b/http.go index c549cb6..f46904f 100644 --- a/http.go +++ b/http.go @@ -4,6 +4,8 @@ import ( "errors" "net/http" "strings" + + "git.tilde.institute/tilde/api/internal/endpoints" ) const mimePlain = "text/plain; charset=utf-8" @@ -11,6 +13,8 @@ const mimeJSON = "application/json; charset=utf-8" // Validates the request and then sends it off to where it needs to go. // Eventually. +// I chose this monolithic handler that calls validation functions to +// determine what to do next because this will make it easier to test. func validateRequest(w http.ResponseWriter, r *http.Request) { if !methodHop(r) { errHTTP(w, r, errors.New("405 Method Not Allowed"), http.StatusMethodNotAllowed) @@ -22,6 +26,31 @@ func validateRequest(w http.ResponseWriter, r *http.Request) { errHTTP(w, r, errors.New("400 Bad Request"), http.StatusBadRequest) return } + + var err error + switch routingHop(r) { + case "pkgs": + err = endpoints.Pkgs(w, r, format) + case "query": + err = endpoints.Query(w, r, format) + case "uptime": + err = endpoints.Uptime(w, r, format) + case "usercount": + err = endpoints.UserCount(w, r, format) + case "users": + err = endpoints.Users(w, r, format) + case "osversion": + err = endpoints.OSVersion(w, r, format) + default: + errHTTP(w, r, errors.New("Unknown endpoint"), http.StatusNotFound) + return + } + + if err != nil { + errHTTP(w, r, err, http.StatusBadRequest) + return + } + log200(r) } // Simple HTTP method check @@ -29,13 +58,14 @@ func methodHop(r *http.Request) bool { return r.Method == http.MethodGet || r.Method == http.MethodHead } -// Makes sure the format requested is either plaintext or JSON +// Yoinks the response format func formatHop(r *http.Request) string { split := strings.Split(r.URL.Path[1:], "/") return split[0] } -// Chooses next hop based on the endpoint -func routingHop(w http.ResponseWriter, r *http.Request, format string) { - +// Yoinks the endpoint +func routingHop(r *http.Request) string { + split := strings.Split(r.URL.Path[1:], "/") + return split[1] } diff --git a/internal/endpoints/osversion.go b/internal/endpoints/osversion.go new file mode 100644 index 0000000..da8ea87 --- /dev/null +++ b/internal/endpoints/osversion.go @@ -0,0 +1,10 @@ +package endpoints // import git.tilde.institute/tilde/api/internal/endpoints + +import "net/http" + +// OSVersion handles the //osversion endpoint. +// Responds with the OpenBSD version. +func OSVersion(w http.ResponseWriter, r *http.Request, format string) error { + + return nil +} diff --git a/internal/endpoints/pkgs.go b/internal/endpoints/pkgs.go new file mode 100644 index 0000000..018dea8 --- /dev/null +++ b/internal/endpoints/pkgs.go @@ -0,0 +1,10 @@ +package endpoints // import git.tilde.institute/tilde/api/internal/endpoints + +import "net/http" + +// Pkgs handles the //pkgs endpoint. +// Sends a list of installed packages. +func Pkgs(w http.ResponseWriter, r *http.Request, format string) error { + + return nil +} diff --git a/internal/endpoints/query.go b/internal/endpoints/query.go new file mode 100644 index 0000000..c16cfed --- /dev/null +++ b/internal/endpoints/query.go @@ -0,0 +1,11 @@ +package endpoints // import git.tilde.institute/tilde/api/internal/endpoints + +import "net/http" + +// Query handles the //query endpoint. +// Accept a query param and responds with the appropriate info. +// ?pkg=$PACKAGENAME +func Query(w http.ResponseWriter, r *http.Request, format string) error { + + return nil +} diff --git a/internal/endpoints/uptime.go b/internal/endpoints/uptime.go new file mode 100644 index 0000000..a50c3e6 --- /dev/null +++ b/internal/endpoints/uptime.go @@ -0,0 +1,10 @@ +package endpoints // import git.tilde.institute/tilde/api/internal/endpoints + +import "net/http" + +// Uptime handles the //uptime endpoint. +// Sends uptime and load +func Uptime(w http.ResponseWriter, r *http.Request, format string) error { + + return nil +} diff --git a/internal/endpoints/usercount.go b/internal/endpoints/usercount.go new file mode 100644 index 0000000..1e76071 --- /dev/null +++ b/internal/endpoints/usercount.go @@ -0,0 +1,10 @@ +package endpoints // import git.tilde.institute/tilde/api/internal/endpoints + +import "net/http" + +// UserCount handles the //usercount endpoint. +// Responds with the number of registered users on the system. +func UserCount(w http.ResponseWriter, r *http.Request, format string) error { + + return nil +} diff --git a/internal/endpoints/users.go b/internal/endpoints/users.go new file mode 100644 index 0000000..e315793 --- /dev/null +++ b/internal/endpoints/users.go @@ -0,0 +1,10 @@ +package endpoints // import git.tilde.institute/tilde/api/internal/endpoints + +import "net/http" + +// Users handles the //users endpoint. +// Responds with information on the system's users. +func Users(w http.ResponseWriter, r *http.Request, format string) error { + + return nil +} -- cgit 1.4.1-2-gfad0