hutil

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2021 License: GPL-3.0 Imports: 14 Imported by: 0

README

hutil

Some utils for my go/http (sleepy REST) APIs

Documentation

Index

Constants

This section is empty.

Variables

View Source
var JSON = http.Header{"Content-type": {"application/json"}, "Cache-Control": {"no-cache, no-store, must-revalidate"}, "Pragma": {"no-cache"}}

JSON http Content-type header

Functions

func CacheAlive

func CacheAlive() error

CacheAlive returns nil if anything is ok or error

func CacheDelete

func CacheDelete(key string) error

CacheDelete removes key from redis

func CacheDial

func CacheDial(address, password string, dbnum int) (redis.Conn, error)

CacheDial simply creates connection to redis

func CacheGet

func CacheGet(key string) ([]byte, error)

CacheGet returns value for key in redis

func CacheGetEncoded

func CacheGetEncoded(key string, value interface{}) error

CacheGetEncoded returns decoded gob value for key

func CacheInit

func CacheInit(servers []string, password string, dbnum int) error

CacheInit creates redis connections pool and tests paramaters

func CacheList

func CacheList(patern string) ([]string, error)

CacheList returns keys for patern

func CacheProlong

func CacheProlong(key string, timeout int64) error

CacheProlong set expire of key

func CachePublish

func CachePublish(channel string, value interface{}) error

CachePublish publish value to redis channel

func CacheSet

func CacheSet(key string, value interface{}, timeout int64) error

CacheSet sets redis key/value for timeout expire

func CacheSetEncoded

func CacheSetEncoded(key string, value interface{}, timeout int64) error

CacheSetEncoded sets key/value using gob enoder

func CacheSubscribe

func CacheSubscribe(channel string) redis.PubSubConn

CacheSubscribe subscribes to redis channel

func Error

func Error(w http.ResponseWriter, r *http.Request, code int, message string)

Error func ouputs error message in json format

func ErrorLog

func ErrorLog(w http.ResponseWriter, r *http.Request, code int, message string, comment string)

ErrorLog func ouputs error message in json format and logs separate log comment

func ErrorLogErr

func ErrorLogErr(w http.ResponseWriter, r *http.Request, code int, message string, err error)

ErrorLogErr func ouputs error message in json format and logs error

func IsHexString

func IsHexString(str string) bool

IsHexString allows to check hashes and so on

func IsLangID

func IsLangID(str string) bool

IsLangID checks if argument is 2-char string and both of them are a-z

func IsRedisMaster

func IsRedisMaster(c redis.Conn) (bool, []interface{}, error)

IsRedisMaster returns true, nil, []interface{} if connected to redis master

func IsUUID

func IsUUID(str string) bool

IsUUID allows to check is string contains only hex symbols and dashes

func OK

func OK(w http.ResponseWriter, r *http.Request, j interface{})

OK func simple outputs json with 200 status

func Request2json

func Request2json(request *http.Request, v interface{}) error

Request2json reads requests body and unmarshals json from it

func SessionGetUser

func SessionGetUser(r *http.Request) (int64, error)

SessionGetUser returns all user Id based on session id

func SessionLogin

func SessionLogin(uid int64, w http.ResponseWriter) (string, error)

SessionLogin creates session and saves uid

func SessionLogout

func SessionLogout(w http.ResponseWriter, r *http.Request)

SessionLogout removed sid cookie and data from redis

func SessionLogoutAllUserSessions

func SessionLogoutAllUserSessions(uid int64) error

SessionLogoutAllUserSessions reads all stored sessions and delete ones belong to user

func SessionNew

func SessionNew(data interface{}, w http.ResponseWriter) (string, error)

SessionNew returns id of newly created session

func SlackInit

func SlackInit(url, name, icon, channel string)

SlackInit function sets slack team, token and channel for logging

func SlackLog

func SlackLog(message string) error

SlackLog sends log message to slack's #log channel

func Sleepy1Result

func Sleepy1Result(result interface{}) (int, interface{}, http.Header)

Sleepy1Result warps sigle value into array for rest/sleepy

func Sleepy1in1Map

func Sleepy1in1Map(result interface{}, key string) (int, interface{}, http.Header)

Sleepy1in1Map warps sigle value into map for rest/sleepy

func Sleepy1inNMap

func Sleepy1inNMap(result interface{}, key string) (int, interface{}, http.Header)

Sleepy1inNMap warps sigle value into array in map for rest/sleepy

func SleepyError

func SleepyError(status int, msg string, request *http.Request) (int, interface{}, http.Header)

SleepyError function outputs json error and needed values for sleepy framework

func SleepyStError

func SleepyStError(msg string, request *http.Request) (int, interface{}, http.Header)

SleepyStError function outputs structured json error for sleepy framework

func SleepyStOk

func SleepyStOk() (int, interface{}, http.Header)

SleepyStOk return just "status:ok" result

func SleepyStResult

func SleepyStResult(result interface{}, key string) (int, interface{}, http.Header)

SleepyStResult warps sigle value into {"status": status, key:result}

func WriteRawJSON

func WriteRawJSON(w http.ResponseWriter, r *http.Request, message string)

WriteRawJSON func ouputs message with 200 status

Types

type SessionInfo

type SessionInfo struct {
	UID int64 `msg:"uid"`
}

SessionInfo is type stored to redis

func (SessionInfo) MarshalMsg

func (z SessionInfo) MarshalMsg(b []byte) (o []byte, err error)

MarshalMsg implements msgp.Marshaler

func (SessionInfo) Msgsize

func (z SessionInfo) Msgsize() (s int)

Msgsize ...

func (*SessionInfo) UnmarshalMsg

func (z *SessionInfo) UnmarshalMsg(bts []byte) (o []byte, err error)

UnmarshalMsg implements msgp.Unmarshaler

Jump to

Keyboard shortcuts

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