Popularity
0.5
Stable
Activity
0.0
Stable
0
3
0

Description

Flash Restful API framework in Golang. Wrapper on gin-gonic engine to structure the api in better way and easy to maintain.

Programming language: Go
License: BSD 3-clause "New" or "Revised" License
Tags: Go-gin     Restful Api     Api-server     Go Api     Microservice    
Latest version: v1.2

README

Build Status codecov.io Godocs Go Report Card Open Source Helpers Release

flash

Flash Restful API framework in Golang. Wrapper on gin-gonic engine to structure the api in better way and easy to maintain.

Contents

Installation

To install flash package, you need to install Go and set your Go workspace first. As flash works on gin-gonic, So all gin-gonic Prerequisite are required.

  1. Download and install it:
$ go get -u github.com/mayur-tolexo/flash
  1. Import it in your code:
import "github.com/mayur-tolexo/flash"

Example

# create a main file
$ vim main.go
package main

import (
    "github.com/gin-gonic/gin"
    "github.com/mayur-tolexo/flash"
)

//PingService service struct containing only one api
type PingService struct {
    flash.Server `v:"1" root:"/test/"`
    ping         flash.GET `url:"/ping"`
}

//Ping api defination
func (*PingService) Ping(c *gin.Context) {
    c.JSON(200, gin.H{
        "message": "pong",
    })
}

func main() {
    router := flash.Default()
    router.AddService(&PingService{})
    router.Start(":8080")
}
# now run the main service
$ go run main.go

now open http://localhost:8080/v1/test/ping

Configuration

Tag Usage
prefix, pre Url prefix as in: http://localhost:8080/[prefix]/v1/root/url
version, v Url version as in: http://localhost:8080/prefix/v[1]/root/url
root Url root as in: http://localhost:8080/prefix/v1/[root]/url
url Url path as in: http://localhost:8080/prefix/v1/root/[url]

API Examples

Using GET, POST, PUT, PATCH, DELETE and OPTIONS

//TestService service struct
type TestService struct {
    flash.Server    `v:"1" root:"/test/"`
    getAPI          flash.GET `url:"/"`
    postAPI         flash.POST `url:"/"`
    putAPI          flash.PUT `url:"/"`
    patchAPI        flash.PATCH `url:"/"`
    putAPI          flash.PUT `url:"/"`
    deleteAPI       flash.DELETE `url:"/"`
    optionsAPI      flash.OPTIONS `url:"/"`
}

func main() {
    router := flash.Default()
    router.AddService(&TestService{})
    router.Start(":8080")
}

Using Configuration

import "github.com/mayur-tolexo/flash"

//PingService service struct containing only one api
type PingService struct {
    flash.Server `v:"1" root:"test" pre:"abc/"`
    ping         flash.GET `url:"/ping"`
    ping2         flash.GET `url:"/ping" v:"2"`
}

//Ping api defination
func (*PingService) Ping(c *gin.Context) {
    c.JSON(200, gin.H{
        "message": "pong",
    })
}

//Ping2 api defination
func (*PingService) Ping2(c *gin.Context) {
    c.JSON(200, gin.H{
        "message": "pong from version 2",
    })
}

func main() {
    router := flash.Default()
    router.AddService(&PingService{})
    router.Start(":8080")
}

First API url http://localhost:8080/abc/v1/test/ping
Second API url http://localhost:8080/abc/v2/test/ping

Middleware

Global Middleware
package main

import (
    "github.com/gin-gonic/gin"
    "github.com/mayur-tolexo/flash"
)

//PingService service struct containing only one api
type PingService struct {
    flash.Server `v:"1" root:"/test/"`
    ping         flash.GET `url:"/ping"`
}

//Ping api defination
func (*PingService) Ping(c *gin.Context) {
    c.JSON(200, gin.H{
        "message": "pong",
    })
}

func globalMiddleware() gin.HandlerFunc {
    return func(c *gin.Context) {
        start := time.Now()
        c.Next()
        fmt.Println("Global Middleware Response time", time.Since(start).Seconds())
    }
}

func main() {
    router := flash.Default()
    router.AddService(&PingService{})

    //this is global middleware which will be call for all the services' endpoints
    router.Use(globalMiddleware())
    router.Start(":8080")
}
Local middleware i.e. service-wise middleware
package main

import (
    "github.com/gin-gonic/gin"
    "github.com/mayur-tolexo/flash"
)

//PingService service struct containing only one api
type PingService struct {
    flash.Server `v:"1" root:"/test/"`
    ping         flash.GET `url:"/ping"`
}

//Ping api defination
func (*PingService) Ping(c *gin.Context) {
    c.JSON(200, gin.H{
        "message": "pong",
    })
}

//Middlewares defined only on ping service endpoints
func (*PingService) Middlewares() []gin.HandlerFunc {
    return []gin.HandlerFunc{serviceMiddleware()}
}

func serviceMiddleware() gin.HandlerFunc {
    return func(c *gin.Context) {
        start := time.Now()
        c.Next()
        fmt.Println("Service Middleware Response time", time.Since(start).Seconds())
    }
}

func main() {
    router := flash.Default()
    router.AddService(&PingService{})
    router.Start(":8080")
}