pulse

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2023 License: MIT Imports: 9 Imported by: 1

README

Go Report Card GitHub license Go Reference Go Doc Discord Online codecov CircleCI

A Golang framework for web development that keeps your web applications and services responsive with its fast and lightweight design.

Features

  • Routing
  • Route groups
  • Static files
  • Simple and elegant API
  • Middleware support

Installation

Make sure you have Go installed on your machine. Then run the following command:

Initialize your project (Learn). Then install Pulse with the go get command:

go get github.com/gopulse/pulse

Getting Started

package main

import (
	"github.com/gopulse/pulse"
)

func main() {
	app := pulse.New()
	router := pulse.NewRouter()

	app.Router = router

	router.Get("/", func(c *pulse.Context) error {
		c.String("Hello, World!")
		return nil
	})

	app.Run(":3000")
}

Examples

  • Routing

Supports GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD, CONNECT, TRACE

package main

import (
	"github.com/gopulse/pulse"
)

func main() {
    app := pulse.New()
    router := pulse.NewRouter()
    
    // GET /hello
    router.Get("/", func(c *pulse.Context) error {
        c.String("Hello, World!")
        return nil
    })
    
    // GET /hello/:name
    router.Get("/profile/:id", func(c *pulse.Context) error {
        c.String("Profile: " + c.Param("id"))
        return nil
    })
    
	// GET /user/
    router.Get("/user/*", func(c *pulse.Context) error {
        c.String("Hello, World!")
        return nil
    })
    
    app.Router = router
    
    app.Run(":3000")
}
  • Route groups

Supports GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD, CONNECT, TRACE

package main

import (
	"github.com/gopulse/pulse"
)

func main() {
	app := pulse.New()
	router := pulse.NewRouter()
	api := &pulse.Group{
		prefix: "/api",
		router: router,
	}

	v1 := api.Group("/v1")
	v1.GET("/users", func(ctx *Context) error {
		ctx.String("users")
		return nil
	})

	app.Router = router

	app.Run(":3000")
}
  • Static files
package main

import (
	"github.com/gopulse/pulse"
	"time"
)

func main() {
	app := pulse.New()
	router := pulse.NewRouter()

	// Static files (./static) with cache duration 24 hours
	router.Static("/", "./static", &pulse.Static{
		Compress:      true,
		ByteRange:     false,
		IndexName:     "index.html",
		CacheDuration: 24 * time.Hour,
	})

	app.Router = router

	app.Run(":3000")
}
  • Middleware
package main

import (
	"github.com/gopulse/pulse"
)

func main() {
	app := pulse.New()
	router := pulse.NewRouter()

	router.Get("/profile/:name", func(ctx *pulse.Context) error {
		if ctx.Param("name") != "test" {
			ctx.Abort()
			ctx.Status(404)
			return nil
		}
		ctx.String("hello")
		ctx.Next()
		return nil
	})

	app.Router = router

	app.Run(":3000")
}

Available Middleware

  • CORS Middleware: Enable cross-origin resource sharing (CORS) with various options.
package main

import (
	"github.com/gopulse/pulse"
)

func main() {
	app := pulse.New()
	router := pulse.NewRouter()

	router.Get("/", func(ctx *pulse.Context) error {
		return nil
	})

	router.Use("GET", pulse.CORSMiddleware())

	app.Router = router

	app.Run(":3000")
}
  • Logger Middleware: Log every request with configurable options. (Coming soon)
  • Encrypt Cookie Middleware: Encrypt and decrypt cookie values. (Coming soon)
  • Timeout Middleware: Set a timeout for requests. (Coming soon)

License

Pulse is licensed under the MIT License. See LICENSE for the full license text.

Contributing

Contributions are welcome! Please read the contribution guidelines first.

Support

If you want to say thank you and/or support the active development of Pulse:

  1. Add a GitHub Star to the project.
  2. Tweet about the project on your Twitter
  3. Write a review or tutorial on Medium, dev.to, Reddit or personal blog.
  4. Buy Me a Coffee

Contributors

Stargarazers over time

Stargazers over time

Documentation

Index

Constants

View Source
const (
	// DefaultAppName is the default app name
	DefaultAppName = "Pulse"

	// DefaultNetwork is the default network
	DefaultNetwork = "tcp"
)

Variables

This section is empty.

Functions

func RouterHandler

func RouterHandler(router *Router) http.HandlerFunc

Types

type Config

type Config struct {
	// AppName is the name of the app
	AppName string `json:"app_name"`

	// Network is the network to use
	Network string `json:"network"`
}

type Context

type Context struct {
	ResponseWriter http.ResponseWriter
	Response       http.Response
	Request        *http.Request
	Params         map[string]string

	Cookies []*http.Cookie
	// contains filtered or unexported fields
}

func NewContext

func NewContext(w http.ResponseWriter, req *http.Request) *Context

NewContext returns a new Context.

func (*Context) Abort

func (c *Context) Abort()

Abort aborts the chain.

func (*Context) Accepts

func (c *Context) Accepts(types ...string) string

Accepts checks if the specified content types are acceptable.

func (*Context) BodyParser added in v0.5.0

func (c *Context) BodyParser(v interface{}) error

func (*Context) ClearCookie

func (c *Context) ClearCookie(name string)

ClearCookie deletes the cookie with the given name.

func (*Context) GetCookie

func (c *Context) GetCookie(name string) string

GetCookie returns the value of the cookie with the given name.

func (*Context) GetRequestHeader added in v0.4.0

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

GetRequestHeader GetResponseHeader returns the http header value for the given key.

func (*Context) GetResponseHeader added in v0.4.0

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

GetResponseHeader returns the http header value for the given key.

func (*Context) GetValue added in v1.0.0

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

GetValue returns the value for the given key.

func (*Context) JSON added in v0.4.0

func (c *Context) JSON(code int, obj interface{}) ([]byte, error)

JSON sets the response body to the given JSON representation.

func (*Context) Next

func (c *Context) Next() error

Next calls the next handler in the chain.

func (*Context) Param

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

Param returns the param value for the given key.

func (*Context) Query added in v0.4.0

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

Query returns the query value for the given key.

func (*Context) Reset

func (c *Context) Reset()

Reset resets the Context.

func (*Context) SetContentType added in v0.4.0

func (c *Context) SetContentType(value string)

SetContentType sets the Content-Type header in the response to the given value.

func (*Context) SetCookie

func (c *Context) SetCookie(cookie *Cookie)

SetCookie sets a cookie with the given name, value, and options.

func (*Context) SetRequestHeader added in v0.4.0

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

SetRequestHeader SetResponseHeader sets the http header value to the given key.

func (*Context) SetResponseHeader added in v0.4.0

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

SetResponseHeader sets the http header value to the given key.

func (*Context) SetValue added in v1.0.0

func (c *Context) SetValue(key interface{}, value interface{})

SetValue create a middleware that adds a value to the context

func (*Context) Status

func (c *Context) Status(code int)

Status sets the response status code.

func (*Context) String

func (c *Context) String(value string)

String sets the response body to the given string.

func (*Context) WithParams

func (c *Context) WithParams(params map[string]string) *Context

WithParams sets the params for the context.

func (*Context) Write added in v1.0.0

func (c *Context) Write(p []byte) (n int, err error)
type Cookie struct {
	Name     string
	Value    string
	Path     string
	Domain   string
	MaxAge   int
	Expires  time.Time
	Secure   bool
	HTTPOnly bool
	SameSite http.SameSite
}

type Group added in v0.2.0

type Group struct {
	Prefix string
	Router *Router
}

func (*Group) DELETE added in v0.2.0

func (g *Group) DELETE(path string, handlers ...Handler)

func (*Group) GET added in v0.2.0

func (g *Group) GET(path string, handlers ...Handler)

func (*Group) Group added in v0.2.0

func (g *Group) Group(prefix string) *Group

func (*Group) HEAD added in v0.2.0

func (g *Group) HEAD(path string, handlers ...Handler)

func (*Group) OPTIONS added in v0.2.0

func (g *Group) OPTIONS(path string, handlers ...Handler)

func (*Group) PATCH added in v0.2.0

func (g *Group) PATCH(path string, handlers ...Handler)

func (*Group) POST added in v0.2.0

func (g *Group) POST(path string, handlers ...Handler)

func (*Group) PUT added in v0.2.0

func (g *Group) PUT(path string, handlers ...Handler)

func (*Group) Static added in v0.2.0

func (g *Group) Static(path, root string, config *Static)

func (*Group) Use added in v0.2.0

func (g *Group) Use(middleware Middleware)

type Handler

type Handler func(ctx *Context) error

type HandlerFunc added in v1.0.0

type HandlerFunc func(w http.ResponseWriter, r *http.Request) error

type Middleware

type Middleware interface {
	Middleware(handler Handler) Handler
	Handle(ctx *Context, next Handler) error
}

type MiddlewareFunc

type MiddlewareFunc func(handler Handler) Handler

func CORSMiddleware

func CORSMiddleware() MiddlewareFunc

func (MiddlewareFunc) Handle

func (m MiddlewareFunc) Handle(ctx *Context, next Handler) error

func (MiddlewareFunc) Middleware

func (m MiddlewareFunc) Middleware(handler Handler) Handler

type Pulse

type Pulse struct {
	Router *Router
	// contains filtered or unexported fields
}

func New

func New(config ...Config) *Pulse

func (*Pulse) Run

func (p *Pulse) Run(address string)

func (*Pulse) Stop added in v0.3.0

func (p *Pulse) Stop() error

type Route

type Route struct {
	Method     string
	Path       string
	Handlers   []Handler
	ParamNames []string
}

type Router

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

func NewRouter

func NewRouter() *Router

func (*Router) Add added in v1.0.0

func (r *Router) Add(method, path string, handlers ...Handler)

func (*Router) Connect

func (r *Router) Connect(path string, handlers ...Handler)

Connect adds the route to the router with the CONNECT method

func (*Router) Delete

func (r *Router) Delete(path string, handlers ...Handler)

Delete adds the route to the router with the DELETE method

func (*Router) Find added in v1.0.0

func (r *Router) Find(method, path string) []Handler

func (*Router) Get

func (r *Router) Get(path string, handlers ...Handler)

Get adds the route to the router with the GET method

func (*Router) Head

func (r *Router) Head(path string, handlers ...Handler)

Head adds the route to the router with the HEAD method

func (*Router) Options

func (r *Router) Options(path string, handlers ...Handler)

Options adds the route to the router with the OPTIONS method

func (*Router) Patch

func (r *Router) Patch(path string, handlers ...Handler)

Patch adds the route to the router with the PATCH method

func (*Router) Post

func (r *Router) Post(path string, handlers ...Handler)

Post adds the route to the router with the POST method

func (*Router) Put

func (r *Router) Put(path string, handlers ...Handler)

Put adds the route to the router with the PUT method

func (*Router) Static

func (r *Router) Static(prefix, root string, options *Static)

func (*Router) Trace

func (r *Router) Trace(path string, handlers ...Handler)

Trace adds the route to the router with the TRACE method

func (*Router) Use

func (r *Router) Use(method string, middlewares ...interface{})

type Static

type Static struct {
	Root          string
	Compress      bool
	ByteRange     bool
	IndexName     string
	CacheDuration time.Duration
}

func (*Static) PathRewrite added in v1.0.0

func (options *Static) PathRewrite(r *http.Request) []byte

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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