-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
48 lines (41 loc) · 1.5 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
/*
Main entry point for Boldly Go RESTful Application.
Simple REST API to expose a few endpoints to get a feel for GoLang.
Endpoints:
- /api/v1/ping // HEALTH CHECK PING ENDPOINT
- /api/v1/user/{owningUserId}/bank/{bankId} // GET THE USERS BANK RECORD
- /api/v1/bank // SAVE A BANK RECORD
*/
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
const appPortKey = ":5002"
var awsSvc AwsConfig = &awsConf{}
func main() {
// instantiate aws configuration
awsSvc.Init()
// instantiate mux router
router := mux.NewRouter().StrictSlash(true)
router.Methods("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS").Schemes("http")
// give me that api path-prefix
api := router.PathPrefix("/api").Subrouter()
// endpoint registry
api.HandleFunc("/v1/ping", PingHandler).Methods("GET")
api.HandleFunc("/v1/user/{owningUserId}/bank/{bankId}", GetBankHandler).Methods("GET")
api.HandleFunc("/v1/bank", SaveBankHandler).Methods("POST")
// add CORS acceptance to all requests
handler := handlers.CORS(
handlers.AllowedOrigins([]string{"*"}),
handlers.AllowedMethods([]string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}),
handlers.AllowedHeaders([]string{"Content-Type", "X-Requested-With", "Accept", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization"}),
)(api)
// start app
fmt.Println(fmt.Sprintf("App Running on Port %s", appPortKey))
log.Fatal(http.ListenAndServe(appPortKey, handlers.LoggingHandler(os.Stdout, handler)))
}