1
0
mirror of https://shylinux.com/x/icebergs synced 2025-04-26 01:24:05 +08:00
icebergs/base/web/web.go
2020-06-24 07:44:10 +08:00

180 lines
5.4 KiB
Go

package web
import (
ice "github.com/shylinux/icebergs"
"github.com/shylinux/icebergs/base/cli"
"github.com/shylinux/icebergs/base/gdb"
"github.com/shylinux/icebergs/base/mdb"
"github.com/shylinux/icebergs/base/nfs"
kit "github.com/shylinux/toolkits"
"net/http"
"path"
"strings"
)
type Frame struct {
*http.Client
*http.Server
*http.ServeMux
m *ice.Message
send map[string]*ice.Message
}
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{}
}
func (web *Frame) Begin(m *ice.Message, arg ...string) ice.Server {
web.send = map[string]*ice.Message{}
return web
}
func (web *Frame) Start(m *ice.Message, arg ...string) bool {
m.Travel(func(p *ice.Context, s *ice.Context) {
if w, ok := s.Server().(*Frame); ok {
if w.ServeMux != nil {
return
}
w.ServeMux = http.NewServeMux()
// 静态路由
msg := m.Spawns(s)
m.Confm(SERVE, "meta.static", func(key string, value string) {
m.Log("route", "%s <- %s <- %s", s.Name, key, value)
w.Handle(key, http.StripPrefix(key, http.FileServer(http.Dir(value))))
})
// 级联路由
route := "/" + s.Name + "/"
if n, ok := p.Server().(*Frame); ok && n.ServeMux != nil {
msg.Log("route", "%s <= %s", p.Name, route)
n.Handle(route, http.StripPrefix(path.Dir(route), w))
}
// 命令路由
m.Travel(func(p *ice.Context, sub *ice.Context, k string, x *ice.Command) {
if s == sub && k[0] == '/' {
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) {
_serve_handle(k, x, msg, w, r)
})
})
}
})
}
})
// TODO simple
m.Richs(SPIDE, nil, arg[0], func(key string, value map[string]interface{}) {
client := value["client"].(map[string]interface{})
// 服务地址
port := m.Cap(ice.CTX_STREAM, client["hostname"])
m.Log("serve", "listen %s %s %v", arg[0], port, m.Conf(cli.RUNTIME, "node"))
// 启动服务
web.m, web.Server = m, &http.Server{Addr: port, Handler: web}
m.Event(gdb.SERVE_START, arg[0])
m.Warn(true, "listen %s", web.Server.ListenAndServe())
m.Event(gdb.SERVE_CLOSE, arg[0])
})
return true
}
func (web *Frame) Close(m *ice.Message, arg ...string) bool {
return true
}
var Index = &ice.Context{Name: "web", Help: "网络模块",
Caches: map[string]*ice.Cache{},
Configs: map[string]*ice.Config{},
Commands: map[string]*ice.Command{
ice.CTX_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Load()
for k := range c.Commands[mdb.RENDER].Action {
m.Cmdy(mdb.RENDER, mdb.CREATE, k, mdb.RENDER, c.Cap(ice.CTX_FOLLOW))
}
m.Cmd(SPIDE, mdb.CREATE, "self", kit.Select("http://:9020", m.Conf(cli.RUNTIME, "conf.ctx_self")))
m.Cmd(SPIDE, mdb.CREATE, "dev", kit.Select("http://:9020", m.Conf(cli.RUNTIME, "conf.ctx_dev")))
m.Cmd(SPIDE, mdb.CREATE, "shy", kit.Select("https://shylinux.com:443", m.Conf(cli.RUNTIME, "conf.ctx_shy")))
m.Cmd("mdb.search", "create", "favor", "favor", "web")
m.Cmd(nfs.SEARCH, "add", "story", "base", m.AddCmd(&ice.Command{Name: "search word", Help: "搜索引擎", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
switch arg[0] {
case "set":
m.Cmdy(STORY, "index", arg[2])
return
}
m.Richs(STORY, "head", "*", func(key string, val map[string]interface{}) {
if val["story"] == arg[0] {
m.Push("pod", m.Option(ice.MSG_USERPOD))
m.Push("engine", "story")
m.Push("favor", val["story"])
m.Push("id", val["list"])
m.Push("time", val["time"])
m.Push("type", val["scene"])
m.Push("name", val["story"])
m.Push("text", val["count"])
}
})
}}))
m.Cmd(nfs.SEARCH, "add", "share", "base", m.AddCmd(&ice.Command{Name: "search word", Help: "搜索引擎", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
switch arg[0] {
case "set":
m.Cmdy(SHARE, arg[2])
return
}
m.Option("cache.limit", -2)
m.Grows(SHARE, nil, "", "", func(index int, value map[string]interface{}) {
if value["share"] == arg[0] || value["type"] == arg[0] ||
strings.Contains(kit.Format(value["name"]), arg[0]) || strings.Contains(kit.Format(value["text"]), arg[0]) {
m.Push("pod", m.Option(ice.MSG_USERPOD))
m.Push("engine", "share")
m.Push("favor", value["type"])
m.Push("id", value["share"])
m.Push("time", value["time"])
m.Push("type", value["type"])
m.Push("name", value["name"])
m.Push("text", value["text"])
}
})
}}))
m.Conf(FAVOR, "meta.render.bench", m.AddCmd(&ice.Command{Name: "render type name text arg...", Help: "渲染引擎", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Cmdy("web.code.bench", "action", "show", arg)
}}))
}},
ice.CTX_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Save(SPIDE, SERVE, GROUP, LABEL,
FAVOR, CACHE, STORY, SHARE)
m.Done()
m.Richs(SPACE, nil, "*", func(key string, value map[string]interface{}) {
if kit.Format(value["type"]) == "master" {
m.Done()
}
})
}},
},
}
func init() {
ice.Index.Register(Index, &Frame{},
SPIDE, SERVE, SPACE, DREAM,
CACHE, FAVOR, STORY, SHARE,
)
}