-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
125 lines (102 loc) · 3.69 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"log"
"net/http"
"os"
"encoding/base64"
"encoding/json"
"time"
"github.com/joho/godotenv"
"github.com/auth0/go-jwt-middleware"
"github.com/dgrijalva/jwt-go"
"github.com/gorilla/mux"
"github.com/gorilla/handlers"
)
type Product struct {
Id int
Name string
Slug string
Description string
}
var products = []Product{
{Id: 1, Name: "Hover Shooters", Slug: "hover-shooters", Description : "Shoot your way to the top on 14 different hoverboards"},
{Id: 2, Name: "Ocean Explorer", Slug: "ocean-explorer", Description : "Explore the depths of the sea in this one of a kind underwater experience"},
{Id: 3, Name: "Dinosaur Park", Slug : "dinosaur-park", Description : "Go back 65 million years in the past and ride a T-Rex"},
{Id: 4, Name: "Cars VR", Slug : "cars-vr", Description: "Get behind the wheel of the fastest cars in the world."},
{Id: 5, Name: "Robin Hood", Slug: "robin-hood", Description : "Pick up the bow and arrow and master the art of archery"},
{Id: 6, Name: "Real World VR", Slug: "real-world-vr", Description : "Explore the seven wonders of the world in VR"},
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
r := mux.NewRouter()
r.Handle("/", http.FileServer(http.Dir("./views/")))
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
// Auth0 Token
r.Handle("/status", StatusHandler).Methods("GET")
r.Handle("/products", jwtMiddleware.Handler(ProductsHandler)).Methods("GET")
r.Handle("/products/{slug}/feedback", jwtMiddleware.Handler(AddFeedbackHandler)).Methods("POST")
// Manual Token
r.Handle("/get-token", GetTokenHandler).Methods("GET")
r.Handle("/manual/products/", ValidateToken.Handler(ProductsHandler)).Methods("GET")
r.Handle("/manual/products/{slug}/feedback", ValidateToken.Handler(AddFeedbackHandler)).Methods("POST")
http.ListenAndServe(":3000", handlers.LoggingHandler(os.Stdout, r))
}
var mySigningKey = []byte("secret")
// Handlers
var GetTokenHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
token := jwt.New(jwt.SigningMethodHS256)
token.Claims["admin"] = true
token.Claims["name"] = "Ado Kukic"
token.Claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
tokenString, err := token.SignedString(mySigningKey)
if(err != nil){
log.Fatal(err)
}
w.Write([]byte(tokenString))
})
var ValidateToken = jwtmiddleware.New(jwtmiddleware.Options{
ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
return mySigningKey, nil
},
SigningMethod: jwt.SigningMethodHS256,
})
var jwtMiddleware = jwtmiddleware.New(jwtmiddleware.Options{
ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
decoded, err := base64.URLEncoding.DecodeString(os.Getenv("AUTH0_CLIENT_SECRET"))
if err != nil {
return nil, err
}
return decoded, nil
},
})
var NotImplemented = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
w.Write([]byte("Not Implemented"))
})
var StatusHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
w.Write([]byte("API is up and running"))
})
var ProductsHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
payload, _ := json.Marshal(products)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(payload))
})
var AddFeedbackHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
var product Product
vars := mux.Vars(r)
slug := vars["slug"]
for _, p := range products {
if p.Slug == slug {
product = p
}
}
w.Header().Set("Content-Type", "application/json")
if product.Slug != "" {
payload, _ := json.Marshal(product)
w.Write([]byte(payload))
} else {
w.Write([]byte("Product Not Found"))
}
})