1
0
forked from x/ContextOS

vps mod AppendJson

This commit is contained in:
shaoying 2018-04-08 00:21:30 +08:00
parent 7ddbee3fd5
commit 548cfe512e
2 changed files with 34 additions and 8 deletions

View File

@ -1,6 +1,6 @@
package tcp
import (
package tcp // {{{
// }}}
import ( // {{{
"context"
"crypto/tls"
@ -9,13 +9,15 @@ import (
"strconv"
)
// }}}
type TCP struct {
net.Conn
net.Listener
*ctx.Context
}
func (tcp *TCP) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server {
func (tcp *TCP) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{
c.Caches = map[string]*ctx.Cache{
"protocol": &ctx.Cache{Name: "网络协议(tcp/tcp4/tcp6)", Value: m.Conf("protocol"), Help: "网络协议"},
"security": &ctx.Cache{Name: "加密通信(true/false)", Value: m.Conf("security"), Help: "加密通信"},
@ -28,7 +30,8 @@ func (tcp *TCP) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
return s
}
func (tcp *TCP) Begin(m *ctx.Message, arg ...string) ctx.Server {
// }}}
func (tcp *TCP) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
tcp.Context.Master(nil)
if tcp.Context == Index {
Pulse = m
@ -36,7 +39,8 @@ func (tcp *TCP) Begin(m *ctx.Message, arg ...string) ctx.Server {
return tcp
}
func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool {
// }}}
func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool { // {{{
if len(arg) > 1 {
m.Cap("address", arg[1])
}
@ -105,7 +109,8 @@ func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool {
return true
}
func (tcp *TCP) Close(m *ctx.Message, arg ...string) bool {
// }}}
func (tcp *TCP) Close(m *ctx.Message, arg ...string) bool { // {{{
switch tcp.Context {
case m.Target():
if tcp.Listener != nil {
@ -130,6 +135,8 @@ func (tcp *TCP) Close(m *ctx.Message, arg ...string) bool {
return true
}
// }}}
var Pulse *ctx.Message
var Index = &ctx.Context{Name: "tcp", Help: "网络中心",
Caches: map[string]*ctx.Cache{
@ -166,6 +173,18 @@ var Index = &ctx.Context{Name: "tcp", Help: "网络中心",
tcp.Conn.Read(buf)
m.Echo(string(buf))
}},
"ifconfig": &ctx.Command{Name: "ifconfig", Help: "接收消息",
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
ifs, e := net.Interfaces()
m.Assert(e)
for _, v := range ifs {
ips, e := v.Addrs()
m.Assert(e)
for _, x := range ips {
m.Echo("%d %s %v %v\n", v.Index, v.Name, v.HardwareAddr, x.String())
}
}
}},
},
Index: map[string]*ctx.Context{
"void": &ctx.Context{

View File

@ -3,8 +3,10 @@ package web // {{{
import ( // {{{
"context"
"encoding/json"
"html/template"
"net/http"
"net/url"
"bufio"
"fmt"
@ -31,11 +33,15 @@ type WEB struct {
}
func (web *WEB) AppendJson(msg *ctx.Message) string { // {{{
b, e := json.Marshal(msg.Meta)
msg.Assert(e)
return string(b)
result := []string{"{"}
for i, k := range msg.Meta["append"] {
result = append(result, fmt.Sprintf("\"%s\": [", k))
for j, v := range msg.Meta[k] {
result = append(result, fmt.Sprintf("\"%s\"", v))
result = append(result, fmt.Sprintf("\"%s\"", url.QueryEscape(v)))
if j < len(msg.Meta[k])-1 {
result = append(result, ",")
}
@ -46,6 +52,7 @@ func (web *WEB) AppendJson(msg *ctx.Message) string { // {{{
}
}
result = append(result, "}")
return strings.Join(result, "")
}