)

// override the standard Gin-Gonic middleware to add the CORS headers
func CORSMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {

		c.Writer.Header().Set("Content-Type", "application/json")
		c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
		c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
	}
}

// set some globally used vars
var (
	ConfigObj  *Config
	logFile, _ = lumber.NewFileLogger("/tmp/haproxy-rest.log", lumber.INFO, lumber.ROTATE, 1000, 3, 100)
	logConsole = lumber.NewConsoleLogger(lumber.INFO)
	log        = lumber.NewMultiLogger()
	zkClient   ZookeeperClient
)

func main() {

	log.Prefix("Haproxy-rest")

	log.AddLoggers(logFile, logConsole)

	// implicit -h prints out help messages
	port := flag.Int("port", 10001, "Port/IP to use for the REST interface. Overrides $PORT0 env variable")
	lbConfigFile := flag.String("lbConfigFile", "resources/haproxy_new.cfg", "Location of the target HAproxy config file")
	lbTemplateFile := flag.String("lbTemplate", "resources/templates/haproxy_cfg.template", "Template file to build HAproxy load balancer config")
Exemple #2
0
func startPortal(ccmd *cobra.Command, args []string) error {
	if config.LogFile == "" {
		config.Log = lumber.NewConsoleLogger(lumber.LvlInt(config.LogLevel))
	} else {
		var err error
		config.Log, err = lumber.NewFileLogger(config.LogFile, lumber.LvlInt(config.LogLevel), lumber.ROTATE, 5000, 9, 100)
		if err != nil {
			config.Log.Fatal("File logger init failed - %v", err)
			return fmt.Errorf("")
		}
	}

	// ensure proxy ports are unique. we need to check because tls will not listen
	// until a cert is added. we want it to break sooner.
	if config.RouteHttp == config.RouteTls {
		config.Log.Fatal("Proxy addresses must be unique")
		return fmt.Errorf("")
	}
	// need ':' in case tls is double apiport (8080, 80)
	apiPort := fmt.Sprintf(":%s", config.ApiPort)
	if strings.HasSuffix(config.RouteTls, apiPort) {
		config.Log.Fatal("TLS proxy address must be unique")
		return fmt.Errorf("")
	}

	// initialize database
	err := database.Init()
	if err != nil {
		config.Log.Fatal("Database init failed - %v", err)
		return fmt.Errorf("")
	}
	// initialize balancer
	err = balance.Init()
	if err != nil {
		config.Log.Fatal("Balancer init failed - %v", err)
		return fmt.Errorf("")
	}
	// initialize proxymgr
	err = proxymgr.Init()
	if err != nil {
		config.Log.Fatal("Proxymgr init failed - %v", err)
		return fmt.Errorf("")
	}
	// initialize vipmgr
	err = vipmgr.Init()
	if err != nil {
		config.Log.Fatal("Vipmgr init failed - %v", err)
		return fmt.Errorf("")
	}
	// initialize cluster
	err = cluster.Init()
	if err != nil {
		config.Log.Fatal("Cluster init failed - %v", err)
		return fmt.Errorf("")
	}

	go sigHandle()

	// start api
	err = api.StartApi()
	if err != nil {
		config.Log.Fatal("Api start failed - %v", err)
		return fmt.Errorf("")
	}
	return nil
}