myGin

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

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

Go to latest
Published: Jun 23, 2022 License: MIT Imports: 10 Imported by: 0

README

myGin

基于net/http编写的路由框架

介绍

模仿gin框架编写的一个极简路由框架,实现了路由管理、路由分组、中间件管理等基础功能。

example

package main


import (
	"fmt"
	"myGin"
)


func main() {
	//初始化操作:路由注册
	r:= myGin.NewEngine()
	r.Use(func(c * myGin.Context){
		fmt.Println("begin middle1")
		c.Next()
		fmt.Println("end middle1")
	})

	defaultRouters := r.Group("/")
	{
		defaultRouters.GET("/", func(c *myGin.Context) {
			c.Writer.Write([]byte("首页"))
		})
		defaultRouters.GET("/news", func(c *myGin.Context) {
			c.JSON(200, myGin.H{
				"message": "这是新闻首页",
			})
		})
	}

	apiRouters := r.Group("/api")
	{

		apiRouters.Use(func(c * myGin.Context){
			fmt.Println("begin middle2")
			c.Next()
			fmt.Println("end middle2")
		})
		apiRouters.GET("/", func(c *myGin.Context) {
			c.String(200,"我是一个api接口")
		})
		apiRouters.GET("/userlist", func(c *myGin.Context) {
			c.Writer.Write([]byte("我是一个api接口-userlist"))
		})
		play := apiRouters.Group("xx")
		play.Use(func(c * myGin.Context){
			fmt.Println("begin middle3")
			c.Next()
			fmt.Println("end middle3")
		})
		play.GET("/plist", func(c *myGin.Context) {
			c.Writer.Write([]byte("我是一个api接口-xx/plist"))
		})
	}

	//启动服务
	r.Run("127.0.0.1:8000",r)

}

```  

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StringToBytes

func StringToBytes(s string) []byte

StringToBytes converts string to byte slice without a memory allocation.

func WriteJSON

func WriteJSON(w http.ResponseWriter, obj interface{}) error

WriteJSON marshals the given interface object and writes it with custom ContentType.

func WriteString

func WriteString(w http.ResponseWriter, format string, data []interface{}) (err error)

Types

type Context

type Context struct {
	Request *http.Request

	//ResponseWriter 包含了:
	// http.ResponseWriter,http.Hijacker,http.Flusher,http.CloseNotifier和额外方法
	// 暴露给handler,是writermen的复制
	Writer ResponseWriter
	// contains filtered or unexported fields
}

封装Request、ResponseWriter

func (*Context) Abort

func (c *Context) Abort()

func (*Context) GetQuery

func (c *Context) GetQuery(key string) (string, bool)

func (*Context) GetQueryArray

func (c *Context) GetQueryArray(key string) ([]string, bool)

GetQueryArray returns a slice of strings for a given query key, plus a boolean value whether at least one value exists for the given key.

func (*Context) IsAborted

func (c *Context) IsAborted() bool

func (*Context) JSON

func (c *Context) JSON(code int, obj interface{})

JSON 将给定结构序列化为 JSON 到响应主中。 将 Content-Type 设置为 “application/json” 。

func (*Context) Next

func (c *Context) Next()

func (*Context) Query

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

Query returns the keyed url query value if it exists, otherwise it returns an empty string `("")`.

func (*Context) Render

func (c *Context) Render(code int, r Render)

通过 c.Render() 这个渲染的通用方法来适配不同的渲染器

func (*Context) String

func (c *Context) String(code int, format string, values ...interface{})

String 将给定字符串写入到响应体中。

type Engine

type Engine struct {
	Router
	// contains filtered or unexported fields
}

func NewEngine

func NewEngine() *Engine

func (*Engine) Run

func (engine *Engine) Run(addr string, handler http.Handler)

func (*Engine) ServeHTTP

func (engine *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Engine) Use

func (engine *Engine) Use(middleware ...handler) IRouter

type H

type H map[string]interface{}

type HandlersChain

type HandlersChain []handler

type IRouter

type IRouter interface {
	Use(...handler) IRouter

	//Handle(string, string, ...handler) IRouter
	//Any(string, ...handler) IRouter
	GET(string, ...handler) IRouter
	POST(string, ...handler) IRouter
	DELETE(string, ...handler) IRouter
	PATCH(string, ...handler) IRouter
	PUT(string, ...handler) IRouter
	OPTIONS(string, ...handler) IRouter
	HEAD(string, ...handler) IRouter
}

IRouter defines all router handle interface.

type JSON

type JSON struct {
	Data interface{}
}

func (JSON) Render

func (r JSON) Render(w http.ResponseWriter) (err error)

Render (JSON) writes data with custom ContentType.

func (JSON) WriteContentType

func (r JSON) WriteContentType(w http.ResponseWriter)

WriteContentType (JSON) writes JSON ContentType.

type R

type R struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

构建一个实现ResponseWriter的结构体类型

func (*R) CloseNotify

func (r *R) CloseNotify() <-chan bool

func (*R) Flush

func (r *R) Flush()

func (*R) Hijack

func (r *R) Hijack() (net.Conn, *bufio.ReadWriter, error)

通过http.ResponseWriter分别实现了Hijacker、Flusher、CloseNotifier接口,接管http

func (*R) Pusher

func (r *R) Pusher() (pusher http.Pusher)

func (*R) WriteHeaderNow

func (r *R) WriteHeaderNow()

func (*R) Written

func (r *R) Written() bool

type Render

type Render interface {
	// Render writes data with custom ContentType.
	Render(http.ResponseWriter) error
	// WriteContentType writes custom ContentType.
	WriteContentType(w http.ResponseWriter)
}

Render接口被JSON、xml、html等实现后提供不同的ContentType内容

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter
	http.Hijacker
	http.Flusher
	http.CloseNotifier

	// get the http.Pusher for server push
	Pusher() http.Pusher

	// 强制写入http header(status code + headers).
	WriteHeaderNow()
}

封装http.ResponseWriter接口,该接口在http中被实现response

type Router

type Router struct {
	Handlers HandlersChain
	// contains filtered or unexported fields
}

func (*Router) DELETE

func (group *Router) DELETE(relativePath string, handlers ...handler) IRouter

func (*Router) GET

func (group *Router) GET(relativePath string, handlers ...handler) IRouter

func (*Router) Group

func (group *Router) Group(relativePath string, handlers ...handler) *Router

func (*Router) HEAD

func (group *Router) HEAD(relativePath string, handlers ...handler) IRouter

func (*Router) OPTIONS

func (group *Router) OPTIONS(relativePath string, handlers ...handler) IRouter

func (*Router) PATCH

func (group *Router) PATCH(relativePath string, handlers ...handler) IRouter

func (*Router) POST

func (group *Router) POST(relativePath string, handlers ...handler) IRouter

func (*Router) PUT

func (group *Router) PUT(relativePath string, handlers ...handler) IRouter

func (*Router) Use

func (group *Router) Use(middleware ...handler) IRouter

type String

type String struct {
	Format string
	Data   []interface{}
}

func (String) Render

func (r String) Render(w http.ResponseWriter) error

func (String) WriteContentType

func (r String) WriteContentType(w http.ResponseWriter)

Jump to

Keyboard shortcuts

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