Example #1
0
func main() {

	m := martini.Classic()

	m.Use(render.Renderer())

	martini.Env = martini.Prod // You have to set the environment to `production` for all of secure to work properly!

	m.Use(secure.Secure(secure.Options{
		AllowedHosts:         []string{"localhost:1433"},
		SSLRedirect:          true,
		SSLHost:              "localhost:1433",
		SSLProxyHeaders:      map[string]string{"X-Forwarded-Proto": "https"},
		STSSeconds:           315360000,
		STSIncludeSubdomains: true,
		FrameDeny:            true,
		ContentTypeNosniff:   true,
		BrowserXssFilter:     true,
	}))

	model := models.New()

	m.Map(sessionStore)
	m.Map(pool)

	RouteAuth(m, model)
	RouteReminders(m, model)

	m.Map(model.DB.DB())

	m.Use(martini.Static("html_project/site_v1/public_html"))

	folderPath, err := osext.ExecutableFolder()
	if err != nil {
		log.Fatal(err)
	}

	if err := http.ListenAndServeTLS(":1433", folderPath+"/cert/myrsacert.pem", folderPath+"/cert/myrsakey.pem", m); err != nil {
		log.Fatal(err)
	}
}
Example #2
0
File: wigo.go Project: root-gg/wigo
func threadHttp(config *wigo.HttpConfig) {
	apiAddress := config.Address
	apiPort := config.Port

	m := martini.New()

	if wigo.GetLocalWigo().GetConfig().Global.Debug {
		// Log requests
		m.Use(martini.Logger())
	}

	// Compress http responses with gzip
	if config.Gzip {
		log.Println("Http server : gzip compression enabled")
		m.Use(gzip.All())
	}

	// Add some basic security checks
	m.Use(secure.Secure(secure.Options{}))

	// Http basic auth
	if config.Login != "" && config.Password != "" {
		log.Println("Http server : basic auth enabled")
		m.Use(auth.Basic(config.Login, config.Password))
	}

	// Serve static files
	m.Use(martini.Static("public"))

	// Handle errors // TODO is this even working ?
	m.Use(martini.Recovery())

	// Define the routes.

	r := martini.NewRouter()

	r.Get("/api", func() (int, string) {
		json, err := wigo.GetLocalWigo().ToJsonString()
		if err != nil {
			return 500, fmt.Sprintf("%s", err)
		}
		return 200, json
	})

	r.Get("/api/status", func() string { return strconv.Itoa((wigo.GetLocalWigo().GlobalStatus)) })
	r.Get("/api/logs", wigo.HttpLogsHandler)
	r.Get("/api/logs/indexes", wigo.HttpLogsIndexesHandler)
	r.Get("/api/groups", wigo.HttpGroupsHandler)
	r.Get("/api/groups/:group", wigo.HttpGroupsHandler)
	r.Get("/api/groups/:group/logs", wigo.HttpLogsHandler)
	r.Get("/api/groups/:group/probes/:probe/logs", wigo.HttpLogsHandler)
	r.Get("/api/hosts", wigo.HttpRemotesHandler)
	r.Get("/api/hosts/:hostname", wigo.HttpRemotesHandler)
	r.Get("/api/hosts/:hostname/status", wigo.HttpRemotesStatusHandler)
	r.Get("/api/hosts/:hostname/logs", wigo.HttpLogsHandler)
	r.Get("/api/hosts/:hostname/probes", wigo.HttpRemotesProbesHandler)
	r.Get("/api/hosts/:hostname/probes/:probe", wigo.HttpRemotesProbesHandler)
	r.Get("/api/hosts/:hostname/probes/:probe/status", wigo.HttpRemotesProbesStatusHandler)
	r.Get("/api/hosts/:hostname/probes/:probe/logs", wigo.HttpLogsHandler)
	r.Get("/api/probes/:probe/logs", wigo.HttpLogsHandler)
	r.Get("/api/authority/hosts", wigo.HttpAuthorityListHandler)
	r.Post("/api/authority/hosts/:uuid/allow", wigo.HttpAuthorityAllowHandler)
	r.Post("/api/authority/hosts/:uuid/revoke", wigo.HttpAuthorityRevokeHandler)

	m.Use(func(c martini.Context, w http.ResponseWriter, r *http.Request) {
		if strings.HasPrefix(r.URL.Path, "/api") {
			w.Header().Set("Content-Type", "application/json")
		}
	})

	m.Action(r.Handle)

	// Create a listner and serv connections forever.
	if config.SslEnabled {
		address := apiAddress + ":" + strconv.Itoa(apiPort)
		log.Println("Http server : starting tls server @ " + address)
		tlsConfig := &tls.Config{MinVersion: tls.VersionTLS10}
		server := &http.Server{Addr: address, Handler: m, TLSConfig: tlsConfig}
		err := server.ListenAndServeTLS(config.SslCert, config.SslKey)
		if err != nil {
			log.Fatalf("Failed to start http server : %s", err)
		}
	} else {
		address := apiAddress + ":" + strconv.Itoa(apiPort)
		log.Println("Http server : starting plain http server @ " + address)
		if err := http.ListenAndServe(address, m); err != nil {
			log.Fatalf("Failed to start http server : %s", err)
		}
	}
}
Example #3
0
// Daemon mode (agent mode)
func CmdDaemon(c *cli.Context) {

	fp, err := reopen.NewFileWriter(c.String("logfile"))
	if err != nil {
		fmt.Println(err)
	}
	log.SetOutput(fp)
	sigHup := make(chan os.Signal, 1)
	signal.Notify(sigHup, syscall.SIGHUP)
	go func() {
		for {
			<-sigHup
			fp.Reopen()
		}
	}()

	m := customClassic()
	m.Use(render.Renderer())
	m.Use(util.ACL(c.StringSlice("allowed-hosts")))
	m.Use(
		secure.Secure(secure.Options{
			SSLRedirect:      true,
			DisableProdCheck: true,
		}))

	// CPU Profiling
	if c.String("cpu-profile") != "" {
		cpuprofile := c.String("cpu-profile")
		f, err := os.Create(cpuprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
		cpuprof := make(chan os.Signal, 1)
		signal.Notify(cpuprof, os.Interrupt)
		go func() {
			for sig := range cpuprof {
				log.Printf("captured %v, stopping profiler and exiting...", sig)
				pprof.StopCPUProfile()
				os.Exit(1)
			}
		}()
	}

	m.Get("/", func() string {
		return "OK"
	})

	util.CommandTimeout = time.Duration(c.Int("command-timeout"))
	model.MetricConfigFile = c.String("metric-config")

	m.Post("/proxy", binding.Json(happo_agent.ProxyRequest{}), model.Proxy)
	m.Post("/inventory", binding.Json(happo_agent.InventoryRequest{}), model.Inventory)
	m.Post("/monitor", binding.Json(happo_agent.MonitorRequest{}), model.Monitor)
	m.Post("/metric", binding.Json(happo_agent.MetricRequest{}), model.Metric)
	m.Post("/metric/config/update", binding.Json(happo_agent.MetricConfigUpdateRequest{}), model.MetricConfigUpdate)
	m.Get("/metric/status", model.MetricDataBufferStatus)

	// Listener
	var lis daemonListener
	lis.Port = fmt.Sprintf(":%d", c.Int("port"))
	lis.Handler = m
	lis.Timeout = happo_agent.HTTP_TIMEOUT
	lis.MaxConnections = c.Int("max-connections")
	lis.PublicKey = c.String("public-key")
	lis.PrivateKey = c.String("private-key")
	go func() {
		err := lis.listenAndServe()
		if err != nil {
			log.Fatal(err)
		}
	}()

	// Metric collect timer
	time_metrics := time.NewTicker(time.Minute).C
	for {
		select {
		case <-time_metrics:
			err := collect.Metrics(c.String("metric-config"))
			if err != nil {
				log.Fatal(err)
			}
		}
	}
}