utils

package
v0.0.0-...-f5986cb Latest Latest
Warning

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

Go to latest
Published: May 21, 2020 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Overview

Package utils provides utils for web interaction and handling requests, project configuration, interaction with primitive types etc.

Index

Constants

View Source
const (
	// AuthenticatedUserModelKey is a key used by auth middleware to store user model.
	AuthenticatedUserModelKey = "AuthenticatedUserModelKey"
)

Variables

View Source
var ErrIntParamInvalid = errors.New("int_param_invalid")

ErrIntParamInvalid indicates an error of a validated integer parameter.

View Source
var ErrUnauthorized = errors.New("unauthorized")

ErrUnauthorized indicates bad user authentication.

Functions

func Contains

func Contains(val string, arr []string) bool

Contains returns true if "arr" array contains a "val" string.

func ContainsAll

func ContainsAll(what []string, where []string) bool

ContainsAll returns true if "where" array contains each element from a "what" array.

func FileExists

func FileExists(filename string) bool

FileExists returns true if a file exists and no errors occurred.

func GetUser

func GetUser(c *gin.Context) (*model.User, error)

GetUser returns user object based on an extracted user id.

func InterfaceArrayToStringArray

func InterfaceArrayToStringArray(i []interface{}) []string

InterfaceArrayToStringArray converts interface array to a string array.

func ReadFile

func ReadFile(path string) ([]byte, error)

ReadFile reads file content from "path".

func RespondError

func RespondError(c *gin.Context, code int, payload interface{})

RespondError writes provided payload to "error" field of a response JSON object with a provided HTTP code.

func RespondJSON

func RespondJSON(c *gin.Context, code int, payload interface{})

RespondJSON writes provided payload to response as JSON with provided HTTP code.

func RespondResult

func RespondResult(c *gin.Context, payload interface{})

RespondResult writes provided payload to "result" field of a response JSON object with a 200 HTTP code.

func Substring

func Substring(input string, start int, length int) string

Substring returns substring of an "input" string from "start" till "end".

func ValidIntQueryParam

func ValidIntQueryParam(param string, min int, max int) (*int, error)

ValidIntQueryParam validates provided string parameter as an integer in a [min, max] range.

func WaitForShutdown

func WaitForShutdown(srv *http.Server)

WaitForShutdown provides graceful shutdown of an HTTP server.

Types

type ClientConfig

type ClientConfig struct {
	Secret        string
	AllowedScopes []string `yaml:"allowed_scopes"`
}

ClientConfig is a config for oauth clients.

type Config

type Config struct {
	Server struct {
		Port           uint16
		PathPrefixData string `yaml:"path_prefix_data"`
	}
	Clients map[string]ClientConfig
	Mongo   data.MongoSettings
	Vk      vk.Settings
	Jwt     model.JwtSettings
}

Config is a project config.

func ReadConfig

func ReadConfig(path string) (*Config, error)

ReadConfig maps yml config from path to Config struct. Edited: now private config moved to environment variables, and set from Drone CI secrets.

Jump to

Keyboard shortcuts

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