Browse Source

Added API

master
Tim Schuster 2 years ago
parent
commit
a4ce5c359b
Signed by: Tim Schuster <mail@timschuster.info> GPG Key ID: F9E27097EFB77F61
1 changed files with 34 additions and 27 deletions
  1. 34
    27
      servertest.go

+ 34
- 27
servertest.go View File

@@ -4,43 +4,50 @@ import (
4 4
 	"fmt"
5 5
 	"log"
6 6
 	"net/http"
7
-	"strings"
8 7
 )
9 8
 
10 9
 type httphandlerrework struct {
11 10
 }
12 11
 
13
-// formatRequest generates ascii representation of a request
14
-func formatRequest(r *http.Request) string {
15
-	// Create return string
16
-	var request []string
17
-	// Add the request string
18
-	url := fmt.Sprintf("%v %v %v", r.Method, r.URL, r.Proto)
19
-	request = append(request, fmt.Sprintf("%v", r.URL.Query()))
20
-	request = append(request, url)
21
-	// Add the host
22
-	request = append(request, fmt.Sprintf("Host: %v", r.Host))
23
-	// Loop through headers
24
-	for name, headers := range r.Header {
25
-		name = strings.ToLower(name)
26
-		for _, h := range headers {
27
-			request = append(request, fmt.Sprintf("%v: %v", name, h))
12
+func (h *httphandlerrework) ServeHTTP(w http.ResponseWriter, r *http.Request) {
13
+	if r.URL.Path == "/api/store" {
14
+		if r.Method == http.MethodGet {
15
+			h.regalReq(w, r)
16
+			return
17
+		}
18
+		if r.Method == http.MethodPut {
19
+			h.regalPut(w, r)
20
+			return
28 21
 		}
29 22
 	}
30
-
31
-	// If this is a POST, add post data
32
-	if r.Method == "POST" {
33
-		r.ParseForm()
34
-		request = append(request, "\n")
35
-		request = append(request, r.Form.Encode())
23
+	if r.URL.Path == "/api/retrieve" && r.Method == http.MethodGet {
24
+		h.waitForItem(w, r)
25
+		return
36 26
 	}
37
-	// Return the request as a string
38
-	return strings.Join(request, "\n")
39 27
 }
40 28
 
41
-func (h *httphandlerrework) ServeHTTP(writer http.ResponseWriter, req *http.Request) {
42
-	//	fmt.Println("s%", formatRequest(req))
43
-	fmt.Fprint(writer, formatRequest(req))
29
+func (h *httphandlerrework) regalReq(w http.ResponseWriter, r *http.Request) {
30
+	//TODO: Retrieve Item from Storage
31
+	w.WriteHeader(http.StatusInternalServerError)
32
+	regalNum := r.URL.Query().Get("regal") // Regal
33
+	floorNum := r.URL.Query().Get("floor") // Fach
34
+	fmt.Fprintf(w, "POK: %d %d\n", regalNum, floorNum)
35
+	fmt.Fprint(w, "NOT OK\n")
36
+}
37
+
38
+func (h *httphandlerrework) regalPut(w http.ResponseWriter, r *http.Request) {
39
+	//TODO: Put Item into Storage
40
+	w.WriteHeader(http.StatusInternalServerError)
41
+	regalNum := r.URL.Query().Get("regal") // Regal
42
+	floorNum := r.URL.Query().Get("floor") // Fach
43
+	fmt.Fprintf(w, "POK: %d %d\n", regalNum, floorNum)
44
+	fmt.Fprint(w, "NOT OK")
45
+}
46
+
47
+func (h *httphandlerrework) waitForItem(w http.ResponseWriter, r *http.Request) {
48
+	//TODO: Move into position to accept a new item
49
+	w.WriteHeader(http.StatusInternalServerError)
50
+	fmt.Fprint(w, "NOT OK")
44 51
 }
45 52
 
46 53
 func main() {

Loading…
Cancel
Save