-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
50 lines (39 loc) · 1.29 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"git.coding.net/bobxuyang/cy-gateway-BN/app"
"git.coding.net/bobxuyang/cy-gateway-BN/controllers"
"github.com/gorilla/mux"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Do stuff here
log.Println(r.RequestURI)
// Call the next handler, which can be another middleware in the chain, or the final handler.
next.ServeHTTP(w, r)
})
}
func main() {
router := mux.NewRouter()
router.HandleFunc("/api/user/new", controllers.CreateAccount).Methods("POST")
router.HandleFunc("/api/user/login", controllers.Authenticate).Methods("POST")
router.HandleFunc("/api/contacts/new", controllers.CreateContact).Methods("POST")
router.HandleFunc("/api/me/contacts", controllers.GetContactsFor).Methods("GET") // user/2/contacts
router.Use(app.JwtAuthentication) //attach JWT auth middleware
router.Use(loggingMiddleware)
//router.NotFoundHandler = app.NotFoundHandler
port := os.Getenv("PORT")
if port == "" {
port = "8000" //localhost
}
fmt.Println(port)
err := http.ListenAndServe(":"+port, router) //Launch the app, visit localhost:8000/api
if err != nil {
fmt.Print(err)
}
// TODO: graceful shutdown
}