1
0
forked from x/ContextOS

vps syn 0.3.0 同步0.3.0修改

This commit is contained in:
shaoying 2017-11-25 16:28:19 +08:00
parent 72a361c4b7
commit be806e2e1d

View File

@ -18,13 +18,14 @@ type WEB struct {
*http.ServeMux *http.ServeMux
*http.Server *http.Server
m *ctx.Message M *ctx.Message
*ctx.Context *ctx.Context
} }
type MUX interface { type MUX interface {
Handle(string, http.Handler) Handle(string, http.Handler)
HandleFunc(string, func(http.ResponseWriter, *http.Request)) HandleFunc(string, func(http.ResponseWriter, *http.Request))
Trans(*ctx.Message, string, ...string)
} }
func (web *WEB) Trans(m *ctx.Message, key string, arg ...string) { // {{{ func (web *WEB) Trans(m *ctx.Message, key string, arg ...string) { // {{{
@ -55,10 +56,10 @@ func (web *WEB) Trans(m *ctx.Message, key string, arg ...string) { // {{{
func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{ func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{
if web.Master { if web.Master {
log.Println() log.Println()
log.Println(web.Name, r.RemoteAddr, r.Method, r.URL) web.M.Log("info", "%s: %v %s %s", web.Name, r.RemoteAddr, r.Method, r.URL)
defer log.Println() defer log.Println()
if web.m.Conf("logheaders") == "yes" { if web.M.Conf("logheaders") == "yes" {
for k, v := range r.Header { for k, v := range r.Header {
log.Println(k+":", v[0]) log.Println(k+":", v[0])
} }
@ -79,7 +80,7 @@ func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{
web.ServeMux.ServeHTTP(w, r) web.ServeMux.ServeHTTP(w, r)
if web.Master { if web.Master {
if web.m.Conf("logheaders") == "yes" { if web.M.Conf("logheaders") == "yes" {
for k, v := range w.Header() { for k, v := range w.Header() {
log.Println(k+":", v[0]) log.Println(k+":", v[0])
} }
@ -91,15 +92,16 @@ func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{
func (web *WEB) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{ func (web *WEB) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
web.Configs["logheaders"] = &ctx.Config{Name: "logheaders", Value: "yes", Help: "日志输出请求头"} web.Configs["logheaders"] = &ctx.Config{Name: "logheaders", Value: "yes", Help: "日志输出请求头"}
web.Configs["directory"] = &ctx.Config{Name: "directory", Value: "./", Help: "服务目录"} web.Configs["directory"] = &ctx.Config{Name: "directory", Value: "usr", Help: "服务目录"}
web.Configs["protocol"] = &ctx.Config{Name: "protocol", Value: "http", Help: "服务协议"} web.Configs["protocol"] = &ctx.Config{Name: "protocol", Value: "http", Help: "服务协议"}
web.Configs["address"] = &ctx.Config{Name: "address", Value: ":9393", Help: "监听地址"} web.Configs["address"] = &ctx.Config{Name: "address", Value: ":9393", Help: "监听地址"}
web.Configs["route"] = &ctx.Config{Name: "route", Value: "/" + web.Name + "/", Help: "请求路径"} web.Configs["route"] = &ctx.Config{Name: "route", Value: "/" + web.Name + "/", Help: "请求路径"}
web.ServeMux = http.NewServeMux() web.ServeMux = http.NewServeMux()
mux := m.Target.Server.(MUX)
for k, _ := range web.Commands { for k, _ := range web.Commands {
if k[0] == '/' { if k[0] == '/' {
web.Trans(m.Spawn(web.Context), k) mux.Trans(m, k)
} }
} }
@ -108,18 +110,20 @@ func (web *WEB) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
// }}} // }}}
func (web *WEB) Start(m *ctx.Message, arg ...string) bool { // {{{ func (web *WEB) Start(m *ctx.Message, arg ...string) bool { // {{{
web.m = m web.M = m
if !web.Run { if !web.Run {
web.Run = true web.Run = true
if s, ok := web.Context.Context.Server.(MUX); ok { if s, ok := m.Target.Context.Server.(MUX); ok {
log.Println(web.Context.Name, "route:", m.Conf("route"), "->", web.Name) if h, ok := m.Target.Server.(http.Handler); ok {
s.Handle(m.Conf("route"), http.StripPrefix(path.Dir(m.Conf("route")), web)) m.Log("info", "%s: route %s -> %s", web.Context.Context.Name, m.Conf("route"), web.Name)
s.Handle(m.Conf("route"), http.StripPrefix(path.Dir(m.Conf("route")), h))
}
} }
if m.Conf("directory") != "" { if m.Conf("directory") != "" {
log.Println(web.Name, "directory:", m.Conf("directory")) m.Log("info", "%s: directory [%s]", web.Name, m.Conf("directory"))
web.Handle("/", http.FileServer(http.Dir(m.Conf("directory")))) web.Handle("/", http.FileServer(http.Dir(m.Conf("directory"))))
} }
@ -140,13 +144,14 @@ func (web *WEB) Start(m *ctx.Message, arg ...string) bool { // {{{
} }
web.Server = &http.Server{Addr: m.Conf("address"), Handler: web} web.Server = &http.Server{Addr: m.Conf("address"), Handler: web}
log.Println(web.Name, "protocol:", m.Conf("protocol"))
log.Println(web.Name, "address:", m.Conf("address")) m.Log("info", "%s: protocol [%s]", web.Name, m.Conf("protocol"))
m.Log("info", "%s: address [%s]", web.Name, m.Conf("address"))
web.Master = true web.Master = true
if m.Conf("protocol") == "https" { if m.Conf("protocol") == "https" {
log.Println(web.Name, "cert:", m.Conf("cert")) m.Log("info", "%s: cert [%s]", web.Name, m.Conf("cert"))
log.Println(web.Name, "key:", m.Conf("key")) m.Log("info", "%s: key [%s]", web.Name, m.Conf("key"))
web.Server.ListenAndServeTLS(m.Conf("cert"), m.Conf("key")) web.Server.ListenAndServeTLS(m.Conf("cert"), m.Conf("key"))
} else { } else {
web.Server.ListenAndServe() web.Server.ListenAndServe()
@ -159,7 +164,6 @@ func (web *WEB) Start(m *ctx.Message, arg ...string) bool { // {{{
func (web *WEB) Spawn(c *ctx.Context, m *ctx.Message, arg ...string) ctx.Server { // {{{ func (web *WEB) Spawn(c *ctx.Context, m *ctx.Message, arg ...string) ctx.Server { // {{{
c.Caches = map[string]*ctx.Cache{} c.Caches = map[string]*ctx.Cache{}
c.Configs = map[string]*ctx.Config{} c.Configs = map[string]*ctx.Config{}
c.Commands = map[string]*ctx.Command{}
s := new(WEB) s := new(WEB)
s.Context = c s.Context = c
@ -208,14 +212,10 @@ var Index = &ctx.Context{Name: "web", Help: "网页服务",
// }}} // }}}
}}, }},
"/hi": &ctx.Command{Name: "/hi", Help: "添加响应", Hand: func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string { "/hi": &ctx.Command{Name: "/hi", Help: "添加响应", Hand: func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string {
m.Echo("hello") m.Echo("hello\n")
m.Echo("hello")
m.Echo("hello")
m.Echo("hello")
m.Add("append", "hi", "hello") m.Add("append", "hi", "hello")
m.Add("append", "hi", "hello") m.Add("append", "hi", "hello")
log.Println(m.Meta) return "hello\n"
return "hello"
}}, }},
}, },
} }