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-11-19 19:15:34 +08:00
parent 77bf29e83c
commit fa4a74af53
4 changed files with 74 additions and 2 deletions

View File

@ -1,6 +1,9 @@
package web
import (
"io"
"os"
ice "github.com/shylinux/icebergs"
"github.com/shylinux/icebergs/base/aaa"
"github.com/shylinux/icebergs/base/cli"
@ -16,6 +19,51 @@ import (
"strings"
)
func _serve_proxy(m *ice.Message, w http.ResponseWriter, r *http.Request) bool {
m.Option(SPIDE_CB, func(msg *ice.Message, req *http.Request, res *http.Response) {
p := path.Join("var/proxy", strings.ReplaceAll(r.URL.String(), "/", "_"))
size := 0
if s, e := os.Stat(p); os.IsNotExist(e) {
if f, p, e := kit.Create(p); m.Assert(e) {
defer f.Close()
if n, e := io.Copy(f, res.Body); m.Assert(e) {
m.Debug("proxy %s res: %v", p, n)
size = int(n)
}
}
} else {
size = int(s.Size())
}
h := w.Header()
for k, v := range res.Header {
for _, v := range v {
switch k {
case ContentLength:
h.Add(k, kit.Format(size))
m.Debug("proxy res: %v %v", k, size+1)
default:
m.Debug("proxy res: %v %v", k, v)
h.Add(k, v)
}
}
}
w.WriteHeader(res.StatusCode)
if f, e := os.Open(p); m.Assert(e) {
defer f.Close()
if n, e := io.Copy(w, f); e == nil {
m.Debug("proxy res: %v", n)
} else {
m.Debug("proxy res: %v %v", n, e)
}
}
})
m.Cmdx(SPIDE, r.URL.Host, SPIDE_PROXY, r.Method, r.URL.String())
return true
}
func _serve_main(m *ice.Message, w http.ResponseWriter, r *http.Request) bool {
if r.Header.Get("index.module") != "" {
return true
@ -33,6 +81,13 @@ func _serve_main(m *ice.Message, w http.ResponseWriter, r *http.Request) bool {
}
m.Info("").Info("%s %s %s", r.Header.Get(ice.MSG_USERIP), r.Method, r.URL)
if strings.HasPrefix(r.URL.String(), "http") {
if m == nil {
m = ice.Pulse.Spawn()
}
return _serve_proxy(m, w, r)
}
// 请求地址
r.Header.Set("index.module", m.Target().Name)
r.Header.Set("index.path", r.URL.Path)

View File

@ -48,6 +48,8 @@ const (
SPIDE_MSG = "msg"
SPIDE_SAVE = "save"
SPIDE_CACHE = "cache"
SPIDE_PROXY = "proxy"
SPIDE_CB = "spide.cb"
SPIDE_GET = "GET"
SPIDE_PUT = "PUT"
@ -113,7 +115,7 @@ func init() {
// 缓存数据
cache, save := "", ""
switch arg[1] {
case SPIDE_RAW:
case SPIDE_RAW, SPIDE_PROXY:
cache, arg = arg[1], arg[1:]
case SPIDE_MSG:
cache, arg = arg[1], arg[1:]
@ -251,6 +253,13 @@ func init() {
// 检查结果
defer res.Body.Close()
m.Cost("status", res.Status, "length", res.Header.Get(ContentLength), "type", res.Header.Get(ContentType))
switch cb := m.Optionv(SPIDE_CB).(type) {
case func(*ice.Message, *http.Request, *http.Response):
cb(m, req, res)
return
}
if m.Warn(res.StatusCode != http.StatusOK, res.Status) {
m.Set(ice.MSG_RESULT)
// return
@ -264,6 +273,11 @@ func init() {
// 解析引擎
switch cache {
case SPIDE_PROXY:
m.Optionv(RESPONSE, res)
m.Cmdy(CACHE, DOWNLOAD, res.Header.Get(ContentType), uri)
m.Echo(m.Append(DATA))
case SPIDE_CACHE:
m.Optionv(RESPONSE, res)
m.Cmdy(CACHE, DOWNLOAD, res.Header.Get(ContentType), uri)

View File

@ -17,7 +17,7 @@ const VIMER = "vimer"
func init() {
Index.Merge(&ice.Context{
Commands: map[string]*ice.Command{
VIMER: {Name: "vimer path=src/ file=main.go line=1 刷新:button=auto save display project search", Help: "编辑器", Meta: kit.Dict(
VIMER: {Name: "vimer path=src/ file=main.go line=1 刷新:button=auto save project search", Help: "编辑器", Meta: kit.Dict(
"display", "/plugin/local/code/vimer.js", "style", "editor",
"trans", kit.Dict("display", "运行", "project", "项目", "search", "搜索"),
), Action: map[string]*ice.Action{

View File

@ -68,6 +68,9 @@ func (m *Message) Log(level string, str string, arg ...interface{}) *Message {
return m.log(level, str, arg...)
}
func (m *Message) Info(str string, arg ...interface{}) *Message {
if m == nil {
return m
}
return m.log(LOG_INFO, str, arg...)
}
func (m *Message) Cost(arg ...interface{}) *Message {