This repository was archived by the owner on Mar 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
116 lines (87 loc) · 2.97 KB
/
Copy pathmain.go
File metadata and controls
116 lines (87 loc) · 2.97 KB
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
package main
import (
"log"
"net/http"
"os"
stdjwt "github.com/dgrijalva/jwt-go"
"github.com/go-kit/kit/auth/jwt"
LOG "github.com/go-kit/kit/log"
httptransport "github.com/go-kit/kit/transport/http"
"golang.org/x/net/context"
"github.com/gorilla/mux"
"github.com/go-kit/kit/endpoint"
"github.com/darenegade/SimpleGoKitService/database"
"github.com/darenegade/SimpleGoKitService/employee"
"github.com/darenegade/SimpleGoKitService/hello"
"github.com/darenegade/SimpleGoKitService/middleware"
)
var (
logger = LOG.NewLogfmtLogger(os.Stdout)
ctx = context.Background()
kf = func(token *stdjwt.Token) (interface{}, error) { return []byte("TEST"), nil }
)
func main() {
database.Initialize()
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
r := mux.NewRouter()
handleHelloWorld(r)
handleEmployees(r)
http.Handle("/", accessControl(r))
log.Fatal(http.ListenAndServe(":"+port, nil))
}
func handleHelloWorld(r *mux.Router) {
endpointPath, createdEndpoint := hello.MakeHelloWorldEndpoint()
createdEndpoint = configEndpoint(createdEndpoint, endpointPath)
helloWorldHandler := httptransport.NewServer(
ctx,
createdEndpoint,
hello.MakeHelloWorldDecoder(),
httptransport.EncodeJSONResponse,
httptransport.ServerBefore(jwt.ToHTTPContext()),
httptransport.ServerErrorLogger(logger),
)
r.Handle(endpointPath, helloWorldHandler).Methods(http.MethodPost)
}
func handleEmployees(r *mux.Router) {
endpointPath, createdEndpoint := employee.MakeEmployeesEndpoint()
createdEndpoint = configEndpoint(createdEndpoint, endpointPath)
handler := httptransport.NewServer(
ctx,
createdEndpoint,
employee.MakeEmployeesDecoder(),
httptransport.EncodeJSONResponse,
httptransport.ServerBefore(jwt.ToHTTPContext()),
httptransport.ServerErrorLogger(logger),
)
r.Handle(endpointPath, handler).Methods(http.MethodGet, http.MethodPost)
endpointPath, createdEndpoint = employee.MakeEmployeeEndpoint()
createdEndpoint = configEndpoint(createdEndpoint, endpointPath)
handler = httptransport.NewServer(
ctx,
createdEndpoint,
employee.MakeEmployeeDecoder(),
httptransport.EncodeJSONResponse,
httptransport.ServerBefore(jwt.ToHTTPContext()),
httptransport.ServerErrorLogger(logger),
)
r.Handle(endpointPath, handler).Methods(http.MethodGet, http.MethodPut, http.MethodDelete)
}
func configEndpoint(endpoint endpoint.Endpoint, endpointName string) endpoint.Endpoint {
endpoint = jwt.NewParser(kf, stdjwt.SigningMethodHS256)(endpoint)
endpoint = middleware.Logging(LOG.NewContext(logger).With("method", endpointName))(endpoint)
return endpoint
}
func accessControl(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Origin, Authorization, Content-Type")
if r.Method == "OPTIONS" {
return
}
h.ServeHTTP(w, r)
})
}