diff --git a/codeExamples/09_HTTP/server/example_03/main.go b/codeExamples/09_HTTP/server/example_03/main.go new file mode 100644 index 0000000000000000000000000000000000000000..2f919eea960cab6c9f27dfbe7b3ab26babd1a251 --- /dev/null +++ b/codeExamples/09_HTTP/server/example_03/main.go @@ -0,0 +1,47 @@ +package main + +import ( + "log" + "net/http" +) + +func apiResponse(w http.ResponseWriter, r *http.Request) { + // Set the return Content-Type as JSON + w.Header().Set("Content-Type", "application/json") + log.Println(r.RequestURI) + // Change the response depending on the method being requested + switch r.Method { + case "GET": + w.WriteHeader(http.StatusOK) + w.Write([]byte(`{"message": "GET method requested"}`)) + case "POST": + w.WriteHeader(http.StatusCreated) + w.Write([]byte(`{"message": "POST method requested"}`)) + default: + w.WriteHeader(http.StatusNotFound) + w.Write([]byte(`{"message": "Can't find method requested"}`)) + } +} + +func todoApiResponse(w http.ResponseWriter, r *http.Request) { + // Set the return Content-Type as JSON + w.Header().Set("Content-Type", "application/json") + log.Println(r.RequestURI) + // Change the response depending on the method being requested + switch r.Method { + case "GET": + w.WriteHeader(http.StatusOK) + w.Write([]byte(`{"message": "GET method for todo requested"}`)) + case "POST": + w.WriteHeader(http.StatusCreated) + w.Write([]byte(`{"message": "POST method for todo requested"}`)) + default: + w.WriteHeader(http.StatusNotFound) + w.Write([]byte(`{"message": "Can't find todo method requested"}`)) + } +} +func main() { + http.HandleFunc("/",apiResponse) + http.HandleFunc("/todos",todoApiResponse) + log.Fatal(http.ListenAndServe(":8090",nil)) +} \ No newline at end of file diff --git a/codeExamples/09_HTTP/server/example_03/server b/codeExamples/09_HTTP/server/example_03/server new file mode 100755 index 0000000000000000000000000000000000000000..64b417b5202f2f3330921581a747852058438944 Binary files /dev/null and b/codeExamples/09_HTTP/server/example_03/server differ