Skip to content

Commit

Permalink
Merge pull request #70 from kelchy/main
Browse files Browse the repository at this point in the history
Release rmq/publisher 0.1.0
  • Loading branch information
kelchy committed Apr 18, 2023
2 parents 03dfdd7 + b41a70f commit a4ba506
Show file tree
Hide file tree
Showing 65 changed files with 3,756 additions and 932 deletions.
2 changes: 1 addition & 1 deletion http/client/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//v0.0.6
//v0.1.0
module github.com/kelchy/go-lib/http/client

require (
Expand Down
4 changes: 2 additions & 2 deletions http/client/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Res struct {
Error error
log log.Log
HTML string
JSON interface{}
JSON json.RawMessage
}

// HTMLparse - method to return the html content of response
Expand All @@ -34,7 +34,7 @@ func (r *Res) HTMLparse() {

// JSONparse - method to return the json content of response
func (r *Res) JSONparse() {
var data interface{}
var data json.RawMessage
if r.Error != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion http/server/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//v0.1.0
//v0.1.1
module github.com/kelchy/go-lib/http/server

require (
Expand Down
26 changes: 19 additions & 7 deletions http/server/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,27 @@ func (rtr *Router) catchall(next http.Handler) http.Handler {
w.WriteHeader(http.StatusInternalServerError)
w.Write(jsonBody)
} else if rtr.logRequest {
msg, _ := json.Marshal(map[string]string{
"method": r.Method,
"status": strconv.Itoa(w2.Status()),
"src": r.RemoteAddr,
"ms": diffStr,
})
rtr.log.Out(r.URL.Path, string(msg))
if !contains(rtr.logSkipPath, r.URL.Path) {
msg, _ := json.Marshal(map[string]string{
"method": r.Method,
"status": strconv.Itoa(w2.Status()),
"src": r.RemoteAddr,
"ms": diffStr,
})
rtr.log.Out(r.URL.Path, string(msg))
}
}
}()
next.ServeHTTP(w2, r)
})
}

func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}

return false
}
16 changes: 13 additions & 3 deletions http/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ type ChiRouter = chi.Router

// Router - initialized instance
type Router struct {
Engine *chi.Mux
log log.Log
logRequest bool
Engine *chi.Mux
log log.Log
logRequest bool
logSkipPath []string
}

// New - constructor function to initialize instance
Expand All @@ -33,6 +34,10 @@ func New(origins []string, headers []string) (*Router, error) {
rtr.log = l
rtr.logRequest = true

// by default middleware don't log root path which is
// usually used by health checks
rtr.logSkipPath = []string{"/"}

if len(origins) == 0 {
origins = []string{"http://localhost", "https://localhost"}
}
Expand Down Expand Up @@ -64,6 +69,11 @@ func (rtr *Router) SetLogger(logtype string) {
}
}

// SetLogSkipPath - changes the middleware logging behaviour
func (rtr *Router) SetLogSkipPath(list []string) {
rtr.logSkipPath = list
}

// SetLogRequest - changes behaviour on whether to log requests or not
func (rtr *Router) SetLogRequest(lr bool) {
rtr.logRequest = lr
Expand Down
1 change: 1 addition & 0 deletions log/example/log_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func main() {
panic(err)
}
log.Out("Example: scope", "message")
log.Debug("Example: scope", "you should not see this if GO_ENV is production")

empty, _ := Log.New("empty")
empty.Out("Empty: You should", "not see this")
Expand Down
2 changes: 1 addition & 1 deletion log/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//v0.0.10
//v0.0.11
module github.com/kelchy/go-lib/log

go 1.18
3 changes: 3 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ func (l Log) Out(scope string, msg string) {

// Debug - outputs debugging to stdout
func (l Log) Debug(scope string, msg string) {
if os.Getenv("GO_ENV") == "production" {
return
}
if l.config != "empty" {
logPrint(scope, msg, l.json)
}
Expand Down
61 changes: 58 additions & 3 deletions redis/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ package redis

import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
redis "github.com/go-redis/redis/v8"
"github.com/kelchy/go-lib/log"
"strings"
)

// KeepTTL - helper to expose KeepTTL
var KeepTTL = redis.KeepTTL

// Client - instance created when initializing client
type Client struct {
Client *redis.Client
ctx context.Context
log log.Log
Client *redis.Client
ctx context.Context
log log.Log
}

// New - constructor to create an instance of the client
Expand All @@ -26,6 +29,58 @@ func New(uri string) (Client, error) {
l.Error("REDIS_PARSE_URL", err)
return r, err
}

// hack for redis labs CA issue
if strings.Contains(uri, "rediss") && strings.Contains(opt.Addr, "redislabs.com") {
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM([]byte(CACert))
opt.TLSConfig = &tls.Config{
MinVersion: tls.VersionTLS12,
RootCAs: caCertPool,
}
}

r.Client = redis.NewClient(opt)
r.ctx = context.Background()
r.log = l
pong, err := r.Client.Ping(r.ctx).Result()
if err != nil {
l.Error("REDIS_PING", err)
return r, err
} else if pong != "PONG" {
err = errors.New("redis ping no pong")
l.Error("REDIS_PONG", err)
return r, err
}
return r, nil
}

// NewSecure - constructor to create an instance of the client
func NewSecure(uri string, clientCertPath string, clientKeyPath string, skipVerify bool) (Client, error) {
l, _ := log.New("")
var r Client

tlsCert, tlsErr := tls.LoadX509KeyPair(clientCertPath, clientKeyPath)
if tlsErr != nil {
l.Error("err", tlsErr)
return r, tlsErr
}

opt, err := redis.ParseURL(uri)
if err != nil {
l.Error("REDIS_PARSE_URL", err)
return r, err
}

// tlsCerts generated with tls.X509KeyPair()
// enable TLS connections if input provided
if tlsCert.Certificate != nil {
opt.TLSConfig = &tls.Config{
Certificates: []tls.Certificate{tlsCert},
InsecureSkipVerify: skipVerify,
}
}

r.Client = redis.NewClient(opt)
r.ctx = context.Background()
r.log = l
Expand Down
34 changes: 22 additions & 12 deletions redis/example/redis_example.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,48 @@
package main

import (
"time"
"context"
"fmt"
"os"
"time"

"github.com/kelchy/go-lib/redis"
)

func main() {
uri := os.Getenv("REDIS_URI")
redisclient, e := redis.New(uri)
if e != nil {

uri := "<redis uri here>"

// path to cert files
clientCertPath := "<file-path>"
clientKeyPath := `<file-path>`

// use redis.New if TLS connection is not required
// skipVerifyCondition should only be true when running locally
redisclient, e := redis.NewSecure(uri, clientCertPath, clientKeyPath, false)
if e != nil {
fmt.Println(e)
return
}

keys, _ := redisclient.Keys(nil, "*")
// inputting nil will cause an error, context.TODO() is preferred
keys, _ := redisclient.Keys(context.TODO(), "*")
fmt.Println("keys", keys)

res, _ := redisclient.Set(nil, "key", "value2", 10 * time.Second)
res, _ := redisclient.Set(context.TODO(), "key", "value2", 10*time.Second)
fmt.Println("result", res)

resi, _ := redisclient.Del(nil, "key")
resi, _ := redisclient.Del(context.TODO(), "key")
fmt.Println("result int", resi)

resb, _ := redisclient.SetNX(nil, "key", "value2", 10 * time.Second)
resb, _ := redisclient.SetNX(context.TODO(), "key", "value2", 10*time.Second)
fmt.Println("result bool", resb)

val, _ := redisclient.Get(nil, "key")
val, _ := redisclient.Get(context.TODO(), "key")
fmt.Println("result value", val)

lock, _ := redisclient.Lock(nil, "locktest", 20 * time.Second)
lock, _ := redisclient.Lock(context.TODO(), "locktest", 20*time.Second)
fmt.Println("result lock", lock)

unlock, _ := redisclient.Unlock(nil, "locktest")
unlock, _ := redisclient.Unlock(context.TODO(), "locktest")
fmt.Println("result unlock", unlock)
}
2 changes: 1 addition & 1 deletion redis/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//v0.0.4
//v0.0.9
module github.com/kelchy/go-lib/redis

require (
Expand Down
9 changes: 9 additions & 0 deletions redis/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cb
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
github.com/kelchy/go-lib/log v0.0.10 h1:K2ilS1c3pHwzXuQhKbTYagiNPYJYaGJq6BHr8TjPM2g=
github.com/kelchy/go-lib/log v0.0.10/go.mod h1:08sbkvkTs1hFLUcHsOqCUXJBAF1VUrllqKkB3lmDEFM=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781 h1:DzZ89McO9/gWPsQXS/FVKAlG02ZjaQ6AlZRBimEYOd0=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
33 changes: 32 additions & 1 deletion redis/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package redis

import (
"context"
"fmt"
"time"

redis "github.com/go-redis/redis/v8"
)

Expand Down Expand Up @@ -33,4 +36,32 @@ func (r Client) Keys(ctx context.Context, match string) ([]string, error) {
}
}
return list, nil
}
}

// TTL - implementation of redis TTL, ctx can be nil
// -- returns in time.Duration which is always in nanoseconds; convert to seconds
// -- it returns -1 (-1ns) when there is no associated expiry
// -- it returns -2 (-2ns) when there is no key
func (r Client) TTL(ctx context.Context, key string) (int64, error) {
val, err := r.Client.TTL(ctx, key).Result()

// return a negative number to not confuse it with a positive value response
if err != nil {
r.log.Error("REDIS_TTL", err)
return -3, err
}

if val == -2 {
errMsg := fmt.Errorf("no TTL found for key")
r.log.Error("REDIS_TTL_NOT_FOUND", errMsg)
return -2, errMsg
}

if val == -1 {
errMsg := fmt.Errorf("TTL does not expire")
r.log.Error("REDIS_TTL_NO_EXPIRY", errMsg)
return -1, errMsg
}

return int64(val / time.Second), nil
}

0 comments on commit a4ba506

Please sign in to comment.