Skip to content

Commit

Permalink
重写example
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Oct 14, 2023
1 parent 470a6c4 commit a98cc67
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
5 changes: 3 additions & 2 deletions example/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ mode="test"
host="0.0.0.0:8080"

[api]
haobase_host = "http://127.0.0.1:8082"
haoquote_host = "http://127.0.0.1:8081"
haobase_host = "//127.0.0.1:8082"
haoquote_host = "//127.0.0.1:8081"
haoquote_ws_host = "ws//127.0.0.1:8081"
16 changes: 12 additions & 4 deletions example/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,14 @@

var symbol = "{{ .symbol }}";
var latest_price = "";
var PORT = parseInt(window.location.port) + 1;
var APIHOST = "//"+window.location.hostname+":"+PORT;
var WSHOST = "ws://"+window.location.hostname+":"+PORT;



var API_HAOBASE_HOST = {{ .haobase_host | unsafe }};
var API_HAOQUOTE_HOST = {{ .haoquote_host | unsafe }};
var WSHOST = {{ .ws_host | unsafe}};


if(symbol=="usdjpy"){
kchart.setPriceVolumePrecision(3, 2);
}else if(symbol=="eurusd"){
Expand Down Expand Up @@ -322,10 +327,13 @@
var mtype = $("input[name='mtype']:checked").val();

$.ajax({
url: "/api/new_order",
url: "/api/v1/base/order/create",
type: "post",
dataType: "json",
contentType: "application/json",
beforeSend: function(r) {
r.setRequestHeader("User-Id", Cookies.get("user_id"));
},
data: function () {
var data = {
symbol: symbol,
Expand Down
30 changes: 11 additions & 19 deletions example/example.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"log"
"html/template"
"net/http"
"os"
"strings"
Expand All @@ -12,7 +12,6 @@ import (
"github.com/spf13/viper"
"github.com/urfave/cli/v2"
"github.com/yzimhao/trading_engine/utils/app"
"github.com/zehuamama/balancer/proxy"

_ "net/http/pprof"

Expand Down Expand Up @@ -85,25 +84,15 @@ func main() {

func startWeb(host string) {
web = gin.New()
web.SetFuncMap(template.FuncMap{
"unsafe": func(str string) template.HTML {
return template.HTML(str)
},
})

web.LoadHTMLGlob("./*.html")
web.StaticFS("/statics", http.Dir("./statics"))

//代理交易系统的后端接口,实际应用中可以用nginx直接代理
web.Any("/api/v1/base/*any", func(ctx *gin.Context) {
httpProxy, err := proxy.NewHTTPProxy([]string{viper.GetString("api.haobase_host")}, "round-robin")
if err != nil {
log.Fatalf("create proxy error: %s", err)
}
httpProxy.ServeHTTP(ctx.Writer, ctx.Request)
})
web.Any("/api/v1/quote/*any", func(ctx *gin.Context) {
httpProxy, err := proxy.NewHTTPProxy([]string{viper.GetString("api.haoquote_host")}, "round-robin")
if err != nil {
log.Fatalf("create proxy error: %s", err)
}
httpProxy.ServeHTTP(ctx.Writer, ctx.Request)
})

web.GET("/:symbol", func(c *gin.Context) {
support := []string{"usdjpy", "eurusd"}
symbol := strings.ToLower(c.Param("symbol"))
Expand All @@ -114,7 +103,10 @@ func startWeb(host string) {
}

c.HTML(200, "demo.html", gin.H{
"symbol": symbol,
"haobase_host": viper.GetString("api.haobase_host"),
"haoquote_host": viper.GetString("api.haoquote_host"),
"ws_host": viper.GetString("api.haoquote_ws_host"),
"symbol": symbol,
})
})

Expand Down

0 comments on commit a98cc67

Please sign in to comment.