/
dynamic-pricing.go
134 lines (100 loc) · 3.87 KB
/
dynamic-pricing.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"net/http"
"os"
"fmt"
"github.com/op/go-logging"
"github.com/gorilla/mux"
"github.com/hudl/fargo"
"github.com/joliva-ob/onebox-dynamic-pricing/dataservice"
"github.com/joliva-ob/onebox-dynamic-pricing/configuration"
"github.com/joliva-ob/onebox-dynamic-pricing/controller"
"time"
)
// Global vars
var config configuration.Config
var log *logging.Logger
var eurekaConn fargo.EurekaConnection
/**
* Main command to load configuration by given environment argument
* and start application server to listen the exposed endpoints and
* provide the requested resources operations
*
* Mandatory parameters are path (/tmp...) and environment (dev, qa, pre, pro...)
*/
func main() {
// Load configuration to start application
conf_path, env := checkParams( os.Args )
var filename = conf_path + "/" + env + ".yml"
config = configuration.LoadConfiguration(filename)
log = configuration.GetLog()
dataservice.Initialize(config)
log.Infof("dynamic-pricing started with environment: %s and listening in port: %v\n", env, config.Server_port)
// Register to Eureka and then set up to only heartbeat one of them
filename = conf_path + "/eureka_" + env + ".gcfg"
// ec, i := registerToEureka( filename )
// go sendHeartBeatToEureka( ec, i )
// Create the router to handle requests
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/dynamic-pricing-api/1.0/prices/{id}", controller.PricesController) // Prices endpoint
router.HandleFunc("/dynamic-pricing-api/1.0/prices", controller.PricesController) // Prices endpoint
router.HandleFunc("/dynamic-pricing-api/1.0/sales/{id}", controller.SalesController) // Sales endpoint
router.HandleFunc("/dynamic-pricing-api/1.0/sales", controller.SalesController) // Sales endpoint
router.HandleFunc("/dynamic-pricing-api/1.0/info", controller.InfoController)
router.HandleFunc("/dynamic-pricing-api/1.0/health", controller.HealthController)
router.HandleFunc("/dynamic-pricing-api/1.0/reload-restrictions", controller.ReloadRestrictionsController)
// Starting server on given port number
log.Fatal( http.ListenAndServe(":" + config.Server_port, router) ) // Start the server at listening port
}
// Check the arguments to launch the application
// and provide specifications if needed.
func checkParams( args []string ) (string, string) {
path := os.Getenv("CONF_PATH")
env := os.Getenv("ENV")
if path == "" || env == "" {
if len(args) < 2 {
fmt.Println("ERROR: invalid arguments")
fmt.Println("Usage:")
fmt.Println("./dynamic-pricing [path-to-config-files] [environment] or set CONF_PATH set ENV")
os.Exit(0)
} else {
path = args[1]
env = args[2]
}
}
return path, env
}
// Register and keep the eureka connection
func registerToEureka( configFile string ) (fargo.EurekaConnection, fargo.Instance) {
eurekaConn, _ = fargo.NewConnFromConfigFile(configFile)
hostname, _ := os.Hostname()
i := fargo.Instance{
HostName: hostname,
Port: 8000,
App: config.Eureka_app_name,
IPAddr: hostname,
VipAddress: config.Eureka_app_name,
DataCenterInfo: fargo.DataCenterInfo{Name: fargo.Amazon},
SecureVipAddress: config.Eureka_ip_addr,
Status: fargo.UP,
HealthCheckUrl: "http://" +hostname+ ":" +config.Server_port+ "/dynamic-pricing-api/1.0/health",
StatusPageUrl: "http://" +hostname+ ":" +config.Server_port+ "/dynamic-pricing-api/1.0/health",
HomePageUrl: "http://" +hostname+ ":" +config.Server_port+ "/dynamic-pricing-api/1.0/health",
}
err := eurekaConn.RegisterInstance(&i)
if err != nil {
log.Error("%v", err)
}
return eurekaConn, i
}
// Go routine to keep registered into
// Eureka service discovery
func sendHeartBeatToEureka( ec fargo.EurekaConnection, i fargo.Instance ) {
ticker := time.Tick(time.Duration(30 * 1000) * time.Millisecond)
for {
select {
case <- ticker:
ec.HeartBeatInstance(&i)
}
}
}