mirror of
https://shylinux.com/x/ContextOS
synced 2025-06-26 18:07:30 +08:00
mac mod 0.2.0 维护mdb tcp nfs web四个模块
This commit is contained in:
parent
7fffcde987
commit
d3012e41b9
175
src/context/mdb/mdb.go
Normal file
175
src/context/mdb/mdb.go
Normal file
@ -0,0 +1,175 @@
|
|||||||
|
package mdb // {{{
|
||||||
|
// }}}
|
||||||
|
import ( // {{{
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"database/sql"
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
type MDB struct {
|
||||||
|
*sql.DB
|
||||||
|
*ctx.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mdb *MDB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{
|
||||||
|
c.Caches = map[string]*ctx.Cache{
|
||||||
|
"source": &ctx.Cache{Name: "数据库参数", Value: "", Help: "数据库参数"},
|
||||||
|
"driver": &ctx.Cache{Name: "数据库驱动", Value: "", Help: "数据库驱动"},
|
||||||
|
}
|
||||||
|
c.Configs = map[string]*ctx.Config{}
|
||||||
|
|
||||||
|
s := new(MDB)
|
||||||
|
s.Context = c
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
func (mdb *MDB) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
||||||
|
return mdb
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
func (mdb *MDB) Start(m *ctx.Message, arg ...string) bool { // {{{
|
||||||
|
if len(arg) > 0 {
|
||||||
|
m.Cap("source", arg[0])
|
||||||
|
}
|
||||||
|
if len(arg) > 1 {
|
||||||
|
m.Cap("driver", arg[1])
|
||||||
|
} else {
|
||||||
|
m.Cap("driver", m.Conf("driver"))
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.Cap("source") == "" || m.Cap("driver") == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
m.Cap("stream", m.Cap("source"))
|
||||||
|
db, e := sql.Open(m.Cap("driver"), m.Cap("source"))
|
||||||
|
m.Assert(e)
|
||||||
|
mdb.DB = db
|
||||||
|
|
||||||
|
m.Log("info", nil, "%d open %s %s", m.Capi("nsource", 1), m.Cap("driver"), m.Cap("source"))
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
func (mdb *MDB) Close(m *ctx.Message, arg ...string) bool { // {{{
|
||||||
|
if mdb.Context == Index {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
switch mdb.Context {
|
||||||
|
case m.Target:
|
||||||
|
case m.Source:
|
||||||
|
}
|
||||||
|
|
||||||
|
if mdb.DB != nil {
|
||||||
|
m.Log("info", nil, "%d close %s %s", m.Capi("nsource", -1)+1, m.Cap("driver"), m.Cap("source"))
|
||||||
|
mdb.DB.Close()
|
||||||
|
mdb.DB = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
|
||||||
|
Caches: map[string]*ctx.Cache{
|
||||||
|
"nsource": &ctx.Cache{Name: "数据源数量", Value: "0", Help: "已打开数据库的数量"},
|
||||||
|
},
|
||||||
|
Configs: map[string]*ctx.Config{
|
||||||
|
"driver": &ctx.Config{Name: "数据库驱动(mysql)", Value: "mysql", Help: "数据库驱动"},
|
||||||
|
},
|
||||||
|
Commands: map[string]*ctx.Command{
|
||||||
|
"open": &ctx.Command{Name: "open name help source [driver]", Help: "打开数据库", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
||||||
|
m.Assert(len(arg) > 2, "缺少参数") // {{{
|
||||||
|
m.Master, m.Target = c, c
|
||||||
|
m.Cap("stream", m.Cap("nsource"))
|
||||||
|
m.Start(arg[0], "数据存储", arg[2:]...)
|
||||||
|
return ""
|
||||||
|
// }}}
|
||||||
|
}},
|
||||||
|
"exec": &ctx.Command{Name: "exec sql [arg]", Help: "操作数据库",
|
||||||
|
Appends: map[string]string{"LastInsertId": "最后插入元组的标识", "RowsAffected": "修改元组的数量"},
|
||||||
|
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
||||||
|
mdb, ok := m.Target.Server.(*MDB) // {{{
|
||||||
|
m.Assert(ok, "目标模块类型错误")
|
||||||
|
m.Assert(len(arg) > 0, "缺少参数")
|
||||||
|
m.Assert(mdb.DB != nil, "数据库未打开")
|
||||||
|
|
||||||
|
which := make([]interface{}, 0, len(arg))
|
||||||
|
for _, v := range arg[1:] {
|
||||||
|
which = append(which, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
ret, e := mdb.Exec(arg[0], which...)
|
||||||
|
m.Assert(e)
|
||||||
|
id, e := ret.LastInsertId()
|
||||||
|
m.Assert(e)
|
||||||
|
n, e := ret.RowsAffected()
|
||||||
|
m.Assert(e)
|
||||||
|
|
||||||
|
m.Echo("%d", id).Echo("%d", n)
|
||||||
|
m.Add("append", "LastInsertId", fmt.Sprintf("%d", id))
|
||||||
|
m.Add("append", "RowsAffected", fmt.Sprintf("%d", n))
|
||||||
|
m.Log("info", nil, "last(%d) rows(%d)", id, n)
|
||||||
|
return ""
|
||||||
|
// }}}
|
||||||
|
}},
|
||||||
|
"query": &ctx.Command{Name: "query sql [arg]", Help: "执行查询语句", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
||||||
|
mdb, ok := m.Target.Server.(*MDB) // {{{
|
||||||
|
m.Assert(ok, "目标模块类型错误")
|
||||||
|
m.Assert(len(arg) > 0, "缺少参数")
|
||||||
|
m.Assert(mdb.DB != nil, "数据库未打开")
|
||||||
|
|
||||||
|
which := make([]interface{}, 0, len(arg))
|
||||||
|
for _, v := range arg[1:] {
|
||||||
|
which = append(which, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, e := mdb.Query(arg[0], which...)
|
||||||
|
m.Assert(e)
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
cols, e := rows.Columns()
|
||||||
|
m.Assert(e)
|
||||||
|
num := len(cols)
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
vals := make([]interface{}, num)
|
||||||
|
ptrs := make([]interface{}, num)
|
||||||
|
for i := range vals {
|
||||||
|
ptrs[i] = &vals[i]
|
||||||
|
}
|
||||||
|
rows.Scan(ptrs...)
|
||||||
|
|
||||||
|
for i, k := range cols {
|
||||||
|
switch b := vals[i].(type) {
|
||||||
|
case []byte:
|
||||||
|
m.Add("append", k, string(b))
|
||||||
|
case int64:
|
||||||
|
m.Add("append", k, fmt.Sprintf("%d", b))
|
||||||
|
default:
|
||||||
|
m.Add("append", k, "")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m.Log("info", nil, "rows(%d) cols(%d)", len(m.Meta[m.Meta["append"][0]]), len(m.Meta["append"]))
|
||||||
|
return ""
|
||||||
|
// }}}
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
mdb := &MDB{}
|
||||||
|
mdb.Context = Index
|
||||||
|
ctx.Index.Register(Index, mdb)
|
||||||
|
}
|
61
src/context/mdb/mdb_test.go
Normal file
61
src/context/mdb/mdb_test.go
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
package mdb
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"flag"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestOpen(t *testing.T) {
|
||||||
|
flag.Parse()
|
||||||
|
args := flag.Args()
|
||||||
|
if len(args) < 2 {
|
||||||
|
t.Fatal("usages: -args source driver [table]")
|
||||||
|
}
|
||||||
|
|
||||||
|
source := "user:word@/book"
|
||||||
|
driver := "mysql"
|
||||||
|
source = args[0]
|
||||||
|
driver = args[1]
|
||||||
|
|
||||||
|
//mysql -u root -p;
|
||||||
|
//create database book;
|
||||||
|
//grant all on book.* to user identified by 'word'
|
||||||
|
|
||||||
|
ctx.Start()
|
||||||
|
ctx.Index.Conf("debug", "off")
|
||||||
|
log.SetOutput(os.Stdout)
|
||||||
|
m := ctx.Pulse.Spawn(Index)
|
||||||
|
|
||||||
|
m.Meta = nil
|
||||||
|
m.Cmd("open", source, driver)
|
||||||
|
|
||||||
|
m.Meta = nil
|
||||||
|
m.Cmd("exec", "insert into program(time, hash, name) values(?, ?, ?)", "1", "2", "3")
|
||||||
|
|
||||||
|
m.Meta = nil
|
||||||
|
m.Cmd("exec", "insert into program(time, hash, name) values(?, ?, ?)", "1", "2", "3")
|
||||||
|
|
||||||
|
m.Meta = nil
|
||||||
|
m.Cmd("exec", "insert into program(time, hash, name) values(?, ?, ?)", "2", "3", "4")
|
||||||
|
|
||||||
|
m.Meta = nil
|
||||||
|
m.Cmd("query", "select time, hash, name from program")
|
||||||
|
|
||||||
|
t.Log()
|
||||||
|
for i, rows := 0, len(m.Meta[m.Meta["append"][0]]); i < rows; i++ {
|
||||||
|
for _, k := range m.Meta["append"] {
|
||||||
|
t.Log(k, m.Meta[k][i])
|
||||||
|
}
|
||||||
|
t.Log()
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(m.Meta["append"]) != 3 || len(m.Meta[m.Meta["append"][0]]) != 2 {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
m.Meta = nil
|
||||||
|
// Index.Exit(m)
|
||||||
|
}
|
42
src/context/nfs/nfs.go
Normal file
42
src/context/nfs/nfs.go
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package nfs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
)
|
||||||
|
|
||||||
|
type NFS struct {
|
||||||
|
*ctx.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
func (nfs *NFS) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server {
|
||||||
|
c.Caches = map[string]*ctx.Cache{}
|
||||||
|
c.Configs = map[string]*ctx.Config{}
|
||||||
|
|
||||||
|
s := new(NFS)
|
||||||
|
s.Context = c
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (nfs *NFS) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
||||||
|
return nfs
|
||||||
|
}
|
||||||
|
|
||||||
|
func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (nfs *NFS) Exit(m *ctx.Message, arg ...string) bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
|
||||||
|
Caches: map[string]*ctx.Cache{},
|
||||||
|
Configs: map[string]*ctx.Config{},
|
||||||
|
Commands: map[string]*ctx.Command{},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
nfs := &NFS{}
|
||||||
|
nfs.Context = Index
|
||||||
|
ctx.Index.Register(Index, nfs)
|
||||||
|
}
|
207
src/context/tcp/tcp.go
Normal file
207
src/context/tcp/tcp.go
Normal file
@ -0,0 +1,207 @@
|
|||||||
|
package tcp // {{{
|
||||||
|
// }}}
|
||||||
|
import ( // {{{
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
type TCP struct {
|
||||||
|
net.Conn
|
||||||
|
net.Listener
|
||||||
|
*ctx.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tcp *TCP) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{
|
||||||
|
c.Caches = map[string]*ctx.Cache{
|
||||||
|
"protocol": &ctx.Cache{Name: "protocol(tcp/tcp4/tcp6)", Value: m.Conf("protocol"), Help: "监听地址"},
|
||||||
|
"security": &ctx.Cache{Name: "security(true/false)", Value: m.Conf("security"), Help: "加密通信"},
|
||||||
|
"address": &ctx.Cache{Name: "address", Value: "", Help: "监听地址"},
|
||||||
|
}
|
||||||
|
c.Configs = map[string]*ctx.Config{}
|
||||||
|
|
||||||
|
s := new(TCP)
|
||||||
|
s.Context = c
|
||||||
|
return s
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
func (tcp *TCP) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
||||||
|
if m.Target == Index {
|
||||||
|
Pulse = m
|
||||||
|
}
|
||||||
|
|
||||||
|
return tcp
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool { // {{{
|
||||||
|
if len(arg) > 1 {
|
||||||
|
m.Cap("address", arg[1])
|
||||||
|
}
|
||||||
|
if len(arg) > 2 {
|
||||||
|
m.Cap("security", arg[2])
|
||||||
|
}
|
||||||
|
|
||||||
|
switch arg[0] {
|
||||||
|
case "dial":
|
||||||
|
c, e := net.Dial(m.Cap("protocol"), m.Cap("address"))
|
||||||
|
m.Assert(e)
|
||||||
|
tcp.Conn = c
|
||||||
|
m.Log("info", nil, "dial(%d) %v->%v", m.Capi("nclient", 1), tcp.LocalAddr(), tcp.RemoteAddr())
|
||||||
|
m.Cap("stream", fmt.Sprintf("%s->%s", tcp.LocalAddr(), tcp.RemoteAddr()))
|
||||||
|
|
||||||
|
// m.Reply(c.LocalAddr().String()).Put("option", "io", c).Cmd("open")
|
||||||
|
return false
|
||||||
|
case "accept":
|
||||||
|
c, e := m.Data["io"].(net.Conn)
|
||||||
|
m.Assert(e)
|
||||||
|
tcp.Conn = c
|
||||||
|
m.Log("info", nil, "accept(%d) %v<-%v", m.Capi("nclient", 1), tcp.LocalAddr(), tcp.RemoteAddr())
|
||||||
|
m.Cap("stream", fmt.Sprintf("%s<-%s", tcp.LocalAddr(), tcp.RemoteAddr()))
|
||||||
|
|
||||||
|
s, e := m.Data["source"].(*ctx.Context)
|
||||||
|
m.Assert(e)
|
||||||
|
msg := m.Spawn(s).Put("option", "io", c)
|
||||||
|
msg.Cmd("open")
|
||||||
|
msg.Cap("stream", tcp.RemoteAddr().String())
|
||||||
|
|
||||||
|
if tcp.Sessions == nil {
|
||||||
|
tcp.Sessions = make(map[string]*ctx.Message)
|
||||||
|
}
|
||||||
|
tcp.Sessions["open"] = msg
|
||||||
|
msg.Name = "open"
|
||||||
|
|
||||||
|
// m.Reply(c.RemoteAddr().String())
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
l, e := net.Listen(m.Cap("protocol"), m.Cap("address"))
|
||||||
|
m.Assert(e)
|
||||||
|
tcp.Listener = l
|
||||||
|
m.Log("info", nil, "listen(%d) %v", m.Capi("nlisten", 1), l.Addr())
|
||||||
|
m.Cap("stream", fmt.Sprintf("%s", l.Addr()))
|
||||||
|
|
||||||
|
for {
|
||||||
|
c, e := l.Accept()
|
||||||
|
m.Assert(e)
|
||||||
|
m.Spawn(Index).Put("option", "io", c).Put("option", "source", m.Source).Start(fmt.Sprintf("com%d", m.Capi("nclient", 1)), "网络连接", "accept", c.RemoteAddr().String())
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
func (tcp *TCP) Close(m *ctx.Message, arg ...string) bool { // {{{
|
||||||
|
if tcp.Context == Index {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
switch tcp.Context {
|
||||||
|
case m.Target:
|
||||||
|
if tcp.Listener != nil {
|
||||||
|
m.Log("info", nil, "%d close %v", Pulse.Capi("nlisten", -1)+1, tcp.Listener.Addr())
|
||||||
|
tcp.Listener.Close()
|
||||||
|
tcp.Listener = nil
|
||||||
|
}
|
||||||
|
case m.Source:
|
||||||
|
if tcp.Conn != nil {
|
||||||
|
msg := m.Spawn(tcp.Context)
|
||||||
|
msg.Master = tcp.Context
|
||||||
|
if !tcp.Context.Close(msg, arg...) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if tcp.Conn != nil {
|
||||||
|
m.Log("info", nil, "%d close %v", Pulse.Capi("nclient", -1)+1, tcp.Conn.LocalAddr())
|
||||||
|
tcp.Conn.Close()
|
||||||
|
tcp.Conn = nil
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
var Index = &ctx.Context{Name: "tcp", Help: "网络中心",
|
||||||
|
Caches: map[string]*ctx.Cache{
|
||||||
|
"nlisten": &ctx.Cache{Name: "nlisten", Value: "0", Help: "监听数量"},
|
||||||
|
"nclient": &ctx.Cache{Name: "nclient", Value: "0", Help: "连接数量"},
|
||||||
|
},
|
||||||
|
Configs: map[string]*ctx.Config{
|
||||||
|
"protocol": &ctx.Config{Name: "protocol(tcp/tcp4/tcp6)", Value: "tcp4", Help: "连接协议"},
|
||||||
|
"security": &ctx.Config{Name: "security(true/false)", Value: "false", Help: "加密通信"},
|
||||||
|
},
|
||||||
|
Commands: map[string]*ctx.Command{
|
||||||
|
"listen": &ctx.Command{Name: "listen [address [security]]", Help: "监听连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
||||||
|
switch len(arg) { // {{{
|
||||||
|
case 0:
|
||||||
|
m.Travel(nil, func(m *ctx.Message) bool {
|
||||||
|
if tcp, ok := m.Target.Server.(*TCP); ok && tcp.Listener != nil {
|
||||||
|
m.Echo("%s %v\n", m.Target.Name, tcp.Addr())
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
default:
|
||||||
|
m.Start(fmt.Sprintf("pub%d", m.Capi("nlisten")+1), "网络监听", m.Meta["detail"]...)
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
// }}}
|
||||||
|
}},
|
||||||
|
"dial": &ctx.Command{Name: "dial [address [security]]", Help: "建立连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
||||||
|
switch len(arg) { // {{{
|
||||||
|
case 0:
|
||||||
|
m.Travel(nil, func(m *ctx.Message) bool {
|
||||||
|
if tcp, ok := m.Target.Server.(*TCP); ok && tcp.Conn != nil {
|
||||||
|
m.Echo("%s %v<->%v\n", m.Target.Name, tcp.LocalAddr(), tcp.RemoteAddr())
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
default:
|
||||||
|
m.Start(fmt.Sprintf("com%d", m.Capi("nclient")+1), "网络连接", m.Meta["detail"]...)
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
// }}}
|
||||||
|
}},
|
||||||
|
"send": &ctx.Command{Name: "send message", Help: "发送消息", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
||||||
|
if tcp, ok := m.Target.Server.(*TCP); ok && tcp.Conn != nil { // {{{
|
||||||
|
tcp.Conn.Write([]byte(arg[0]))
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
// }}}
|
||||||
|
}},
|
||||||
|
"recv": &ctx.Command{Name: "recv size", Help: "接收消息", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
||||||
|
size, e := strconv.Atoi(arg[0])
|
||||||
|
m.Assert(e)
|
||||||
|
if tcp, ok := m.Target.Server.(*TCP); ok && tcp.Conn != nil { // {{{
|
||||||
|
buf := make([]byte, size)
|
||||||
|
tcp.Conn.Read(buf)
|
||||||
|
return string(buf)
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
// }}}
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
Index: map[string]*ctx.Context{
|
||||||
|
"void": &ctx.Context{
|
||||||
|
Commands: map[string]*ctx.Command{
|
||||||
|
"listen": &ctx.Command{},
|
||||||
|
"dial": &ctx.Command{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var Pulse *ctx.Message
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
tcp := &TCP{}
|
||||||
|
tcp.Context = Index
|
||||||
|
ctx.Index.Register(Index, tcp)
|
||||||
|
}
|
29
src/context/tcp/tcp_test.go
Normal file
29
src/context/tcp/tcp_test.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package tcp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"flag"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestOpen(t *testing.T) {
|
||||||
|
flag.Parse()
|
||||||
|
args := flag.Args()
|
||||||
|
if len(args) < 1 {
|
||||||
|
t.Fatal("usages: -args address")
|
||||||
|
}
|
||||||
|
|
||||||
|
address := ":9393"
|
||||||
|
address = args[0]
|
||||||
|
|
||||||
|
//mysql -u root -p;
|
||||||
|
//create database book;
|
||||||
|
//grant all on book.* to user identified by 'word'
|
||||||
|
|
||||||
|
ctx.Start()
|
||||||
|
m := ctx.Pulse.Spawn(Index)
|
||||||
|
|
||||||
|
m.Meta = nil
|
||||||
|
Index.Cmd(m, "listen", address)
|
||||||
|
}
|
278
src/context/web/web.go
Normal file
278
src/context/web/web.go
Normal file
@ -0,0 +1,278 @@
|
|||||||
|
package web // {{{
|
||||||
|
// }}}
|
||||||
|
import ( // {{{
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"html/template"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"bufio"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
type MUX interface {
|
||||||
|
Handle(string, http.Handler)
|
||||||
|
HandleFunc(string, func(http.ResponseWriter, *http.Request))
|
||||||
|
Trans(*ctx.Message, string, func(*ctx.Message, *ctx.Context, string, ...string) string)
|
||||||
|
}
|
||||||
|
|
||||||
|
type WEB struct {
|
||||||
|
*http.ServeMux
|
||||||
|
*http.Server
|
||||||
|
|
||||||
|
*ctx.Message
|
||||||
|
*ctx.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
func (web *WEB) Trans(m *ctx.Message, key string, hand func(*ctx.Message, *ctx.Context, string, ...string) string) { // {{{
|
||||||
|
web.HandleFunc(key, func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
msg := m.Spawn(m.Target)
|
||||||
|
msg.Set("detail", key)
|
||||||
|
for k, v := range r.Form {
|
||||||
|
msg.Add("option", k)
|
||||||
|
msg.Meta[k] = v
|
||||||
|
}
|
||||||
|
for _, v := range r.Cookies() {
|
||||||
|
msg.Add("option", v.Name, v.Value)
|
||||||
|
}
|
||||||
|
|
||||||
|
msg.Log("cmd", nil, "%s %v", key, msg.Meta["option"])
|
||||||
|
msg.Put("option", "request", r)
|
||||||
|
msg.Put("option", "response", w)
|
||||||
|
|
||||||
|
ret := hand(msg, msg.Target, key)
|
||||||
|
if ret != "" {
|
||||||
|
msg.Echo(ret)
|
||||||
|
}
|
||||||
|
|
||||||
|
header := w.Header()
|
||||||
|
for _, k := range msg.Meta["append"] {
|
||||||
|
ce := &http.Cookie{Name: k, Value: msg.Get(k)}
|
||||||
|
header.Add("Set-Cookie", ce.String())
|
||||||
|
}
|
||||||
|
for _, v := range msg.Meta["result"] {
|
||||||
|
w.Write([]byte(v))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{
|
||||||
|
if web.Message != nil {
|
||||||
|
log.Println()
|
||||||
|
web.Log("cmd", nil, "%v %s %s", r.RemoteAddr, r.Method, r.URL)
|
||||||
|
defer log.Println()
|
||||||
|
|
||||||
|
if web.Cap("logheaders") == "yes" {
|
||||||
|
for k, v := range r.Header {
|
||||||
|
log.Println(k+":", v[0])
|
||||||
|
}
|
||||||
|
log.Println()
|
||||||
|
}
|
||||||
|
|
||||||
|
r.ParseForm()
|
||||||
|
if len(r.PostForm) > 0 {
|
||||||
|
for k, v := range r.PostForm {
|
||||||
|
log.Printf("%s: %s", k, v[0])
|
||||||
|
}
|
||||||
|
log.Println()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
web.ServeMux.ServeHTTP(w, r)
|
||||||
|
|
||||||
|
if web.Message != nil {
|
||||||
|
if web.Cap("logheaders") == "yes" {
|
||||||
|
for k, v := range w.Header() {
|
||||||
|
log.Println(k+":", v[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
func (web *WEB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{
|
||||||
|
c.Caches = map[string]*ctx.Cache{
|
||||||
|
"directory": &ctx.Cache{Name: "directory", Value: "usr", Help: "服务目录"},
|
||||||
|
}
|
||||||
|
c.Configs = map[string]*ctx.Config{}
|
||||||
|
|
||||||
|
s := new(WEB)
|
||||||
|
s.Context = c
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
func (web *WEB) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
||||||
|
if len(arg) > 0 {
|
||||||
|
m.Cap("directory", arg[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
web.Caches["route"] = &ctx.Cache{Name: "route", Value: "/" + web.Context.Name + "/", Help: "请求路径"}
|
||||||
|
web.Caches["register"] = &ctx.Cache{Name: "已初始化(yes/no)", Value: "no", Help: "模块是否已注册"}
|
||||||
|
web.Caches["master"] = &ctx.Cache{Name: "master(yes/no)", Value: "no", Help: "日志输出请求头"}
|
||||||
|
|
||||||
|
web.ServeMux = http.NewServeMux()
|
||||||
|
if mux, ok := m.Target.Server.(MUX); ok {
|
||||||
|
for k, x := range web.Commands {
|
||||||
|
if k[0] == '/' {
|
||||||
|
mux.Trans(m, k, x.Hand)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return web
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
func (web *WEB) Start(m *ctx.Message, arg ...string) bool { // {{{
|
||||||
|
if len(arg) > 0 {
|
||||||
|
m.Cap("directory", arg[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
web.Message = m
|
||||||
|
m.Cap("master", "yes")
|
||||||
|
|
||||||
|
m.Travel(m.Target, func(m *ctx.Message) bool {
|
||||||
|
if h, ok := m.Target.Server.(http.Handler); ok && m.Cap("register") == "no" {
|
||||||
|
m.Cap("register", "yes")
|
||||||
|
|
||||||
|
p, i := m.Target, 0
|
||||||
|
m.BackTrace(func(m *ctx.Message) bool {
|
||||||
|
p = m.Target
|
||||||
|
if i++; i == 2 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
|
if s, ok := p.Server.(MUX); ok {
|
||||||
|
m.Log("info", p, "route %s -> %s", m.Cap("route"), m.Target.Name)
|
||||||
|
s.Handle(m.Cap("route"), http.StripPrefix(path.Dir(m.Cap("route")), h))
|
||||||
|
}
|
||||||
|
|
||||||
|
if s, ok := m.Target.Server.(MUX); ok && m.Cap("directory") != "" {
|
||||||
|
m.Log("info", nil, "dir / -> [%s]", m.Cap("directory"))
|
||||||
|
s.Handle("/", http.FileServer(http.Dir(m.Cap("directory"))))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
|
web.Caches["address"] = &ctx.Cache{Name: "address", Value: ":9191", Help: "监听地址"}
|
||||||
|
web.Caches["protocol"] = &ctx.Cache{Name: "protocol", Value: "http", Help: "服务协议"}
|
||||||
|
if len(arg) > 1 {
|
||||||
|
m.Cap("address", arg[1])
|
||||||
|
}
|
||||||
|
if len(arg) > 2 {
|
||||||
|
m.Cap("protocol", arg[2])
|
||||||
|
}
|
||||||
|
|
||||||
|
m.Cap("stream", m.Cap("address"))
|
||||||
|
m.Log("info", nil, "address [%s]", m.Cap("address"))
|
||||||
|
m.Log("info", nil, "protocol [%s]", m.Cap("protocol"))
|
||||||
|
web.Server = &http.Server{Addr: m.Cap("address"), Handler: web}
|
||||||
|
|
||||||
|
web.Caches["logheaders"] = &ctx.Cache{Name: "日志输出报文头(yes/no)", Value: "yes", Help: "日志输出请求头"}
|
||||||
|
|
||||||
|
if m.Cap("protocol") == "https" {
|
||||||
|
m.Log("info", nil, "key [%s]", m.Cap("key"))
|
||||||
|
m.Log("info", nil, "cert [%s]", m.Cap("cert"))
|
||||||
|
web.Server.ListenAndServeTLS(m.Cap("cert"), m.Cap("key"))
|
||||||
|
} else {
|
||||||
|
web.Server.ListenAndServe()
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
func (web *WEB) Close(m *ctx.Message, arg ...string) bool { // {{{
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
var Index = &ctx.Context{Name: "web", Help: "应用中心",
|
||||||
|
Caches: map[string]*ctx.Cache{
|
||||||
|
"directory": &ctx.Cache{Name: "directory", Value: "usr", Help: "服务目录"},
|
||||||
|
},
|
||||||
|
Configs: map[string]*ctx.Config{},
|
||||||
|
Commands: map[string]*ctx.Command{
|
||||||
|
"listen": &ctx.Command{Name: "listen [directory [address [protocol]]]", Help: "开启网页服务", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
||||||
|
m.Meta["detail"] = arg // {{{
|
||||||
|
m.Target.Start(m)
|
||||||
|
return ""
|
||||||
|
// }}}
|
||||||
|
}},
|
||||||
|
"route": &ctx.Command{Name: "route [directory|template|script] route string", Help: "添加响应", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
||||||
|
mux, ok := m.Target.Server.(MUX) // {{{
|
||||||
|
m.Assert(ok, "模块类型错误")
|
||||||
|
m.Assert(len(arg) == 3, "缺少参数")
|
||||||
|
|
||||||
|
switch arg[0] {
|
||||||
|
case "directory":
|
||||||
|
mux.Handle(arg[1], http.FileServer(http.Dir(arg[2])))
|
||||||
|
case "template":
|
||||||
|
mux.Trans(m, arg[1], func(m *ctx.Message, c *ctx.Context, key string, a ...string) string { // {{{
|
||||||
|
w := m.Data["response"].(http.ResponseWriter)
|
||||||
|
|
||||||
|
if _, e := os.Stat(arg[2]); e == nil {
|
||||||
|
template.Must(template.ParseGlob(arg[2])).Execute(w, m)
|
||||||
|
} else {
|
||||||
|
template.Must(template.New("temp").Parse(arg[2])).Execute(w, m)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
})
|
||||||
|
// }}}
|
||||||
|
case "script":
|
||||||
|
cli := m.Find("cli", true) // {{{
|
||||||
|
lex := m.Find("lex", true)
|
||||||
|
mux.Trans(m, arg[1], func(m *ctx.Message, c *ctx.Context, key string, a ...string) string {
|
||||||
|
f, e := os.Open(arg[2])
|
||||||
|
line, bio := "", bufio.NewReader(f)
|
||||||
|
|
||||||
|
if e != nil {
|
||||||
|
line = arg[2]
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
if line = strings.TrimSpace(line); line != "" {
|
||||||
|
lex.Cmd("split", line, "void")
|
||||||
|
cli.Wait = make(chan bool)
|
||||||
|
cli.Cmd(lex.Meta["result"]...)
|
||||||
|
m.Meta["result"] = cli.Meta["result"]
|
||||||
|
}
|
||||||
|
|
||||||
|
if line, e = bio.ReadString('\n'); e != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
})
|
||||||
|
// }}}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
// }}}
|
||||||
|
}},
|
||||||
|
"/hi": &ctx.Command{Name: "/hi", Help: "添加响应", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
||||||
|
m.Add("append", "hi", "hello")
|
||||||
|
return "hello"
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
web := &WEB{}
|
||||||
|
web.Context = Index
|
||||||
|
ctx.Index.Register(Index, web)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user