keing

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2021 License: MIT Imports: 13 Imported by: 0

README

Keing Web Framework

Goproxy.cn Go Report Card GoDoc Tag Go

🏠 Homepage
Example

👨‍🎓 Author

👤 hxoreyer

🎁 Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2021 hxoreyer.

This project is MIT licensed.


使用✔

当然,提前得先要有go和git

下载

go get -u github.com/hxoreyer/keing

在你的go代码里import

import "github.com/hxoreyer/keing"
例子💯

创建最简单的HTTP端点

pakage main

import "github.com/hxoreyer/keing"

func main(){
	k := keing.Init()
	k.GET("/hello",func(c *keing.Context){
		c.String(200,"hello keing")
	})
	
	k.Run(":8080")
}

获取路径中的参数

pakage main

import "github.com/hxoreyer/keing"

func main(){
	k := keing.Init()
	k.GET("/username/:name",func(c *keing.Context){
        c.JSON(200, keing.K{
            "msg": "hello " + c.Name("name")
        })
	})
	
	k.Run(":8080")
}

分组路由

pakage main

import "github.com/hxoreyer/keing"

func main(){
	k := keing.Init()
	
	v1 := k.Group("/v1)
	{
		v1.POST("login",LoginHandler)
		v1.GET("read",ReadHandler)
	}
	
	v2 := k.Group("/v2")
	{
		v2.POST("login",LoginHandler)
		v2.GET("read",ReadHandler)
	}
	
	k.Run(":8080")
}
无默认中间件初始化🚫

使用

k := keig.New()

代替

k := keing.Init()
中间件使用🗝
k.Use(Logger())

JWT中间件

package main

import (
	"github.com/hxoreyer/keing"
	"time"
)

func main() {
	k := keing.Init()
	k.GET("/login", func(c *keing.Context) {
		username := c.Form("username")
		//中间省略判断用户账号和密码的过程
		tok, err := keing.CreateToken(username)
		if err != nil {
			c.JSON(200, keing.K{
				"msg": err.Error(),
			})
			return
		}
		c.JSON(200, keing.K{
			"token":    tok,
		})
	})

	ks := k.Group("/secret")
	ks.Use(keing.JWTAuth())
	{
		ks.GET("/date", func(c *keing.Context) {
			username := c.MustGet("username")
			c.JSON(200, keing.K{
				"code":     2000,
				"msg":      time.Now().String(),
				"username": username,
			})
		})
	}
	k.Run(":8090")
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	//TokenDuration token持续时间
	TokenDuration = time.Hour * 2
	//JWTSecret token密钥
	JWTSecret = []byte("KeingWebSecret")
)

Functions

func CreateToken added in v0.1.2

func CreateToken(username string) (string, error)

CreateToken 生成token

func JWTAuth added in v0.1.2

func JWTAuth() func(c *Context)

JWTAuth JWT验证中间件

Types

type Claims added in v0.1.2

type Claims struct {
	Username string `json:"username"`
	jwt.StandardClaims
}

Claims 用户信息类,作为生成token的参数

func ParseToken added in v0.1.2

func ParseToken(tokenString string) (*Claims, error)

ParseToken 将token字符串解析为用户信息类

type Context

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

	Errors []ErrorMsg
	Keys   map[string]interface{}

	Code int
	// contains filtered or unexported fields
}

Context 用来响应的上下文

func (*Context) Abort

func (c *Context) Abort(code int)

Abort 终止处理 强制系统不调用挂起的程序

func (*Context) BindJSON added in v0.1.2

func (c *Context) BindJSON(js interface{}) error

BindJSON 将正文内容转化为json

func (*Context) Data

func (c *Context) Data(code int, data []byte)

Data 将字节组写至响应正文中

func (*Context) Error

func (c *Context) Error(err error, meta interface{})

Error 将错误附加到当前上下文 将错误推送至错误列表 中间件会从错误列表获取错误信息

func (*Context) Fail added in v0.1.1

func (c *Context) Fail(code int, err error)

Fail 以上的结合

func (*Context) Form

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

Form 表单数据获取

func (*Context) Get added in v0.1.2

func (c *Context) Get(key string) (interface{}, error)

Get 从上下文中读取键值对

func (*Context) HTML added in v0.1.2

func (c *Context) HTML(code int, name string, data interface{})

HTML 发送HTML文件到响应正文中

func (*Context) Header added in v0.2.1

func (c *Context) Header(key, value string)

Header c.Writer.Header().Set(key, value)的快捷方式 如果value为空,则删除头

func (*Context) JSON added in v0.1.2

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

JSON 将给定的结构转化为Json至响应正文中

func (*Context) MustGet added in v0.1.2

func (c *Context) MustGet(key string) interface{}

MustGet 返回给定键的值,如果值不存在,则发生panic

func (*Context) Name

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

Name 路由值获取 例如: /hello/:name request: localhost:8080/hello/keing c.Name("name")将获取keing

func (*Context) Next

func (c *Context) Next()

Next 运行后面的handlers(中间件可使用)

func (*Context) Query

func (c *Context) Query(key string) interface{}

Query 键值获取

func (*Context) QueryFloat64

func (c *Context) QueryFloat64(key string) float64

QueryFloat64 键值获取(转化为float64)

func (*Context) QueryInt

func (c *Context) QueryInt(key string) int

QueryInt 键值获取(转化为int)

func (*Context) QueryInt32

func (c *Context) QueryInt32(key string) int32

QueryInt32 键值获取(转化为int32)

func (*Context) QueryInt64

func (c *Context) QueryInt64(key string) int64

QueryInt64 键值获取(转化为int64)

func (*Context) QueryString

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

QueryString 键值获取(转化为sring)

func (*Context) Set added in v0.1.2

func (c *Context) Set(key string, item interface{})

Set 为上下文添加一个键值对 可用Get获取键值

func (*Context) Status added in v0.2.1

func (c *Context) Status(code int)

Status 设置HTTP响应状态码

func (*Context) String

func (c *Context) String(code int, msg string)

String 将字符串写至响应正文中 更改Content-Type

func (*Context) XML added in v0.1.1

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

XML 将给定的结构转化为XML至响应正文中

type Engine

type Engine struct {
	*RouterGroup

	HTMLTemplates *template.Template
	// contains filtered or unexported fields
}

Engine 路由核心

func Init

func Init() *Engine

Init 路由初始化(带默认中间件)

func New

func New() *Engine

New 路由初始化(无默认中间件)

func (*Engine) Run

func (engine *Engine) Run(addr string)

Run 通过addr运行

func (*Engine) ServeHTTP added in v0.1.1

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

ServeHTTP 使用ServeHTTP使路由实现 http.Handler的接口

func (*Engine) SetHTMLTemplates added in v0.1.2

func (engine *Engine) SetHTMLTemplates(pattren string)

SetHTMLTemplates 确保HTML模板无错误,再将其赋予变量中

type ErrorMsg

type ErrorMsg struct {
	Message string      `json:"msg"`
	Meta    interface{} `json:"meta"`
}

ErrorMsg 错误信息结构

type HandlerFunc

type HandlerFunc func(*Context)

HandlerFunc 路由函数类型

func Cors added in v0.2.1

func Cors() HandlerFunc

Cors 跨域中间件

func Logger

func Logger() HandlerFunc

Logger 中间件,显示监听的路由和请求信息

type K

type K map[string]interface{}

K 方便JSON格式数据发送

type Path

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

Path 单个路径结构

type Paths

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

Paths 全部路径结构

func (*Paths) AddPath

func (p *Paths) AddPath(method, path string, n int)

AddPath 将路径添加到路径表里

func (*Paths) ShowAllPathString

func (p *Paths) ShowAllPathString()

ShowAllPathString 获取当前所里路径并格式化输出

type RouterGroup added in v0.1.1

type RouterGroup struct {
	Handlers []HandlerFunc
	// contains filtered or unexported fields
}

RouterGroup 路由组结构

func (*RouterGroup) DELETE added in v0.1.1

func (rt *RouterGroup) DELETE(path string, handlers ...HandlerFunc)

DELETE DELETE请求

func (*RouterGroup) GET added in v0.1.1

func (rt *RouterGroup) GET(path string, handlers ...HandlerFunc)

GET GET请求

func (*RouterGroup) Group added in v0.1.1

func (rt *RouterGroup) Group(component string, handlers ...HandlerFunc) *RouterGroup

Group 创建一个路由组,并初始化

func (*RouterGroup) Handle added in v0.1.1

func (rt *RouterGroup) Handle(method, p string, handlers []HandlerFunc)

Handle 给路由添加路由函数

func (*RouterGroup) PATCH added in v0.1.1

func (rt *RouterGroup) PATCH(path string, handlers ...HandlerFunc)

PATCH PATCH请求

func (*RouterGroup) POST added in v0.1.1

func (rt *RouterGroup) POST(path string, handlers ...HandlerFunc)

POST POST请求

func (*RouterGroup) PUT added in v0.1.1

func (rt *RouterGroup) PUT(path string, handlers ...HandlerFunc)

PUT PUT请求

func (*RouterGroup) Use added in v0.1.1

func (rt *RouterGroup) Use(middlewares ...HandlerFunc)

Use 使用中间件

Jump to

Keyboard shortcuts

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