otils

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2022 License: Apache-2.0 Imports: 10 Imported by: 24

README

otils

Build status Go Reference

Go utility functions

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CORSMiddleware

func CORSMiddleware(c *CORS, next http.Handler) http.Handler

func CORSMiddlewareAllInclusive

func CORSMiddlewareAllInclusive(next http.Handler) http.Handler

CORSMiddlewareAllInclusive is a convenience helper that uses the all inclusive CORS: Access-Control-Allow-Origin: * Access-Control-Allow-Methods: * Access-Control-Allow-Headers: * Access-Control-Allow-Credentials: * thus enabling all origins, all methods and all headers.

func EnvOrAlternates

func EnvOrAlternates(envVar string, alternates ...string) string

func FirstNonEmptyString

func FirstNonEmptyString(args ...string) string

FirstNonEmptyString iterates through its arguments trying to find the first string that is not blank or consists entirely of spaces.

func NonEmptyStrings

func NonEmptyStrings(args ...string) (nonEmpties []string)

func RedirectAllTrafficTo

func RedirectAllTrafficTo(host string) http.Handler

RedirectAllTrafficTo creates a handler that can be attached to an HTTP traffic multiplexer to perform a 301 Permanent Redirect to the specified host for any path, anytime that the handler receives a request. Sample usage is:

httpsRedirectHandler := RedirectAllTrafficTo("https://orijtech.com")
if err := http.ListenAndServe(":80", httpsRedirectHandler); err != nil {
  log.Fatal(err)
}

which is used in production at orijtech.com to redirect any non-https traffic from http://orijtech.com/* to https://orijtech.com/*

func StatusOK

func StatusOK(code int) bool

StatusOK returns true if a status code is a 2XX code

func ToURLValues

func ToURLValues(v interface{}) (url.Values, error)

ToURLValues transforms any type with fields into a url.Values map so that it can be used to make the QUERY string in HTTP GET requests for example:

Transforming a struct whose JSON representation is:

{
   "url": "https://orijtech.com",
   "logo": {
	"url": "https://orijtech.com/favicon.ico",
	"dimens": {
	  "width": 100, "height": 120,
	  "extra": {
	    "overlap": true,
	    "shade": "48%"
	  }
	}
   }
}

Into: "logo.dimension.extra.shade=48%25&logo.dimension.extra.zoom=false&logo.dimension.height=120&logo.dimension.width=100&logo.url=https%3A%2F%2Forijtech.com%2Ffavicon.ico"

func UniqStrings

func UniqStrings(strs ...string) []string

UniqStrings returns a slice contains unique element from given input strings.

Types

type CORS

type CORS struct {
	Origins []string
	Methods []string
	Headers []string

	// AllowCredentials when set signifies that the header
	// "Access-Control-Allow-Credentials" which will allow
	// the possibility of the frontend XHR's withCredentials=true
	// to be set.
	AllowCredentials bool
	// contains filtered or unexported fields
}

func (*CORS) ServeHTTP

func (c *CORS) ServeHTTP(rw http.ResponseWriter, req *http.Request)

type CodedError

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

func MakeCodedError

func MakeCodedError(msg string, code int) *CodedError

func (*CodedError) Code

func (cerr *CodedError) Code() int

func (*CodedError) Error

func (cerr *CodedError) Error() string

type NullableFloat64

type NullableFloat64 float64

func (*NullableFloat64) UnmarshalJSON

func (nf64 *NullableFloat64) UnmarshalJSON(b []byte) error

type NullableString

type NullableString string

NullableString represents a string that is sent back by some APIs as null, in JSON unquoted which makes them un-unmarshalable in Go. NullableString interprets null as "".

func (*NullableString) UnmarshalJSON

func (ns *NullableString) UnmarshalJSON(b []byte) error

type NullableTime

type NullableTime time.Time

func (*NullableTime) UnmarshalJSON

func (nt *NullableTime) UnmarshalJSON(b []byte) error

type NumericBool

type NumericBool bool

func (*NumericBool) UnmarshalJSON

func (nb *NumericBool) UnmarshalJSON(blob []byte) error

Directories

Path Synopsis
corpus2fuzz converts a corpus in native Go format to OSS-Fuzz format.
corpus2fuzz converts a corpus in native Go format to OSS-Fuzz format.
fmterrorffix module

Jump to

Keyboard shortcuts

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