haruka

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

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

Go to latest
Published: Mar 9, 2023 License: MIT Imports: 15 Imported by: 7

README

haruka

web framework

Example

package main

import "github.com/allentom/haruka"

func main() {
	e := haruka.NewEngine()
	e.Router.GET("/ping", func(context *Context) {
		context.JSONWithStatus(haruka.JSON{
			"message":"Pong",
		}, http.StatusOK)
    })
	e.ListenAndServe(":8080")
}

Router

With specific path
e.Router.GET("/ping", <YourHandler>)
// with path parameters
e.Router.GET("/ping/{name}", <YourHandler>)
// with regex
e.Router.GET("/ping/{name:[0-9|a-z|A-Z]+}", <YourHandler>)
With different methods
e.Router.GET("/ping", <YourHandler>)
e.Router.POST("/ping", <YourHandler>)
e.Router.PATCH("/ping", <YourHandler>)
e.Router.Put("/ping", <YourHandler>)
e.Router.OPTION("/ping", <YourHandler>)

// for all methods
e.Router.AddHandler("/ping", <YourHandler>)
// for multiple methods
e.Router.METHODS("/ping", []string{"GET","POST"}, <YourHandler>)

Handler

Get query parameters
func (c *Context) {
    context.GetQueryString("id")
    context.GetQueryStrings("ids")
    context.GetQueryInt("id")
}
Get path parameters
func (c *Context) {
    context.GetPathParameterAsInt("id")
    context.GetPathParameterAsString("id")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BindingValue

func BindingValue(valueField reflect.Value, rawValue []string, tags reflect.StructTag) error

Types

type Context

type Context struct {
	// Pattern matched url path
	Pattern string
	// The http.ResponseWriter object.
	Writer http.ResponseWriter
	// The http.Request object.
	Request *http.Request
	// Path params
	Parameters map[string]string
	// For middleware to store dara
	Param map[string]interface{}
	// contains filtered or unexported fields
}

Context for request

func (*Context) Abort

func (c *Context) Abort()

interrupt middleware chain

func (*Context) BindingInput

func (c *Context) BindingInput(input interface{}) error

func (*Context) GetPathParameterAsInt

func (c *Context) GetPathParameterAsInt(key string) (int, error)

get path parameter as int

func (*Context) GetPathParameterAsString

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

get path parameter as string

func (*Context) GetQueryInt

func (c *Context) GetQueryInt(key string) (int, error)

get query string as int

func (*Context) GetQueryString

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

get query string from url

func (*Context) GetQueryStrings

func (c *Context) GetQueryStrings(key string) []string

get query string as []string

func (*Context) HTML

func (c *Context) HTML(templatePath string, data interface{}) (err error)

func (*Context) JSON

func (c *Context) JSON(data interface{}) (err error)

func (*Context) JSONWithStatus

func (c *Context) JSONWithStatus(data interface{}, status int) (err error)

func (*Context) ParseJson

func (c *Context) ParseJson(target interface{}) error

func (*Context) ParseXML

func (c *Context) ParseXML(target interface{}) error

func (*Context) XML

func (c *Context) XML(data interface{}) (err error)

func (*Context) XMLWithStatus

func (c *Context) XMLWithStatus(data interface{}, status int) (err error)

type Engine

type Engine struct {
	Router      *Router
	Middlewares []Middleware

	Logger *logrus.Logger
	Cros   *cors.Cors
	// contains filtered or unexported fields
}

func NewEngine

func NewEngine() *Engine

func (*Engine) RunAndListen

func (e *Engine) RunAndListen(addr string) error

func (*Engine) Stop

func (e *Engine) Stop() error

func (*Engine) UseCors

func (e *Engine) UseCors(cors *cors.Cors)

func (*Engine) UseMiddleware

func (e *Engine) UseMiddleware(middleware Middleware)

type FormFile

type FormFile struct {
	Header *multipart.FileHeader
	File   multipart.File
}

type HTMLView

type HTMLView struct {
	Data   []byte
	Writer io.Writer
	// contains filtered or unexported fields
}

func (*HTMLView) Render

func (v *HTMLView) Render(data interface{}) (err error)

func (*HTMLView) Write

func (v *HTMLView) Write(context *Context)

type JSON

type JSON map[string]interface{}

type JSONParser

type JSONParser struct {
}

func (*JSONParser) Parse

func (p *JSONParser) Parse(r *http.Request, target interface{}) error

type JsonView

type JsonView struct {
	Data []byte
}

func (*JsonView) Render

func (v *JsonView) Render(data interface{}) (err error)

func (*JsonView) Write

func (v *JsonView) Write(context *Context)

func (*JsonView) WriteWithStatus

func (v *JsonView) WriteWithStatus(context *Context, status int)

type Middleware

type Middleware interface {
	OnRequest(ctx *Context)
}

type Parser

type Parser interface {
	Parse(r *http.Request, target interface{}) error
}

type RequestHandler

type RequestHandler func(context *Context)

type Router

type Router struct {
	Handlers      []RouterMapping
	HandlerRouter *mux.Router
	Middleware    []Middleware
}

func NewRouter

func NewRouter() *Router

func (*Router) AddHandler

func (r *Router) AddHandler(pattern string, handler RequestHandler)

func (*Router) DELETE

func (r *Router) DELETE(pattern string, handler RequestHandler)

func (*Router) GET

func (r *Router) GET(pattern string, handler RequestHandler)

func (*Router) METHODS

func (r *Router) METHODS(pattern string, methods []string, handler RequestHandler)

func (*Router) MakeHandlerContext

func (r *Router) MakeHandlerContext(writer http.ResponseWriter, request *http.Request, pattern string) *Context

func (*Router) PATCH

func (r *Router) PATCH(pattern string, handler RequestHandler)

func (*Router) POST

func (r *Router) POST(pattern string, handler RequestHandler)

func (*Router) PUT

func (r *Router) PUT(pattern string, handler RequestHandler)

func (*Router) Static

func (r *Router) Static(pattern string, staticPath string)

type RouterMapping

type RouterMapping struct {
	Pattern     string
	HandlerFunc RequestHandler
}

type View

type View interface {
	Render(data interface{}) (err error)
	Write(context *Context)
}

type XML

type XML map[string]interface{}

type XMLParser

type XMLParser struct {
}

func (*XMLParser) Parse

func (p *XMLParser) Parse(r *http.Request, target interface{}) error

type XMLView

type XMLView struct {
	Data []byte
}

func (*XMLView) Render

func (v *XMLView) Render(data interface{}) (err error)

func (*XMLView) Write

func (v *XMLView) Write(context *Context)

func (*XMLView) WriteWithStatus

func (v *XMLView) WriteWithStatus(context *Context, status int)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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