ack

package module
v1.8.2 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2019 License: Apache-2.0 Imports: 16 Imported by: 10

README

Ack

Ack: A TXN2 common API acknowledgement (ACK) struct and wrapped gin-gonic Go library.

Ack server wraps gin-gonic and contains flag and environment variable configuration for providing health and metrics endpoints:

  • Basic Auth protected /healthz for liveness probes.
  • Prometheus metrics on port 2112 at /metrics.

Example Implementation

package main

import (
	"flag"
	"github.com/gin-gonic/gin"
	"github.com/txn2/ack"
)

func main() {

	test := flag.Bool("test", true, "A test flag")

	server := ack.NewServer()

	if *test {
		server.Router.GET("/test", func(c *gin.Context) {
			ak := ack.Gin(c)
			ak.SetPayloadType("Message")
			ak.GinSend("A test message.")
		})
	}

	server.Run()
}

Run Example

AGENT=test SERVICE_ENV=dev SERVICE_NS=example go run ./example/server.go --test

Example Ack Payload

{
    "ack_version": 8,
    "agent": "test",
    "srv_env": "dev",
    "srv_ns": "example",
    "ack_uuid": "65143cad-8b06-4388-b618-ce3a364b7136",
    "req_uuid": "",
    "date_time": "2019-04-11T19:09:32-07:00",
    "success": true,
    "error_code": "",
    "error_message": "",
    "server_code": 200,
    "location": "/test",
    "payload_type": "Message",
    "payload": "A test message.",
    "duration": "514ns"
}

Example Ack Headers

< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< X-Ack-Agent: test
< X-Ack-Duration: 487ns
< X-Ack-Payload-Type: Message
< X-Ack-Req-Uuid: 2031e500-20d6-454a-a641-dc409334e8f3
< X-Ack-Srv-Env: dev
< X-Ack-Srv-Ns: example
< X-Ack-Uuid: 21fe6c71-6148-4ade-b64c-d9ccd71f8325
< X-Ack-Version: 8
< Date: Fri, 12 Apr 2019 02:58:52 GMT
< Content-Length: 366

Configuration

Flag Environment Variable Description
-help Display help
-debug DEBUG Debug logging mode (default false)
-ip IP Server IP address to bind to. (default "127.0.0.1")
-port PORT Server port. (default "8080")
-healthz HEALTHZ Enable or disable /healthz (default true)
-healthzUser HEALTHZ_USER /healthz basic auth username (default "healthz")
-healthzPass HEALTHZ_PASS /healthz basic auth password (default "healthz")
-logout LOGOUT log output stdout | (default "stdout")
-metric METRICS Enable or Disable metrics (default true)
-metricsIP METRICS_IP Falls back to same IP as server. (default "127.0.0.1")
-metricsPort METRICS_PORT Metrics port. (default "2112")
-readTimeout READ_TIMEOUT HTTP read timeout in seconds (default 10)
-writeTimeout WRITE_TIMEOUT HTTP write timeout (default 10)
AGENT Populates the agent key of Ack.
SERVICE_ENV Populates the srv_env key of Ack.
SERVICE_NS Populates the srv_ns key of Ack.

Documentation

Overview

Copyright 2019 txn2

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const VERSION = 8

Variables

This section is empty.

Functions

func HealthzHandler

func HealthzHandler() func(c *gin.Context)

HealthzHandler

func NoRouteHandler

func NoRouteHandler() func(c *gin.Context)

NoRouteHandler

Types

type Ack

type Ack struct {
	Version      int         `json:"ack_version"`
	Agent        string      `json:"agent"`
	SrvEnv       string      `json:"srv_env"`
	SrvNS        string      `json:"srv_ns"`
	Uuid         string      `json:"ack_uuid"`
	RequestUuid  string      `json:"req_uuid"`
	DateTime     string      `json:"date_time"`
	Success      bool        `json:"success"`
	ErrorCode    string      `json:"error_code"`
	ErrorMessage string      `json:"error_message"`
	ServerCode   int         `json:"server_code"`
	Location     string      `json:"location"`
	PayloadType  string      `json:"payload_type"`
	Payload      interface{} `json:"payload"`
	Duration     string      `json:"duration"`
	// contains filtered or unexported fields
}

Ack

func (*Ack) MakeError

func (a *Ack) MakeError(ServerCode int, errorCode string, errorMessage string)

MakeError

func (*Ack) SetPayload

func (a *Ack) SetPayload(payload interface{})

SetPayload

func (*Ack) SetPayloadType

func (a *Ack) SetPayloadType(payloadType string)

SetPayloadType

func (*Ack) StartTimer

func (a *Ack) StartTimer()

StartTimer

type Client added in v1.8.2

type Client struct {
	Cfg  ClientCfg
	Http *http.Client
}

Client

func NewHttpClient added in v1.8.2

func NewHttpClient(cfg *ClientCfg) *Client

NewHttpClient

type ClientCfg added in v1.8.2

type ClientCfg struct {
	MaxIdleConnsPerHost int // 10 connections
	DialContextTimeout  int // 10 seconds
	NetTimeout          int // 10 seconds
	ConTimeout          int // 60 seconds
}

ClientCfg

type GinAck

type GinAck struct {
	Ack Ack
	// contains filtered or unexported fields
}

GinAck

func Gin

func Gin(c *gin.Context) GinAck

Gin Ack

func (*GinAck) GinErrorAbort

func (ga *GinAck) GinErrorAbort(ServerCode int, errorCode string, errorMessage string)

GinError aborts gin context with JSON error

func (*GinAck) GinSend

func (ga *GinAck) GinSend(payload interface{})

GinSend responds with JSON on the gin context

func (*GinAck) MakeError

func (ga *GinAck) MakeError(ServerCode int, errorCode string, errorMessage string)

MakeError

func (*GinAck) SetPayload

func (ga *GinAck) SetPayload(payload interface{})

SetPayload

func (*GinAck) SetPayloadType

func (ga *GinAck) SetPayloadType(payloadType string)

SetPayloadType

func (*GinAck) UnmarshalAbort

func (ga *GinAck) UnmarshalAbort(data []byte, v interface{}) error

UnmarshalAbort unmarshals data and aborts if it can not.

func (*GinAck) UnmarshalPostAbort

func (ga *GinAck) UnmarshalPostAbort(v interface{}) error

UnmarshalPostAbort unmarshal raw data posted through gin or aborts.

type MappedMetricFamily

type MappedMetricFamily map[string]*io_prometheus_client.MetricFamily

MappedMetricFamily

type Server

type Server struct {
	Cfg    *ServerCfg
	Logger *zap.Logger
	Router *gin.Engine
	Client *Client
}

Server

func NewServer

func NewServer() *Server

NewServer

func (*Server) Run

func (srv *Server) Run()

Run server

type ServerCfg

type ServerCfg struct {
	Ip           string
	Port         string
	Metrics      bool
	MetricsIp    string
	MetricsPort  string
	Healthz      bool
	HealthzUser  string
	HealthzPass  string
	LogOut       string
	Debug        bool
	ReadTimeout  int
	WriteTimeout int
}

ServerCfg

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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