goweb

package module
v0.0.0-...-d71590a Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2024 License: MIT Imports: 6 Imported by: 1

README

一个纯原生简单的web api框架

支持中间件,路由编写更友好

go get github.com/Rehtt/Kit/web

使用jsoniter:

go build -tags=jsoniter
package main

import (
	"fmt"
	goweb "github.com/Rehtt/Kit/web"
	"net/http"
)

func main() {
	web := goweb.New()
	web.SetValue("test","123")
	web.Middleware(func(ctx *goweb.Context) {
		fmt.Println("中间件")
	})
	web.NoRoute(func(ctx *goweb.Context) {
		ctx.Writer.Write([]byte("找不到啊大佬"))
	})

	web.Any("/123/#asd/234", func(ctx *goweb.Context) {
		fmt.Println(ctx.GetParam("asd"), "获取动态路由参数")
	})
    // curl 127.0.0.1:9090/123/zxcv/234
    // print: zxcv 获取动态路由参数

    web.Any("/1234/#...",func(ctx *goweb.Context){
        fmt.Println(ctx.GetParam("#"), "获取参数")
    })
    // curl 127.0.0.1:9090/1234/qwe/asd/sdf
    // print: qwe/asd/sdf 获取参数

	api := web.Grep("/api")
	api.GET("/test", func(ctx *goweb.Context) {
		fmt.Println(ctx.GetValue("test"))
	})

	http.ListenAndServe(":9090", web)
}

并发测试:

g := goweb.New()
g.GET("/ping", func(ctx *goweb.Context) {
	ctx.Writer.Write([]byte("pong"))
})
http.ListenAndServe(":8070", g)
$ wrk -d 100s -c 1024 -t 8 http://127.0.0.1:8070/ping
Running 2m test @ http://127.0.0.1:8070/ping
  8 threads and 1024 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     4.30ms    5.17ms  92.06ms   86.25%
    Req/Sec    42.37k     7.90k  130.44k    69.17%
  33674619 requests in 1.67m, 3.76GB read
  Socket errors: connect 0, read 0, write 0, timeout 38
Requests/sec: 336435.08
Transfer/sec:     38.50MB

gin:

g := gin.New()
g.GET("/ping", func(context *gin.Context) {
	context.Writer.Write([]byte("pong"))
})
http.ListenAndServe(":8060", g)
wrk -d 100s -c 1024 -t 8 http://127.0.0.1:8060/ping
Running 2m test @ http://127.0.0.1:8060/ping
  8 threads and 1024 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     4.43ms    5.99ms 224.24ms   87.84%
    Req/Sec    43.33k     9.81k  112.97k    71.84%
  34451839 requests in 1.67m, 3.85GB read
  Socket errors: connect 0, read 0, write 0, timeout 100
Requests/sec: 344178.03
Transfer/sec:     39.39MB

Documentation

Index

Constants

View Source
const (
	GET     = "GET"
	POST    = "POST"
	DELETE  = "DELETE"
	PUT     = "PUT"
	HEAD    = "HEAD"
	OPTIONS = "OPTIONS"
	CONNECT = "CONNECT"
	ANY     = "ANY"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	Request *http.Request
	Writer  http.ResponseWriter

	context.Context
	// contains filtered or unexported fields
}

func (*Context) GetParam

func (c *Context) GetParam(key string) string

func (*Context) GetValue

func (c *Context) GetValue(key any) any

func (*Context) ReadJSON

func (c *Context) ReadJSON(v any) error

func (*Context) SetValue

func (c *Context) SetValue(key any, value any)

func (*Context) Stop

func (c *Context) Stop()

func (*Context) WriteJSON

func (c *Context) WriteJSON(v any, statusCode ...int) error

type GOweb

type GOweb struct {
	RouterGroup

	context.Context
	// contains filtered or unexported fields
}

func New

func New() (g *GOweb)

func (*GOweb) GetValue

func (g *GOweb) GetValue(key any) any

func (*GOweb) NoRoute

func (g *GOweb) NoRoute(handlerFunc HandlerFunc)

func (*GOweb) ServeHTTP

func (g *GOweb) ServeHTTP(writer http.ResponseWriter, request *http.Request)

func (*GOweb) SetValue

func (g *GOweb) SetValue(key, value any)

type HandlerFunc

type HandlerFunc func(ctx *Context)

type RouterGroup

type RouterGroup struct {
	// contains filtered or unexported fields
}

func (*RouterGroup) Any

func (g *RouterGroup) Any(path string, handlerFunc HandlerFunc)

func (*RouterGroup) BottomNodeList

func (g *RouterGroup) BottomNodeList() (sub []*RouterGroup)

func (*RouterGroup) CONNECT

func (g *RouterGroup) CONNECT(path string, handlerFunc HandlerFunc)

func (*RouterGroup) DELETE

func (g *RouterGroup) DELETE(path string, handlerFunc HandlerFunc)

func (*RouterGroup) FootMiddleware

func (g *RouterGroup) FootMiddleware(handlers ...HandlerFunc)

FootMiddleware 最后运行的中间件

func (*RouterGroup) GET

func (g *RouterGroup) GET(path string, handlerFunc HandlerFunc)

func (*RouterGroup) Grep

func (g *RouterGroup) Grep(path string) *RouterGroup

func (*RouterGroup) HEAD

func (g *RouterGroup) HEAD(path string, handlerFunc HandlerFunc)

func (*RouterGroup) List

func (g *RouterGroup) List() (method, path []string)

func (*RouterGroup) Middleware

func (g *RouterGroup) Middleware(handlers ...HandlerFunc)

Middleware 中间件,头部运行

func (*RouterGroup) OPTIONS

func (g *RouterGroup) OPTIONS(path string, handlerFunc HandlerFunc)

func (*RouterGroup) POST

func (g *RouterGroup) POST(path string, handlerFunc HandlerFunc)

func (*RouterGroup) PUT

func (g *RouterGroup) PUT(path string, handlerFunc HandlerFunc)

func (*RouterGroup) PathMatch

func (g *RouterGroup) PathMatch(path, method string) (match map[string]string, handle HandlerFunc, grep *RouterGroup)

Jump to

Keyboard shortcuts

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