api-router-go

command module
v0.9.8 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

README

Build Code Coverage CII Best Practices Maintainability

Api-Router-Go

Routing DSL to possibly help in building go API quickly for projects.

Running Example App

go run router-example.go

Quickstart

package main

import (
  "github.com/skhatri/api-router-go/router"
  "github.com/skhatri/api-router-go/router/functions"
  "github.com/skhatri/api-router-go/starter"
  "os"
)

func main() {
  starter.StartApp(os.Args, 6200, func (cfg router.ApiConfigurer) {
    cfg.Get("/echo", functions.EchoFunc)
  })
}

Start your own server

import (
  "github.com/skhatri/api-router-go/router"
  "github.com/skhatri/api-router-go/starter"
  "github.com/skhatri/api-router-go/router/functions"
)

func main() {
  starter.RunApp(func(configurer router.ApiConfigurer) {
		configurer.Get("/echo", functions.EchoFunc)
	})
}

Serving Static Paths

Static paths can be provided by ApiConfigurer DSL like this

    configurer.
        Static("test", "test").

It can also be provided in router.json. Here is the relevant configuration in router.json

  "static": {
    "source": ""
  },

The above configuration will create static file server to serve all content in the current directory under the path /source

Path Variables Support

Path variables can be specified with a prefix : in the configured uri.

    configurer.
        Get("/greetings/:id", functions.EchoFunc)

They can be extracted from injected WebRequest using the following:

    func myHandler(web *router.WebRequest) *router.Container {
        id := web.GetPathParam("id")
        ...
    }

Other Configurations

A "router.json" can be provided either in the same folder as the binary or via ROUTE_SETTINGS environment variable to add default response headers and variables that are to be exposed via RouteSettings.

{
  "response-headers": {
    "x-served-by": "Api-Router-Go",
    "access-control-allow-origin": "http://localhost:5000",
    "access-control-allow-methods": "GET, POST, OPTIONS",
    "access-control-allow-headers": "X-Auth-Token, Content-Type, X-Client-Id",
    "access-control-allow-credentials": "false",
    "access-control-max-age": "7200"
  },
  "variables": {
    "service_a_url": "http://localhost:7999"
  },
  "toggles": {
    "read-mode": true,
    "write-mode": false
  },
  "app": {
    
  }
}

Variables and Toggles can be read like this

settings.GetSettings().Variable("service_a_url");

settings.GetSettings().IsToggleOn("read-mode");
settings.GetSettings().IsToggleOff("write-mode");
settings.GetSettings().IsOn("read-mode");

Returning Responses

Container type can be returned from each function which decorates with nodes like errors, warning, metadata and data.

func StatusFunc(_ *router.WebRequest) *model.Container {
	return model.Response(statusResult)
}

The above snippet will render

{"data":{"status": "OK"}}

If decoration is not preferred, you can create container using WithDataOnly

func StatusFunc(_ *router.WebRequest) *model.Container {
	return model.WithDataOnly(statusResult)
}

The above snippet will render

{"status": "OK"}

Running TLS

openssl genrsa -out private.key 2048
openssl req -new -x509 -sha256 -key private.key -out cert.pem -days 730

Add generated key and cert into router.json

"transport": {
    "port": 6100,
    "tls": {
      "enabled": true,
      "private-key": "private.key",
      "public-key": "cert.pem"
    }
  }

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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