utils

package
v4.0.11+incompatible Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2021 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Version   = "None"
	BuildTS   = "None"
	GitHash   = "None"
	GitBranch = "None"
)

Version information.

Functions

func ClientWithTLS

func ClientWithTLS(tlsCfg *tls.Config) *http.Client

ClientWithTLS creates a http client wit tls

func GetCPUPercentage

func GetCPUPercentage() float64

GetCPUPercentage calculates CPU usage and returns percentage in float64(e.g. 2.5 means 2.5%). http://man7.org/linux/man-pages/man2/getrusage.2.html

func GetJSON

func GetJSON(client *http.Client, url string, v interface{}) error

GetJSON fetches a page and parses it as JSON. The parsed result will be stored into the `v`. The variable `v` must be a pointer to a type that can be unmarshalled from JSON.

Example:

client := &http.Client{}
var resp struct { IP string }
if err := util.GetJSON(client, "http://api.ipify.org/?format=json", &resp); err != nil {
	return errors.Trace(err)
}
fmt.Println(resp.IP)

func GetRawInfo

func GetRawInfo(app string) string

GetRawInfo do what its name tells

func OriginError

func OriginError(err error) error

OriginError return original err

func ParseHostPortAddr

func ParseHostPortAddr(s string) ([]string, error)

ParseHostPortAddr returns a scheme://host:port or host:port list

func PrintInfo

func PrintInfo(app string)

PrintInfo prints the app's basic information in log

func SliceToMap

func SliceToMap(slice []string) map[string]interface{}

SliceToMap converts slice to map

func StringsToInterfaces

func StringsToInterfaces(strs []string) []interface{}

StringsToInterfaces converts string slice to interface slice

func TSOToRoughTime

func TSOToRoughTime(ts int64) time.Time

TSOToRoughTime translates tso to rough time that used to display

func ToTLSConfig

func ToTLSConfig(caPath, certPath, keyPath string) (*tls.Config, error)

ToTLSConfig generates tls's config.

func ToTLSConfigWithVerify

func ToTLSConfigWithVerify(caPath, certPath, keyPath string, verifyCN []string) (*tls.Config, error)

ToTLSConfigWithVerify constructs a `*tls.Config` from the CA, certification and key paths, and add verify for CN.

If the CA path is empty, returns nil.

Types

type TLS

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

TLS saves some information about tls

func NewTLS

func NewTLS(caPath, certPath, keyPath, host string, verifyCN []string) (*TLS, error)

NewTLS constructs a new HTTP client with TLS configured with the CA, certificate and key paths.

If the CA path is empty, returns an instance where TLS is disabled.

func NewTLSFromMockServer

func NewTLSFromMockServer(server *httptest.Server) *TLS

NewTLSFromMockServer constructs a new TLS instance from the certificates of an *httptest.Server.

func (*TLS) GetJSON

func (tc *TLS) GetJSON(path string, v interface{}) error

GetJSON obtains JSON result with the HTTP GET method.

func (*TLS) TLSConfig

func (tc *TLS) TLSConfig() *tls.Config

TLSConfig returns tls config

func (*TLS) ToGRPCDialOption

func (tc *TLS) ToGRPCDialOption() grpc.DialOption

ToGRPCDialOption constructs a gRPC dial option.

func (*TLS) ToGRPCServerOption

func (tc *TLS) ToGRPCServerOption() grpc.ServerOption

ToGRPCServerOption constructs a gRPC server option.

func (*TLS) WithHost

func (tc *TLS) WithHost(host string) *TLS

WithHost creates a new TLS instance with the host replaced.

func (*TLS) WrapListener

func (tc *TLS) WrapListener(l net.Listener) net.Listener

WrapListener places a TLS layer on top of the existing listener.

Jump to

Keyboard shortcuts

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