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 & server
|
||||||
alias * message
|
alias * message
|
||||||
|
|
||||||
~web
|
@debug on
|
||||||
listen :9393 etc
|
|
||||||
|
~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) { // {{{
|
func (c *Context) Safe(m *Message, hand ...func(c *Context, m *Message)) (ok bool) { // {{{
|
||||||
defer func() {
|
defer func() {
|
||||||
if e := recover(); e != nil {
|
if e := recover(); e != nil {
|
||||||
if c.Conf("debug") == "on" {
|
|
||||||
log.Println(c.Name, "error:", e)
|
log.Println(c.Name, "error:", e)
|
||||||
|
if c.Conf("debug") == "on" {
|
||||||
if e != io.EOF {
|
if e != io.EOF {
|
||||||
debug.PrintStack()
|
debug.PrintStack()
|
||||||
}
|
}
|
||||||
|
@ -2,46 +2,66 @@ package web // {{{
|
|||||||
// }}}
|
// }}}
|
||||||
import ( // {{{
|
import ( // {{{
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path"
|
||||||
)
|
)
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
type WEB struct {
|
type WEB struct {
|
||||||
|
hands map[string]bool
|
||||||
|
Mux *http.ServeMux
|
||||||
*ctx.Context
|
*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 { // {{{
|
func (web *WEB) Begin(m *ctx.Message) ctx.Server { // {{{
|
||||||
return web
|
return web
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
func (web *WEB) Start(m *ctx.Message) bool { // {{{
|
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())
|
log.Println(s.ListenAndServe())
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
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{}
|
||||||
"status": &ctx.Cache{Name: "status", Value: "stop", Help: "服务状态"},
|
c.Configs = map[string]*ctx.Config{}
|
||||||
}
|
|
||||||
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.Commands = map[string]*ctx.Command{}
|
c.Commands = map[string]*ctx.Command{}
|
||||||
|
|
||||||
s := new(WEB)
|
s := new(WEB)
|
||||||
@ -56,20 +76,23 @@ func (web *WEB) Exit(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
var Index = &ctx.Context{Name: "web", Help: "网页服务", // {{{
|
var Index = &ctx.Context{Name: "web", Help: "网页服务",
|
||||||
Caches: map[string]*ctx.Cache{},
|
Caches: map[string]*ctx.Cache{
|
||||||
Configs: map[string]*ctx.Config{},
|
"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{
|
Commands: map[string]*ctx.Command{
|
||||||
"listen": &ctx.Command{"listen address directory", "设置监听地址和目录", func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string {
|
"listen": &ctx.Command{"listen address directory", "设置监听地址和目录", func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string {
|
||||||
switch len(arg) { // {{{
|
switch len(arg) { // {{{
|
||||||
case 0:
|
case 0:
|
||||||
default:
|
default:
|
||||||
m.Add("detail", "/hi")
|
c.Conf("address", arg[0])
|
||||||
m.Put("/hi", func(w http.ResponseWriter, r *http.Request) {
|
c.Conf("directory", arg[1])
|
||||||
w.Write([]byte("hello context world!"))
|
go c.Start(m)
|
||||||
})
|
|
||||||
|
|
||||||
m.Start(m.Meta["detail"][1:]...)
|
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
// }}}
|
// }}}
|
||||||
@ -77,12 +100,12 @@ var Index = &ctx.Context{Name: "web", Help: "网页服务", // {{{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
|
||||||
|
|
||||||
func init() { // {{{
|
func init() { // {{{
|
||||||
web := &WEB{}
|
web := &WEB{}
|
||||||
web.Context = Index
|
web.Context = Index
|
||||||
ctx.Index.Register(Index, web)
|
ctx.Index.Register(Index, web)
|
||||||
|
|
||||||
|
web.Mux = http.NewServeMux()
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
|
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
_ "context/cli"
|
_ "context/cli"
|
||||||
|
_ "context/web/mp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user