1
0
forked from x/ContextOS

mac mod web 修改了继承关系

This commit is contained in:
shaoying 2017-11-07 22:09:42 +08:00
parent d57a74196a
commit 75a8b5e544
3 changed files with 14 additions and 54 deletions

View File

@ -1,5 +1,3 @@
@debug off @debug off
~tcp ~web
listen :9393 listen :9393
~aaa
login shy shy

View File

@ -2,7 +2,6 @@ package web // {{{
// }}} // }}}
import ( // {{{ import ( // {{{
"context" "context"
"fmt"
"log" "log"
"net/http" "net/http"
"path" "path"
@ -11,56 +10,18 @@ import ( // {{{
// }}} // }}}
type WEB struct { type WEB struct {
Server *http.Server Run bool
Mux *http.ServeMux
hands map[string]bool
run bool *http.ServeMux
*http.Server
*ctx.Context *ctx.Context
} }
func (web *WEB) Handle(p string, h http.Handler) { // {{{
if web.hands == nil {
web.hands = make(map[string]bool)
}
p = path.Clean(p)
if _, ok := web.hands[p]; ok {
panic(fmt.Sprintln(web.Name, "handle exits", p))
}
web.hands[p] = true
if p == "/" {
panic(fmt.Sprintln(web.Name, "handle exits", p))
}
web.Mux.Handle(p+"/", http.StripPrefix(p, h))
}
// }}}
func (web *WEB) HandleFunc(p string, h func(http.ResponseWriter, *http.Request)) { // {{{
if web.hands == nil {
web.hands = make(map[string]bool)
}
p = path.Clean(p)
if _, ok := web.hands[p]; ok {
panic(fmt.Sprintln(web.Name, "handle exits", p))
}
web.hands[p] = true
if p == "/" {
panic(fmt.Sprintln(web.Name, "handle exits", p))
}
web.Mux.HandleFunc(p, h)
log.Println(web.Name, "hand:", p)
}
// }}}
func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{ func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{
log.Println() log.Println()
log.Println(web.Name, r.RemoteAddr, r.Method, r.URL.Path) log.Println(web.Name, r.RemoteAddr, r.Method, r.URL.Path)
web.Mux.ServeHTTP(w, r) web.ServeMux.ServeHTTP(w, r)
} }
// }}} // }}}
@ -72,21 +33,21 @@ func (web *WEB) Begin(m *ctx.Message) ctx.Server { // {{{
// }}} // }}}
func (web *WEB) Start(m *ctx.Message) bool { // {{{ func (web *WEB) Start(m *ctx.Message) bool { // {{{
if !web.run { if !web.Run {
if web.Conf("directory") != "" { if web.Conf("directory") != "" {
web.Mux.Handle("/", http.FileServer(http.Dir(web.Conf("directory")))) web.Handle("/", http.FileServer(http.Dir(web.Conf("directory"))))
log.Println(web.Name, "directory:", web.Conf("directory")) log.Println(web.Name, "directory:", web.Conf("directory"))
} }
for _, v := range web.Contexts { for _, v := range web.Contexts {
if s, ok := v.Server.(*WEB); ok && s.Mux != nil { if s, ok := v.Server.(http.Handler); ok {
log.Println(web.Name, "route:", s.Conf("route"), "->", s.Name) log.Println(web.Name, "route:", v.Conf("route"), "->", v.Name)
web.Handle(s.Conf("route"), s.Mux) web.Handle(v.Conf("route"), http.StripPrefix(path.Dir(v.Conf("route")), s))
s.Start(m) v.Start(m)
} }
} }
} }
web.run = true web.Run = true
if m.Target != web.Context { if m.Target != web.Context {
return true return true
@ -166,7 +127,7 @@ func init() {
web.Context = Index web.Context = Index
ctx.Index.Register(Index, web) ctx.Index.Register(Index, web)
web.Mux = http.NewServeMux() web.ServeMux = http.NewServeMux()
web.HandleFunc("/hi", func(w http.ResponseWriter, r *http.Request) { web.HandleFunc("/hi", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(web.Conf("default"))) w.Write([]byte(web.Conf("default")))
}) })

View File

@ -6,6 +6,7 @@ import (
_ "context/cli" _ "context/cli"
_ "context/tcp" _ "context/tcp"
_ "context/web" _ "context/web"
_ "context/web/mp"
) )
func main() { func main() {