1
0
mirror of https://shylinux.com/x/icebergs synced 2025-04-29 02:29:22 +08:00

opt render

This commit is contained in:
shylinux 2021-01-07 10:58:47 +08:00
parent ccafa12f39
commit 9febae9beb
9 changed files with 74 additions and 70 deletions

View File

@ -39,8 +39,10 @@ func (c *Conn) Close() error {
const ( const (
OPEN = "open" OPEN = "open"
CLOSE = "close" START = "start"
ERROR = "error" ERROR = "error"
CLOSE = "close"
STOP = "stop"
) )
const ( const (
DIAL_CB = "dial.cb" DIAL_CB = "dial.cb"

View File

@ -8,83 +8,85 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"os"
"path" "path"
"strings" "strings"
"time" "time"
) )
const ( const (
REDIRECT = "redirect"
REFRESH = "refresh"
STATUS = "status" STATUS = "status"
COOKIE = "cookie" COOKIE = "cookie"
) )
func Render(msg *ice.Message, cmd string, args ...interface{}) { func Render(msg *ice.Message, cmd string, args ...interface{}) {
if cmd != "" { if cmd != "" {
defer func() { msg.Log(ice.LOG_EXPORT, "%s: %v", cmd, args) }() defer func() { msg.Log_EXPORT(cmd, args) }()
} }
switch arg := kit.Simple(args...); cmd { switch arg := kit.Simple(args...); cmd {
case ice.RENDER_VOID: case REDIRECT: // url [arg...]
case ice.RENDER_OUTPUT:
case "redirect":
http.Redirect(msg.W, msg.R, kit.MergeURL(arg[0], arg[1:]), 307) http.Redirect(msg.W, msg.R, kit.MergeURL(arg[0], arg[1:]), 307)
case "refresh": case REFRESH: // [delay [text]]
arg = []string{"200", fmt.Sprintf(`<!DOCTYPE html><head><meta charset="utf-8"><meta http-equiv="Refresh" content="%d"></head><body>%s</body>`, arg = []string{"200", fmt.Sprintf(`<!DOCTYPE html><head><meta charset="utf-8"><meta http-equiv="Refresh" content="%d"></head><body>%s</body>`,
kit.Int(kit.Select("3", arg, 0)), kit.Select("请稍后,系统初始化中...", arg, 1), kit.Int(kit.Select("3", arg, 0)), kit.Select("请稍后,系统初始化中...", arg, 1),
)} )}
fallthrough fallthrough
case STATUS: case STATUS: // [code [text]]
RenderStatus(msg, kit.Int(kit.Select("200", arg, 0)), kit.Select("", arg, 1)) RenderStatus(msg, kit.Int(kit.Select("200", arg, 0)), kit.Select("", arg, 1))
case COOKIE: case COOKIE: // value [name [path [expire]]]
RenderCookie(msg, arg[0], arg[1:]...) RenderCookie(msg, arg[0], arg[1:]...)
case ice.RENDER_DOWNLOAD: case ice.RENDER_DOWNLOAD: // file [type [name]]
msg.W.Header().Set("Content-Disposition", fmt.Sprintf("filename=%s", kit.Select(path.Base(arg[0]), arg, 2))) msg.W.Header().Set("Content-Disposition", fmt.Sprintf("filename=%s", kit.Select(path.Base(arg[0]), arg, 2)))
msg.W.Header().Set("Content-Type", kit.Select("text/html", arg, 1)) if RenderType(msg.W, arg[0], kit.Select("", arg, 1)); !ice.DumpBinPack(msg.W, arg[0], nil) {
if !ice.DumpBinPack(msg.W, arg[0], func(name string) { RenderType(msg.W, name) }) {
if _, e := os.Stat(arg[0]); e == nil {
http.ServeFile(msg.W, msg.R, arg[0]) http.ServeFile(msg.W, msg.R, arg[0])
} }
case ice.RENDER_QRCODE: // text [size]
if qr, e := qrcode.New(arg[0], qrcode.Medium); msg.Assert(e) {
msg.W.Header().Set(ContentType, ContentPNG)
msg.Assert(qr.Write(kit.Int(kit.Select("256", arg, 1)), msg.W))
} }
case ice.RENDER_RESULT: case ice.RENDER_RESULT:
if len(arg) > 0 { if len(arg) > 0 { // [str [arg...]]
msg.W.Write([]byte(kit.Format(arg[0], args[1:]...))) msg.W.Write([]byte(kit.Format(arg[0], args[1:]...)))
} else { } else {
args = append(args, "length:", len(msg.Result())) args = append(args, "length:", len(msg.Result()))
msg.W.Write([]byte(msg.Result())) msg.W.Write([]byte(msg.Result()))
} }
case ice.RENDER_QRCODE: case ice.RENDER_VOID:
if qr, e := qrcode.New(arg[0], qrcode.Medium); msg.Assert(e) { // no output
msg.W.Header().Set("Content-Type", "image/png")
msg.Assert(qr.Write(kit.Int(kit.Select("256", arg, 1)), msg.W))
}
default: default:
if cmd != "" { if cmd != "" { // [str [arg...]]
msg.Echo(kit.Format(cmd, args...)) msg.Echo(kit.Format(cmd, args...))
} }
msg.W.Header().Set("Content-Type", "application/json") msg.W.Header().Set(ContentType, ContentJSON)
fmt.Fprint(msg.W, msg.Formats("meta")) fmt.Fprint(msg.W, msg.Formats(kit.MDB_META))
}
msg.Append(ice.MSG_OUTPUT, ice.RENDER_OUTPUT)
}
func RenderType(w http.ResponseWriter, name string) {
if strings.HasSuffix(name, ".css") {
w.Header().Set("Content-Type", "text/css; charset=utf-8")
} }
} }
func RenderCookie(msg *ice.Message, value string, arg ...string) { // name path expire
expire := time.Now().Add(kit.Duration(kit.Select(msg.Conf(aaa.SESS, "meta.expire"), arg, 2)))
http.SetCookie(msg.W, &http.Cookie{Value: value, Name: kit.Select(ice.MSG_SESSID, arg, 0), Path: kit.Select("/", arg, 1), Expires: expire})
}
func RenderStatus(msg *ice.Message, code int, text string) { func RenderStatus(msg *ice.Message, code int, text string) {
msg.W.WriteHeader(code) msg.W.WriteHeader(code)
msg.W.Write([]byte(text)) msg.W.Write([]byte(text))
} }
func RenderCookie(msg *ice.Message, value string, arg ...string) { // name path expire
expire := time.Now().Add(kit.Duration(kit.Select(msg.Conf(aaa.SESS, "meta.expire"), arg, 2)))
http.SetCookie(msg.W, &http.Cookie{Value: value, Name: kit.Select(ice.MSG_SESSID, arg, 0), Path: kit.Select("/", arg, 1), Expires: expire})
}
func RenderType(w http.ResponseWriter, name, mime string) {
if mime != "" {
w.Header().Set(ContentType, mime)
} else if strings.HasSuffix(name, ".css") {
w.Header().Set(ContentType, "text/css; charset=utf-8")
} else {
w.Header().Set(ContentType, ContentHTML)
}
}

View File

@ -68,7 +68,7 @@ func _serve_main(m *ice.Message, w http.ResponseWriter, r *http.Request) bool {
} }
// 内置文件 // 内置文件
return !ice.DumpBinPack(w, r.URL.Path, func(name string) { RenderType(w, name) }) return !ice.DumpBinPack(w, r.URL.Path, func(name string) { RenderType(w, name, "") })
} }
func _serve_handle(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() { defer func() {
@ -219,16 +219,9 @@ func init() {
}, },
Commands: map[string]*ice.Command{ Commands: map[string]*ice.Command{
SERVE: {Name: "serve name auto start", Help: "服务器", Action: map[string]*ice.Action{ SERVE: {Name: "serve name auto start", Help: "服务器", Action: map[string]*ice.Action{
gdb.START: {Name: "start dev= name=self proto=http host= port=9020", Help: "启动", Hand: func(m *ice.Message, arg ...string) { aaa.BLACK: {Name: "black", Help: "黑名单", Hand: func(m *ice.Message, arg ...string) {
if cli.NodeInfo(m, SERVER, ice.Info.HostName); m.Option(tcp.PORT) == "random" { for _, k := range arg {
m.Option(tcp.PORT, m.Cmdx(tcp.PORT, aaa.RIGHT)) m.Conf(SERVE, kit.Keys(kit.MDB_META, aaa.BLACK, k), true)
}
m.Target().Start(m, kit.MDB_NAME, m.Option(kit.MDB_NAME), tcp.HOST, m.Option(tcp.HOST), tcp.PORT, m.Option(tcp.PORT))
m.Sleep("1s")
for _, k := range kit.Split(m.Option(SPIDE_DEV)) {
m.Cmd(SPACE, tcp.DIAL, SPIDE_DEV, k, kit.MDB_NAME, ice.Info.NodeName)
} }
}}, }},
aaa.WHITE: {Name: "white", Help: "白名单", Hand: func(m *ice.Message, arg ...string) { aaa.WHITE: {Name: "white", Help: "白名单", Hand: func(m *ice.Message, arg ...string) {
@ -236,9 +229,16 @@ func init() {
m.Conf(SERVE, kit.Keys(kit.MDB_META, aaa.WHITE, k), true) m.Conf(SERVE, kit.Keys(kit.MDB_META, aaa.WHITE, k), true)
} }
}}, }},
aaa.BLACK: {Name: "black", Help: "黑名单", Hand: func(m *ice.Message, arg ...string) { gdb.START: {Name: "start dev= name=self proto=http host= port=9020", Help: "启动", Hand: func(m *ice.Message, arg ...string) {
for _, k := range arg { if cli.NodeInfo(m, SERVER, ice.Info.HostName); m.Option(tcp.PORT) == "random" {
m.Conf(SERVE, kit.Keys(kit.MDB_META, aaa.BLACK, k), true) m.Option(tcp.PORT, m.Cmdx(tcp.PORT, aaa.RIGHT))
}
m.Target().Start(m, kit.MDB_NAME, m.Option(kit.MDB_NAME), tcp.HOST, m.Option(tcp.HOST), tcp.PORT, m.Option(tcp.PORT))
m.Sleep(ice.MOD_TICK)
for _, k := range kit.Split(m.Option(SPIDE_DEV)) {
m.Cmd(SPACE, tcp.DIAL, SPIDE_DEV, k, kit.MDB_NAME, ice.Info.NodeName)
} }
}}, }},
}, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
@ -253,10 +253,10 @@ func init() {
m.Render(ice.RENDER_DOWNLOAD, path.Join(m.Conf(SERVE, "meta.volcanos.path"), path.Join(arg...))) m.Render(ice.RENDER_DOWNLOAD, path.Join(m.Conf(SERVE, "meta.volcanos.path"), path.Join(arg...)))
}}, }},
"/publish/": {Name: "/publish/", Help: "源码", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { "/publish/": {Name: "/publish/", Help: "源码", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
_share_local(m, m.Conf(SERVE, "meta.publish"), path.Join(arg...)) _share_local(m, m.Conf(SERVE, kit.Keym("publish")), path.Join(arg...))
}}, }},
"/plugin/github.com/": {Name: "/plugin/github.com/", Help: "源码", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { "/plugin/github.com/": {Name: "/plugin/github.com/", Help: "源码", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
_share_repos(m, "github.com/"+arg[0]+"/"+arg[1], arg[2:]...) _share_repos(m, path.Join("github.com", arg[0], arg[1]), arg[2:]...)
}}, }},
}}) }})
} }

View File

@ -73,6 +73,7 @@ const (
ContentFORM = "application/x-www-form-urlencoded" ContentFORM = "application/x-www-form-urlencoded"
ContentJSON = "application/json" ContentJSON = "application/json"
ContentHTML = "text/html" ContentHTML = "text/html"
ContentPNG = "image/png"
) )
const ( const (
ADDRESS = "address" ADDRESS = "address"

View File

@ -71,11 +71,12 @@ func (web *Frame) Start(m *ice.Message, arg ...string) bool {
web.m, web.Server = m, &http.Server{Handler: web} web.m, web.Server = m, &http.Server{Handler: web}
m.Option(tcp.LISTEN_CB, func(l net.Listener) { m.Option(tcp.LISTEN_CB, func(l net.Listener) {
m.Cmdy(mdb.INSERT, SERVE, "", mdb.HASH, arg, kit.MDB_STATUS, "start", "proto", m.Option("proto"), "dev", m.Option("dev")) m.Cmdy(mdb.INSERT, SERVE, "", mdb.HASH, arg, kit.MDB_STATUS, tcp.START, kit.MDB_PROTO, m.Option(kit.MDB_PROTO), SPIDE_DEV, m.Option(SPIDE_DEV))
m.Event(SERVE_START, arg...) m.Event(SERVE_START, arg...)
defer m.Event(SERVE_CLOSE, arg...) defer m.Event(SERVE_CLOSE, arg...)
defer m.Cmd(mdb.MODIFY, SERVE, "", mdb.HASH, kit.MDB_NAME, m.Option(kit.MDB_NAME), kit.MDB_STATUS, "stop") defer m.Cmd(mdb.MODIFY, SERVE, "", mdb.HASH, kit.MDB_NAME, m.Option(kit.MDB_NAME), kit.MDB_STATUS, tcp.STOP)
// 启动服务
m.Warn(true, SERVE, ": ", web.Server.Serve(l)) m.Warn(true, SERVE, ": ", web.Server.Serve(l))
}) })
@ -97,7 +98,7 @@ var Index = &ice.Context{Name: WEB, Help: "网络模块",
m.Cmd(SPIDE, mdb.CREATE, SPIDE_DEV, kit.Select("http://:9020", m.Conf(cli.RUNTIME, "conf.ctx_dev"))) m.Cmd(SPIDE, mdb.CREATE, SPIDE_DEV, kit.Select("http://:9020", m.Conf(cli.RUNTIME, "conf.ctx_dev")))
m.Cmd(SPIDE, mdb.CREATE, SPIDE_SELF, kit.Select("http://:9020", m.Conf(cli.RUNTIME, "conf.ctx_self"))) m.Cmd(SPIDE, mdb.CREATE, SPIDE_SELF, kit.Select("http://:9020", m.Conf(cli.RUNTIME, "conf.ctx_self")))
m.Cmd(SPIDE, mdb.CREATE, SPIDE_SHY, kit.Select("https://shylinux.com:443", m.Conf(cli.RUNTIME, "conf.ctx_shy"))) m.Cmd(SPIDE, mdb.CREATE, SPIDE_SHY, kit.Select("https://shylinux.com:443", m.Conf(cli.RUNTIME, "conf.ctx_shy")))
m.Cmd(mdb.SEARCH, mdb.CREATE, SPACE, SPACE, WEB) m.Cmd(mdb.SEARCH, mdb.CREATE, SPACE, m.Prefix(SPACE))
}}, }},
ice.CTX_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { ice.CTX_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Save() m.Save()
@ -112,6 +113,7 @@ var Index = &ice.Context{Name: WEB, Help: "网络模块",
func init() { func init() {
ice.Index.Register(Index, &Frame{}, ice.Index.Register(Index, &Frame{},
SPIDE, SERVE, SPACE, DREAM, ROUTE, SHARE, CACHE, STORY, SPIDE, SERVE, SPACE, DREAM,
ROUTE, CACHE, SHARE, STORY,
) )
} }

View File

@ -8,6 +8,5 @@ field "梦想家" web.dream
field "路由器" web.route field "路由器" web.route
field "共享链" web.share field "共享链" web.share
field "缓存池" web.cache field "缓存池" web.cache
return
field "故事会" web.story field "故事会" web.story

18
conf.go
View File

@ -30,8 +30,8 @@ const ( // MSG
MSG_PROCESS = "_process" MSG_PROCESS = "_process"
MSG_CMDS = "cmds" MSG_CMDS = "cmds"
MSG_SESSID = "sessid"
MSG_DOMAIN = "domain" MSG_DOMAIN = "domain"
MSG_SESSID = "sessid"
MSG_USERIP = "user.ip" MSG_USERIP = "user.ip"
MSG_USERUA = "user.ua" MSG_USERUA = "user.ua"
@ -43,7 +43,6 @@ const ( // MSG
MSG_USERNAME = "user.name" MSG_USERNAME = "user.name"
MSG_USERZONE = "user.zone" MSG_USERZONE = "user.zone"
MSG_USERROLE = "user.role" MSG_USERROLE = "user.role"
MSG_USERDATA = "user.data" MSG_USERDATA = "user.data"
MSG_USERADDR = "user.addr" MSG_USERADDR = "user.addr"
@ -51,6 +50,13 @@ const ( // MSG
MSG_STORM = "sess.storm" MSG_STORM = "sess.storm"
MSG_LOCAL = "sess.local" MSG_LOCAL = "sess.local"
) )
const ( // RENDER
RENDER_VOID = "_void"
RENDER_RESULT = "_result"
RENDER_QRCODE = "_qrcode"
RENDER_DOWNLOAD = "_download"
RENDER_TEMPLATE = "_template"
)
const ( const (
CONTROL_PAGE = "_page" CONTROL_PAGE = "_page"
@ -98,11 +104,3 @@ const ( // LOG
LOG_ERROR = "error" LOG_ERROR = "error"
LOG_DEBUG = "debug" LOG_DEBUG = "debug"
) )
const ( // RENDER
RENDER_VOID = "_void"
RENDER_OUTPUT = "_output"
RENDER_RESULT = "_result"
RENDER_QRCODE = "_qrcode"
RENDER_DOWNLOAD = "_download"
RENDER_TEMPLATE = "_template"
)

View File

@ -347,7 +347,7 @@ func (m *Message) Render(cmd string, args ...interface{}) *Message {
m.Optionv(MSG_ARGS, args) m.Optionv(MSG_ARGS, args)
switch cmd { switch cmd {
case RENDER_TEMPLATE: case RENDER_TEMPLATE: // text [data [type]]
if len(args) == 1 { if len(args) == 1 {
args = append(args, m) args = append(args, m)
} }

View File

@ -20,10 +20,10 @@ func init() {
}, },
Commands: map[string]*ice.Command{ Commands: map[string]*ice.Command{
web.LOGIN: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { web.LOGIN: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Option(ice.RENDER_OUTPUT, ice.RENDER_RESULT) m.Render(ice.RENDER_RESULT)
}}, }},
"/github.com/": {Name: "github.com", Help: "github.com", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { "/github.com/": {Name: "github.com", Help: "github.com", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
m.Render(ice.RENDER_OUTPUT, ice.RENDER_VOID) m.Render(ice.RENDER_VOID)
p := kit.Path(".ish/pluged") p := kit.Path(".ish/pluged")
m.Debug("what %v", p) m.Debug("what %v", p)
git := githttp.New(p) git := githttp.New(p)