1
0
mirror of https://shylinux.com/x/icebergs synced 2025-04-28 02:02:02 +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 (
OPEN = "open"
CLOSE = "close"
START = "start"
ERROR = "error"
CLOSE = "close"
STOP = "stop"
)
const (
DIAL_CB = "dial.cb"

View File

@ -8,83 +8,85 @@ import (
"fmt"
"net/http"
"os"
"path"
"strings"
"time"
)
const (
STATUS = "status"
COOKIE = "cookie"
REDIRECT = "redirect"
REFRESH = "refresh"
STATUS = "status"
COOKIE = "cookie"
)
func Render(msg *ice.Message, cmd string, args ...interface{}) {
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 {
case ice.RENDER_VOID:
case ice.RENDER_OUTPUT:
case "redirect":
case REDIRECT: // url [arg...]
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>`,
kit.Int(kit.Select("3", arg, 0)), kit.Select("请稍后,系统初始化中...", arg, 1),
)}
fallthrough
case STATUS:
case STATUS: // [code [text]]
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:]...)
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-Type", kit.Select("text/html", arg, 1))
if RenderType(msg.W, arg[0], kit.Select("", arg, 1)); !ice.DumpBinPack(msg.W, arg[0], nil) {
http.ServeFile(msg.W, msg.R, arg[0])
}
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])
}
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:
if len(arg) > 0 {
if len(arg) > 0 { // [str [arg...]]
msg.W.Write([]byte(kit.Format(arg[0], args[1:]...)))
} else {
args = append(args, "length:", len(msg.Result()))
msg.W.Write([]byte(msg.Result()))
}
case ice.RENDER_QRCODE:
if qr, e := qrcode.New(arg[0], qrcode.Medium); msg.Assert(e) {
msg.W.Header().Set("Content-Type", "image/png")
msg.Assert(qr.Write(kit.Int(kit.Select("256", arg, 1)), msg.W))
}
case ice.RENDER_VOID:
// no output
default:
if cmd != "" {
if cmd != "" { // [str [arg...]]
msg.Echo(kit.Format(cmd, args...))
}
msg.W.Header().Set("Content-Type", "application/json")
fmt.Fprint(msg.W, msg.Formats("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")
msg.W.Header().Set(ContentType, ContentJSON)
fmt.Fprint(msg.W, msg.Formats(kit.MDB_META))
}
}
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) {
msg.W.WriteHeader(code)
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) {
defer func() {
@ -219,16 +219,9 @@ func init() {
},
Commands: map[string]*ice.Command{
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) {
if cli.NodeInfo(m, SERVER, ice.Info.HostName); m.Option(tcp.PORT) == "random" {
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("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.BLACK: {Name: "black", Help: "黑名单", Hand: func(m *ice.Message, arg ...string) {
for _, k := range arg {
m.Conf(SERVE, kit.Keys(kit.MDB_META, aaa.BLACK, k), true)
}
}},
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)
}
}},
aaa.BLACK: {Name: "black", Help: "黑名单", Hand: func(m *ice.Message, arg ...string) {
for _, k := range arg {
m.Conf(SERVE, kit.Keys(kit.MDB_META, aaa.BLACK, k), true)
gdb.START: {Name: "start dev= name=self proto=http host= port=9020", Help: "启动", Hand: func(m *ice.Message, arg ...string) {
if cli.NodeInfo(m, SERVER, ice.Info.HostName); m.Option(tcp.PORT) == "random" {
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) {
@ -253,10 +253,10 @@ func init() {
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) {
_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) {
_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"
ContentJSON = "application/json"
ContentHTML = "text/html"
ContentPNG = "image/png"
)
const (
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}
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...)
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))
})
@ -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_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(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) {
m.Save()
@ -112,6 +113,7 @@ var Index = &ice.Context{Name: WEB, Help: "网络模块",
func init() {
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.share
field "缓存池" web.cache
return
field "故事会" web.story

18
conf.go
View File

@ -30,8 +30,8 @@ const ( // MSG
MSG_PROCESS = "_process"
MSG_CMDS = "cmds"
MSG_SESSID = "sessid"
MSG_DOMAIN = "domain"
MSG_SESSID = "sessid"
MSG_USERIP = "user.ip"
MSG_USERUA = "user.ua"
@ -43,7 +43,6 @@ const ( // MSG
MSG_USERNAME = "user.name"
MSG_USERZONE = "user.zone"
MSG_USERROLE = "user.role"
MSG_USERDATA = "user.data"
MSG_USERADDR = "user.addr"
@ -51,6 +50,13 @@ const ( // MSG
MSG_STORM = "sess.storm"
MSG_LOCAL = "sess.local"
)
const ( // RENDER
RENDER_VOID = "_void"
RENDER_RESULT = "_result"
RENDER_QRCODE = "_qrcode"
RENDER_DOWNLOAD = "_download"
RENDER_TEMPLATE = "_template"
)
const (
CONTROL_PAGE = "_page"
@ -98,11 +104,3 @@ const ( // LOG
LOG_ERROR = "error"
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)
switch cmd {
case RENDER_TEMPLATE:
case RENDER_TEMPLATE: // text [data [type]]
if len(args) == 1 {
args = append(args, m)
}

View File

@ -20,10 +20,10 @@ func init() {
},
Commands: map[string]*ice.Command{
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) {
m.Render(ice.RENDER_OUTPUT, ice.RENDER_VOID)
m.Render(ice.RENDER_VOID)
p := kit.Path(".ish/pluged")
m.Debug("what %v", p)
git := githttp.New(p)