2 Star 4 Fork 4

aosfather/myway-mock

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
fasthttp.go 2.88 KB
一键复制 编辑 原始数据 按行查看 历史
aosfather 提交于 2020-05-03 23:21 +08:00 . 增加log输出和增加日期的类型定义
package main
import (
"encoding/json"
"encoding/xml"
"fmt"
"github.com/valyala/fasthttp"
"io/ioutil"
"log"
"strings"
)
/*
基于fasthttp的实现
*/
type HttpServer struct {
port int
server *fasthttp.Server
dispatch *DispatchManager
}
func (this *HttpServer) Start() {
this.server = &fasthttp.Server{Handler: this.ServeHTTP}
if this.port <= 0 {
this.port = 80
}
addr := fmt.Sprintf("0.0.0.0:%d", this.port)
this.server.ListenAndServe(addr)
}
func (this *HttpServer) ServeHTTP(ctx *fasthttp.RequestCtx) {
//获取访问的url
url := string(ctx.Request.URI().RequestURI())
log.Println("access the url:", url)
domain := string(ctx.Request.Header.Host())
//处理favicon.ico
if url == "/favicon.ico" {
ico, _ := ioutil.ReadFile("favicon.ico")
ctx.Response.Header.Set(CONTENT_TYPE, "image/x-icon")
ctx.Response.SetBodyRaw(ico)
} else {
//通过dispatch,获取api的定义
api := this.dispatch.GetApi(domain, url)
if api == nil { //不存在的时候的处理
ctx.Response.Header.Set(CONTENT_TYPE, "text/html;charset=utf-8")
ctx.Response.SetBodyString("<b>the url not found!</b>")
ctx.Response.SetStatusCode(404)
log.Printf("the url %s not found\n", url)
} else {
//检查http method 看是否支持该类型
if api.IsSupportMethod(ParseHttpMethodType(string(ctx.Method()))) {
this.call(api, ctx)
} else {
//不支持的 http method 处理
ctx.Response.Header.Set(CONTENT_TYPE, "text/html;charset=utf-8")
ctx.Response.SetBodyString("<b>the method not support !</b>")
ctx.Response.SetStatusCode(405)
}
}
}
//设置服务器名称
ctx.Response.Header.Set("Server", SERVER_NAME)
}
const CONTENT_TYPE = "Content-Type"
const SERVER_NAME = "myway-mock"
func (this *HttpServer) call(api *Service, ctx *fasthttp.RequestCtx) {
//校验参数
contentType := string(ctx.Request.Header.ContentType())
var input map[string]interface{} = make(map[string]interface{})
//不支持文件流
if ctx.Request.IsBodyStream() {
ctx.Response.SetBodyString("<b>not surpport stream!</b>")
ctx.Response.SetStatusCode(400)
return
}
//json格式请求处理
if strings.Contains(contentType, "application/json") {
body := ctx.Request.Body()
err := json.Unmarshal(body, &input)
if err != nil {
}
//xml格式请求处理
} else if strings.Contains(contentType, "text/xml") {
body := ctx.Request.Body()
err := xml.Unmarshal(body, &input)
if err != nil {
}
//form方式
} else if strings.Contains(contentType, "application/x-www-form-urlencoded") {
ctx.Request.PostArgs().VisitAll(func(key, value []byte) {
input[string(key)] = string(value)
})
//普通查询
} else {
args := ctx.QueryArgs()
if args != nil {
args.VisitAll(func(key, value []byte) {
input[string(key)] = string(value)
})
}
}
st := api.Select(ctx.Response.BodyWriter(), input)
ctx.Response.Header.Set(CONTENT_TYPE, st.GetContentType())
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/aosfather/myway-mock.git
git@gitee.com:aosfather/myway-mock.git
aosfather
myway-mock
myway-mock
master

搜索帮助