pz-gocommon: github.com/venicegeo/pz-gocommon/gocommon Index | Files

package piazza

import "github.com/venicegeo/pz-gocommon/gocommon"

Index

Package Files

GenericServer.go HttpQueryParams.go JsonPagination.go JsonResponse.go SystemConfig.go TimeStamp.go Uuid.go VcapApplication.go VcapServices.go authz.go http.go http_utils.go nt.go util.go

Constants

const (
    // ContentTypeJSON is the http content-type for JSON.
    ContentTypeJSON = "application/json"

    // ContentTypeText is the http content-type for plain text.
    ContentTypeText = "text/plain"
)
const DefaultDomain = ".venicegeo.io"
const DefaultElasticsearchAddress = "localhost:9200"
const DefaultKafkaAddress = "localhost:9092"
const DefaultProtocol = "http"
const DefaultPzLoggerAddress = "localhost:14600"
const DefaultPzUuidgenAddress = "localhost:14800"
const GET, PUT, POST, DELETE, HEAD = "GET", "PUT", "POST", "DELETE", "HEAD"

Variables

var EndpointPrefixes = map[ServiceName]string{
    PzDiscover:          "",
    PzElasticSearch:     "",
    PzKafka:             "",
    PzLogger:            "",
    PzUuidgen:           "",
    PzWorkflow:          "",
    PzsvcHello:          "",
    PzServiceController: "",
    PzMetrics:           "",
    PzIdam:              "",
}
var HealthcheckEndpoints = map[ServiceName]string{
    PzDiscover:          "",
    PzElasticSearch:     "",
    PzKafka:             "",
    PzLogger:            "/",
    PzUuidgen:           "/",
    PzWorkflow:          "/",
    PzsvcHello:          "/",
    PzServiceController: "",
    PzMetrics:           "/",
    PzIdam:              "",
}
var JsonResponseDataTypes = map[string]string{}
var LocalPortNumbers = map[ServiceName]string{
    PzWorkflow:          "20000",
    PzLogger:            "20001",
    PzUuidgen:           "20002",
    PzDiscover:          "20003",
    PzElasticSearch:     "20004",
    PzServiceController: "20005",
    PzMetrics:           "20006",
    PzKafka:             "20007",
    PzsvcHello:          "20008",
    PzGoCommon:          "20009",
    PzGoCommonTest:      "20010",
    PzIdam:              "20011",
}

func CharAt Uses

func CharAt(str string, index int) string

func GetApiKey Uses

func GetApiKey(pzserver string) (string, error)

GetApiKey retrieves the Pz API key for the given server, in this order:

(1) if $PZKEY present, use that (2) if ~/.pzkey exists, use that (3) error

And no, we don't uspport Windows.

func GetApiServer Uses

func GetApiServer() (string, error)

TODO: just for backwards compatability

func GetBasicAuthHeader Uses

func GetBasicAuthHeader(username, password string) [2]string

func GetExternalIP Uses

func GetExternalIP() (string, error)

GetExternalIP returns the "best"(?) IP address we can reasonably get. see: http://stackoverflow.com/a/23558495

func GetPiazzaServiceUrl Uses

func GetPiazzaServiceUrl(serviceName ServiceName) (string, error)

GetServiceServer returns the host name of the given service, based on $PZSERVER.

func GetPiazzaUrl Uses

func GetPiazzaUrl() (string, error)

GetPiazzaServer returns the URL of the $PZSERVER host, i.e. the public entry point.

func GetValueFromHeader Uses

func GetValueFromHeader(header http.Header, field string) string

func GetVarsFromStruct Uses

func GetVarsFromStruct(struc interface{}) (map[string]interface{}, error)

func GetVarsFromStructSkip Uses

func GetVarsFromStructSkip(struc interface{}, skipMaps map[string]bool) (map[string]interface{}, error)

func GinReturnJson Uses

func GinReturnJson(c *gin.Context, resp *JsonResponse)

func HTTP Uses

func HTTP(requestType, url string, headers [][2]string, toSend io.Reader) (int, []byte, http.Header, error)

func HTTPDelete Uses

func HTTPDelete(url string) (*http.Response, error)

Delete, because there is no http.Delete.

func HTTPPut Uses

func HTTPPut(url string, contentType string, body io.Reader) (*http.Response, error)

Put, because there is no http.Put.

func InsertString Uses

func InsertString(str, insert string, index int) string

func RemoveWhitespace Uses

func RemoveWhitespace(str string) string

func RequestAuthZAccess Uses

func RequestAuthZAccess(idamURL string, actor string) (authorized bool, err error)

action auditString

func SplitString Uses

func SplitString(str string, whereToSplit int) (string, string)

func StructInterfaceToString Uses

func StructInterfaceToString(stru interface{}) (string, error)

func StructStringToInterface Uses

func StructStringToInterface(stru string) (interface{}, error)

func UnmarshalNumber Uses

func UnmarshalNumber(r io.Reader, v interface{}) error

func ValidUuid Uses

func ValidUuid(uuid string) bool

func ValueIsValidArray Uses

func ValueIsValidArray(value interface{}) bool

func WaitForService Uses

func WaitForService(name ServiceName, url string) error

func WaitForServiceToDie Uses

func WaitForServiceToDie(name ServiceName, url string) error

type FlightCheck Uses

type FlightCheck interface {
    Preflight(verb string, url string, obj string) error
    Postflight(code int, obj string) error
}

type GenericServer Uses

type GenericServer struct {
    Sys *SystemConfig
    // contains filtered or unexported fields
}

GenericServer is the basic framework for standing up a gin-based server. It has methods to Start and Stop as well as to define the routing paths.

func (*GenericServer) Configure Uses

func (server *GenericServer) Configure(routeData []RouteData) error

Configure will take the give RouteData and register them with the server.

func (*GenericServer) Start Uses

func (server *GenericServer) Start() (chan error, error)

Start will start the service. You must call Configure first.

func (*GenericServer) Stop Uses

func (server *GenericServer) Stop() error

Stop will request the server to shutdown. It will wait for the service to die before returning.

type HeaderBuilder Uses

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

func NewHeaderBuilder Uses

func NewHeaderBuilder() *HeaderBuilder

func (*HeaderBuilder) AddBasicAuth Uses

func (builder *HeaderBuilder) AddBasicAuth(username, password string) *HeaderBuilder

func (*HeaderBuilder) AddHeader Uses

func (builder *HeaderBuilder) AddHeader(key, value string) *HeaderBuilder

func (*HeaderBuilder) AddJsonContentType Uses

func (builder *HeaderBuilder) AddJsonContentType() *HeaderBuilder

func (*HeaderBuilder) GetHeader Uses

func (builder *HeaderBuilder) GetHeader() [][2]string

type Http Uses

type Http struct {
    FlightCheck FlightCheck
    ApiKey      string
    BaseUrl     string
    User        string
    Pass        string
}

func (*Http) Delete Uses

func (h *Http) Delete(endpoint string, output interface{}) (int, error)

expects endpoint to return nothing

func (*Http) Get Uses

func (h *Http) Get(endpoint string, output interface{}) (int, error)

expects endpoint to return JSON

func (*Http) Get2 Uses

func (h *Http) Get2(endpoint string, input interface{}, output interface{}) (int, error)

func (*Http) Post Uses

func (h *Http) Post(endpoint string, input interface{}, output interface{}) (int, error)

expects endpoint to take in and return JSON

func (*Http) Put Uses

func (h *Http) Put(endpoint string, input interface{}, output interface{}) (int, error)

expects endpoint to take in and return JSON

func (*Http) PzDelete Uses

func (h *Http) PzDelete(endpoint string) *JsonResponse

func (*Http) PzGet Uses

func (h *Http) PzGet(endpoint string) *JsonResponse

func (*Http) PzGet2 Uses

func (h *Http) PzGet2(endpoint string, input interface{}) *JsonResponse

func (*Http) PzPost Uses

func (h *Http) PzPost(endpoint string, input interface{}) *JsonResponse

func (*Http) PzPut Uses

func (h *Http) PzPut(endpoint string, input interface{}) *JsonResponse

func (*Http) Verb Uses

func (h *Http) Verb(verb string, endpoint string, input interface{}, output interface{}) (int, error)

expects endpoint to take in and return JSON

type HttpQueryParams Uses

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

HttpQueryParams holds a list of the query parameters of a URL, e.g.

...?count=8&foo=bar

We don't want to pass http.Request objects into the Services classes (and certainly not a gin.Context!), and we need this kind of information a lot, so we'll keep a special data structure which actually understands the semantics as well as the syntax.

func NewQueryParams Uses

func NewQueryParams(request *http.Request) *HttpQueryParams

NewQueryParams creates an HttpQueryParams object from an http.Request.

func (*HttpQueryParams) AddString Uses

func (params *HttpQueryParams) AddString(key string, value string)

AddString adds a key and string value to the parameter set, e.g. "?foo=bar".

func (*HttpQueryParams) AddTime Uses

func (params *HttpQueryParams) AddTime(key string, value time.Time)

AddTime adds a key and time value to the parameter set.

func (*HttpQueryParams) GetAfter Uses

func (params *HttpQueryParams) GetAfter(defalt time.Time) (time.Time, error)

GetAfter retrieves the value of the "after" parameter.

func (*HttpQueryParams) GetAsID Uses

func (params *HttpQueryParams) GetAsID(key string, defalt string) (Ident, error)

GetAsID retrieves the (string) value of a key from parameter set and returns it as an Ident.

func (*HttpQueryParams) GetAsInt Uses

func (params *HttpQueryParams) GetAsInt(key string, defalt int) (int, error)

GetAsInt retrieves the (string) value of a key from parameter set and returns it as an int.

func (*HttpQueryParams) GetAsSortOrder Uses

func (params *HttpQueryParams) GetAsSortOrder(key string, defalt SortOrder) (SortOrder, error)

GetAsSortOrder retrieves the (string) value of a key from parameter set and returns it as SortOrder value.

func (*HttpQueryParams) GetAsString Uses

func (params *HttpQueryParams) GetAsString(key string, defalt string) (string, error)

GetAsString retrieves the (string) value of a key from parameter set and returns it.

func (*HttpQueryParams) GetAsTime Uses

func (params *HttpQueryParams) GetAsTime(key string, defalt time.Time) (time.Time, error)

GetAsTime retrieves the (string) value of a key from parameter set and returns it as time value.

func (*HttpQueryParams) GetBefore Uses

func (params *HttpQueryParams) GetBefore(defalt time.Time) (time.Time, error)

GetBefore retrieves the value of the "before" parameter.

func (*HttpQueryParams) GetCount Uses

func (params *HttpQueryParams) GetCount(defalt int) (int, error)

GetCount retrieves the value of the "count" parameter.

func (*HttpQueryParams) GetPage Uses

func (params *HttpQueryParams) GetPage(defalt int) (int, error)

GetPage retrieves the value of the "page" parameter.

func (*HttpQueryParams) GetPerPage Uses

func (params *HttpQueryParams) GetPerPage(defalt int) (int, error)

GetPerPage retrieves the value of the "perPage" parameter.

func (*HttpQueryParams) GetSortBy Uses

func (params *HttpQueryParams) GetSortBy(defalt string) (string, error)

GetSortBy retrieves the value of the "sortBy" parameter.

func (*HttpQueryParams) GetSortOrder Uses

func (params *HttpQueryParams) GetSortOrder(defalt SortOrder) (SortOrder, error)

GetSortOrder retrieves the value of the "order" parameter.

func (*HttpQueryParams) String Uses

func (params *HttpQueryParams) String() string

String returns the parameter list expressed in URL style.

type Ident Uses

type Ident string

Ident is the type used for representing ID values.

const NoIdent Ident = ""

NoIdent is the "empty" ID value.

func (Ident) String Uses

func (id Ident) String() string

String returns an id as a string.

type JsonPagination Uses

type JsonPagination struct {
    Count   int       `json:"count"` // only used when writing output
    Page    int       `json:"page"`
    PerPage int       `json:"perPage"`
    SortBy  string    `json:"sortBy"`
    Order   SortOrder `json:"order"`
}

JsonPagination is the Piazza model for pagination json responses.

func NewJsonPagination Uses

func NewJsonPagination(params *HttpQueryParams) (*JsonPagination, error)

NewJsonPagination creates a JsonPagination object. The default values will be overwritten with any appropriate values from the params list.

func (*JsonPagination) EndIndex Uses

func (p *JsonPagination) EndIndex() int

EndIndex returns the index number of the last element to be used.

func (*JsonPagination) StartIndex Uses

func (p *JsonPagination) StartIndex() int

StartIndex returns the index number of the first element to be used.

func (*JsonPagination) String Uses

func (p *JsonPagination) String() string

String returns a URL-style string of the pagination settings.

func (*JsonPagination) SyncPagination Uses

func (format *JsonPagination) SyncPagination(dslString string) (string, error)

type JsonResponse Uses

type JsonResponse struct {
    StatusCode int `json:"statusCode" binding:"required"`

    // only 2xxx -- Data is required (and Type too)
    // Type is a string, taken from the Java model list
    Type       string          `json:"type,omitempty"`
    Data       interface{}     `json:"data,omitempty" binding:"required"`
    Pagination *JsonPagination `json:"pagination,omitempty"` // optional

    // only 4xx and 5xx -- Message is required
    Message string        `json:"message,omitempty"`
    Origin  string        `json:"origin,omitempty"` // optional
    Inner   *JsonResponse `json:"inner,omitempty"`  // optional

    // optional
    Metadata interface{} `json:"metadata,omitempty"`
}

func (*JsonResponse) ExtractData Uses

func (resp *JsonResponse) ExtractData(output interface{}) error

given a JsonResponse object returned from an http call, and with Data set convert it to the given output type (formerly called SuperConverter)

func (*JsonResponse) IsError Uses

func (resp *JsonResponse) IsError() bool

func (*JsonResponse) SetType Uses

func (resp *JsonResponse) SetType() error

func (*JsonResponse) String Uses

func (resp *JsonResponse) String() string

func (*JsonResponse) ToError Uses

func (resp *JsonResponse) ToError() error

type JsonString Uses

type JsonString string

JsonString is the type used to represent JSON values.

type RouteData Uses

type RouteData struct {
    Verb    string
    Path    string
    Handler gin.HandlerFunc
}

RouteData describes one server route: which http verb, the path string, and the handler to use.

type ServiceName Uses

type ServiceName string
const (
    PzDiscover          ServiceName = "pz-discover"
    PzElasticSearch     ServiceName = "pz-elasticsearch"
    PzGoCommon          ServiceName = "PZ-GOCOMMON"     // not a real service, just for testing
    PzGoCommonTest      ServiceName = "PZ-GOCOMMONTEST" // not a real service, just for testing
    PzKafka             ServiceName = "pz-kafka"
    PzLogger            ServiceName = "pz-logger"
    PzUuidgen           ServiceName = "pz-uuidgen"
    PzWorkflow          ServiceName = "pz-workflow"
    PzsvcHello          ServiceName = "pzsvc-hello"
    PzServiceController ServiceName = "pz-servicecontroller"
    PzMetrics           ServiceName = "pz-metrics"
    PzIdam              ServiceName = "pz-idam"
)

type ServicesMap Uses

type ServicesMap map[ServiceName]string

type SimpleFlightCheck Uses

type SimpleFlightCheck struct {
    NumPreflights  int
    NumPostflights int
}

func (*SimpleFlightCheck) Postflight Uses

func (fc *SimpleFlightCheck) Postflight(code int, obj string) error

func (*SimpleFlightCheck) Preflight Uses

func (fc *SimpleFlightCheck) Preflight(verb string, url string, obj string) error

type SortOrder Uses

type SortOrder string

SortOrder indicates ascending (1,2,3) or descending (3,2,1) order.

const (
    // SortOrderAscending is for "a, b, c, ..."
    SortOrderAscending SortOrder = "asc"

    // SortOrderDescending is for "z, y, x, ..."
    SortOrderDescending SortOrder = "desc"
)

type SystemConfig Uses

type SystemConfig struct {
    // our own service
    Name    ServiceName
    Address string
    BindTo  string

    Space        string // int or stage or prod or...
    PiazzaSystem string // System-level username
    // contains filtered or unexported fields
}

func NewSystemConfig Uses

func NewSystemConfig(serviceName ServiceName,
    requiredServices []ServiceName) (*SystemConfig, error)

func (*SystemConfig) AddService Uses

func (sys *SystemConfig) AddService(name ServiceName, address string)

it is explicitly allowed for outsiders to update an existing service, but we'll log it just to be safe

func (*SystemConfig) GetAddress Uses

func (sys *SystemConfig) GetAddress(name ServiceName) (string, error)

func (*SystemConfig) GetDomain Uses

func (sys *SystemConfig) GetDomain() string

func (*SystemConfig) GetURL Uses

func (sys *SystemConfig) GetURL(name ServiceName) (string, error)

func (*SystemConfig) WaitForService Uses

func (sys *SystemConfig) WaitForService(name ServiceName) error

func (*SystemConfig) WaitForServiceToDie Uses

func (sys *SystemConfig) WaitForServiceToDie(name ServiceName) error

type TimeStamp Uses

type TimeStamp time.Time

TimeStamp is used for the timeStamp field of a syslog.Message, but it is here in pz-gocommon because it may be generally useful.

A Piazza timestamp must always be in RFC3339 format and in UTC. That is, we use yyyy-MM-dd'T'HH:mm:ss.SSSZ, with three caveats: (1) the ".SSS" (fractional seconds) part is optional (2) if the fractional part is present, there may actually be 1-7 digits used (3) however, if the fractional part is present when reading in or writing out, then the system may silently truncate or round to only three digits (milliseconds).

There are three basic operations: Now, Parse, and String. In these functions, note how we always are careful to round the time to milliseconds and then convert it to UTC before proceeding with any usages of Time objects or time strings.

Note that when Go marshals a Time object into JSON, it uses the "RFC3339Nano" format which may have several fractional digits. Note also that when storing a time object into Elasticsearch, the entire fractional part may be truncated to zero fractional digits (second accuracy) or three fractional digits (millisecond accuracy).

func NewTimeStamp Uses

func NewTimeStamp() TimeStamp

func ParseTimeStamp Uses

func ParseTimeStamp(s string) (TimeStamp, error)

func (TimeStamp) MarshalJSON Uses

func (ts TimeStamp) MarshalJSON() ([]byte, error)

func (TimeStamp) String Uses

func (ts TimeStamp) String() string

func (*TimeStamp) UnmarshalJSON Uses

func (ts *TimeStamp) UnmarshalJSON(data []byte) error

func (*TimeStamp) Validate Uses

func (ts *TimeStamp) Validate() error

type Uuid Uses

type Uuid []byte

func NewUuid Uses

func NewUuid() Uuid

NewRandom returns a random (variant 4) UUID.

func (Uuid) String Uses

func (uuid Uuid) String() string

String returns the string form of uuid: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

func (Uuid) Valid Uses

func (uuid Uuid) Valid() bool

type VcapApplication Uses

type VcapApplication struct {
    ApplicationID      string         `json:"application_id"`
    ApplicationName    string         `json:"application_name"`
    ApplicationURIs    []string       `json:"application_uris"`
    ApplicationVersion string         `json:"application_version"`
    Limits             map[string]int `json:"limits"`
    Name               string         `json:"name"`
    SpaceId            string         `json:"space_id"`
    SpaceName          string         `json:"space_name"`
    URIs               []string       `json:"uris"`
    Users              interface{}    `json:"users"` // don't know what the datattype actually is
    Version            string         `json:"version"`
    // contains filtered or unexported fields
}

func NewVcapApplication Uses

func NewVcapApplication(serviceName ServiceName) (*VcapApplication, error)

func (*VcapApplication) GetAddress Uses

func (vcap *VcapApplication) GetAddress() string

func (*VcapApplication) GetBindToPort Uses

func (vcap *VcapApplication) GetBindToPort() string

func (*VcapApplication) GetDomain Uses

func (vcap *VcapApplication) GetDomain() string

func (*VcapApplication) GetName Uses

func (vcap *VcapApplication) GetName() ServiceName

type VcapCredentials Uses

type VcapCredentials struct {
    Host string `json:"host"`
}

type VcapServiceEntry Uses

type VcapServiceEntry struct {
    Credentials    VcapCredentials `json:"credentials"`
    Label          string          `json:"label"`
    Name           string          `json:"name"`
    SyslogDrainUrl string          `json:"syslog_drain_url"`
    Tags           []string        `json:"tags"`
}

type VcapServices Uses

type VcapServices struct {
    UserProvided []VcapServiceEntry `json:"user-provided"`

    Services map[ServiceName]string
}

func NewVcapServices Uses

func NewVcapServices() (*VcapServices, error)

type Version Uses

type Version struct {
    Version string
}

Version captures the information about the version of a service. For now, it is just a string.

Package piazza imports 21 packages (graph) and is imported by 10 packages. Updated 2018-07-02. Refresh now. Tools for package owners.