sfutils

package module
v0.0.0-...-6e3dd02 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2021 License: MIT Imports: 25 Imported by: 0

README

SF labs utils

The main purpose of this repository is to collect code that can be reused in one or more applications in order to speed up their development.

Gin

Entry point for gin configuration is NewGin function.

Configuration can be done via next environment variables:

  • GIN_MODE - debug/release/test
  • GIN_TEMPLATES_MODE - debug/release (if the variable is not set, templates are not loaded)
  • GIN_TEMPLATES_PATH - absolute/relative path to html templates
  • ENABLE_PPROF - enable/disable golang profiler on path /system/pprof
  • ENABLE_PROMETHEUS - enable/disable gin metrics for prometheus on path /system/prometheus
  • ENABLE_OPENAPI - enable/disable openapi schema on path /system/openapi
Gin templates

A small object that makes it easier to work with html templates in gin. You can do without it if you don't need to render any html at all.

Gin OpenAPI

A small hack to add openapi specification file to /system/openapi.

How to use it:

  1. Set environment variable ENABLE_OPENAPI=true
  2. Add the following lines to your project:
//go:embed api.yml
var openAPIData []byte
  1. Point sfutils to this variable:
sfutils.SetOpenAPIData(openAPIData)
Remark about go pprof with basic auth
go tool pprof 'user:password@127.0.0.1:8080/system/pprof/heap'
go tool pprof 'user:password@127.0.0.1:8080/system/pprof/profile?seconds=30'

Documentation

Index

Constants

This section is empty.

Variables

View Source
var GooglePayPackageName string
View Source
var GooglePayPathCredentials string

Functions

func CheckGooglePlay

func CheckGooglePlay(productId, token string) (*androidpublisher.ProductPurchase, error)

CheckGooglePlay check if the service is available for payment and then get information about the product using his product_id

func GetOrCreateGooglePlayService

func GetOrCreateGooglePlayService() *androidpublisher.Service

GetOrCreateGooglePlayService return or create a new service for payment verification

func NewGin

func NewGin() *gin.Engine

NewGin returns new gin.Engine object, ready to use. Configuration can be done via next environment variables: GIN_MODE - debug/release/test GIN_TEMPLATES_MODE - debug/release (if the variable is not set, templates are not loaded) GIN_TEMPLATES_PATH - absolute/relative path to html templates ENABLE_PPROF - enable/disable golang profiler on path /system/pprof ENABLE_PROMETHEUS - enable/disable gin metrics for prometheus on path /system/prometheus ENABLE_OPENAPI - enable/disable openapi schema on path /system/openapi

func NewMetric

func NewMetric(m *Metric, subsystem string) prometheus.Collector

NewMetric associates prometheus.Collector based on Metric.Type

func SetOpenAPIData

func SetOpenAPIData(data []byte)

Types

type GinErrorHandler

type GinErrorHandler struct {
	// contains filtered or unexported fields
}

func NewGinErrorHandler

func NewGinErrorHandler() *GinErrorHandler

func (*GinErrorHandler) RegisterErrorType

func (g *GinErrorHandler) RegisterErrorType(i interface{})

func (*GinErrorHandler) S

type GinHandlerWithError

type GinHandlerWithError func(c *gin.Context) error

type IOSResponseData

type IOSResponseData struct {
	Status  float64 `json:"status"`
	Receipt struct {
		InApp []struct {
			ProductID             string `json:"product_id"`
			OriginalTransactionID string `json:"original_transaction_id"`
		} `json:"in_app"`
	} `json:"receipt"`
}

IOSResponseData Apple's response structure

func CheckApplePay

func CheckApplePay(password, receipt string) (*IOSResponseData, error)

CheckApplePay request the AppStore for complete product information

func RequestToPayPlatform

func RequestToPayPlatform(url, platform string, data *bytes.Buffer) (*IOSResponseData, error)

RequestToPayPlatform parsing the answer and issuing an answer on a given platform

type Metric

type Metric struct {
	MetricCollector prometheus.Collector
	ID              string
	Name            string
	Description     string
	Type            string
	Args            []string
}

Metric is a definition for the name, description, type, ID, and prometheus.Collector type (i.e. CounterVec, Summary, etc) of each metric

type Prometheus

type Prometheus struct {
	Ppg PrometheusPushGateway

	MetricsList []*Metric
	MetricsPath string

	ReqCntURLLabelMappingFn RequestCounterURLLabelMappingFn

	// gin.Context string to use as a prometheus URL label
	URLLabelFromContext string
	// contains filtered or unexported fields
}

Prometheus contains the metrics gathered by the instance and its path

func NewPrometheus

func NewPrometheus(subsystem string, customMetricsList ...[]*Metric) *Prometheus

NewPrometheus generates a new set of metrics with a certain subsystem name

func (*Prometheus) HandlerFunc

func (p *Prometheus) HandlerFunc() gin.HandlerFunc

HandlerFunc defines handler function for middleware

func (*Prometheus) SetListenAddress

func (p *Prometheus) SetListenAddress(address string)

SetListenAddress for exposing metrics on address. If not set, it will be exposed at the same address of the gin engine that is being used

func (*Prometheus) SetListenAddressWithRouter

func (p *Prometheus) SetListenAddressWithRouter(listenAddress string, r *gin.Engine)

SetListenAddressWithRouter for using a separate router to expose metrics. (this keeps things like GET /metrics out of your content's access log).

func (*Prometheus) SetMetricsPath

func (p *Prometheus) SetMetricsPath(e *gin.Engine)

SetMetricsPath set metrics paths

func (*Prometheus) SetMetricsPathWithAuth

func (p *Prometheus) SetMetricsPathWithAuth(e *gin.Engine, accounts gin.Accounts)

SetMetricsPathWithAuth set metrics paths with authentication

func (*Prometheus) SetPushGateway

func (p *Prometheus) SetPushGateway(pushGatewayURL, metricsURL string, pushIntervalSeconds time.Duration)

SetPushGateway sends metrics to a remote pushgateway exposed on pushGatewayURL every pushIntervalSeconds. Metrics are fetched from metricsURL

func (*Prometheus) SetPushGatewayJob

func (p *Prometheus) SetPushGatewayJob(j string)

SetPushGatewayJob job name, defaults to "gin"

func (*Prometheus) Use

func (p *Prometheus) Use(e *gin.Engine)

Use adds the middleware to a gin engine.

func (*Prometheus) UseRouterGroup

func (p *Prometheus) UseRouterGroup(e *gin.Engine, rg *gin.RouterGroup)

UseRouterGroup adds the middleware to a gin engine using provided RouterGroup In this case, MetricsPath WILL BE set to /system/prometheus

func (*Prometheus) UseWithAuth

func (p *Prometheus) UseWithAuth(e *gin.Engine, accounts gin.Accounts)

UseWithAuth adds the middleware to a gin engine with BasicAuth.

type PrometheusPushGateway

type PrometheusPushGateway struct {

	// Push interval in seconds
	PushIntervalSeconds time.Duration

	// Push Gateway URL in format http://domain:port
	// where JOBNAME can be any string of your choice
	PushGatewayURL string

	// Local metrics URL where metrics are fetched from, this could be ommited in the future
	// if implemented using prometheus common/expfmt instead
	MetricsURL string

	// pushgateway job name, defaults to "gin"
	Job string
}

PrometheusPushGateway contains the configuration for pushing to a Prometheus pushgateway (optional)

type RequestCounterURLLabelMappingFn

type RequestCounterURLLabelMappingFn func(c *gin.Context) string

RequestCounterURLLabelMappingFn is a function which can be supplied to the middleware to control the cardinality of the request counter's "url" label, which might be required in some contexts. For instance, if for a "/customer/:name" route you don't want to generate a time series for every possible customer name, you could use this function:

func(c *gin.Context) string {
	url := c.Request.URL.Path
	for _, p := range c.Params {
		if p.Key == "name" {
			url = strings.Replace(url, p.Value, ":name", 1)
			break
		}
	}
	return url
}

which would map "/customer/alice" and "/customer/bob" to their template "/customer/:name".

type TemplateRender

type TemplateRender struct {
	// contains filtered or unexported fields
}

func NewTemplateRender

func NewTemplateRender(templatesDir string, ext string, debug bool) *TemplateRender

func (*TemplateRender) GetTemplate

func (r *TemplateRender) GetTemplate(name string) *template.Template

func (*TemplateRender) Instance

func (r *TemplateRender) Instance(name string, data interface{}) render.Render

func (*TemplateRender) Reload

func (r *TemplateRender) Reload()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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