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 (
"fmt"
"log"
"net/http"
"strings"
)

type httphandlerrework struct {
}

// formatRequest generates ascii representation of a request
func formatRequest(r *http.Request) string {
// Create return string
var request []string
// Add the request string
url := fmt.Sprintf("%v %v %v", r.Method, r.URL, r.Proto)
request = append(request, fmt.Sprintf("%v", r.URL.Query()))
request = append(request, url)
// Add the host
request = append(request, fmt.Sprintf("Host: %v", r.Host))
// Loop through headers
for name, headers := range r.Header {
name = strings.ToLower(name)
for _, h := range headers {
request = append(request, fmt.Sprintf("%v: %v", name, h))
func (h *httphandlerrework) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/api/store" {
if r.Method == http.MethodGet {
h.regalReq(w, r)
return
}
if r.Method == http.MethodPut {
h.regalPut(w, r)
return
}
}

// If this is a POST, add post data
if r.Method == "POST" {
r.ParseForm()
request = append(request, "\n")
request = append(request, r.Form.Encode())
if r.URL.Path == "/api/retrieve" && r.Method == http.MethodGet {
h.waitForItem(w, r)
return
}
// Return the request as a string
return strings.Join(request, "\n")
}

func (h *httphandlerrework) ServeHTTP(writer http.ResponseWriter, req *http.Request) {
// fmt.Println("s%", formatRequest(req))
fmt.Fprint(writer, formatRequest(req))
func (h *httphandlerrework) regalReq(w http.ResponseWriter, r *http.Request) {
//TODO: Retrieve Item from Storage
w.WriteHeader(http.StatusInternalServerError)
regalNum := r.URL.Query().Get("regal") // Regal
floorNum := r.URL.Query().Get("floor") // Fach
fmt.Fprintf(w, "POK: %d %d\n", regalNum, floorNum)
fmt.Fprint(w, "NOT OK\n")
}

func (h *httphandlerrework) regalPut(w http.ResponseWriter, r *http.Request) {
//TODO: Put Item into Storage
w.WriteHeader(http.StatusInternalServerError)
regalNum := r.URL.Query().Get("regal") // Regal
floorNum := r.URL.Query().Get("floor") // Fach
fmt.Fprintf(w, "POK: %d %d\n", regalNum, floorNum)
fmt.Fprint(w, "NOT OK")
}

func (h *httphandlerrework) waitForItem(w http.ResponseWriter, r *http.Request) {
//TODO: Move into position to accept a new item
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, "NOT OK")
}

func main() {

Loading…
Cancel
Save