Skip to content

Commit

Permalink
feat: implemented handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
ihor_ostapovych committed Jun 30, 2022
1 parent 6fb3ca6 commit 88908c8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 26 deletions.
59 changes: 57 additions & 2 deletions cmd/muxing/muxing.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"io"
"log"
"net/http"
"os"
Expand All @@ -17,12 +18,66 @@ Feel free to drop gorilla.mux if you want and use any other solution available.
main function reads host/port from env just for an example, flavor it following your taste
*/

func NameHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprintf(w, "Hello, %v!", vars["PARAM"])
if err != nil {
_ = fmt.Errorf("[NameHandler] Error: %w", err)
}
}

func BadHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}

func DataHandler(w http.ResponseWriter, r *http.Request) {
b, err := io.ReadAll(r.Body)
if err != nil {
_ = fmt.Errorf("[DataHandler] Error: %w", err)
return
}

w.WriteHeader(http.StatusOK)
_, err = io.WriteString(w, fmt.Sprintf("I got message:\n%v", string(b)))
if err != nil {
_ = fmt.Errorf("[DataHandler] Error: %w", err)
}
}

func HeadersHandler(w http.ResponseWriter, r *http.Request) {
h := r.Header
a, err := strconv.Atoi(h.Get("a"))
if err != nil {
_ = fmt.Errorf("[DataHandler] Error: %w", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
b, err := strconv.Atoi(h.Get("b"))
if err != nil {
_ = fmt.Errorf("[DataHandler] Error: %w", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
sum := a + b

fmt.Println(fmt.Sprintf("%d", sum))

w.Header().Add("a+b", fmt.Sprintf("%d", sum))
w.WriteHeader(http.StatusOK)
}

// Start /** Starts the web server listener on given host and port.
func Start(host string, port int) {
router := mux.NewRouter()
r := mux.NewRouter()

r.HandleFunc("/name/{PARAM}", NameHandler).Methods("GET")
r.HandleFunc("/bad", BadHandler).Methods("GET")
r.HandleFunc("/data", DataHandler).Methods("POST")
r.HandleFunc("/headers", HeadersHandler).Methods("POST")

log.Println(fmt.Printf("Starting API server on %s:%d\n", host, port))
if err := http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), router); err != nil {
if err := http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), r); err != nil {
log.Fatal(err)
}
}
Expand Down
12 changes: 1 addition & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,4 @@ module github.com/GolangUnited/helloweb

go 1.17

require (
github.com/gorilla/mux v1.8.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.0
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
require github.com/gorilla/mux v1.8.0
13 changes: 0 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +1,2 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 88908c8

Please sign in to comment.