Gateway
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