web

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 30, 2018 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Index = &ctx.Context{Name: "web", Help: "应用中心",
	Caches: map[string]*ctx.Cache{
		"nserve": &ctx.Cache{Name: "nserve", Value: "0", Help: "主机数量"},
		"nroute": &ctx.Cache{Name: "nroute", Value: "0", Help: "路由数量"},
	},
	Configs: map[string]*ctx.Config{},
	Commands: map[string]*ctx.Command{
		"client": &ctx.Command{
			Name: "client address [output [editor]]",
			Help: "添加请求配置, address: 默认地址, output: 输出路径, editor: 编辑器",
			Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
				if _, e := m.Target().Server.(*WEB); m.Assert(e) {
					if len(arg) == 0 {
						return
					}

					uri, e := url.Parse(arg[0])
					m.Assert(e)
					m.Conf("method", "method", "GET", "请求方法")
					m.Conf("protocol", "protocol", uri.Scheme, "服务协议")
					m.Conf("hostname", "hostname", uri.Host, "服务主机")

					dir, file := path.Split(uri.EscapedPath())
					m.Conf("path", "path", dir, "服务路由")
					m.Conf("file", "file", file, "服务文件")
					m.Conf("query", "query", uri.RawQuery, "服务参数")

					if m.Conf("output", "output", "stdout", "文件缓存"); len(arg) > 1 {
						m.Conf("output", arg[1])
					}
					if m.Conf("editor", "editor", "vim", "文件编辑器"); len(arg) > 2 {
						m.Conf("editor", arg[2])
					}
				}
			}},
		"cookie": &ctx.Command{
			Name: "cookie [create]|[name [value]]",
			Help: "读写请求的Cookie, name: 变量名, value: 变量值",
			Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
				if web, ok := m.Target().Server.(*WEB); m.Assert(ok) {
					switch len(arg) {
					case 0:
						for k, v := range web.cookie {
							m.Echo("%s: %v\n", k, v.Value)
						}
					case 1:
						if arg[0] == "create" {
							web.cookie = make(map[string]*http.Cookie)
							break
						}
						if v, ok := web.cookie[arg[0]]; ok {
							m.Echo("%s", v.Value)
						}
					default:
						if web.cookie == nil {
							web.cookie = make(map[string]*http.Cookie)
						}
						if v, ok := web.cookie[arg[0]]; ok {
							v.Value = arg[1]
						} else {
							web.cookie[arg[0]] = &http.Cookie{Name: arg[0], Value: arg[1]}
						}
					}
				}
			}},
		"get": &ctx.Command{
			Name: "get [method GET|POST] [file name filename] url arg...",
			Help: "访问服务, method: 请求方法, file: 发送文件, url: 请求地址, arg: 请求参数",
			Form: map[string]int{"method": 1, "file": 2, "type": 1, "body": 1},
			Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
				if web, ok := m.Target().Server.(*WEB); m.Assert(ok) {
					if web.client == nil {
						web.client = &http.Client{}
					}

					method := m.Confx("method")
					uri := web.Merge(m, arg[0], arg[1:]...)
					m.Log("info", "%s %s", method, uri)
					m.Echo("%s: %s\n", method, uri)

					var body io.Reader
					index := strings.Index(uri, "?")
					contenttype := ""

					switch method {
					case "POST":
						if m.Options("file") {
							file, e := os.Open(m.Meta["file"][1])
							m.Assert(e)
							defer file.Close()

							buf := &bytes.Buffer{}
							writer := multipart.NewWriter(buf)

							part, e := writer.CreateFormFile(m.Option("file"), filepath.Base(m.Meta["file"][1]))
							m.Assert(e)
							io.Copy(part, file)

							for i := 0; i < len(arg)-1; i += 2 {
								value := arg[i+1]
								if len(arg[i+1]) > 1 {
									switch arg[i+1][0] {
									case '$':
										value = m.Cap(arg[i+1][1:])
									case '@':
										value = m.Conf(arg[i+1][1:])
									}
								}
								writer.WriteField(arg[i], value)
							}

							contenttype = writer.FormDataContentType()
							body = buf
							writer.Close()
						} else if m.Option("type") == "json" {
							if m.Options("body") {
								data := []interface{}{}
								for _, v := range arg[1:] {
									data = append(data, v)
								}
								m.Log("body", "%v", fmt.Sprintf(m.Option("body"), data...))
								body = strings.NewReader(fmt.Sprintf(m.Option("body"), data...))
							} else {
								data := map[string]string{}
								for i := 1; i < len(arg)-1; i++ {
									data[arg[i]] = arg[i+1]
								}

								b, e := json.Marshal(data)
								m.Assert(e)
								body = bytes.NewReader(b)
							}

							contenttype = "application/json"
							if index > -1 {
								uri = uri[:index]
							}

						} else if index > 0 {
							contenttype = "application/x-www-form-urlencoded"
							body = strings.NewReader(uri[index+1:])
							uri = uri[:index]
						}
					}

					m.Log("info", "content-type: %s", contenttype)
					req, e := http.NewRequest(method, uri, body)
					m.Assert(e)

					if len(contenttype) > 0 {
						req.Header.Set("Content-Type", contenttype)
					}

					for _, v := range web.cookie {
						req.AddCookie(v)
					}

					res, e := web.client.Do(req)
					m.Assert(e)

					if web.cookie == nil {
						web.cookie = make(map[string]*http.Cookie)
					}
					for _, v := range res.Cookies() {
						web.cookie[v.Name] = v
					}

					for k, v := range res.Header {
						m.Log("info", "%s: %v", k, v)
					}

					if m.Confs("output") {
						if _, e := os.Stat(m.Conf("output")); e == nil {
							name := path.Join(m.Conf("output"), fmt.Sprintf("%d", time.Now().Unix()))
							f, e := os.Create(name)
							m.Assert(e)
							io.Copy(f, res.Body)
							if m.Confs("editor") {
								cmd := exec.Command(m.Conf("editor"), name)
								cmd.Stdin = os.Stdin
								cmd.Stdout = os.Stdout
								cmd.Stderr = os.Stderr
								cmd.Run()
							} else {
								m.Echo("write to %s\n", name)
							}
							return
						}
					}

					buf, e := ioutil.ReadAll(res.Body)
					m.Assert(e)

					if res.Header.Get("Content-Type") == "application/json" {
						result := map[string]interface{}{}
						json.Unmarshal(buf, &result)
						for k, v := range result {
							switch value := v.(type) {
							case string:
								m.Append(k, value)
							case float64:
								m.Append(k, fmt.Sprintf("%d", int(value)))
							default:
								m.Put("append", k, value)
							}
						}
					}

					result := string(buf)
					m.Echo(result)
					m.Append("response", result)
				}
			}},
		"post": &ctx.Command{Name: "post", Help: "访问服务",
			Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
				msg := m.Spawn().Cmd("get", "method", "POST", arg)
				m.Copy(msg, "result").Copy(msg, "append")
			}},
		"serve": &ctx.Command{
			Name: "serve [directory [address [protocol]]]",
			Help: "启动服务, directory: 服务路径, address: 服务地址, protocol: 服务协议(https/http)",
			Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
				m.Set("detail", arg...).Target().Start(m)
			}},
		"route": &ctx.Command{
			Name: "route script|template|directory route content",
			Help: "添加响应, script: 脚本响应, template: 模板响应, directory: 目录响应, route: 请求路由, content: 响应内容",
			Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
				if mux, ok := m.Target().Server.(MUX); m.Assert(ok) {
					switch len(arg) {
					case 0:
						for k, v := range m.Target().Commands {
							if k[0] == '/' {
								m.Echo("%s: %s\n", k, v.Name)
							}
						}
					case 1:
						for k, v := range m.Target().Commands {
							if k == arg[0] {
								m.Echo("%s: %s\n%s", k, v.Name, v.Help)
							}
						}
					case 3:
						switch arg[0] {
						case "script":
							mux.Trans(m, arg[1], func(m *ctx.Message, c *ctx.Context, key string, a ...string) {
								msg := m.Find("cli").Cmd("source", arg[2])
								m.Copy(msg, "result").Copy(msg, "append")
							})
						case "template":
							mux.Trans(m, arg[1], func(m *ctx.Message, c *ctx.Context, key string, a ...string) {
								w := m.Optionv("response").(http.ResponseWriter)
								if _, e := os.Stat(arg[2]); e == nil {
									template.Must(template.ParseGlob(arg[2])).Execute(w, m)
								} else {
									template.Must(template.New("temp").Parse(arg[2])).Execute(w, m)
								}
							})
						case "directory":
							mux.Handle(arg[1]+"/", http.StripPrefix(arg[1], http.FileServer(http.Dir(arg[2]))))
						}
					}
				}
			}},
		"upload": &ctx.Command{Name: "upload file", Help: "上传文件", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
			msg := m.Spawn(m.Target())
			msg.Cmd("get", "/upload", "method", "POST", "file", "file", arg[0])
			m.Copy(msg, "result")

		}},
		"/travel": &ctx.Command{Name: "/travel", Help: "文件上传", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {

			if !m.Options("dir") {
				m.Option("dir", "ctx")
			}

			check := m.Spawn().Cmd("/share", "/travel", "dir", m.Option("dir"))
			if !check.Results(0) {
				m.Copy(check, "append")
				return
			}

			if m.Option("method") == "POST" {
				if m.Options("domain") {
					msg := m.Find("ssh", true)
					msg.Detail(0, "send", "domain", m.Option("domain"), "context", "find", m.Option("dir"))
					if m.Option("name") != "" {
						msg.Add("detail", m.Option("name"))
					}
					if m.Options("value") {
						value := []string{}
						json.Unmarshal([]byte(m.Option("value")), &value)
						if len(value) > 0 {
							msg.Add("detail", value[0], value[1:])
						}
					}

					msg.CallBack(true, func(sub *ctx.Message) *ctx.Message {
						m.Copy(sub, "result").Copy(sub, "append")
						return nil
					})
					return
				}

				msg := m.Find(m.Option("dir"), true)
				if msg == nil {
					return
				}

				switch m.Option("ccc") {
				case "cache":
					m.Echo(msg.Cap(m.Option("name")))
				case "config":
					if m.Has("value") {
						m.Echo(msg.Conf(m.Option("name"), m.Option("value")))
					} else {
						m.Echo(msg.Conf(m.Option("name")))
					}
				case "command":
					msg = msg.Spawn(msg.Target())
					msg.Detail(0, m.Option("name"))
					if m.Options("value") {
						value := []string{}
						json.Unmarshal([]byte(m.Option("value")), &value)
						if len(value) > 0 {
							msg.Add("detail", value[0], value[1:])
						}
					}

					msg.Cmd()
					m.Copy(msg, "result").Copy(msg, "append")
				}
				return
			}

			m.Set("append", "tmpl", "userinfo", "share")
			msg := m
			for _, v := range []string{"cache", "config", "command", "module", "domain"} {
				if m.Options("domain") {
					msg = m.Find("ssh", true)
					msg.Detail(0, "send", "domain", m.Option("domain"), "context", "find", m.Option("dir"), "list", v)
					msg.CallBack(true, func(sub *ctx.Message) *ctx.Message {
						msg.Copy(sub, "result").Copy(sub, "append")
						return nil
					})
				} else {
					msg = m.Spawn()
					msg.Cmd("context", "find", msg.Option("dir"), "list", v)
				}

				if len(msg.Meta["append"]) > 0 {
					msg.Option("current_module", m.Option("dir"))
					msg.Option("current_domain", m.Option("domain"))
					m.Add("option", "tmpl", v)
					m.Sess(v, msg)
				}
			}
			m.Append("template", m.Conf("travel_main"), m.Conf("travel_tmpl"))

		}},
		"/upload": &ctx.Command{Name: "/upload", Help: "文件上传", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
			r := m.Optionv("request").(*http.Request)
			w := m.Optionv("response").(http.ResponseWriter)

			if !m.Options("dir") {
				m.Option("dir", m.Cap("directory"))
			}

			check := m.Spawn().Cmd("/share", "/upload", "dir", m.Option("dir"))
			if !check.Results(0) {
				m.Copy(check, "append")
				return
			}
			aaa := check.Appendv("aaa").(*ctx.Message)

			s, e := os.Stat(m.Option("dir"))
			if m.Assert(e); !s.IsDir() {
				http.ServeFile(w, r, m.Option("dir"))
				return
			}

			share := m.Sess("share", m.Target())
			index := share.Target().Index
			if index != nil && index[aaa.Append("username")] != nil {
				for k, v := range index[aaa.Append("username")].Index {
					for _, j := range v.Commands {
						for _, n := range j.Shares {
							for _, nn := range n {
								if match, e := regexp.MatchString(nn, m.Option("dir")); m.Assert(e) && match {
									share.Add("append", "group", k)
									share.Add("append", "value", nn)
									share.Add("append", "delete", "delete")
								}
							}
						}
					}
				}
			}
			share.Sort("value", "string")
			share.Sort("argument", "string")

			fs, e := ioutil.ReadDir(m.Option("dir"))
			m.Assert(e)
			fs = append(fs, s)
			list := m.Sess("list", m.Target())
			list.Option("dir", m.Option("dir"))

			for _, v := range fs {
				name := v.Name()
				if v == s {
					if name == m.Option("dir") {
						continue
					}
					name = ".."
				} else if name[0] == '.' {
					continue
				}

				list.Add("append", "time_i", fmt.Sprintf("%d", v.ModTime().Unix()))
				list.Add("append", "size_i", fmt.Sprintf("%d", v.Size()))
				list.Add("append", "time", v.ModTime().Format("2006-01-02 15:04:05"))
				list.Add("append", "size", kit.FmtSize(v.Size()))

				if v.IsDir() {
					name += "/"
					list.Add("append", "type", "dir")
				} else {
					list.Add("append", "type", "file")
				}

				list.Add("append", "name", name)
				list.Add("append", "path", path.Join(m.Option("dir"), name))
			}

			max := true
			if i, e := strconv.Atoi(m.Option("sort_order")); e == nil {
				max = i%2 == 1
			}
			switch m.Option("list") {
			case "name":
				if max {
					list.Sort(m.Option("list"), "string")
				} else {
					list.Sort(m.Option("list"), "string_r")
				}
			case "size", "time":
				if max {
					list.Sort(m.Option("list")+"_i", "int")
				} else {
					list.Sort(m.Option("list")+"_i", "int_r")
				}
			}
			list.Meta["append"] = list.Meta["append"][2:]
			delete(list.Meta, "time_i")
			delete(list.Meta, "size_i")

			switch m.Option("cmd") {
			case "git":
				git := m.Sess("git", m.Target())

				branch := m.Find("nfs").Cmd("git", "-C", m.Option("dir"), "branch")
				git.Option("branch", branch.Result(0))

				status := m.Find("nfs").Cmd("git", "-C", m.Option("dir"), "status")
				git.Option("status", status.Result(0))
			}

			m.Append("title", "upload")
			m.Append("tmpl", "userinfo", "share", "list", "git", "upload", "create")
			m.Append("template", m.Conf("upload_main"), m.Conf("upload_tmpl"))

		}},
		"/create": &ctx.Command{Name: "/create", Help: "创建目录或文件", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
			if check := m.Spawn().Cmd("/share", "/upload", "dir", m.Option("dir")); !check.Results(0) {
				m.Copy(check, "append")
				return
			}

			r := m.Optionv("request").(*http.Request)
			if m.Option("method") == "POST" {
				if m.Options("filename") {
					name := path.Join(m.Option("dir"), m.Option("filename"))
					if _, e := os.Stat(name); e != nil {
						if m.Options("content") {
							f, e := os.Create(name)
							m.Assert(e)
							defer f.Close()

							_, e = f.WriteString(m.Option("content"))
							m.Assert(e)
						} else {
							e = os.Mkdir(name, 0766)
							m.Assert(e)
						}
						m.Append("message", name, " create success!")
					} else {
						m.Append("message", name, " already exist!")
					}
				} else {
					file, header, e := r.FormFile("file")
					m.Assert(e)

					name := path.Join(m.Option("dir"), header.Filename)

					if _, e := os.Stat(name); e != nil {
						f, e := os.Create(name)
						m.Assert(e)
						defer f.Close()

						_, e = io.Copy(f, file)
						m.Assert(e)
						m.Append("message", name, " upload success!")
					} else {
						m.Append("message", name, " already exist!")
					}
				}
			}
			m.Append("redirect", m.Option("referer"))

		}},
		"/share": &ctx.Command{Name: "/share", Help: "资源共享", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
			check := m.Spawn().Cmd("/check", "command", arg[0], arg[1], arg[2])
			if !check.Results(0) {
				m.Copy(check, "append")
				return
			}

			msg := check.Appendv("aaa").(*ctx.Message).Spawn(m.Target())
			if m.Options("shareto") {
				msg.Cmd("right", "add", m.Option("shareto"), "command", arg[0], arg[1], arg[2])
			}
			if m.Options("notshareto") {
				msg.Cmd("right", "del", m.Option("notshareto"), "command", arg[0], arg[1], arg[2])
			}
			m.Echo("ok")

		}},
		"/check": &ctx.Command{Name: "/check cache|config|command name args", Help: "权限检查, cache|config|command: 接口类型, name: 接口名称, args: 其它参数", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
			w := m.Optionv("response").(http.ResponseWriter)
			if login := m.Spawn().Cmd("/login"); login.Has("redirect") {
				aaa := m.Appendv("aaa").(*ctx.Message)
				if msg := m.Spawn().Cmd("right", "check", aaa.Cap("username"), arg); msg.Results(0) {
					m.Copy(login, "append").Echo(msg.Result(0))
					return
				}
				w.WriteHeader(http.StatusForbidden)
				m.Append("message", "please contact manager")
				m.Echo("no")
				return
			} else {
				m.Copy(login, "append").Echo("no")
			}

		}},
		"/login": &ctx.Command{Name: "/login", Help: "用户登录", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
			w := m.Optionv("response").(http.ResponseWriter)

			if m.Options("sessid") {
				if aaa := m.Find("aaa").Cmd("login", m.Option("sessid")); aaa.Results(0) {
					m.Append("redirect", m.Option("referer"))
					m.Appendv("aaa", aaa)
					return
				}
			}

			if m.Options("username") && m.Options("password") {
				if aaa := m.Find("aaa").Cmd("login", m.Option("username"), m.Option("password")); aaa.Results(0) {
					http.SetCookie(w, &http.Cookie{Name: "sessid", Value: aaa.Result(0)})
					m.Append("redirect", m.Option("referer"))
					m.Appendv("aaa", aaa)
					return
				}
			}

			w.WriteHeader(http.StatusUnauthorized)
			m.Append("template", "login.html")

		}},
		"/render": &ctx.Command{Name: "/render [main [tmpl]]", Help: "模板响应, main: 模板入口, tmpl: 附加模板", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
			w := m.Optionv("response").(http.ResponseWriter)

			tpl := template.Must(template.New("render").Funcs(ctx.CGI).ParseGlob(path.Join(m.Conf("template_dir"), m.Conf("common_tmpl"))))
			if len(arg) > 1 {
				tpl = template.Must(tpl.ParseGlob(path.Join(m.Conf("template_dir"), arg[1])))
			}

			main := m.Conf("common_main")
			if len(arg) > 0 {
				main = arg[0]
			}

			w.Header().Add("Content-Type", "text/html")
			m.Assert(tpl.ExecuteTemplate(w, main, m.Message()))

		}},
		"/json": &ctx.Command{Name: "/json", Help: "json响应", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
			w := m.Optionv("response").(http.ResponseWriter)

			meta := map[string][]string{}
			if len(m.Meta["append"]) > 0 {
				meta["append"] = m.Meta["append"]
				for _, v := range m.Meta["append"] {
					meta[v] = m.Meta[v]
				}
			}

			if b, e := json.Marshal(meta); m.Assert(e) {
				w.Header().Set("Content-Type", "application/javascript")
				w.Write(b)
			}

		}},
		"temp": &ctx.Command{Name: "temp", Help: "应用示例", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
			msg := m.Spawn(m.Target())
			question := []string{}
			for i := 1; i < 21; i++ {
				question = append(question, fmt.Sprintf("{\"type\":\"1001\",\"title\":{\"text\":\"第%d题\"}}", i))
			}
			qs := "[" + strings.Join(question, ",") + "]"

			msg.Cmd("get", "method", "POST", "evaluating_add/", "questions", qs)
			m.Add("append", "hi", "hello")
		}},
		"brow": &ctx.Command{Name: "brow url", Help: "浏览器网页", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
			url := fmt.Sprintf("http://localhost:9094")
			if len(arg) > 0 {
				url = arg[0]
			}
			m.Log("fucK", "os %v", runtime.GOOS)
			switch runtime.GOOS {
			case "windows":
				m.Find("cli").Cmd("system", "explorer", url)
			case "darwin":
				m.Find("cli").Cmd("system", "open", url)
			case "linux":
				m.Spawn().Cmd("open", url)
			}
		}},
	},
}

Functions

This section is empty.

Types

type MUX

type MUX interface {
	Handle(string, http.Handler)
	HandleFunc(string, func(http.ResponseWriter, *http.Request))
	Trans(*ctx.Message, string, func(*ctx.Message, *ctx.Context, string, ...string))
}

type WEB

type WEB struct {
	*http.ServeMux
	*http.Server

	*ctx.Message
	*ctx.Context
	// contains filtered or unexported fields
}

func (*WEB) Begin

func (web *WEB) Begin(m *ctx.Message, arg ...string) ctx.Server

}}}

func (*WEB) Close

func (web *WEB) Close(m *ctx.Message, arg ...string) bool

}}}

func (*WEB) Merge

func (web *WEB) Merge(m *ctx.Message, uri string, arg ...string) string

func (*WEB) ServeHTTP

func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request)

}}}

func (*WEB) Spawn

func (web *WEB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server

func (*WEB) Start

func (web *WEB) Start(m *ctx.Message, arg ...string) bool

}}}

func (*WEB) Trans

func (web *WEB) Trans(m *ctx.Message, key string, hand func(*ctx.Message, *ctx.Context, string, ...string))

}}}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL