Skip to content

Commit

Permalink
增加 schema 设计; 暂时删除了 DynPlugin 设计
Browse files Browse the repository at this point in the history
  • Loading branch information
fumeboy committed Mar 11, 2021
1 parent fea2f87 commit a9f560c
Show file tree
Hide file tree
Showing 30 changed files with 338 additions and 722 deletions.
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
- File
- group pluginmm
- ~~Auth(JWT) plugin~~
- ~~LazyRouter~~
- ~~view pool~~
- ~~GC~~
2 changes: 1 addition & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (engine *Engine) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
ctx := engine.contextPool.Get().(*Context)
defer func() {
if err := recover(); err != nil {
ctx.Resp.String(500,"Internal Server Error")
ctx.Resp.String(500,"Internal Server err")
}
}()
ctx.reset(req, resp)
Expand Down
44 changes: 1 addition & 43 deletions app_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package nji_test

import (
"fmt"
"github.com/stretchr/testify/assert"
"io"
"nji/plugins"
"time"

"net/http"
"net/http/httptest"
"nji"
Expand Down Expand Up @@ -39,42 +34,5 @@ func TestPanicEvent(t *testing.T) {
}
w := httptest.NewRecorder()
app.ServeHTTP(w, r)
assert.Equal(t, w.Body.String(), "Internal Server Error")
}

type mock struct {
A plugins.PathParam
}

func (v *mock) Handle(c *nji.Context) {
c.Resp.String(200,v.A.Value)
}

func TestMock(t *testing.T) {
app := nji.NewLazyRouter()
app.GET(&mock{})
go app.Run(8003)

go func() {
for i := 0;i<100;i++{
go func() {
buf := make([]byte, 100)
for j := 0;j<10;j++{
r, err := http.Get(fmt.Sprintf("http://127.0.0.1:8003/mock/%d",j))
if err == nil{
n,err := r.Body.Read(buf)
if err != io.EOF{
fmt.Println(err)
return
}
if fmt.Sprintf("%d", j) != string(buf[:n]){
fmt.Println(123)
return
}
}
}
}()
}
}()
time.Sleep(19*time.Second)
assert.Equal(t, w.Body.String(), "Internal Server err")
}
162 changes: 28 additions & 134 deletions context.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package nji

import (
"context"
"net"
"io/ioutil"
"net/http"
"net/url"
"strings"
Expand All @@ -17,9 +16,12 @@ type Context struct {
engine *Engine
Request *http.Request
index int8
parsed bool // 是否已解析body

Error error
parsed bool // 是否已解析body
isJSON bool
JSON []byte

err error
}

var emptyValues url.Values
Expand All @@ -33,11 +35,31 @@ func (ctx *Context) reset(req *http.Request, resp http.ResponseWriter) {
ctx.index = -1
ctx.fullPath = ""
ctx.parsed = false
ctx.Error = nil
ctx.isJSON = false
ctx.JSON = nil
ctx.err = nil
}

func (ctx *Context) IsJSON() bool {
if ctx.parsed{
return ctx.isJSON
}
if ct := ctx.Request.Header.Get("Content-Type"); ct != "application/json" {
ctx.parsed = true
return false
}
ctx.parsed = true
data, err := ioutil.ReadAll(ctx.Request.Body)
if err != nil{
return false
}
ctx.JSON = data
ctx.isJSON = true
return true
}

// 解析form数据
func (ctx *Context) parseForm() error {
func (ctx *Context) ParseForm() error {
if ctx.parsed {
return nil
}
Expand Down Expand Up @@ -73,131 +95,3 @@ func (ctx *Context) Abort() {
func (ctx *Context) IsAborted() bool {
return ctx.index >= abortIndex
}

// 在Context中写值
func (ctx *Context) SetValue(key, value interface{}) {
if key == nil {
return
}
ctx.Request = ctx.Request.WithContext(context.WithValue(ctx.Request.Context(), key, value))
}

// 从Context中取值
func (ctx *Context) GetValue(key interface{}) interface{} {
if key == nil {
return nil
}
return ctx.Request.Context().Value(key)
}

// 向客户端发送重定向响应
func (ctx *Context) Redirect(code int, url string) {
if code < 300 || code > 308 {
// panic
return
}
ctx.Resp.Writer.Header().Set("Location", url)
ctx.Resp.Writer.WriteHeader(code)
}

// 获得客户端真实IP
func (ctx *Context) RemoteIP() string {
ra := ctx.Request.RemoteAddr
if ip := ctx.Request.Header.Get("X-Forwarded-For"); ip != "" {
ra = strings.Split(ip, ", ")[0]
} else if ip := ctx.Request.Header.Get("X-Real-IP"); ip != "" {
ra = ip
} else {
var err error
ra, _, err = net.SplitHostPort(ra)
if err != nil {
return ""
}
}
return ra
}

// 获取所有GET参数值
func (ctx *Context) QueryParams() url.Values {
return ctx.Request.URL.Query()
}

// 获取某个GET参数值的string类型
func (ctx *Context) Query(key string) string {
if len(ctx.Request.URL.Query()[key]) == 0 {
return ""
}
return ctx.Request.URL.Query()[key][0]
}

// 获取某个GET参数
func (ctx *Context) QueryParam(key string) (string, bool) {
if len(ctx.Request.URL.Query()[key]) == 0 {
return "", false
}
return ctx.Request.URL.Query()[key][0], true
}

// 获取所有POST/PATCH/PUT参数值
func (ctx *Context) PostParams() url.Values {
if err := ctx.parseForm(); err != nil {
return emptyValues
}
return ctx.Request.PostForm
}

// 获取某个POST/PATCH/PUT参数值的string类型
func (ctx *Context) Post(key string) string {
if err := ctx.parseForm(); err != nil {
return ""
}
vs := ctx.Request.PostForm[key]
if len(vs) == 0 {
return ""
}
return ctx.Request.PostForm[key][0]
}

// 获取某个POST/PATCH/PUT参数
func (ctx *Context) PostParam(key string) (string, bool) {
if err := ctx.parseForm(); err != nil {
return "", false
}
vs := ctx.Request.PostForm[key]
if len(vs) == 0 {
return "", false
}
return ctx.Request.PostForm[key][0], true
}

// 获取所有GET/POST/PUT参数值
func (ctx *Context) FormParams() url.Values {
if err := ctx.parseForm(); err != nil {
return emptyValues
}
return ctx.Request.Form
}

// 获取某个GET/POST/PUT参数值的string类型
func (ctx *Context) Form(key string) string {
if err := ctx.parseForm(); err != nil {
return ""
}
vs := ctx.Request.Form[key]
if len(vs) == 0 {
return ""
}
return ctx.Request.Form[key][0]
}

// 获取单个GET/POST/PUT参数
func (ctx *Context) FormParam(key string) (string, bool) {
if err := ctx.parseForm(); err != nil {
return "", false
}
vs := ctx.Request.Form[key]
if len(vs) == 0 {
return "", false
}
return ctx.Request.Form[key][0], true
}
Loading

0 comments on commit a9f560c

Please sign in to comment.