flash

package module
v0.0.0-...-87d2a91 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2020 License: BSD-3-Clause Imports: 8 Imported by: 6

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")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DELETE

type DELETE struct{ MetaData }

DELETE api

type GET

type GET struct{ MetaData }

GET api

type MetaData

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

MetaData contains the api metadata

type Method

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

Method need to call by the api

type OPTIONS

type OPTIONS struct{ MetaData }

OPTIONS api

type PATCH

type PATCH struct{ MetaData }

PATCH api

type POST

type POST struct{ MetaData }

POST api

type PUT

type PUT struct{ MetaData }

PUT api

type Server

type Server struct {
	*gin.Engine
	MetaData
	// contains filtered or unexported fields
}

Server model

func Default

func Default() *Server

Default will return default service engine

func New

func New() *Server

New will return new service engine

func (*Server) AddService

func (s *Server) AddService(service interface{}) (err error)

AddService will add new service in server service is a pointer to the struct of the api

func (*Server) Start

func (s *Server) Start(port ...string) (err error)

Start will start the server

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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