2017-05-03 06:52:14 +02:00
|
|
|
// Package that containts the cmd binary of the microservice to run it
|
2017-03-25 14:27:47 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2017-03-25 16:09:17 +01:00
|
|
|
"net/http"
|
2017-03-26 19:48:59 +02:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2017-03-25 16:09:17 +01:00
|
|
|
|
2017-04-07 11:32:49 +02:00
|
|
|
"github.com/NYTimes/gziphandler"
|
2017-03-25 16:09:17 +01:00
|
|
|
goji "goji.io"
|
2017-04-07 11:32:49 +02:00
|
|
|
"goji.io/pat"
|
2017-03-25 16:09:17 +01:00
|
|
|
|
2017-03-30 15:36:04 +02:00
|
|
|
web "github.com/genofire/hs_master-kss-monolith/http"
|
2017-03-30 16:09:44 +02:00
|
|
|
"github.com/genofire/hs_master-kss-monolith/lib/database"
|
2017-03-30 15:36:04 +02:00
|
|
|
"github.com/genofire/hs_master-kss-monolith/lib/log"
|
2017-03-25 16:09:17 +01:00
|
|
|
"github.com/genofire/hs_master-kss-monolith/models"
|
2017-04-07 11:56:28 +02:00
|
|
|
"github.com/genofire/hs_master-kss-monolith/runtime"
|
2017-03-25 14:27:47 +01:00
|
|
|
)
|
|
|
|
|
2017-05-03 06:52:14 +02:00
|
|
|
// Configuration File
|
2017-03-25 14:27:47 +01:00
|
|
|
var (
|
2017-03-25 16:09:17 +01:00
|
|
|
configFile string
|
|
|
|
config *models.Config
|
2017-03-25 14:27:47 +01:00
|
|
|
)
|
|
|
|
|
2017-05-03 06:52:14 +02:00
|
|
|
// Function to run this go program
|
2017-03-25 14:27:47 +01:00
|
|
|
func main() {
|
2017-03-25 16:09:17 +01:00
|
|
|
flag.StringVar(&configFile, "config", "config.conf", "path of configuration file (default:config.conf)")
|
2017-03-25 14:27:47 +01:00
|
|
|
flag.Parse()
|
|
|
|
|
2017-03-25 16:09:17 +01:00
|
|
|
// load config
|
|
|
|
config = models.ReadConfigFile(configFile)
|
2017-04-07 13:13:37 +02:00
|
|
|
|
|
|
|
// Config packages:
|
2017-04-04 23:21:05 +02:00
|
|
|
web.GoodAvailablityTemplate = config.GoodAvailablityTemplate
|
2017-05-12 10:54:05 +02:00
|
|
|
web.GoodFreshnessTemplate = config.GoodFreshnessTemplate
|
2017-04-07 11:56:28 +02:00
|
|
|
runtime.CacheConfig = config.CacheClean
|
2017-04-07 13:13:37 +02:00
|
|
|
runtime.ProductURL = config.MicroserviceDependencies.Product
|
|
|
|
runtime.PermissionURL = config.MicroserviceDependencies.Permission
|
2017-03-25 16:09:17 +01:00
|
|
|
|
2017-04-07 13:13:37 +02:00
|
|
|
log.Log.Info("Starting stock microservice")
|
2017-03-25 16:09:17 +01:00
|
|
|
|
2017-03-30 16:09:44 +02:00
|
|
|
err := database.Open(config.Database)
|
2017-03-30 17:02:20 +02:00
|
|
|
if err != nil {
|
2017-03-30 16:09:44 +02:00
|
|
|
log.Log.Panic(err)
|
|
|
|
}
|
2017-04-07 11:56:28 +02:00
|
|
|
grw := runtime.NewGoodReleaseWorker(config.GoodRelease)
|
|
|
|
cw := runtime.NewCacheWorker()
|
2017-04-04 19:28:46 +02:00
|
|
|
go grw.Start()
|
2017-04-05 19:03:44 +02:00
|
|
|
go cw.Start()
|
2017-03-25 16:09:17 +01:00
|
|
|
// Startwebsrver
|
|
|
|
router := goji.NewMux()
|
2017-03-30 15:36:04 +02:00
|
|
|
web.BindAPI(router)
|
2017-04-07 11:32:49 +02:00
|
|
|
|
|
|
|
router.Handle(pat.New("/*"), gziphandler.GzipHandler(http.FileServer(http.Dir(config.Webroot))))
|
|
|
|
|
2017-03-26 19:48:59 +02:00
|
|
|
srv := &http.Server{
|
|
|
|
Addr: config.WebserverBind,
|
|
|
|
Handler: router,
|
|
|
|
}
|
2017-04-07 11:32:49 +02:00
|
|
|
go func() {
|
|
|
|
if err := srv.ListenAndServe(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
2017-03-26 19:48:59 +02:00
|
|
|
|
2017-04-07 13:13:37 +02:00
|
|
|
log.Log.Info("Started stock microservice")
|
|
|
|
|
2017-03-26 19:48:59 +02:00
|
|
|
// Wait for system signal
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
sig := <-sigs
|
|
|
|
|
|
|
|
// Stop services
|
|
|
|
srv.Close()
|
2017-04-04 19:28:46 +02:00
|
|
|
grw.Close()
|
2017-04-05 19:03:44 +02:00
|
|
|
cw.Close()
|
2017-03-30 16:09:44 +02:00
|
|
|
database.Close()
|
2017-03-26 19:48:59 +02:00
|
|
|
|
2017-03-30 15:36:04 +02:00
|
|
|
log.Log.Info("received", sig)
|
2017-03-25 14:27:47 +01:00
|
|
|
}
|