mirror of
https://shylinux.com/x/icebergs
synced 2025-04-28 18:22:02 +08:00
opt some
This commit is contained in:
parent
77bf29e83c
commit
fa4a74af53
@ -1,6 +1,9 @@
|
|||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
ice "github.com/shylinux/icebergs"
|
ice "github.com/shylinux/icebergs"
|
||||||
"github.com/shylinux/icebergs/base/aaa"
|
"github.com/shylinux/icebergs/base/aaa"
|
||||||
"github.com/shylinux/icebergs/base/cli"
|
"github.com/shylinux/icebergs/base/cli"
|
||||||
@ -16,6 +19,51 @@ import (
|
|||||||
"strings"
|
"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 {
|
func _serve_main(m *ice.Message, w http.ResponseWriter, r *http.Request) bool {
|
||||||
if r.Header.Get("index.module") != "" {
|
if r.Header.Get("index.module") != "" {
|
||||||
return true
|
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)
|
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.module", m.Target().Name)
|
||||||
r.Header.Set("index.path", r.URL.Path)
|
r.Header.Set("index.path", r.URL.Path)
|
||||||
|
@ -48,6 +48,8 @@ const (
|
|||||||
SPIDE_MSG = "msg"
|
SPIDE_MSG = "msg"
|
||||||
SPIDE_SAVE = "save"
|
SPIDE_SAVE = "save"
|
||||||
SPIDE_CACHE = "cache"
|
SPIDE_CACHE = "cache"
|
||||||
|
SPIDE_PROXY = "proxy"
|
||||||
|
SPIDE_CB = "spide.cb"
|
||||||
|
|
||||||
SPIDE_GET = "GET"
|
SPIDE_GET = "GET"
|
||||||
SPIDE_PUT = "PUT"
|
SPIDE_PUT = "PUT"
|
||||||
@ -113,7 +115,7 @@ func init() {
|
|||||||
// 缓存数据
|
// 缓存数据
|
||||||
cache, save := "", ""
|
cache, save := "", ""
|
||||||
switch arg[1] {
|
switch arg[1] {
|
||||||
case SPIDE_RAW:
|
case SPIDE_RAW, SPIDE_PROXY:
|
||||||
cache, arg = arg[1], arg[1:]
|
cache, arg = arg[1], arg[1:]
|
||||||
case SPIDE_MSG:
|
case SPIDE_MSG:
|
||||||
cache, arg = arg[1], arg[1:]
|
cache, arg = arg[1], arg[1:]
|
||||||
@ -251,6 +253,13 @@ func init() {
|
|||||||
// 检查结果
|
// 检查结果
|
||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
m.Cost("status", res.Status, "length", res.Header.Get(ContentLength), "type", res.Header.Get(ContentType))
|
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) {
|
if m.Warn(res.StatusCode != http.StatusOK, res.Status) {
|
||||||
m.Set(ice.MSG_RESULT)
|
m.Set(ice.MSG_RESULT)
|
||||||
// return
|
// return
|
||||||
@ -264,6 +273,11 @@ func init() {
|
|||||||
|
|
||||||
// 解析引擎
|
// 解析引擎
|
||||||
switch cache {
|
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:
|
case SPIDE_CACHE:
|
||||||
m.Optionv(RESPONSE, res)
|
m.Optionv(RESPONSE, res)
|
||||||
m.Cmdy(CACHE, DOWNLOAD, res.Header.Get(ContentType), uri)
|
m.Cmdy(CACHE, DOWNLOAD, res.Header.Get(ContentType), uri)
|
||||||
|
@ -17,7 +17,7 @@ const VIMER = "vimer"
|
|||||||
func init() {
|
func init() {
|
||||||
Index.Merge(&ice.Context{
|
Index.Merge(&ice.Context{
|
||||||
Commands: map[string]*ice.Command{
|
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",
|
"display", "/plugin/local/code/vimer.js", "style", "editor",
|
||||||
"trans", kit.Dict("display", "运行", "project", "项目", "search", "搜索"),
|
"trans", kit.Dict("display", "运行", "project", "项目", "search", "搜索"),
|
||||||
), Action: map[string]*ice.Action{
|
), Action: map[string]*ice.Action{
|
||||||
|
3
logs.go
3
logs.go
@ -68,6 +68,9 @@ func (m *Message) Log(level string, str string, arg ...interface{}) *Message {
|
|||||||
return m.log(level, str, arg...)
|
return m.log(level, str, arg...)
|
||||||
}
|
}
|
||||||
func (m *Message) Info(str string, arg ...interface{}) *Message {
|
func (m *Message) Info(str string, arg ...interface{}) *Message {
|
||||||
|
if m == nil {
|
||||||
|
return m
|
||||||
|
}
|
||||||
return m.log(LOG_INFO, str, arg...)
|
return m.log(LOG_INFO, str, arg...)
|
||||||
}
|
}
|
||||||
func (m *Message) Cost(arg ...interface{}) *Message {
|
func (m *Message) Cost(arg ...interface{}) *Message {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user