Skip to content

Commit a784707

Browse files
committed
V1.5
1、增加自动同步online数据模块,目前支持写入influxdb 2、2017-05-24 18:00
1 parent 7109653 commit a784707

19 files changed

+488
-110
lines changed

app.conf

+4
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@
88
<apps>
99
<app appid="10000" appname="官网" domain="www.emoney.cn" messageapi="" authapi="" timeout="600"/>
1010
</apps>
11+
<syncnode>
12+
<!-- udp mode -->
13+
<influxdb id="monitordb" serverip="192.168.8.115:8089" username="admin" password="admin" dbname="emmonitor"></influxdb>
14+
</syncnode>
1115
</config>

config/appconfigmodel.go

+14
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type AppConfig struct {
1111
Apps []AppInfo `xml:"apps>app"`
1212
HttpServer HttpServer `xml:"httpserver"`
1313
AllowIps []string `xml:"allowips>ip"`
14+
SyncNode *SyncNode `xml:"syncnode"`
1415
}
1516

1617
//全局配置
@@ -35,3 +36,16 @@ type AppInfo struct {
3536
AuthApi string `xml:"authapi,attr"`
3637
TimeOut int64 `xml:"timeout,attr"`
3738
}
39+
40+
type SyncNode struct {
41+
InfluxdbInfo *InfluxdbInfo `xml:"influxdb"`
42+
}
43+
44+
//Influxdb信息
45+
type InfluxdbInfo struct {
46+
ID string `xml:"id,attr"`
47+
ServerIP string `xml:"serverip,attr"`
48+
UserName string `xml:"username,attr"`
49+
Password string `xml:"password,attr"`
50+
DBName string `xml:"dbname,attr"`
51+
}

exception/exception.go

+31-12
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,38 @@ package exception
22

33
import (
44
"fmt"
5-
"os"
6-
"runtime"
7-
8-
. "github.com/devfeel/longweb/const"
9-
"github.com/devfeel/longweb/framework/log"
5+
"runtime/debug"
106
)
117

8+
type Exception interface {
9+
GetErrString() string
10+
GetStackString() string
11+
GetDefaultLogString() string
12+
}
13+
14+
type exception struct {
15+
ErrString string
16+
StackString string
17+
DefaultLogString string
18+
}
19+
20+
func (e *exception) GetErrString() string {
21+
return e.ErrString
22+
}
23+
24+
func (e *exception) GetStackString() string {
25+
return e.StackString
26+
}
27+
28+
func (e *exception) GetDefaultLogString() string {
29+
return e.DefaultLogString
30+
}
31+
1232
//统一异常处理
13-
func CatchError(title string, logtarget string, err interface{}) (errmsg string) {
14-
errmsg = fmt.Sprintln(err)
15-
os.Stdout.Write([]byte(title + " error! => " + errmsg + " => "))
16-
buf := make([]byte, 4096)
17-
n := runtime.Stack(buf, true)
18-
logger.Log(title+" error! => "+errmsg+" => "+string(buf[:n]), logtarget, LogLevel_Error)
19-
return errmsg
33+
func CatchError(title string, err interface{}) Exception {
34+
ex := new(exception)
35+
ex.ErrString = fmt.Sprintln(err)
36+
ex.StackString = string(debug.Stack())
37+
ex.DefaultLogString = title + " CatchError [" + ex.GetErrString() + "] [" + ex.GetStackString() + "]"
38+
return ex
2039
}

framework/slice/slices.go

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package slices
2+
3+
import (
4+
"math/rand"
5+
"time"
6+
)
7+
8+
type reducetype func(interface{}) interface{}
9+
type filtertype func(interface{}) bool
10+
11+
func Slice_randList(min, max int) []int {
12+
if max < min {
13+
min, max = max, min
14+
}
15+
length := max - min + 1
16+
t0 := time.Now()
17+
rand.Seed(int64(t0.Nanosecond()))
18+
list := rand.Perm(length)
19+
for index, _ := range list {
20+
list[index] += min
21+
}
22+
return list
23+
}
24+
25+
func Slice_merge(slice1, slice2 []interface{}) (c []interface{}) {
26+
c = append(slice1, slice2...)
27+
return
28+
}
29+
30+
func In_slice(val interface{}, slice []interface{}) bool {
31+
for _, v := range slice {
32+
if v == val {
33+
return true
34+
}
35+
}
36+
return false
37+
}
38+
39+
func Slice_reduce(slice []interface{}, a reducetype) (dslice []interface{}) {
40+
for _, v := range slice {
41+
dslice = append(dslice, a(v))
42+
}
43+
return
44+
}
45+
46+
func Slice_rand(a []interface{}) (b interface{}) {
47+
randnum := rand.Intn(len(a))
48+
b = a[randnum]
49+
return
50+
}
51+
52+
func Slice_sum(intslice []int64) (sum int64) {
53+
for _, v := range intslice {
54+
sum += v
55+
}
56+
return
57+
}
58+
59+
func Slice_filter(slice []interface{}, a filtertype) (ftslice []interface{}) {
60+
for _, v := range slice {
61+
if a(v) {
62+
ftslice = append(ftslice, v)
63+
}
64+
}
65+
return
66+
}
67+
68+
func Slice_diff(slice1, slice2 []interface{}) (diffslice []interface{}) {
69+
for _, v := range slice1 {
70+
if !In_slice(v, slice2) {
71+
diffslice = append(diffslice, v)
72+
}
73+
}
74+
return
75+
}
76+
77+
func Slice_intersect(slice1, slice2 []interface{}) (diffslice []interface{}) {
78+
for _, v := range slice1 {
79+
if !In_slice(v, slice2) {
80+
diffslice = append(diffslice, v)
81+
}
82+
}
83+
return
84+
}
85+
86+
func Slice_chunk(slice []interface{}, size int) (chunkslice [][]interface{}) {
87+
if size >= len(slice) {
88+
chunkslice = append(chunkslice, slice)
89+
return
90+
}
91+
end := size
92+
for i := 0; i <= (len(slice) - size); i += size {
93+
chunkslice = append(chunkslice, slice[i:end])
94+
end += size
95+
}
96+
return
97+
}
98+
99+
func Slice_range(start, end, step int64) (intslice []int64) {
100+
for i := start; i <= end; i += step {
101+
intslice = append(intslice, i)
102+
}
103+
return
104+
}
105+
106+
func Slice_pad(slice []interface{}, size int, val interface{}) []interface{} {
107+
if size <= len(slice) {
108+
return slice
109+
}
110+
for i := 0; i < (size - len(slice)); i++ {
111+
slice = append(slice, val)
112+
}
113+
return slice
114+
}
115+
116+
func Slice_unique(slice []interface{}) (uniqueslice []interface{}) {
117+
for _, v := range slice {
118+
if !In_slice(v, uniqueslice) {
119+
uniqueslice = append(uniqueslice, v)
120+
}
121+
}
122+
return
123+
}
124+
125+
func Slice_shuffle(slice []interface{}) []interface{} {
126+
for i := 0; i < len(slice); i++ {
127+
a := rand.Intn(len(slice))
128+
b := rand.Intn(len(slice))
129+
slice[a], slice[b] = slice[b], slice[a]
130+
}
131+
return slice
132+
}

httpserver/handlers/apihandler.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
//-2:message is empty
1414
//-10001:message format error
1515
//-10002:this appid no have permission
16-
func SendMessage(ctx *dotweb.HttpContext) {
16+
func SendMessage(ctx dotweb.Context) error {
1717
type retJson struct {
1818
RetCode int
1919
RetMsg string
@@ -30,13 +30,14 @@ func SendMessage(ctx *dotweb.HttpContext) {
3030

3131
//push message
3232
if result.RetCode == 0 {
33-
message := string(ctx.PostBody())
33+
message := string(ctx.Request().PostBody())
3434
if message != "" {
3535
result.RetCode, result.RetMsg = PushMessage(message)
3636
} else {
3737
result.RetCode = -2
3838
result.RetMsg = "message is empty"
3939
}
4040
}
41-
ctx.WriteJson(result)
41+
_, err := ctx.WriteJson(result)
42+
return err
4243
}

httpserver/handlers/defaulthandler.go

+17-12
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,40 @@ import (
1010
"runtime"
1111
)
1212

13-
func Index(ctx *dotweb.HttpContext) {
14-
ctx.Response.Header().Set("Content-Type", "text/html; charset=utf-8")
13+
func Index(ctx dotweb.Context) error {
14+
ctx.Response().Header().Set("Content-Type", "text/html; charset=utf-8")
1515

1616
//ctx.WriteString("welcome to websocket proxy<br>" + ctx.Request.RequestURI + "<br>" + fmt.Sprintln(ctx.Request))
1717
ctx.WriteString("welcome to websocket proxy | version=" + constdefine.Version)
18+
return nil
1819
}
1920

20-
func Memstate(ctx *dotweb.HttpContext) {
21+
func Memstate(ctx dotweb.Context) error {
2122
stats := &runtime.MemStats{}
2223
runtime.ReadMemStats(stats)
2324
ctx.WriteString(fmt.Sprint(stats))
25+
return nil
2426
}
2527

26-
func Test(ctx *dotweb.HttpContext) {
28+
func Test(ctx dotweb.Context) error {
2729
filePath := fileutil.GetCurrentDirectory()
2830
filePath = filePath + "/test.html"
2931
tmpl, err := template.New("test.html").ParseFiles(filePath)
3032
if err != nil {
3133
ctx.WriteString("version template Parse error => " + err.Error())
32-
return
34+
return err
3335
}
3436
viewdata := make(map[string]string)
35-
ctx.Response.Header().Set("Content-Type", "text/html; charset=utf8")
36-
err = tmpl.Execute(ctx.Response.Writer(), viewdata)
37+
ctx.Response().Header().Set("Content-Type", "text/html; charset=utf8")
38+
err = tmpl.Execute(ctx.Response().Writer(), viewdata)
3739
if err != nil {
3840
ctx.WriteString("version template Execute error => " + err.Error())
39-
return
41+
return err
4042
}
43+
return nil
4144
}
4245

43-
func TestAuth(ctx *dotweb.HttpContext) {
46+
func TestAuth(ctx dotweb.Context) error {
4447
appId := ctx.QueryString("appid")
4548
groupId := ctx.QueryString("groupid")
4649
userId := ctx.QueryString("userid")
@@ -62,9 +65,11 @@ func TestAuth(ctx *dotweb.HttpContext) {
6265
res.UserID = userId
6366

6467
body := jsonutil.GetJsonString(res)
65-
ctx.WriteString(body)
68+
_, err := ctx.WriteString(body)
69+
return err
6670
}
6771

68-
func TestMessage(ctx *dotweb.HttpContext) {
69-
ctx.WriteString("")
72+
func TestMessage(ctx dotweb.Context) error {
73+
_, err := ctx.WriteString("")
74+
return err
7075
}

0 commit comments

Comments
 (0)