about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2020-05-07 01:24:58 -0400
committerBen Morrison <ben@gbmor.dev>2020-05-07 01:24:58 -0400
commit248a157779d3ef8199a53b246a33e3040fdca3ce (patch)
treea67449c4db304bf1df6130b226de679878bf1016
parent002dce776e5e71a5af6da3ab8f5e934c95d6bc0c (diff)
downloadapi-248a157779d3ef8199a53b246a33e3040fdca3ce.tar.gz
stubbing out endpoints
-rw-r--r--http.go38
-rw-r--r--internal/endpoints/osversion.go10
-rw-r--r--internal/endpoints/pkgs.go10
-rw-r--r--internal/endpoints/query.go11
-rw-r--r--internal/endpoints/uptime.go10
-rw-r--r--internal/endpoints/usercount.go10
-rw-r--r--internal/endpoints/users.go10
7 files changed, 95 insertions, 4 deletions
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 /<format>/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 /<format>/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 /<format>/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 /<format>/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 /<format>/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 /<format>/users endpoint.
+// Responds with information on the system's users.
+func Users(w http.ResponseWriter, r *http.Request, format string) error {
+
+	return nil
+}