gateway

package module
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2020 License: Apache-2.0 Imports: 11 Imported by: 0

README

Neuron Logo

Gateway Go Report Card GoDoc Build Status GitHub

The Gateway is golang http gateway based on the neuron.

What is Gateway?

The Gateway is the golang library that allows to create http gatway based on the neuron models. Current version implements the jsonapi server. It uses httprouter as a handler. This also applies to middlewares used by the gateway - it uses custom httprouter based middlewares.

Handler

The default handler used by the Gateway is 'jsonapi'. It implements jsonapi v1.0 server.

Config

The gateway uses viper configuration tags, so that it is easy extractable from different sources. It allows to use custom preset middlewares, filters, pagination, sorts for all the endpoints. In order to use custom middlewares they need to be registered in the main code previously.

Quick-Start

The gateway uses neuron-core as it's ORM. Thus the first step requires to initialize it.

import (
    "os"
    "fmt"

    nconfig "github.com/neuronlabs/neuron-core/config"
    "github.com/neuronlabs/neuron-core"    

    "github.com/neuronlabs/gateway"
    "github.com/neuronlabs/gateway/config"
)

// register some default repository (i.e. Postgres)
import _ "github.com/neuronlabs/neuron-pq"

// register the jsonapi handler
import _ "github.com/neuronlabs/gateway/handler/jsonapi"

func main(){
    // Create controller.
    cfg := nconfig.Default()
    c := neuron.NewController(cfg)
    // Register the models - in this example Model1 and Model2
    err = c.RegisterModels(Model1{}, Model2{})
    if err != nil {
        fmt.Printf("ERROR: %s", err.Error())
        os.Exit(1)
    }

    // create gateway config.
    gatewayConfig, err := config.ReadGatewayConfig("gateway-config", "./")
    if err != nil {
        fmt.Printf("ERROR: %s", err.Error())
        os.Exit(1)
    }
    // set the handler to 'jsonapi'.
    gatewayConfig.Router.Name = "jsonapi"

    // create the gateway service
    svc, err := gateway.NewC(c, gatewayConfig)
    if err != nil {
        fmt.Printf("ERROR: %s", err.Error())
        os.Exit(1)
    }

    // run the service
    if err := svc.Run(context.Background()); err != nil {
        fmt.Printf("ERROR: %s", err.Error())
        os.Exit(1)
    }
}

This creates full jsonapi server.

It is also possible to use just the handler for your own server. In this case the following steps should be done:

import (
    "os"
    "fmt"
    "http"

    nconfig "github.com/neuronlabs/neuron-core/config"
    "github.com/neuronlabs/neuron-core"    

    "github.com/neuronlabs/gateway/handler"
    "github.com/neuronlabs/gateway/config"
)

// register some default repository (i.e. Postgres)
import _ "github.com/neuronlabs/neuron-pq"

// register the jsonapi handler
import _ "github.com/neuronlabs/gateway/handler/jsonapi"

func main(){
    // Create controller.
    cfg := nconfig.Default()
    c := neuron.NewController(cfg)
    // Register the models - in this example Model1 and Model2
    err = c.RegisterModels(Model1{}, Model2{})
    if err != nil {
        fmt.Printf("ERROR: %s", err.Error())
        os.Exit(1)
    }

    // create gateway config.
    gatewayConfig, err := config.ReadGatewayConfig("gateway-config", "./")
    if err != nil {
        fmt.Printf("ERROR: %s", err.Error())
        os.Exit(1)
    }
    // set the handler to 'jsonapi'.
    gatewayConfig.Router.Name = "jsonapi"

    creator, err := handler.Get("jsonapi")
    if err != nil {
        fmt.Printf("ERROR: %s", err.Error())
        os.Exit(1)
    }

    // create the handler
    h, err := creator(c, gatewayConfig.Router)
    if err != nil {
        fmt.Printf("ERROR: %s", err.Error())
        os.Exit(1)
    }

    // in order to apply the handler in the http.Server use the router method 
    // on it
    srv := http.Server{Handler: h.Router()}
    if err = srv.ListenAndServe(); err != nil {
        fmt.Printf("ERROR: %s", err.Error())
        os.Exit(1)
    }

Docs

(WIP) Full documentation would be available at: https://docs.neuronlabs.io/gateway

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Service

type Service struct {
	Controller *controller.Controller
	// Config is the configuration for the gateway
	Config *config.Gateway
	// Server is the default gateway service server
	Server *http.Server
	// Handler is the route handler.
	Handler handler.Handler
}

Service is the neuron based API Gateway service.

func Default

func Default(routerName string) *Service

Default creates the default gateway service for the provided router name

func New

func New(cfg *config.Gateway) (*Service, error)

New creates new Gateway Service for provided 'cfg' Config and default controller.

func NewC

func NewC(c *controller.Controller, cfg *config.Gateway) (*Service, error)

NewC creates new Gateway Service with the provided Controller 'c' and the Config 'cfg'.

func (*Service) Run

func (g *Service) Run(ctx context.Context) error

Run starts the service listening and serving processes. It might be interrupted by the provided config, os.Signal or the Server error.

func (*Service) SetHandler

func (g *Service) SetHandler() error

SetHandler sets the handler with the routes for the gateway service. If the function is not used manually the 'Run' would set the handler from the config.

Directories

Path Synopsis
Package errors contains API specific error implementation.
Package errors contains API specific error implementation.
Package middlewares contains all registered router middlewares.
Package middlewares contains all registered router middlewares.

Jump to

Keyboard shortcuts

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