1
0
mirror of https://shylinux.com/x/ContextOS synced 2025-04-25 16:58:06 +08:00

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
~tcp
~web
listen :9393
~aaa
login shy shy

View File

@ -2,7 +2,6 @@ package web // {{{
// }}}
import ( // {{{
"context"
"fmt"
"log"
"net/http"
"path"
@ -11,56 +10,18 @@ import ( // {{{
// }}}
type WEB struct {
Server *http.Server
Mux *http.ServeMux
hands map[string]bool
Run bool
run bool
*http.ServeMux
*http.Server
*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) { // {{{
log.Println()
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 { // {{{
if !web.run {
if !web.Run {
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"))
}
for _, v := range web.Contexts {
if s, ok := v.Server.(*WEB); ok && s.Mux != nil {
log.Println(web.Name, "route:", s.Conf("route"), "->", s.Name)
web.Handle(s.Conf("route"), s.Mux)
s.Start(m)
if s, ok := v.Server.(http.Handler); ok {
log.Println(web.Name, "route:", v.Conf("route"), "->", v.Name)
web.Handle(v.Conf("route"), http.StripPrefix(path.Dir(v.Conf("route")), s))
v.Start(m)
}
}
}
web.run = true
web.Run = true
if m.Target != web.Context {
return true
@ -166,7 +127,7 @@ func init() {
web.Context = Index
ctx.Index.Register(Index, web)
web.Mux = http.NewServeMux()
web.ServeMux = http.NewServeMux()
web.HandleFunc("/hi", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(web.Conf("default")))
})

View File

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