Skip to content

Commit ef05883

Browse files
author
shulg
committed
refactoring
1 parent d73a782 commit ef05883

19 files changed

+446
-253
lines changed

src/api/adminGroup.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package api
2+
3+
import (
4+
"github.com/labstack/echo"
5+
"api/handlers"
6+
)
7+
8+
func AdminGroup(g *echo.Group) {
9+
g.GET("/main", handlers.MainAdmin)
10+
}

src/api/cookieGroup.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package api
2+
3+
import (
4+
"api/handlers"
5+
6+
"github.com/labstack/echo"
7+
)
8+
9+
func CookieGroup(g *echo.Group) {
10+
g.GET("/main", handlers.MainCookie)
11+
}

src/api/handlers/adminhandlers.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package handlers
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/labstack/echo"
7+
)
8+
9+
func MainAdmin(c echo.Context) error {
10+
return c.String(http.StatusOK, "horay you are on the secret amdin main page!")
11+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package handlers
2+
3+
import (
4+
"log"
5+
"net/http"
6+
"time"
7+
8+
"github.com/dgrijalva/jwt-go"
9+
"github.com/labstack/echo"
10+
)
11+
12+
type JwtClaims struct {
13+
Name string `json:"name"`
14+
jwt.StandardClaims
15+
}
16+
17+
func Login(c echo.Context) error {
18+
username := c.QueryParam("username")
19+
password := c.QueryParam("password")
20+
21+
// check username and password against DB after hashing the password
22+
if username == "jack" && password == "1234" {
23+
cookie := &http.Cookie{}
24+
25+
// this is the same
26+
//cookie := new(http.Cookie)
27+
28+
cookie.Name = "sessionID"
29+
cookie.Value = "some_string"
30+
cookie.Expires = time.Now().Add(48 * time.Hour)
31+
32+
c.SetCookie(cookie)
33+
34+
// create jwt token
35+
token, err := createJwtToken()
36+
if err != nil {
37+
log.Println("Error Creating JWT token", err)
38+
return c.String(http.StatusInternalServerError, "something went wrong")
39+
}
40+
41+
return c.JSON(http.StatusOK, map[string]string{
42+
"message": "You were logged in!",
43+
"token": token,
44+
})
45+
}
46+
47+
return c.String(http.StatusUnauthorized, "Your username or password were wrong")
48+
}
49+
50+
func createJwtToken() (string, error) {
51+
claims := JwtClaims{
52+
"jack",
53+
jwt.StandardClaims{
54+
Id: "main_user_id",
55+
ExpiresAt: time.Now().Add(24 * time.Hour).Unix(),
56+
},
57+
}
58+
59+
rawToken := jwt.NewWithClaims(jwt.SigningMethodHS512, claims)
60+
61+
token, err := rawToken.SignedString([]byte("mySecret"))
62+
if err != nil {
63+
return "", err
64+
}
65+
66+
return token, nil
67+
}

src/api/handlers/cathandlers.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package handlers
2+
3+
import (
4+
"net/http"
5+
"fmt"
6+
"log"
7+
"io/ioutil"
8+
"encoding/json"
9+
10+
"github.com/labstack/echo"
11+
)
12+
13+
type Cat struct {
14+
Name string `json:"name"`
15+
Type string `json:"type"`
16+
}
17+
18+
func GetCats(c echo.Context) error {
19+
catName := c.QueryParam("name")
20+
catType := c.QueryParam("type")
21+
22+
dataType := c.Param("data")
23+
24+
if dataType == "string" {
25+
return c.String(http.StatusOK, fmt.Sprintf("your cat name is: %s\nand his type is: %s\n", catName, catType))
26+
}
27+
28+
if dataType == "json" {
29+
return c.JSON(http.StatusOK, map[string]string{
30+
"name": catName,
31+
"type": catType,
32+
})
33+
}
34+
35+
return c.JSON(http.StatusBadRequest, map[string]string{
36+
"error": "you need to lets us know if you want json or string data",
37+
})
38+
}
39+
40+
func AddCat(c echo.Context) error {
41+
cat := Cat{}
42+
43+
defer c.Request().Body.Close()
44+
45+
b, err := ioutil.ReadAll(c.Request().Body)
46+
if err != nil {
47+
log.Printf("Failed reading the request body for addCats: %s\n", err)
48+
return c.String(http.StatusInternalServerError, "")
49+
}
50+
51+
err = json.Unmarshal(b, &cat)
52+
if err != nil {
53+
log.Printf("Failed unmarshaling in addCats: %s\n", err)
54+
return c.String(http.StatusInternalServerError, "")
55+
}
56+
57+
log.Printf("this is your cat: %#v\n", cat)
58+
return c.String(http.StatusOK, "we got your cat!")
59+
}

src/api/handlers/cookiehandlers.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package handlers
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/labstack/echo"
7+
)
8+
9+
func MainCookie(c echo.Context) error {
10+
return c.String(http.StatusOK, "you are on the secret cookie page!")
11+
}

src/api/handlers/doghandlers.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package handlers
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"log"
7+
8+
"github.com/labstack/echo"
9+
)
10+
11+
type Dog struct {
12+
Name string `json:"name"`
13+
Type string `json:"type"`
14+
}
15+
16+
func AddDog(c echo.Context) error {
17+
dog := Dog{}
18+
19+
defer c.Request().Body.Close()
20+
21+
err := json.NewDecoder(c.Request().Body).Decode(&dog)
22+
if err != nil {
23+
log.Printf("Failed processing addDog request: %s\n", err)
24+
return echo.NewHTTPError(http.StatusInternalServerError)
25+
}
26+
27+
log.Printf("this is your dog: %#v", dog)
28+
return c.String(http.StatusOK, "we got your dog!")
29+
}

src/api/handlers/generalhandlers.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package handlers
2+
3+
import (
4+
"github.com/labstack/echo"
5+
"net/http"
6+
)
7+
8+
func Yallo(c echo.Context) error {
9+
return c.String(http.StatusOK, "yallo from the web side!")
10+
}

src/api/handlers/hamsterhandlers.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package handlers
2+
3+
import (
4+
"log"
5+
"github.com/labstack/echo"
6+
"net/http"
7+
)
8+
9+
type Hamster struct {
10+
Name string `json:"name"`
11+
Type string `json:"type"`
12+
}
13+
14+
func AddHamster(c echo.Context) error {
15+
hamster := Hamster{}
16+
17+
err := c.Bind(&hamster)
18+
if err != nil {
19+
log.Printf("Failed processing addHamster request: %s\n", err)
20+
return echo.NewHTTPError(http.StatusInternalServerError)
21+
}
22+
23+
log.Printf("this is your hamster: %#v", hamster)
24+
return c.String(http.StatusOK, "we got your hamster!")
25+
}

src/api/handlers/jwthandlers.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package handlers
2+
3+
import (
4+
"net/http"
5+
"log"
6+
7+
"github.com/labstack/echo"
8+
"github.com/dgrijalva/jwt-go"
9+
)
10+
11+
func MainJwt(c echo.Context) error {
12+
user := c.Get("user")
13+
token := user.(*jwt.Token)
14+
15+
claims := token.Claims.(jwt.MapClaims)
16+
17+
log.Println("User Name: ", claims["name"], "User ID: ", claims["jti"])
18+
19+
return c.String(http.StatusOK, "you are on the top secret jwt page!")
20+
}

0 commit comments

Comments
 (0)