slide

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2020 License: MIT Imports: 16 Imported by: 0

README ¶

Slide, a Go web framework for Building API(s)

codecov Go Report Card

tags: Go Express

Slide is one of the fastest frameworks, built on top of fasthttp. People coming form express will feel at home.

Motivation

While playing around with the Go’s net/http, one thing that we missed most was the lack of middleware support and the problems it solves. After a little reading, we decided to write our own web framework which would solve the issues like middleware support with next, handle a wide range of files, Upload, Download, etc.

💡 Note: We are still in experimental stage, would love to hear feedback.

Installation
go get -u github.com/go-slide/slide
🚀 Example

For more API information check Docs

package main

import (
	"log"
	"github.com/go-slide/slide"
	"github.com/go-playground/validator/v10"
)

func main() {
    validate := validator.New()
    config := slide.Config{
	Validator: validate,
    }
    app := slide.InitServer(&config)
    app.Get("/", func(ctx *slide.Ctx) error {
        return ctx.Send(http.StatusOK, "Hello, World")
    })
    log.Fatal(app.Listen("localhost:4321"))
}

Routing

Slide supports multilevel routing.

app := slide.InitServer(&config)

app.Get("/", func(ctx *slide.Ctx) error {
    return ctx.Send(http.StatusOK, "Hello, World")
})

// Grouping your route
auth := app.Group("/auth")
auth.Get("/login", func(ctx *slide.Ctx) error {
    return ctx.Send(http.StatusOK, "Hello, World")
})

Middleware

Slide supports wide range of middlewares.

  1. Application Level
  2. Group Level
  3. Route Level
app := slide.InitServer(&config)

## Application level
app.Use(func(ctx *slide.Ctx) error {
    fmt.Println("this will run for all URL(s)")
    return ctx.Next()
})

//Group Level
auth := app.Group("/auth")
auth.Use(func(ctx *slide.Ctx) error {
    fmt.Println("this will run for all /auth URL(s)")
    return ctx.Next()
})
auth.Get("/login", func(ctx *slide.Ctx) error {
    return ctx.Send(http.StatusOK, "Hello, World")
})

// Route level, works in Right -> Left or Bottom to Top
app.Get("/routermiddleware", func(ctx *slide.Ctx) error {
    return ctx.Send(http.StatusOK, "hola!")
}, func(ctx *slide.Ctx) error {
    fmt.Println("this prints second", ctx.RequestCtx.UserValue("lol"))
    return ctx.Next()
}, func(ctx *slide.Ctx) error {
    fmt.Println("this prints first")
    return ctx.Next()
})

Benchmark

autocannon -c 100 -d 40 -p http://localhost:4321/
Framework No of requests
Slide 2765K

💻 Core Contributors Sai Umesh Madhuri

Documentation ¶

Index ¶

Constants ¶

View Source
const (
	GET    = http.MethodGet
	POST   = http.MethodPost
	PUT    = http.MethodPut
	DELETE = http.MethodDelete

	ContentType       = "Content-Type"
	ContentDeposition = "Content-Disposition"
	ApplicationJSON   = "application/json"
	Attachment        = "attachment"

	// cors headers
	HeaderOrigin                        = "Origin"
	HeaderVary                          = "Vary"
	HeaderAccessControlAllowOrigin      = "Access-Control-Allow-Origin"
	HeaderAccessControlAllowMethods     = "Access-Control-Allow-Methods"
	HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials"
	HeaderAccessControlExposeHeaders    = "Access-Control-Expose-Headers"
	HeaderAccessControlRequestMethod    = "Access-Control-Request-Method"
	HeaderAccessControlRequestHeaders   = "Access-Control-Request-Headers"
	HeaderAccessControlAllowHeaders     = "Access-Control-Allow-Headers"
	HeaderAccessControlMaxAge           = "Access-Control-Max-Age"

	// routing error messages
	NotFoundMessage = "Not Found, Check URL"
)

...

Variables ¶

This section is empty.

Functions ¶

This section is empty.

Types ¶

type Config ¶

type Config struct {
	Validator *validator.Validate
}

Config -- Configuration for slide

type Ctx ¶

type Ctx struct {
	RequestCtx *fasthttp.RequestCtx
	Next       func() error
	// contains filtered or unexported fields
}

Ctx -- route level context

func (*Ctx) Bind ¶

func (ctx *Ctx) Bind(input interface{}) error

Bind Deserialize body to struct

func (*Ctx) GetParam ¶

func (ctx *Ctx) GetParam(name string) string

GetParam - Getting path param

/name/:name

/name/slide

name := ctx.GetParam("name")

name = slide

func (*Ctx) GetParams ¶

func (ctx *Ctx) GetParams() map[string]string

GetParams returns map of path params

routerPath /auth/:name/:age

requestPath /auth/madhuri/32

returns { name: madhuri, age: 32 }

func (*Ctx) GetQueryParam ¶

func (ctx *Ctx) GetQueryParam(name string) string

GetQueryParam returns value of a single query Param

route path /hello?key=test&value=bbp

keyValue = GetQueryParam(key)

keyValue = test

func (*Ctx) GetQueryParams ¶

func (ctx *Ctx) GetQueryParams() map[string]string

GetQueryParams returns map of query Params

route path /hello?key=test&value=bbp

returns {key : test, value : bbp}

func (*Ctx) JSON ¶

func (ctx *Ctx) JSON(statusCode int, payload interface{}) error

JSON Sending application/json response

func (*Ctx) Redirect ¶

func (ctx *Ctx) Redirect(statusCode int, url string) error

Redirect to new url reference https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections#Temporary_redirections status codes between 300-308

func (*Ctx) Send ¶

func (ctx *Ctx) Send(statusCode int, payload string) error

Send Sending a text response

func (*Ctx) SendAttachment ¶

func (ctx *Ctx) SendAttachment(filePath, fileName string) error

SendAttachment Sending attachment

here fileName is optional

func (*Ctx) SendStatusCode ¶

func (ctx *Ctx) SendStatusCode(statusCode int) error

SendStatusCode -- sending response without any body

func (*Ctx) ServeFile ¶

func (ctx *Ctx) ServeFile(filePath string) error

ServeFile serving file as response

func (*Ctx) UploadFile ¶

func (ctx *Ctx) UploadFile(filePath, fileName string) error

UploadFile uploads file to given path

type Group ¶

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

Group -- router group

func (*Group) Delete ¶

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

Delete method of slide

func (*Group) Get ¶

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

Get method of slide

func (*Group) Group ¶

func (g *Group) Group(path string) *Group

Group method

func (*Group) Post ¶

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

Post method of slide

func (*Group) Put ¶

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

Put method of slide

func (*Group) Use ¶

func (g *Group) Use(h handler)

Use -- group level middleware

type Slide ¶

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

Slide -- Slide config

func InitServer ¶

func InitServer(config *Config) *Slide

InitServer -- initializing server with slide config

func (*Slide) Delete ¶

func (slide *Slide) Delete(path string, h ...handler)

Delete method of slide

func (*Slide) Get ¶

func (slide *Slide) Get(path string, h ...handler)

Get method of slide

func (*Slide) Group ¶

func (slide *Slide) Group(path string) *Group

Group method

func (*Slide) HandleErrors ¶

func (slide *Slide) HandleErrors(h errHandler)

HandleErrors Handling errors at application level

func (*Slide) HandleNotFound ¶

func (slide *Slide) HandleNotFound(h handler)

HandleNotFound custom 404 handler

func (*Slide) Listen ¶

func (slide *Slide) Listen(host string) error

Listen -- starting server with given host

func (*Slide) Post ¶

func (slide *Slide) Post(path string, h ...handler)

Post method of slide

func (*Slide) Put ¶

func (slide *Slide) Put(path string, h ...handler)

Put method of slide

func (*Slide) ServeFile ¶

func (slide *Slide) ServeFile(path, filePath string)

ServeFile -- serving single file

func (*Slide) ServerDir ¶

func (slide *Slide) ServerDir(path, dir string)

ServerDir files as routes

func (*Slide) Use ¶

func (slide *Slide) Use(h handler)

Use -- application level middleware

Directories ¶

Path Synopsis

Jump to

Keyboard shortcuts

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