mserver

package module
v0.0.0-...-8e4b865 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2024 License: MIT Imports: 12 Imported by: 0

README

mserver

mini web server实现

基本功能:

1.路由注册

  • 静态路由
  • 参数路由
  • 通配符路由

2.中间件

  • 中间件注册
  • 可路由中间件(尽可能匹配),匹配多个,越具体越靠后
    • (/a/b, mws),
      • /a/b/c 会执行mws
    • (/a/, mws1), (/a/b/, mws2)
      • /a/c 执行mws1
      • /a/b/c 执行mws1, mws2
    • (/a/*/c, mws1), (/a/b/c, mws2)
      • /a/d/c 执行mws1
      • /a/b/c 执行mws1,mws2
      • /a/b/d 不执行mws1,mws2!
    • (/a/:id, mws1), (/a/123/c, mws2)
      • /a/123,执行mws1
      • /a/123/c, 执行mws1和mws2
    • 不支持:
      • (/a/*/c, mws1),(/a/b/c, mws2), /a/b/c 执行ms2

3.支持group分组

  • 基于group的uri前缀
  • 不支持group的中间件(可使用可路由中间件替代)

快速使用:

package main

import (
  "fmt"
  "github.com/NotFound1911/mserver"
  "github.com/NotFound1911/mserver/middleware/cost"
  "github.com/NotFound1911/mserver/middleware/recovery"
  "net/http"
)

func main() {
  core := mserver.NewCore()
  mw := func(next mserver.HandleFunc) mserver.HandleFunc {
    return func(ctx *mserver.Context) error {
      fmt.Println("this is mid")
      if err := next(ctx); err != nil {
        return err
      }
      return nil
    }
  }
  core.Use(recovery.MiddlewareBuilder{}.Build(), cost.MiddlewareBuilder{}.Build())
  core.Get("/user/home", func(ctx *mserver.Context) error {
    ctx.SetStatus(http.StatusOK).Text("this is /usr/home")
    return nil
  })
  core.Get("/user/school", func(ctx *mserver.Context) error {
    ctx.SetStatus(http.StatusOK).Text("this is /user/school")
    return nil
  })
  core.UsePath(http.MethodGet, "/user/*", mw)
  core.Start(":8888")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	MatchedRoute string

	CustomValues map[string]any // 自定义数据

	// ContextWithFallback enable fallback Context.Deadline(), Context.Done(), Context.Err() and Context.Value()
	// when Context.Request.Context() is not nil.
	ContextWithFallback bool
	// contains filtered or unexported fields
}

func (*Context) Deadline

func (ctx *Context) Deadline() (deadline time.Time, ok bool)

func (*Context) Done

func (ctx *Context) Done() <-chan struct{}

func (*Context) Err

func (ctx *Context) Err() error

func (*Context) GetRequest

func (ctx *Context) GetRequest() *http.Request

func (*Context) GetRespStatusCode

func (ctx *Context) GetRespStatusCode() int

func (*Context) GetResponse

func (ctx *Context) GetResponse() http.ResponseWriter

func (*Context) Header

func (ctx *Context) Header(key, value string) Responser

Header 是set和del方法的缩写,主要是重新响应的header 如果 value == "", 执行`ctx.resp.Header().Set(key, value)` 否则执行 `ctx.resp.Header().Set(key, value)`

func (*Context) Html

func (ctx *Context) Html(file string, obj interface{}) Responser

func (*Context) Json

func (ctx *Context) Json(obj interface{}) Responser

func (*Context) Redirect

func (ctx *Context) Redirect(path string) Responser

func (*Context) Render

func (ctx *Context) Render(tplName string, data any) error

Render 渲染

func (*Context) SetCookie

func (ctx *Context) SetCookie(key string, val string, maxAge int, path string,
	domain string, secure bool, httpOnly bool) Responser

func (*Context) SetHeader

func (ctx *Context) SetHeader(key string, val string) Responser

func (*Context) SetOkStatus

func (ctx *Context) SetOkStatus() Responser

func (*Context) SetParams

func (ctx *Context) SetParams(params map[string]string)

SetParams 设置参数

func (*Context) SetRequest

func (ctx *Context) SetRequest(req *http.Request)

func (*Context) SetRespData

func (ctx *Context) SetRespData(data []byte)

func (*Context) SetStatus

func (ctx *Context) SetStatus(code int) Responser

func (*Context) Text

func (ctx *Context) Text(format string, values ...interface{}) Responser

func (*Context) Value

func (ctx *Context) Value(key any) any

func (*Context) Xml

func (ctx *Context) Xml(obj interface{}) Responser

type Core

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

func NewCore

func NewCore(opts ...CoreOption) *Core

func (*Core) Any

func (c *Core) Any(path string, handler HandleFunc)

func (*Core) Delete

func (c *Core) Delete(path string, handler HandleFunc)

func (*Core) FindRouteNodeByRequest

func (c *Core) FindRouteNodeByRequest(request *http.Request) (*matchNode, bool)

匹配路由

func (*Core) Get

func (c *Core) Get(path string, handler HandleFunc)

func (*Core) Group

func (c *Core) Group(prefix string) Grouper

func (*Core) Post

func (c *Core) Post(path string, handler HandleFunc)

func (*Core) Put

func (c *Core) Put(path string, handler HandleFunc)

func (*Core) ServeHTTP

func (c *Core) ServeHTTP(writer http.ResponseWriter, request *http.Request)

ServeHTTP 处理请求的入口

func (*Core) Start

func (c *Core) Start(addr string) error

Start 启动服务

func (*Core) StartTLS

func (c *Core) StartTLS(addr, certFile, keyFile string) error

func (*Core) Use

func (c *Core) Use(middlewares ...Middleware)

注册中间件

func (*Core) UsePath

func (c *Core) UsePath(method string, path string, mws ...Middleware)

type CoreOption

type CoreOption func(c *Core)

func CoreWithTemplateEngine

func CoreWithTemplateEngine(tplEngine TemplateEngine) CoreOption

type GoTemplateEngine

type GoTemplateEngine struct {
	T *template.Template
}

func (*GoTemplateEngine) ParseGlob

func (g *GoTemplateEngine) ParseGlob(pattern string) error

func (*GoTemplateEngine) Render

func (g *GoTemplateEngine) Render(ctx context.Context, tplName string, data any) ([]byte, error)

type Group

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

func NewGroup

func NewGroup(core *Core, prefix string) *Group

func (*Group) Any

func (g *Group) Any(path string, handler HandleFunc)

func (*Group) Delete

func (g *Group) Delete(path string, handler HandleFunc)

func (*Group) Get

func (g *Group) Get(path string, handler HandleFunc)

func (*Group) Group

func (g *Group) Group(uri string) Grouper

func (*Group) Post

func (g *Group) Post(path string, handler HandleFunc)

func (*Group) Put

func (g *Group) Put(path string, handler HandleFunc)

type Grouper

type Grouper interface {
	Post(path string, handler HandleFunc)
	Put(path string, handler HandleFunc)
	Get(path string, handler HandleFunc)
	Delete(path string, handler HandleFunc)
	Any(path string, handler HandleFunc)
	// Group 实现嵌套
	Group(string string) Grouper
}

Grouper 前缀分组 实现HttpMethod方法

type HandleFunc

type HandleFunc func(ctx *Context) error

type Middleware

type Middleware func(handleFunc HandleFunc) HandleFunc

type Responser

type Responser interface {
	// Json Json输出
	Json(obj interface{}) Responser
	// Xml Xml输出
	Xml(obj interface{}) Responser
	// Html Html输出
	Html(template string, obj interface{}) Responser
	// Text string输出
	Text(format string, values ...interface{}) Responser
	// Redirect 重定向
	Redirect(path string) Responser
	// SetCookie 设置Cookie
	SetCookie(key string, val string, maxAge int, path, domain string, secure, httpOnly bool) Responser
	// SetStatus 设置状态码
	SetStatus(code int) Responser
	// SetHeader 设置header
	SetHeader(key string, val string) Responser
	// SetOkStatus 设置200状态
	SetOkStatus() Responser
	// Header header修订
	Header(key, value string) Responser
}

type Server

type Server interface {
	http.Handler
	// Start 启动服务器
	// addr 是监听地址。如果只指定端口,可以使用 ":8081"
	// 或者 "localhost:8082"
	Start(addr string) error
	// contains filtered or unexported methods
}

type TemplateEngine

type TemplateEngine interface {
	// Render 渲染页面
	// tplName 模板的名字,按名索引
	// data 渲染页面用的数据
	Render(ctx context.Context, tplName string, data any) ([]byte, error)
}

type Tree

type Tree struct {
	Root *node
}

Tree 代表树结构

Directories

Path Synopsis
middleware

Jump to

Keyboard shortcuts

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