Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add controllers package with implementation #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions cmd/muxing/muxing.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,38 @@ package main

import (
"fmt"
"log"
"net/http"
"os"
"strconv"

"github.com/gorilla/mux"
"github.com/GolangUnited/helloweb/internal/controllers"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)

/**
Please note Start functions is a placeholder for you to start your own solution.
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
*/

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

setupMiddleware(r)

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 {
log.Fatal(err)
r.Get("/name/{PARAM}", controllers.NameHandler)
r.Get("/bad", controllers.BadHandler)
r.Post("/data", controllers.DataHandler)
r.Post("/headers", controllers.HeadersHandler)

if err := http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), r); err != nil {
panic(err)
}
}

func setupMiddleware(r *chi.Mux) {
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
}

//main /** starts program, gets HOST:PORT param and calls Start func.
func main() {
host := os.Getenv("HOST")
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/go-chi/chi/v5 v5.0.7
17 changes: 2 additions & 15 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=
github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8=
github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
14 changes: 14 additions & 0 deletions index.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
GET http://localhost:8081/name/PARAM

###
GET http://localhost:8081/bad

###
POST http://localhost:8081/data

PARAM

###
POST http://localhost:8081/headers
a: 2
b: 3
48 changes: 48 additions & 0 deletions internal/controllers/controllers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package controllers

import (
"fmt"
"github.com/go-chi/chi/v5"
"io"
"net/http"
"strconv"
)

func NameHandler(w http.ResponseWriter, r *http.Request) {
param := chi.URLParam(r, "PARAM")
_, err := w.Write([]byte(fmt.Sprintf("Hello, %v", param)))
if err != nil {
http.Error(w, http.StatusText(500), 500)
}
}

func BadHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, http.StatusText(500), 500)
}

func DataHandler(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, http.StatusText(500), 500)
}
_, err = w.Write([]byte(fmt.Sprintf("I got message:\n%v", string(body))))
if err != nil {
http.Error(w, http.StatusText(500), 500)
}
}

func HeadersHandler(w http.ResponseWriter, r *http.Request) {
h := r.Header

a, err := strconv.Atoi(h.Get("a"))
if err != nil {
http.Error(w, http.StatusText(400), 400)
}

b, err := strconv.Atoi(h.Get("b"))
if err != nil {
http.Error(w, http.StatusText(400), 400)
}

w.Header().Add("a+b", strconv.Itoa(a+b))
}