1
0
mirror of https://shylinux.com/x/icebergs synced 2025-04-25 17:18:05 +08:00
icebergs/base/web/web.go
2020-10-03 10:12:42 +08:00

136 lines
3.8 KiB
Go

package web
import (
ice "github.com/shylinux/icebergs"
"github.com/shylinux/icebergs/base/aaa"
"github.com/shylinux/icebergs/base/cli"
"github.com/shylinux/icebergs/base/ctx"
"github.com/shylinux/icebergs/base/mdb"
"github.com/shylinux/icebergs/base/tcp"
kit "github.com/shylinux/toolkits"
"net"
"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)
})
})
}
})
}
})
m.Richs(SPIDE, nil, arg[0], func(key string, value map[string]interface{}) {
client := value["client"].(map[string]interface{})
web.m, web.Server = m, &http.Server{Handler: web}
m.Option(tcp.LISTEN_CB, func(l net.Listener) {
m.Event(SERVE_START, arg[0])
m.Warn(true, "listen %s", web.Server.Serve(l))
m.Event(SERVE_CLOSE, arg[0])
})
ls := strings.Split(m.Cap(ice.CTX_STREAM, client["hostname"]), ":")
m.Cmd(tcp.SERVER, tcp.LISTEN, kit.MDB_NAME, "web", tcp.HOST, ls[0], tcp.PORT, ls[1])
})
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()
m.Cmd(SPIDE, mdb.CREATE, "dev", kit.Select("http://:9020", m.Conf(cli.RUNTIME, "conf.ctx_dev")))
m.Cmd(SPIDE, mdb.CREATE, "self", kit.Select("http://:9020", m.Conf(cli.RUNTIME, "conf.ctx_self")))
m.Cmd(SPIDE, mdb.CREATE, "shy", kit.Select("https://shylinux.com:443", m.Conf(cli.RUNTIME, "conf.ctx_shy")))
m.Cmd(aaa.ROLE, aaa.White, aaa.VOID, "web", "/publish/")
m.Cmd(aaa.ROLE, aaa.White, aaa.VOID, ctx.COMMAND)
m.Cmd(mdb.SEARCH, mdb.CREATE, SPIDE)
m.Cmd(mdb.RENDER, mdb.CREATE, SPIDE)
m.Cmd(mdb.SEARCH, mdb.CREATE, SPACE)
m.Search("_render", func(p *ice.Context, s *ice.Context, key string, cmd *ice.Command) {
for k := range cmd.Action {
m.Cmd(mdb.RENDER, mdb.CREATE, k, "_render", "web")
}
})
}},
ice.CTX_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Save(SPIDE, SERVE, CACHE, SHARE, STORY)
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, SHARE, STORY,
)
}