Exemplo n.º 1
0
func main() {
	m := martini.Classic()
	m.Get("/", func() string {
		return "Hello world!"
	})
	m.Use(auth.Basic("username", "secretpassword"))
	m.Use(martini.Static("./"))
	m.Run()
}
Exemplo n.º 2
0
func init() {
	m = martini.New()
	// Setup middleware
	m.Use(martini.Recovery())
	m.Use(martini.Logger())
	m.Use(auth.Basic(AuthToken, ""))
	m.Use(MapEncoder)
	// Setup routes
	r := martini.NewRouter()
	r.Get(`/albums`, GetAlbums)
	r.Get(`/albums/:id`, GetAlbum)
	r.Post(`/albums`, AddAlbum)
	r.Put(`/albums/:id`, UpdateAlbum)
	r.Delete(`/albums/:id`, DeleteAlbum)
	// Inject database
	m.MapTo(db, (*DB)(nil))
	// Add the router action
	m.Action(r.Handle)
}
Exemplo n.º 3
0
func runWebserver(c *cli.Context) {
	pipelinesConfigPath := c.String("config")
	var err error
	webserverConfig, err = config.LoadConfigFromYAMLFile(pipelinesConfigPath)
	if err != nil {
		log.Fatalln(err)
	}

	m := martini.Classic()
	m.Use(render.Renderer())
	m.Use(auth.Basic(webserverConfig.Auth.Username, webserverConfig.Auth.Password))
	m.Get("/", dashboardShowAll)
	m.Get("/tag/:filter", dashboardFilterByTag)
	m.Get("/db", getDatabase)
	m.Post("/upload", binding.Json(upload.BOSH{}), updateBOSH)
	m.Post("/upload/:reallyuuid/deployments/:name", binding.Json(upload.BOSHDeployment{}), updateDeployment)
	m.Post("/upload/:reallyuuid/deployments/:name/data/:label", binding.Json(upload.DeploymentData{}), updateDeploymentExtraData)
	m.Run()
}
Exemplo n.º 4
0
func webui() {
	m := martini.Classic()
	m.Get("/secret", func() string {
		// show something
		err := LoadSecretOption()
		if err != nil {
			logger.Errorln(err)
			return `{"errno": 1, "msg":", "读取秘钥配置数据失败"}`
		}
		Option_json, err := json.Marshal(SecretOption)
		if err != nil {
			logger.Errorln(err)
			return `{"errno": 1, "msg":", "解析秘钥配置数据失败"}`
		}

		return string(Option_json)
	})

	m.Post("/secret", func(res http.ResponseWriter, req *http.Request) { // res and req are injected by Martini
		SecretOption["username"] = req.FormValue("username")
		SecretOption["password"] = req.FormValue("password")

		SecretOption["bitvc_email"] = req.FormValue("bitvc_email")
		SecretOption["bitvc_password"] = req.FormValue("bitvc_password")

		SecretOption["huobi_access_key"] = req.FormValue("huobi_access_key")
		SecretOption["huobi_secret_key"] = req.FormValue("huobi_secret_key")

		SecretOption["smtp_username"] = req.FormValue("smtp_username")
		SecretOption["smtp_password"] = req.FormValue("smtp_password")
		SecretOption["smtp_host"] = req.FormValue("smtp_host")
		SecretOption["smtp_addr"] = req.FormValue("smtp_addr")

		SecretOption["OKCoinAPIkey"] = req.FormValue("OKCoinAPIkey")

		SecretOption["ok_partner"] = req.FormValue("ok_partner")
		SecretOption["ok_secret_key"] = req.FormValue("ok_secret_key")

		SecretOption["ok_partner1"] = req.FormValue("ok_partner1")
		SecretOption["ok_secret_key1"] = req.FormValue("ok_secret_key1")

		SecretOption["ok_partner2"] = req.FormValue("ok_partner2")
		SecretOption["ok_secret_key2"] = req.FormValue("ok_secret_key2")

		SecretOption["ok_partner3"] = req.FormValue("ok_partner3")
		SecretOption["ok_secret_key3"] = req.FormValue("ok_secret_key3")

		SecretOption["ok_partner4"] = req.FormValue("ok_partner4")
		SecretOption["ok_secret_key4"] = req.FormValue("ok_secret_key4")

		SecretOption["ok_partner5"] = req.FormValue("ok_partner5")
		SecretOption["ok_secret_key5"] = req.FormValue("ok_secret_key5")

		SecretOption["ok_partner6"] = req.FormValue("ok_partner6")
		SecretOption["ok_secret_key6"] = req.FormValue("ok_secret_key6")

		SecretOption["ok_partner7"] = req.FormValue("ok_partner7")
		SecretOption["ok_secret_key7"] = req.FormValue("ok_secret_key7")

		SecretOption["ok_partner8"] = req.FormValue("ok_partner8")
		SecretOption["ok_secret_key8"] = req.FormValue("ok_secret_key8")

		SecretOption["ok_partner9"] = req.FormValue("ok_partner9")
		SecretOption["ok_secret_key9"] = req.FormValue("ok_secret_key9")

		SecretOption["ok_partner10"] = req.FormValue("ok_partner10")
		SecretOption["ok_secret_key10"] = req.FormValue("ok_secret_key10")

		// 更新个人信息
		err := SaveSecretOption()
		if err != nil {
			fmt.Fprint(res, "写入秘钥配置数据失败")
			return
		}

		fmt.Fprint(res, "更新秘钥配置成功!")

		go email.TriggerTrender("btcrobot测试邮件,您能收到这封邮件说明您的SMTP配置成功,来自星星的机器人")

		return
	})

	m.Get("/engine", func() string {
		// show something
		err := LoadOption()
		if err != nil {
			logger.Errorln(err)
			return `{"errno": 1, "msg":", "读取引擎配置数据失败"}`
		}
		Option_json, err := json.Marshal(Option)
		if err != nil {
			logger.Errorln(err)
			return `{"errno": 1, "msg":", "解析引擎配置数据失败"}`
		}

		return string(Option_json)
	})

	m.Post("/engine", func(res http.ResponseWriter, req *http.Request) {
		if req.FormValue("enable_trading") == "on" {
			Option["enable_trading"] = "1"
		} else {
			Option["enable_trading"] = "0"
		}

		if req.FormValue("discipleMode") == "on" {
			Option["discipleMode"] = "1"
		} else {
			Option["discipleMode"] = "0"
		}

		Option["discipleValue"] = req.FormValue("discipleValue")

		// open传递过来的是“on”或没传递
		if req.FormValue("enable_email") == "on" {
			Option["enable_email"] = "1"
		} else {
			Option["enable_email"] = "0"
		}

		Option["to_email"] = req.FormValue("to_email")

		Option["tick_interval"] = req.FormValue("tick_interval")
		Option["datacenter"] = req.FormValue("datacenter")
		Option["tradecenter"] = req.FormValue("tradecenter")
		Option["symbol"] = req.FormValue("symbol")
		Option["strategy"] = req.FormValue("strategy")
		Option["shortEMA"] = req.FormValue("shortEMA")
		Option["longEMA"] = req.FormValue("longEMA")
		Option["signalPeriod"] = req.FormValue("signalPeriod")

		Option["tradeAmount"] = req.FormValue("tradeAmount")
		Option["slippage"] = req.FormValue("slippage")

		Option["totalHour"] = req.FormValue("totalHour")
		Option["buyThreshold"] = req.FormValue("buyThreshold")
		Option["sellThreshold"] = req.FormValue("sellThreshold")
		Option["MACDbuyThreshold"] = req.FormValue("MACDbuyThreshold")
		Option["MACDsellThreshold"] = req.FormValue("MACDsellThreshold")

		Option["basePrice"] = req.FormValue("basePrice")
		Option["fluctuation"] = req.FormValue("fluctuation")

		Option["stoploss"] = req.FormValue("stoploss")

		// 更新个人信息
		err := SaveOption()
		if err != nil {
			fmt.Fprint(res, "写入引擎配置数据失败")
			return
		}

		fmt.Fprint(res, "更新引擎配置成功!")
	})

	m.Get("/trade", func() string {
		// show something
		LoadTrade()

		err := LoadTrade()
		if err != nil {
			logger.Errorln(err)
			return `{"errno": 1, "msg":", "读取Trade配置数据失败"}`
		}

		Option_json, err := json.Marshal(TradeOption)
		if err != nil {
			logger.Errorln(err)
			return `{"errno": 1, "msg":", "解析引擎配置数据失败"}`
		}

		return string(Option_json)
	})

	m.Post("/trade", func(res http.ResponseWriter, req *http.Request) {
		msgtype := req.FormValue("msgtype")
		var nbuyprice, nbuytotalamount, nbuyinterval, nmaxbuyamountratio float64
		var nbuytimes int
		var nsellprice, nselltotalamount, nsellinterval, nmaxsellamountratio float64
		var nselltimes int

		var err error
		if msgtype == "dobuy" {
			TradeOption["buyprice"] = req.FormValue("buyprice")
			TradeOption["buytotalamount"] = req.FormValue("buytotalamount")
			TradeOption["buyinterval"] = req.FormValue("buyinterval")
			TradeOption["buytimes"] = req.FormValue("buytimes")
			TradeOption["maxbuyamountratio"] = req.FormValue("maxbuyamountratio")
			nbuyprice, err = strconv.ParseFloat(Option["buyprice"], 64)
			if err != nil {
				logger.Errorln("config item buyprice is not float")
				return
			}
			nbuytotalamount, err = strconv.ParseFloat(Option["buytotalamount"], 64)
			if err != nil {
				logger.Errorln("config item numbuytotalamount is not float")
				return
			}
			nbuyinterval, err = strconv.ParseFloat(Option["buyinterval"], 64)
			if err != nil {
				logger.Errorln("config item buyinterval is not float")
				return
			}
			nbuytimes, err = strconv.Atoi(Option["buytimes"])
			if err != nil {
				logger.Errorln("config item numbuytimes is not float")
				return
			}

			nmaxbuyamountratio, err = strconv.ParseFloat(Option["maxbuyamountratio"], 64)
			if err != nil {
				logger.Errorln("config item numbuytotalamount is not float")
				return
			}
		} else if msgtype == "dosell" {
			TradeOption["sellprice"] = req.FormValue("sellprice")
			TradeOption["selltotalamount"] = req.FormValue("selltotalamount")
			TradeOption["sellinterval"] = req.FormValue("sellinterval")
			TradeOption["selltimes"] = req.FormValue("selltimes")
			TradeOption["maxsellamountratio"] = req.FormValue("maxsellamountratio")

			nsellprice, err = strconv.ParseFloat(Option["sellprice"], 64)
			if err != nil {
				logger.Errorln("config item sellprice is not float")
				return
			}
			nselltotalamount, err = strconv.ParseFloat(Option["selltotalamount"], 64)
			if err != nil {
				logger.Errorln("config item selltotalamount is not float")
				return
			}
			nsellinterval, err = strconv.ParseFloat(Option["v"], 64)
			if err != nil {
				logger.Errorln("config item sellinterval is not float")
				return
			}
			nselltimes, err = strconv.Atoi(Option["selltimes"])
			if err != nil {
				logger.Errorln("config item selltimes is not float")
				return
			}

			nmaxsellamountratio, err = strconv.ParseFloat(Option["maxsellamountratio"], 64)
			if err != nil {
				logger.Errorln("config item maxsellamountratio is not float")
				return
			}

		} else {
			fmt.Fprint(res, "无效的POST请求")
		}

		// 更新个人信息
		err = SaveTrade()
		if err != nil {
			fmt.Fprint(res, "写入Trade配置数据失败")
		}

		var tradeAPI TradeAPI
		if Option["tradecenter"] == "huobi" {
			tradeAPI = huobi.NewHuobi()
		} else if Option["tradecenter"] == "okcoin" {
			tradeAPI = okcoin.NewOkcoin()
		} else {
			fmt.Fprint(res, "没有选择交易所名称")
		}

		var ret string

		var arrbuyTradePrice []float64
		splitnbuyinterval := nbuyinterval / float64(nbuytimes)
		splitbuyTradeAmount := nbuytotalamount / float64(nbuytimes)
		if splitbuyTradeAmount/nbuytotalamount > nmaxbuyamountratio {
			return
		}

		if msgtype == "dobuy" {
			for i := 0; i < nbuytimes; i++ {
				warning := "oo, 买入buy In<----限价单"

				if i < nbuytimes/2 {
					arrbuyTradePrice[i] = nbuyprice - float64(i)*splitnbuyinterval
				} else {
					arrbuyTradePrice[i] = nbuyprice + float64((i-nbuytimes/2))*splitnbuyinterval
				}

				tradePrice := fmt.Sprintf("%f", arrbuyTradePrice[i])
				strsplitTradeAmount := fmt.Sprintf("%f", splitbuyTradeAmount)

				ret = tradeAPI.Buy(tradePrice, strsplitTradeAmount)

				if ret != "0" {
					fmt.Fprint(res, "交易委托成功")
				} else {
					fmt.Fprint(res, "交易委托失败")
				}
				logger.Infoln(warning)
			}
		}

		var arrsellTradePrice []float64
		splitnsellinterval := nsellinterval / float64(nselltimes)
		splitsellTradeAmount := nselltotalamount / float64(nselltimes)
		if splitsellTradeAmount/nselltotalamount > nmaxsellamountratio {
			return
		}
		if msgtype == "dosell" {
			for i := 0; i < nselltimes; i++ {
				warning := "oo, 卖出buy In<----限价单"

				if i < nselltimes/2 {
					arrsellTradePrice[i] = nsellprice - float64(i)*splitnsellinterval
				} else {
					arrsellTradePrice[i] = nsellprice + float64((i-nselltimes/2))*splitnsellinterval
				}

				tradePrice := fmt.Sprintf("%f", arrsellTradePrice[i])
				strsplitTradeAmount := fmt.Sprintf("%f", splitbuyTradeAmount)

				ret = tradeAPI.Sell(tradePrice, strsplitTradeAmount)

				if ret != "0" {
					fmt.Fprint(res, "交易委托成功")
				} else {
					fmt.Fprint(res, "交易委托失败")
				}
				logger.Infoln(warning)
			}
		}
	})

	m.Use(auth.Basic(SecretOption["username"], SecretOption["password"]))
	m.Use(martini.Static("./static"))
	m.Use(martini.Static("../static"))

	logger.Infoln(http.ListenAndServe(Config["host"], m))

	m.Run()

	fmt.Println("[ ---------------------------------------------------------->>> ")
	fmt.Printf("start web server failed, please check if %s is already used.", Config["host"])
	fmt.Println(" <<<----------------------------------------------------------] ")
}
Exemplo n.º 5
0
func main() {
	client := &http.Client{}

	m := martini.Classic()
	m.Use(auth.Basic(config.Dashboard.User, config.Dashboard.Password))
	m.Use(render.Renderer("templates"))

	m.Get("/", func(r render.Render) {
		r.HTML(200, "index.tmpl", "")
	})

	m.Get("/all", func(w http.ResponseWriter) string {
		var wg sync.WaitGroup
		var mu sync.Mutex

		w.Header().Set("Content-Type", "application/json")

		routes := []string{"events", "checks", "clients", "stashes", "info"}
		outMap := make(map[string]interface{})

		for _, route := range routes {
			wg.Add(1)

			// route changes during loop iterations, passing it as an argument we avoid a race condition
			go (func(route string) {
				defer wg.Done()

				clientRes, err := client.Get("http://" + apiHost + "/" + route)

				if err != nil {
					fmt.Println("Error getting ", route)
					return
				}
				defer clientRes.Body.Close()

				var d interface{}
				content, readErr := ioutil.ReadAll(clientRes.Body)
				if readErr != nil {
					fmt.Println("Error reading ", route)
					return
				}
				if jsonErr := json.Unmarshal(content, &d); jsonErr != nil {
					fmt.Println("Error unmarshalling ", route, string(content), ": ", jsonErr.Error())
					return
				}

				mu.Lock()
				defer mu.Unlock()
				outMap[route] = d
			})(route)
		}
		wg.Wait()

		info, ok := outMap["info"].(map[string]interface{})
		if ok {
			dm := make(map[string]interface{})
			dm["version"] = "0.10.2go"
			dm["poll_frequency"] = 10
			info["sensu_dashboard"] = dm
		}

		out, _ := json.Marshal(&outMap)
		return string(out)
	})

	m.Get("/.*", func(w http.ResponseWriter, req *http.Request) (int, string) {
		w.Header().Set("Content-Type", "application/json")

		req.URL.Host = apiHost
		req.URL.Scheme = "http"
		req.RequestURI = ""
		clientRes, err := client.Do(req)

		if err != nil {
			if req.URL.Path == "/info" {
				return 404, "{}"
			} else {
				return 404, "[]"
			}
		}
		defer clientRes.Body.Close()

		out, _ := ioutil.ReadAll(clientRes.Body)
		return clientRes.StatusCode, string(out)
	})

	proxyOr502 := func(w http.ResponseWriter, req *http.Request) (int, string) {
		w.Header().Set("Content-Type", "application/json")

		req.URL.Host = apiHost
		req.URL.Scheme = "http"
		req.RequestURI = ""
		clientRes, err := client.Do(req)

		if err != nil {
			out, _ := json.Marshal(&map[string]string{"error": err.Error()})
			return 502, string(out)
		}
		defer clientRes.Body.Close()

		out, _ := ioutil.ReadAll(clientRes.Body)
		return clientRes.StatusCode, string(out)
	}

	m.Post("/.*", proxyOr502)

	m.Delete("/.*", proxyOr502)

	http.ListenAndServe(fmt.Sprintf(":%d", config.Dashboard.Port), m)
}
Exemplo n.º 6
0
Arquivo: wigo.go Projeto: 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)
		}
	}
}