web

package module
v0.0.0-...-522deda Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2024 License: MIT Imports: 18 Imported by: 0

README

🪡web

a lightweight web framework for golang

golang

installing

Select the version to install

go get github.com/go-needle/web@version

If you have already get , you may need to update to the latest version

go get -u github.com/go-needle/web

quickly start

package main

import (
	"fmt"
	"github.com/go-needle/web"
)

// Define middleware
func middleware1() web.Handler {
	return web.HandlerFunc(func(c *web.Context) {
		fmt.Println("test1")
		c.Next()
		fmt.Println("test4")
	})
}

// Define middleware
func middleware2(c *web.Context) {
	fmt.Println("test2")
	c.Next()
	fmt.Println("test3")
}

// You need to implement the web.Listener interface
type hello struct {
	web.POST // In this way, you will not need to implement the 'Method()'
	cnt      int
}

func (h *hello) Pattern() string { return "/hello1" }
func (h *hello) Handle(c *web.Context) {
	num := c.FormData("num")
	fmt.Println(num)
	h.cnt++
	c.JSON(200, web.H{"msg": "hello1", "cnt": h.cnt})
}

type response struct {
	Msg string `json:"msg"`
}

func main() {
	// new a server of http
	s := web.Default()
	{
		// define the group and use middleware
		g1 := s.Group("m1").Use(middleware1())
		{
			g2 := g1.Group("m2").Use(web.HandlerFunc(middleware2))
			// bind the listener to work pattern in router
			g2.Bind(&hello{}) //  work at POST /m1/m2/hello1
			// also could use this way to add to router
			g2.GET("/hello2", web.HandlerFunc(func(c *web.Context) {
				fmt.Println(c.Query("num"))
				c.JSON(200, &response{Msg: "hello2"})
			})) // work at GET /m1/m2/hello2
		}
	}
	// listen on the port
	s.Run(9999)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateToken

func CreateToken(key []byte, payloadData any) (string, error)

Types

type Context

type Context struct {
	// origin objects
	Writer  http.ResponseWriter
	Request *http.Request
	// request info
	Path   string
	Method string

	// response info
	StatusCode int
	// contains filtered or unexported fields
}

func (*Context) Abort

func (c *Context) Abort()

Abort is used in middleware, it means stopping the current middleware

func (*Context) Binary

func (c *Context) Binary() ([]byte, error)

func (*Context) BindJson

func (c *Context) BindJson(obj any) (any, error)

func (*Context) ClientIp

func (c *Context) ClientIp() string

func (*Context) Data

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

func (*Context) Extra

func (c *Context) Extra(key string) any

Extra is used to get the info which set by user

func (*Context) Fail

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

func (*Context) FormData

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

func (*Context) FormFile

func (c *Context) FormFile(key string) (multipart.File, *multipart.FileHeader, error)

func (*Context) GetHeader

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

func (*Context) HTML

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

func (*Context) JSON

func (c *Context) JSON(code int, obj any)

func (*Context) Next

func (c *Context) Next()

Next is used in middleware, it means executing the next middleware or handle

func (*Context) Param

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

Param is used to get the parameter at path.

func (*Context) Query

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

func (*Context) SetExtra

func (c *Context) SetExtra(key string, v any)

SetExtra is used to set the info by user

func (*Context) SetHeader

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

func (*Context) Status

func (c *Context) Status(code int)

func (*Context) String

func (c *Context) String(code int, format string, values ...any)

type DELETE

type DELETE struct{}

func (*DELETE) Method

func (*DELETE) Method() string

type Engine

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

Engine implement the interface of ServeHTTP

func (*Engine) ServeHTTP

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

type GET

type GET struct{}

func (*GET) Method

func (*GET) Method() string

type H

type H map[string]any
type HEAD struct{}

func (*HEAD) Method

func (*HEAD) Method() string

type Handler

type Handler interface {
	Handle(*Context)
}

Handler defines the request handler used by web

func JwtConfirm

func JwtConfirm(key []byte, headerKey string, obj any) Handler

JwtConfirm is a middleware which defines to check the verification of jwt

func Logger

func Logger() Handler

Logger is a middleware which defines to log every http request

func RateLimit

func RateLimit(rate time.Duration) Handler

RateLimit is a middleware which limits the frequency of access to the same IP address

func Recovery

func Recovery() Handler

Recovery is a middleware which defines to prevent panic from causing HTTP service termination

func TrafficLimit

func TrafficLimit(tokenTotal int, rate time.Duration) Handler

TrafficLimit is a middleware which uses token bucket algorithm for traffic restriction

type HandlerFunc

type HandlerFunc func(*Context)

HandlerFunc realizes the Handler

func (HandlerFunc) Handle

func (f HandlerFunc) Handle(ctx *Context)

type JWT

type JWT struct {
	Payload []byte
	// contains filtered or unexported fields
}

type JWTDefaultParams

type JWTDefaultParams struct {
	Iss string `json:"iss,omitempty"`
	Exp int64  `json:"exp,omitempty"`
	Sub string `json:"sub,omitempty"`
	Aud string `json:"aud,omitempty"`
	Nbf int64  `json:"nbf,omitempty"`
	Iat int64  `json:"iat,omitempty"`
	Jti string `json:"jti,omitempty"`
}

type Listener

type Listener interface {
	Method() string
	Pattern() string
	Handle(*Context)
}

type OPTIONS

type OPTIONS struct{}

func (*OPTIONS) Method

func (*OPTIONS) Method() string

type PATCH

type PATCH struct{}

func (*PATCH) Method

func (*PATCH) Method() string

type POST

type POST struct{}

func (*POST) Method

func (*POST) Method() string

type PUT

type PUT struct{}

func (*PUT) Method

func (*PUT) Method() string

type RouterGroup

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

func (*RouterGroup) Bind

func (group *RouterGroup) Bind(listeners ...Listener)

Bind is defined to bind all listeners to the router

func (*RouterGroup) DELETE

func (group *RouterGroup) DELETE(pattern string, handler Handler)

DELETE defines the method to add DELETE request

func (*RouterGroup) GET

func (group *RouterGroup) GET(pattern string, handler Handler)

GET defines the method to add GET request

func (*RouterGroup) Group

func (group *RouterGroup) Group(prefix string) *RouterGroup

Group is defined to create a new RouterGroup remember all groups share the same Engine instance

func (*RouterGroup) HEAD

func (group *RouterGroup) HEAD(pattern string, handler Handler)

HEAD defines the method to add HEAD request

func (*RouterGroup) OPTIONS

func (group *RouterGroup) OPTIONS(pattern string, handler Handler)

OPTIONS defines the method to add OPTIONS request

func (*RouterGroup) PATCH

func (group *RouterGroup) PATCH(pattern string, handler Handler)

PATCH defines the method to add PATCH request

func (*RouterGroup) POST

func (group *RouterGroup) POST(pattern string, handler Handler)

POST defines the method to add POST request

func (*RouterGroup) PUT

func (group *RouterGroup) PUT(pattern string, handler Handler)

PUT defines the method to add PUT request

func (*RouterGroup) REQUEST

func (group *RouterGroup) REQUEST(method, pattern string, handler Handler)

REQUEST defines your method to request

func (*RouterGroup) Static

func (group *RouterGroup) Static(relativePath string, root string)

Static is defined to map local static resources

func (*RouterGroup) Use

func (group *RouterGroup) Use(middlewares ...Handler) *RouterGroup

Use is defined to add middleware to the group

type Server

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

func Default

func Default() *Server

Default is the constructor of web.Server with Recovery and Logger

func New

func New() *Server

New is the constructor of web.Server

func (*Server) LoadHTMLGlob

func (server *Server) LoadHTMLGlob(pattern string)

func (*Server) Run

func (server *Server) Run(port int)

Run defines the method to start a http server

func (*Server) RunTLS

func (server *Server) RunTLS(port int, certFile, keyFile string)

RunTLS defines the method to start a https server

func (*Server) SetFuncMap

func (server *Server) SetFuncMap(funcMap template.FuncMap)

func (*Server) Use

func (server *Server) Use(middlewares ...Handler) *Server

Use is defined to add middleware to the server

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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