/
main.go
98 lines (67 loc) · 2.02 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
package main
import (
"log"
"net/http"
"os"
"peer2peer/config"
"peer2peer/controllers"
"peer2peer/db/postgres"
"peer2peer/routers"
"runtime"
"time"
"peer2peer/Godeps/_workspace/src/github.com/codegangsta/negroni"
"peer2peer/Godeps/_workspace/src/github.com/gorilla/mux"
)
func init() {
runtime.GOMAXPROCS(runtime.NumCPU() * 2)
}
func main() {
log.Printf("Current Machine Descriptors %s/%s\n", runtime.GOOS, runtime.GOARCH)
args := os.Args[1:]
if len(args) < 1 {
log.Fatal("Usage : ./peer2peer <mode>")
}
mode := args[0]
// Create all the missing tables before starting server
db.CreateTablesIfNotExists()
switch mode {
case "auth":
log.Println("Auth Server - Logs", time.Now())
log.Printf("Auth Server started at - %s\n", config.AuthServerPort)
// Start the Auth Server
StartAuthServer(config.AuthServerPort)
case "api":
log.Println("API Server - Logs", time.Now())
log.Printf("API Server started at - %s\n", config.APIServerPort)
// Start the API Server
StartServer(config.APIServerPort)
default:
log.Fatal("Incorrect Choice of mode : Only auth / api is legal")
}
}
// StartServer : Start the API Server by calling this function
func StartServer(port string) {
// Need to activate this for token based access
var a controllers.Auth
// Creating a new mux router
var router = mux.NewRouter().StrictSlash(true)
// Add APP routes for various controllers
router = routers.AddVisitorRoutes(router)
// This route is essential to view the monitoring stats for the app.
router.Handle("/debug/vars", http.DefaultServeMux)
n := negroni.Classic()
// Need to activate this for token based access
n.Use(negroni.HandlerFunc(a.RequireTokenAuthentication))
n.UseHandler(router)
n.Run(port)
}
// StartAuthServer is used to start the auth server
func StartAuthServer(port string) {
// Creating a new mux router
var authrouter = mux.NewRouter().StrictSlash(true)
// Add Auth routes
authrouter = routers.AddAuthRoutes(authrouter)
n := negroni.Classic()
n.UseHandler(authrouter)
n.Run(port)
}