1
0
mirror of https://shylinux.com/x/icebergs synced 2025-04-26 01:24:05 +08:00
This commit is contained in:
shaoying 2020-06-18 22:03:48 +08:00
parent 11c02de670
commit fd4486d3db
8 changed files with 89 additions and 91 deletions

View File

@ -11,8 +11,6 @@ import (
"path"
)
var CACHE = ice.Name("cache", Index)
func _cache_list(m *ice.Message, key string) {
if key == "" {
m.Grows(CACHE, nil, "", "", func(index int, value map[string]interface{}) {
@ -168,6 +166,9 @@ func CacheCatch(m *ice.Message, kind, name string) *ice.Message {
_cache_save(m, arg[0], arg[1], arg[2], arg[3], arg[4:]...)
return m
}
const CACHE = "cache"
func init() {
Index.Merge(&ice.Context{
Configs: map[string]*ice.Config{
@ -180,6 +181,10 @@ func init() {
kit.MDB_CREATE: {Name: "create type name text arg...", Help: "创建", Hand: func(m *ice.Message, arg ...string) {
_cache_save(m, "add", arg[0], arg[1], arg[2], arg[3:]...)
}},
kit.MDB_INSERT: {Name: "insert type name", Help: "插入", Hand: func(m *ice.Message, arg ...string) {
arg = _cache_catch(m, arg[0], arg[1])
_cache_save(m, arg[0], arg[1], arg[2], arg[3], arg[4:]...)
}},
kit.MDB_SHOW: {Name: "show type name text arg...", Help: "运行", Hand: func(m *ice.Message, arg ...string) {
if len(arg) > 2 {
_cache_show(m, arg[0], arg[1], arg[2], arg[3:]...)
@ -187,10 +192,6 @@ func init() {
_cache_show(m, "", "", "")
}
}},
kit.MDB_INSERT: {Name: "insert type name", Help: "插入", Hand: func(m *ice.Message, arg ...string) {
arg = _cache_catch(m, arg[0], arg[1])
_cache_save(m, arg[0], arg[1], arg[2], arg[3], arg[4:]...)
}},
}, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
if len(arg) == 0 {
_cache_list(m, "")

View File

@ -2,15 +2,59 @@ package web
import (
ice "github.com/shylinux/icebergs"
cli "github.com/shylinux/icebergs/base/cli"
kit "github.com/shylinux/toolkits"
"io/ioutil"
"os"
"path"
"strings"
"time"
)
func _dream_list(m *ice.Message) {
// 任务列表
m.Cmdy("nfs.dir", m.Conf(DREAM, "meta.path"), "time name").Table(func(index int, value map[string]string, head []string) {
if m.Richs(SPACE, nil, value[kit.MDB_NAME], func(key string, value map[string]interface{}) {
m.Push(kit.MDB_TYPE, value[kit.MDB_TYPE])
m.Push(kit.MDB_STATUS, "start")
}) == nil {
m.Push(kit.MDB_TYPE, "none")
m.Push(kit.MDB_STATUS, "stop")
}
})
m.Sort(kit.MDB_NAME)
}
func _dream_show(m *ice.Message, name string) {
// 规范命名
if !strings.Contains(name, "-") || !strings.HasPrefix(name, "20") {
name = m.Time("20060102-") + name
}
// 创建目录
p := path.Join(m.Conf(DREAM, "meta.path"), name)
os.MkdirAll(p, 0777)
if b, e := ioutil.ReadFile(path.Join(p, m.Conf(ice.GDB_SIGNAL, "meta.pid"))); e == nil {
if s, e := os.Stat("/proc/" + string(b)); e == nil && s.IsDir() {
m.Info("already exists %v", string(b))
return
}
}
if m.Richs(SPACE, nil, name, nil) == nil {
// 启动任务
m.Option(cli.CMD_TYPE, "daemon")
m.Option(cli.CMD_DIR, p)
m.Optionv(cli.CMD_ENV,
"ctx_dev", m.Conf(cli.RUNTIME, "conf.ctx_dev"),
"ctx_log", "boot.log", "ctx_mod", "ctx,log,gdb,ssh",
"PATH", kit.Path(path.Join(p, "bin"))+":"+os.Getenv("PATH"),
)
m.Cmd(m.Confv(DREAM, "meta.cmd"), "self", name)
}
m.Cmdy("nfs.dir", p)
}
const DREAM = "dream"
func init() {
@ -22,67 +66,19 @@ func init() {
)},
},
Commands: map[string]*ice.Command{
ice.WEB_DREAM: {Name: "dream name auto", Help: "梦想家", Meta: kit.Dict(
"exports", []string{"you", "name"}, "detail", []interface{}{"启动", "停止"},
), Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
if len(arg) > 1 && arg[0] == "action" {
switch arg[1] {
case "启动", "start":
arg = []string{arg[4]}
case "停止", "stop":
m.Cmd(ice.WEB_SPACE, kit.Select(m.Option("name"), arg, 4), "exit", "1")
m.Event(ice.DREAM_CLOSE, arg[4])
return
}
}
ice.WEB_DREAM: {Name: "dream [name] auto", Help: "梦想家", Meta: kit.Dict("detail", []interface{}{"启动", "停止"}), Action: map[string]*ice.Action{
"start": {Name: "start", Help: "启动", Hand: func(m *ice.Message, arg ...string) {
_dream_show(m, m.Option(kit.MDB_NAME))
}},
"stop": {Name: "stop", Help: "停止", Hand: func(m *ice.Message, arg ...string) {
m.Cmd(SPACE, m.Option("name"), "exit", "1")
}},
}, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
if len(arg) == 0 {
// 任务列表
m.Cmdy("nfs.dir", m.Conf(ice.WEB_DREAM, "meta.path"), "time name")
m.Table(func(index int, value map[string]string, head []string) {
if m.Richs(ice.WEB_SPACE, nil, value["name"], func(key string, value map[string]interface{}) {
m.Push("type", value["type"])
m.Push("status", "start")
}) == nil {
m.Push("type", "none")
m.Push("status", "stop")
}
})
m.Sort("name")
m.Sort("status")
_dream_list(m)
return
}
// 规范命名
if !strings.Contains(arg[0], "-") || !strings.HasPrefix(arg[0], "20") {
arg[0] = m.Time("20060102-") + arg[0]
}
// 创建目录
p := path.Join(m.Conf(ice.WEB_DREAM, "meta.path"), arg[0])
os.MkdirAll(p, 0777)
if b, e := ioutil.ReadFile(path.Join(p, m.Conf(ice.GDB_SIGNAL, "meta.pid"))); e == nil {
if s, e := os.Stat("/proc/" + string(b)); e == nil && s.IsDir() {
m.Info("already exists %v", string(b))
return
}
}
if m.Richs(ice.WEB_SPACE, nil, arg[0], nil) == nil {
// 启动任务
m.Option("cmd_dir", p)
m.Option("cmd_type", "daemon")
m.Optionv("cmd_env",
"ctx_dev", m.Conf(ice.CLI_RUNTIME, "conf.ctx_dev"),
"ctx_log", "boot.log", "ctx_mod", "ctx,log,gdb,ssh",
"PATH", kit.Path(path.Join(p, "bin"))+":"+os.Getenv("PATH"),
)
m.Cmd(m.Confv(ice.WEB_DREAM, "meta.cmd"), "self", arg[0])
time.Sleep(time.Second * 1)
m.Event(ice.DREAM_START, arg...)
}
m.Cmdy("nfs.dir", p)
_dream_show(m, arg[0])
}},
},
}, nil)

View File

@ -243,10 +243,10 @@ func FavorShow(m *ice.Message, kind string, name, text interface{}, arg ...strin
}
const (
EXPORT = "usr/export/web.favor/favor.csv"
_EXPORT = "usr/export/web.favor/favor.csv"
)
var FAVOR = ice.Name("favor", Index)
const FAVOR = "favor"
func init() {
Index.Merge(&ice.Context{
@ -260,10 +260,10 @@ func init() {
"detail", []string{"编辑", "收藏", "收录", "导出", "删除"},
), Action: map[string]*ice.Action{
kit.MDB_EXPORT: {Name: "export file", Help: "导出", Hand: func(m *ice.Message, arg ...string) {
_favor_export(m, kit.Select(EXPORT, arg, 0))
_favor_export(m, kit.Select(_EXPORT, arg, 0))
}},
kit.MDB_IMPORT: {Name: "import file", Help: "导入", Hand: func(m *ice.Message, arg ...string) {
_favor_import(m, kit.Select(EXPORT, arg, 0))
_favor_import(m, kit.Select(_EXPORT, arg, 0))
}},
kit.MDB_CREATE: {Name: "create zone", Help: "创建", Hand: func(m *ice.Message, arg ...string) {
_favor_create(m, arg[0])

View File

@ -14,7 +14,7 @@ import (
"strings"
)
func Login(msg *ice.Message, w http.ResponseWriter, r *http.Request) bool {
func _serve_login(msg *ice.Message, w http.ResponseWriter, r *http.Request) bool {
msg.Option(ice.MSG_USERNAME, "")
msg.Option(ice.MSG_USERROLE, "")
@ -56,7 +56,7 @@ func Login(msg *ice.Message, w http.ResponseWriter, r *http.Request) bool {
return msg.Option(ice.MSG_USERURL) != ""
}
func HandleCmd(key string, cmd *ice.Command, msg *ice.Message, w http.ResponseWriter, r *http.Request) {
func _serve_handle(key string, cmd *ice.Command, msg *ice.Message, w http.ResponseWriter, r *http.Request) {
defer func() { msg.Cost("%s %v %v", r.URL.Path, msg.Optionv("cmds"), msg.Format("append")) }()
if u, e := url.Parse(r.Header.Get("Referer")); e == nil {
for k, v := range u.Query() {
@ -122,7 +122,7 @@ func HandleCmd(key string, cmd *ice.Command, msg *ice.Message, w http.ResponseWr
}
// 执行命令
if cmds := kit.Simple(msg.Optionv("cmds")); Login(msg, w, r) {
if cmds := kit.Simple(msg.Optionv("cmds")); _serve_login(msg, w, r) {
msg.Option("_option", msg.Optionv(ice.MSG_OPTION))
msg.Target().Run(msg, cmd, msg.Option(ice.MSG_USERURL), cmds...)
}
@ -131,9 +131,7 @@ func HandleCmd(key string, cmd *ice.Command, msg *ice.Message, w http.ResponseWr
_args, _ := msg.Optionv(ice.MSG_ARGS).([]interface{})
Render(msg, msg.Option(ice.MSG_OUTPUT), _args...)
}
func (web *Frame) ServeHTTP(w http.ResponseWriter, r *http.Request) {
m := web.m
func _serve_main(m *ice.Message, w http.ResponseWriter, r *http.Request) bool {
if r.Header.Get("index.module") == "" {
// 解析地址
if ip := r.Header.Get("X-Forwarded-For"); ip != "" {
@ -184,8 +182,9 @@ func (web *Frame) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} else if r.URL.Path == "/share" && r.Method == "GET" {
http.ServeFile(w, r, m.Conf(SERVE, "meta.page.share"))
} else {
web.ServeMux.ServeHTTP(w, r)
return true
}
return false
}
const SERVE = "serve"
@ -229,7 +228,7 @@ func init() {
if cli.NodeType(m, ice.WEB_SERVER, cli.HostName); len(arg) > 0 && arg[0] == "random" {
cli.NodeType(m, ice.WEB_SERVER, cli.PathName)
// 随机端口
SpideCreate(m, "self", "http://random")
m.Cmd(SPIDE, kit.MDB_CREATE, "self", "http://random")
arg = arg[1:]
}

View File

@ -47,7 +47,7 @@ func _space_dial(m *ice.Message, dev, name string, arg ...string) {
host := kit.Format(client["hostname"])
proto := kit.Select("ws", "wss", client["protocol"] == "https")
uri := kit.MergeURL(proto+"://"+host+"/space/", "name", name, "type", m.Conf(cli.RUNTIME, "node.type"))
uri := kit.MergeURL(proto+"://"+host+"/space/", "name", name, "type", cli.NodeType)
if u, e := url.Parse(uri); m.Assert(e) {
task.Put(dev, func(task *task.Task) error {
@ -62,7 +62,7 @@ func _space_dial(m *ice.Message, dev, name string, arg ...string) {
m.Log_CREATE("space", dev, "retry", i, "uri", uri)
m = m.Spawns()
if i = 0; HandleWSS(m, true, web.send, s, dev) {
if i = 0; _space_handle(m, true, web.send, s, dev) {
// 连接关闭
break
}
@ -128,7 +128,7 @@ func _space_exec(msg *ice.Message, source, target []string, c *websocket.Conn, n
_space_echo(msg, []string{}, kit.Revert(source)[1:], c, name)
msg.Cost("%v->%v %v %v", source, target, msg.Detailv(), msg.Format("append"))
}
func HandleWSS(m *ice.Message, safe bool, send map[string]*ice.Message, c *websocket.Conn, name string) bool {
func _space_handle(m *ice.Message, safe bool, send map[string]*ice.Message, c *websocket.Conn, name string) bool {
for running := true; running; {
if t, b, e := c.ReadMessage(); m.Warn(e != nil, "space recv %d msg %v", t, e) {
// 解析失败
@ -226,7 +226,7 @@ func init() {
task.Put(name, func(task *task.Task) error {
// 监听消息
m.Event(ice.SPACE_START, ice.WEB_WORKER, name)
HandleWSS(m, false, m.Target().Server().(*Frame).send, s, name)
_space_handle(m, false, m.Target().Server().(*Frame).send, s, name)
m.Log(ice.LOG_CLOSE, "%s: %s", name, kit.Format(m.Confv(SPACE, kit.Keys(kit.MDB_HASH, h))))
m.Event(ice.SPACE_CLOSE, ice.WEB_WORKER, name)
m.Confv(SPACE, kit.Keys(kit.MDB_HASH, h), "")

View File

@ -81,10 +81,6 @@ func _spide_create(m *ice.Message, name, address string, arg ...string) {
}
}
func SpideCreate(m *ice.Message, name, address string, arg ...string) {
_spide_create(m, name, address, arg...)
}
const SPIDE = "spide"
func init() {

View File

@ -10,8 +10,6 @@ import (
"time"
)
var STORY = ice.Name("story", Index)
func _story_share(m *ice.Message, story string, list string, arg ...string) {
if m.Echo("share: "); list == "" {
msg := m.Cmd(STORY, ice.STORY_INDEX, story)
@ -385,6 +383,8 @@ func StoryAdd(m *ice.Message, mime string, name string, text string, arg ...stri
return m
}
const STORY = "story"
func init() {
Index.Merge(&ice.Context{
Configs: map[string]*ice.Config{

View File

@ -87,6 +87,11 @@ func Render(msg *ice.Message, cmd string, args ...interface{}) {
msg.Append(ice.MSG_OUTPUT, ice.RENDER_OUTPUT)
}
func (web *Frame) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if _serve_main(web.m, w, r) {
web.ServeMux.ServeHTTP(w, r)
}
}
func (web *Frame) Spawn(m *ice.Message, c *ice.Context, arg ...string) ice.Server {
return &Frame{}
}
@ -122,7 +127,7 @@ func (web *Frame) Start(m *ice.Message, arg ...string) bool {
msg.Log("route", "%s <- %s", s.Name, k)
w.HandleFunc(k, func(w http.ResponseWriter, r *http.Request) {
m.TryCatch(msg.Spawns(), true, func(msg *ice.Message) {
HandleCmd(k, x, msg, w, r)
_serve_handle(k, x, msg, w, r)
})
})
}
@ -157,9 +162,9 @@ var Index = &ice.Context{Name: "web", Help: "网络模块",
ice.ICE_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Load()
SpideCreate(m, "self", kit.Select("http://:9020", m.Conf(ice.CLI_RUNTIME, "conf.ctx_self")))
SpideCreate(m, "dev", kit.Select("http://:9020", m.Conf(ice.CLI_RUNTIME, "conf.ctx_dev")))
SpideCreate(m, "shy", kit.Select("https://shylinux.com:443", m.Conf(ice.CLI_RUNTIME, "conf.ctx_shy")))
m.Cmd(SPIDE, kit.MDB_CREATE, "self", kit.Select("http://:9020", m.Conf(ice.CLI_RUNTIME, "conf.ctx_self")))
m.Cmd(SPIDE, kit.MDB_CREATE, "dev", kit.Select("http://:9020", m.Conf(ice.CLI_RUNTIME, "conf.ctx_dev")))
m.Cmd(SPIDE, kit.MDB_CREATE, "shy", kit.Select("https://shylinux.com:443", m.Conf(ice.CLI_RUNTIME, "conf.ctx_shy")))
m.Cmd(ice.APP_SEARCH, "add", "favor", "base", m.AddCmd(&ice.Command{Name: "search word", Help: "搜索引擎", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
switch arg[0] {
@ -265,5 +270,6 @@ var Index = &ice.Context{Name: "web", Help: "网络模块",
func init() {
ice.Index.Register(Index, &Frame{},
SPIDE, SERVE, SPACE, DREAM,
CACHE, FAVOR, STORY, SHARE,
)
}