Skip to content

Fix the order of arguments. #4

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

Open
wants to merge 1 commit into
base: part_5_basic_authentication
Choose a base branch
from
Open
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
169 changes: 84 additions & 85 deletions src/main/main.go
Original file line number Diff line number Diff line change
@@ -1,140 +1,139 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"

import(
"fmt"
"net/http"
"io/ioutil"
"log"
"encoding/json"

"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)

type Cat struct {
Name string `json:"name"`
Type string `json:"type"`
Name string `json:"name"`
Type string `json:"type"`
}

type Dog struct {
Name string `json:"name"`
Type string `json:"type"`
Name string `json:"name"`
Type string `json:"type"`
}

type Hamster struct {
Name string `json:"name"`
Type string `json:"type"`
Name string `json:"name"`
Type string `json:"type"`
}

func yallo(c echo.Context) error {
return c.String(http.StatusOK, "yallo from the web side!")
return c.String(http.StatusOK, "yallo from the web side!")
}

func getCats(c echo.Context) error {
catName := c.QueryParam("name")
catType := c.QueryParam("type")
catName := c.QueryParam("name")
catType := c.QueryParam("type")

dataType := c.Param("data")
dataType := c.Param("data")

if dataType == "string" {
return c.String(http.StatusOK, fmt.Sprintf("your cat name is: %s\nand his type is: %s\n", catName, catType))
}
if dataType == "string" {
return c.String(http.StatusOK, fmt.Sprintf("your cat name is: %s\nand his type is: %s\n", catName, catType))
}

if dataType == "json" {
return c.JSON(http.StatusOK, map[string]string{
"name": catName,
"type": catType,
})
}
if dataType == "json" {
return c.JSON(http.StatusOK, map[string]string{
"name": catName,
"type": catType,
})
}

return c.JSON(http.StatusBadRequest, map[string]string{
"error": "you need to lets us know if you want json or string data",
})
return c.JSON(http.StatusBadRequest, map[string]string{
"error": "you need to lets us know if you want json or string data",
})
}

func addCat(c echo.Context) error {
cat := Cat{}
cat := Cat{}

defer c.Request().Body.Close()
defer c.Request().Body.Close()

b, err := ioutil.ReadAll(c.Request().Body)
if err != nil {
log.Printf("Failed reading the request body for addCats: %s\n", err)
return c.String(http.StatusInternalServerError, "")
}
b, err := ioutil.ReadAll(c.Request().Body)
if err != nil {
log.Printf("Failed reading the request body for addCats: %s\n", err)
return c.String(http.StatusInternalServerError, "")
}

err = json.Unmarshal(b, &cat)
if err != nil {
log.Printf("Failed unmarshaling in addCats: %s\n", err)
return c.String(http.StatusInternalServerError, "")
}
err = json.Unmarshal(b, &cat)
if err != nil {
log.Printf("Failed unmarshaling in addCats: %s\n", err)
return c.String(http.StatusInternalServerError, "")
}

log.Printf("this is your cat: %#v\n", cat)
return c.String(http.StatusOK, "we got your cat!")
log.Printf("this is your cat: %#v\n", cat)
return c.String(http.StatusOK, "we got your cat!")
}

func addDog(c echo.Context) error {
dog := Dog{}
dog := Dog{}

defer c.Request().Body.Close()
defer c.Request().Body.Close()

err := json.NewDecoder(c.Request().Body).Decode(&dog)
if err != nil {
log.Printf("Failed processing addDog request: %s\n", err)
return echo.NewHTTPError(http.StatusInternalServerError)
}
err := json.NewDecoder(c.Request().Body).Decode(&dog)
if err != nil {
log.Printf("Failed processing addDog request: %s\n", err)
return echo.NewHTTPError(http.StatusInternalServerError)
}

log.Printf("this is your dog: %#v", dog)
return c.String(http.StatusOK, "we got your dog!")
log.Printf("this is your dog: %#v", dog)
return c.String(http.StatusOK, "we got your dog!")
}

func addHamster(c echo.Context) error {
hamster := Hamster{}
hamster := Hamster{}

err := c.Bind(&hamster)
if err != nil {
log.Printf("Failed processing addHamster request: %s\n", err)
return echo.NewHTTPError(http.StatusInternalServerError)
}
err := c.Bind(&hamster)
if err != nil {
log.Printf("Failed processing addHamster request: %s\n", err)
return echo.NewHTTPError(http.StatusInternalServerError)
}

log.Printf("this is your hamster: %#v", hamster)
return c.String(http.StatusOK, "we got your hamster!")
log.Printf("this is your hamster: %#v", hamster)
return c.String(http.StatusOK, "we got your hamster!")
}

func mainAdmin(c echo.Context) error {
return c.String(http.StatusOK, "horay you are on the secret amdin main page!")
return c.String(http.StatusOK, "horay you are on the secret amdin main page!")
}

func main() {
fmt.Println("Welcome to the server")
fmt.Println("Welcome to the server")

e := echo.New()
e := echo.New()

g := e.Group("/admin")
g := e.Group("/admin")

// this logs the server interaction
g.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: `[${time_rfc3339}] ${status} ${method} ${host}${path} ${latency_human}` + "\n",
}))
// this logs the server interaction
g.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: `[${time_rfc3339}] ${status} ${method} ${host}${path} ${latency_human}` + "\n",
}))

g.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (error, bool) {
// check in the DB
if username == "jack" && password == "1234" {
return nil, true;
}
g.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
// check in the DB
if username == "jack" && password == "1234" {
return true, nil
}

return nil, false;
}))
return false, nil
}))

g.GET("/main", mainAdmin)
g.GET("/main", mainAdmin)

e.GET("/", yallo)
e.GET("/cats/:data", getCats)
e.GET("/", yallo)
e.GET("/cats/:data", getCats)

e.POST("/cats", addCat)
e.POST("/dogs", addDog)
e.POST("/hamsters", addHamster)
e.POST("/cats", addCat)
e.POST("/dogs", addDog)
e.POST("/hamsters", addHamster)

e.Start(":8000")
e.Start(":8000")
}