vssrouter

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2022 License: GPL-3.0 Imports: 9 Imported by: 0

README

virsas-mod-router

We reuse chi router in many projects. So the router creation is extracted to its own module.

Variables

  • allowedOrigins ([]string) - list of allowed origins - []string{"*"}
  • allowedMethods ([]string) - list of allowed methogs - []string{"GET", "POST", "PUT", "DELETE"}
  • allowedHeaders ([]string) - list of allowed headers - []string{"Accept", "Authorization", "APIKey", "Content-Type", "X-CSRF-Token"}
  • exposedHeaders ([]string) - list of safe headers to expose - []string{"link"}
  • allowedCredentials (bool) - if request can include user credentials
  • maxAge (int) - how long (in seconds) the results of a preflight request can be cached
  • enableLogging (bool) - enable middleware logger. In case you dont want to log health, disable it here and enable it in routes

Environmental variables

  • VSS_ROUTER_HEALTH - Enable / Disable /health for system check. Default true.
  • VSS_ROUTER_RATE_PER_MINUTE - COnfigure a number of concurrent connections from single IP per minute. Default 100 connections.
  • VSS_ROUTER_LOGGING - Enable Logging. In case you dont want to log health, disable it here and enable it in routes. Default true.
  • VSS_ROUTER_TIMEOUT - Timeout that cancels ctx after a given timeout and return a 504 Gateway Timeout error to the client. Default value 60 seconds.

Example

package main

import (
    "app/router"
    ...
    vssrouter "github.com/virsas/virsas-mod-router"
    ...
)

func main () {
    allowedOrigins := []string{"*"},
    allowedMethods := []string{"GET", "POST", "PUT", "DELETE"},
    allowedHeaders := []string{"Accept", "Authorization", "APIKey", "Content-Type", "X-CSRF-Token"},
    exposedHeaders := []string{"Link"}
    allowedCredentials := true
    maxAge := 300

    r, err := vssrouter.CreateRouter(allowedOrigins, allowedMethods, allowedHeaders, exposedHeaders, allowedCredentials, maxAge)
    if err != nil {
        log.Fatal(err)
    }
	router.AddRoutes(r)
    ...
    log.Fatal(http.ListenAndServe(":3000", r))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateRouter

func CreateRouter(allowedOrigins []string, allowedMethods []string, allowedHeaders []string, exposedHeaders []string, allowedCredentials bool, maxAge int) (chi.Router, error)

CreateRouter function

func Respond

func Respond(w http.ResponseWriter, data map[string]interface{})

Respond function - json respond of the router

func RespondMsg

func RespondMsg(status bool, message string) map[string]interface{}

RespondMsg function creates a message for respond including main items like status (true, false) and message (success or err)

Types

This section is empty.

Jump to

Keyboard shortcuts

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