-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
45 lines (38 loc) · 878 Bytes
/
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
package main
import (
"encoding/json"
"log"
"net/http"
"github.com/gorilla/mux"
)
type post struct {
Title string `json:"title"`
Author string `json:"author"`
URL string `json:"url"`
}
func main() {
r := mux.NewRouter()
r.Handle("/public", public)
r.Handle("/auth", GetTokenHandler)
r.Handle("/private", JwtMiddleware.Handler(private))
err := http.ListenAndServe(":8080", r)
if err != nil {
log.Fatal("ListenAndServe:", err)
}
}
var public = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
post := &post{
Title: "Big News",
Author: "Ken",
URL: "https://example.com/test",
}
json.NewEncoder(w).Encode(post)
})
var private = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
post := &post{
Title: "Small News",
Author: "John",
URL: "https://example.com/test",
}
json.NewEncoder(w).Encode(post)
})