forked from x/ContextOS
mac mod context/web 添加了路径分发器
This commit is contained in:
parent
7afce79a45
commit
44e9496f66
@ -5,5 +5,8 @@ alias $ cache
|
||||
alias & server
|
||||
alias * message
|
||||
|
||||
~web
|
||||
listen :9393 etc
|
||||
@debug on
|
||||
|
||||
~mp
|
||||
listen :9090 ./
|
||||
|
||||
|
@ -235,8 +235,8 @@ func (c *Context) Check(e error) bool { // {{{
|
||||
func (c *Context) Safe(m *Message, hand ...func(c *Context, m *Message)) (ok bool) { // {{{
|
||||
defer func() {
|
||||
if e := recover(); e != nil {
|
||||
log.Println(c.Name, "error:", e)
|
||||
if c.Conf("debug") == "on" {
|
||||
log.Println(c.Name, "error:", e)
|
||||
if e != io.EOF {
|
||||
debug.PrintStack()
|
||||
}
|
||||
|
@ -2,46 +2,66 @@ package web // {{{
|
||||
// }}}
|
||||
import ( // {{{
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"path"
|
||||
)
|
||||
|
||||
// }}}
|
||||
|
||||
type WEB struct {
|
||||
hands map[string]bool
|
||||
Mux *http.ServeMux
|
||||
*ctx.Context
|
||||
}
|
||||
|
||||
func (web *WEB) Handle(p string, h http.Handler) { // {{{
|
||||
if web.hands == nil {
|
||||
web.hands = make(map[string]bool)
|
||||
}
|
||||
|
||||
if _, ok := web.hands[p]; ok {
|
||||
panic(fmt.Sprintln(web.Name, "handle exits", p))
|
||||
}
|
||||
|
||||
web.Mux.Handle(path.Clean(p)+"/", http.StripPrefix(path.Clean(p), h))
|
||||
log.Println(web.Name, "mux:", path.Clean(p)+"/")
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (web *WEB) HandleFunc(p string, h func(http.ResponseWriter, *http.Request)) { // {{{
|
||||
if web.hands == nil {
|
||||
web.hands = make(map[string]bool)
|
||||
}
|
||||
|
||||
if _, ok := web.hands[p]; ok {
|
||||
panic(fmt.Sprintln(web.Name, "handle exits", p))
|
||||
}
|
||||
|
||||
web.Mux.HandleFunc(p, h)
|
||||
log.Println(web.Name, "mux:", p)
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (web *WEB) Begin(m *ctx.Message) ctx.Server { // {{{
|
||||
return web
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (web *WEB) Start(m *ctx.Message) bool { // {{{
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/", http.FileServer(http.Dir(web.Conf("directory"))))
|
||||
for _, v := range m.Meta["detail"][2:] {
|
||||
if h, ok := m.Data[v].(func(http.ResponseWriter, *http.Request)); ok {
|
||||
mux.HandleFunc(v, h)
|
||||
}
|
||||
}
|
||||
|
||||
s := &http.Server{Addr: web.Conf("address"), Handler: mux}
|
||||
s.ListenAndServe()
|
||||
|
||||
web.Mux.Handle("/", http.FileServer(http.Dir(web.Conf("directory"))))
|
||||
s := &http.Server{Addr: web.Conf("address"), Handler: web.Mux}
|
||||
log.Println(web.Name, "listen:", web.Conf("address"))
|
||||
log.Println(s.ListenAndServe())
|
||||
return true
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (web *WEB) Spawn(c *ctx.Context, m *ctx.Message, arg ...string) ctx.Server { // {{{
|
||||
c.Caches = map[string]*ctx.Cache{
|
||||
"status": &ctx.Cache{Name: "status", Value: "stop", Help: "服务状态"},
|
||||
}
|
||||
c.Configs = map[string]*ctx.Config{
|
||||
"address": &ctx.Config{Name: "listen", Value: arg[0], Help: "监听地址"},
|
||||
"directory": &ctx.Config{Name: "directory", Value: arg[1], Help: "服务目录"},
|
||||
}
|
||||
c.Caches = map[string]*ctx.Cache{}
|
||||
c.Configs = map[string]*ctx.Config{}
|
||||
c.Commands = map[string]*ctx.Command{}
|
||||
|
||||
s := new(WEB)
|
||||
@ -56,20 +76,23 @@ func (web *WEB) Exit(m *ctx.Message, arg ...string) bool { // {{{
|
||||
|
||||
// }}}
|
||||
|
||||
var Index = &ctx.Context{Name: "web", Help: "网页服务", // {{{
|
||||
Caches: map[string]*ctx.Cache{},
|
||||
Configs: map[string]*ctx.Config{},
|
||||
var Index = &ctx.Context{Name: "web", Help: "网页服务",
|
||||
Caches: map[string]*ctx.Cache{
|
||||
"status": &ctx.Cache{Name: "status", Value: "stop", Help: "服务状态"},
|
||||
},
|
||||
Configs: map[string]*ctx.Config{
|
||||
"address": &ctx.Config{Name: "listen", Value: ":9090", Help: "监听地址"},
|
||||
"directory": &ctx.Config{Name: "directory", Value: "./", Help: "服务目录"},
|
||||
"who": &ctx.Config{Name: "who", Value: "shylinux", Help: "服务目录"},
|
||||
},
|
||||
Commands: map[string]*ctx.Command{
|
||||
"listen": &ctx.Command{"listen address directory", "设置监听地址和目录", func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string {
|
||||
switch len(arg) { // {{{
|
||||
case 0:
|
||||
default:
|
||||
m.Add("detail", "/hi")
|
||||
m.Put("/hi", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("hello context world!"))
|
||||
})
|
||||
|
||||
m.Start(m.Meta["detail"][1:]...)
|
||||
c.Conf("address", arg[0])
|
||||
c.Conf("directory", arg[1])
|
||||
go c.Start(m)
|
||||
}
|
||||
return ""
|
||||
// }}}
|
||||
@ -77,12 +100,12 @@ var Index = &ctx.Context{Name: "web", Help: "网页服务", // {{{
|
||||
},
|
||||
}
|
||||
|
||||
// }}}
|
||||
|
||||
func init() { // {{{
|
||||
web := &WEB{}
|
||||
web.Context = Index
|
||||
ctx.Index.Register(Index, web)
|
||||
|
||||
web.Mux = http.NewServeMux()
|
||||
}
|
||||
|
||||
// }}}
|
||||
|
@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"context"
|
||||
_ "context/cli"
|
||||
_ "context/web/mp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user