Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
8540f12
update handle queue binding
ochom Jan 28, 2025
95bb2cd
update handle queue binding
ochom Jan 28, 2025
fac5996
update message content to data
ochom Jan 30, 2025
b2e4044
update versions
ochom Jan 30, 2025
0cd0ada
fix: set timeouts in fiber
ochom Jan 30, 2025
64d9522
fix: set timeouts in fiber
ochom Jan 30, 2025
abdbd15
get available port
ochom Feb 15, 2025
78468ae
update optionally set queue name
ochom Feb 15, 2025
ff63958
update streamx config
ochom Feb 25, 2025
0aa3a36
update streamx config
ochom Feb 25, 2025
9bca7bf
ignore record not found in logs
ochom Mar 6, 2025
898cd33
create a new raw execution
ochom Mar 10, 2025
87ea126
create a new raw execution
ochom Mar 10, 2025
ffd0598
update add transact
ochom Mar 11, 2025
94c6282
uodate check exists status
ochom Mar 18, 2025
3ea4faa
update: checking is a record exists using gorm
ochom May 13, 2025
e9db15a
add shhort id to uuid
ochom May 13, 2025
cb33c75
update short id implementation
ochom May 21, 2025
7af8c5f
update short id implementation
ochom May 21, 2025
f9434de
fixL parse numbers to kenyan format
ochom May 24, 2025
44bc037
draft: remove arrays chunk already in go
ochom Jun 10, 2025
0717002
update json encoding
ochom Jun 10, 2025
02fc988
update json encoding
ochom Jun 10, 2025
b4e8cf8
update streamx sdk
ochom Jun 13, 2025
cfc51f6
update streamx sdk
ochom Jun 13, 2025
d24beba
update streamx sdk
ochom Jun 13, 2025
9f372d1
update json to bytes payload
ochom Jul 11, 2025
34f9b5a
update marshal json
ochom Jul 11, 2025
5acd5c3
update gson maps
ochom Jul 24, 2025
4bf035a
update handle json encode
ochom Jul 25, 2025
e29e396
gutils update curd with context
ochom Jul 25, 2025
762b873
update json to jsonx
ochom Aug 1, 2025
e9f0063
adjust env vars
ochom Oct 7, 2025
d2193ad
add encryption in auth
ochom Nov 4, 2025
1eae604
Merge branch 'main' into dev
ochom Dec 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions arrays/chunk.go

This file was deleted.

84 changes: 84 additions & 0 deletions auth/encrryption.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package auth

import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"fmt"
"io"

"github.com/ochom/gutils/env"
)

// Vault
type Vault struct {
key string
}

// NewVault creates a new Vault with the given key
func NewVault(keys ...string) (*Vault, error) {
key := env.Get("VAULT_KEY")

if len(keys) > 0 && keys[0] != "" {
key = keys[0]
}

if len(key) != 32 {
return nil, fmt.Errorf("vault key must be 32 bytes long")
}

return &Vault{key: key}, nil
}

// Encrypt text using AES-GCM
func (v Vault) Encrypt(plaintext string) (string, error) {
block, err := aes.NewCipher([]byte(v.key))
if err != nil {
return "", err
}

aesGCM, err := cipher.NewGCM(block)
if err != nil {
return "", err
}

nonce := make([]byte, aesGCM.NonceSize())
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
return "", err
}

ciphertext := aesGCM.Seal(nonce, nonce, []byte(plaintext), nil)
return base64.StdEncoding.EncodeToString(ciphertext), nil
}

// Decrypt text using AES-GCM
func (v Vault) Decrypt(encryptedText string) (string, error) {
data, err := base64.StdEncoding.DecodeString(encryptedText)
if err != nil {
return "", err
}

block, err := aes.NewCipher([]byte(v.key))
if err != nil {
return "", err
}

aesGCM, err := cipher.NewGCM(block)
if err != nil {
return "", err
}

nonceSize := aesGCM.NonceSize()
if len(data) < nonceSize {
return "", fmt.Errorf("ciphertext too short")
}

nonce, ciphertext := data[:nonceSize], data[nonceSize:]
plaintext, err := aesGCM.Open(nil, nonce, ciphertext, nil)
if err != nil {
return "", err
}

return string(plaintext), nil
}
38 changes: 38 additions & 0 deletions auth/encrryption_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package auth_test

import (
"testing"

"github.com/ochom/gutils/auth"
)

func TestVault_Encrypt(t *testing.T) {
vault, err := auth.NewVault("12345678901234567890123456789012")
if err != nil {
t.Fatalf("failed to create vault: %v", err)
}

plaintext := "Hello, World!"
encryptedText, err := vault.Encrypt(plaintext)
if err != nil {
t.Fatalf("encryption failed: %v", err)
}

if encryptedText == plaintext {
t.Fatalf("encrypted text should not be the same as plaintext")
}

vault2, err := auth.NewVault("12345678901234567890123456789012")
if err != nil {
t.Fatalf("failed to create vault: %v", err)
}

decryptedText, err := vault2.Decrypt(encryptedText)
if err != nil {
t.Fatalf("decryption failed: %v", err)
}

if decryptedText != plaintext {
t.Fatalf("decrypted text does not match original plaintext")
}
}
16 changes: 8 additions & 8 deletions env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestGetEnv(t *testing.T) {
os.Setenv("HELLO", "world")
_ = os.Setenv("HELLO", "world")
type args struct {
key string
defaultValue string
Expand Down Expand Up @@ -45,8 +45,8 @@ func TestGetEnv(t *testing.T) {
}

func TestInt(t *testing.T) {
os.Setenv("HELLO", "45")
os.Setenv("HELLO2", "45s")
_ = os.Setenv("HELLO", "45")
_ = os.Setenv("HELLO2", "45s")

type args struct {
key string
Expand Down Expand Up @@ -92,9 +92,9 @@ func TestInt(t *testing.T) {
}

func TestBool(t *testing.T) {
os.Setenv("HELLO", "true")
os.Setenv("HELLO2", "false")
os.Setenv("HELLO3", "test")
_ = os.Setenv("HELLO", "true")
_ = os.Setenv("HELLO2", "false")
_ = os.Setenv("HELLO3", "test")

type args struct {
key string
Expand Down Expand Up @@ -148,8 +148,8 @@ func TestBool(t *testing.T) {
}

func TestFloat(t *testing.T) {
os.Setenv("HELLO", "45.5")
os.Setenv("HELLO2", "45.5s")
_ = os.Setenv("HELLO", "45.5")
_ = os.Setenv("HELLO2", "45.5s")

type args struct {
key string
Expand Down
62 changes: 25 additions & 37 deletions gttp/gofiber.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gttp

import (
"context"
"errors"
"fmt"
"strings"
Expand All @@ -23,71 +22,60 @@ func (c *fiberClient) get(url string, headers M, timeouts ...time.Duration) (res
}

// sendRequest sends a request to the specified URL.
func (c *fiberClient) sendRequest(url, method string, headers M, body []byte, timeouts ...time.Duration) (resp *Response, err error) {
func (c *fiberClient) sendRequest(url, method string, headers M, body []byte, timeouts ...time.Duration) (*Response, error) {
timeout := time.Hour
if len(timeouts) > 0 {
timeout = timeouts[0]
}

ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

result := make(chan *Response, 1)
go func() {
resp := c.makeRequest(url, method, headers, body)
result <- resp
}()

select {
case <-ctx.Done():
return nil, ctx.Err()
case r := <-result:
if len(r.Errors) == 0 {
return r, nil
}

errStrings := []string{}
for _, err := range r.Errors {
errStrings = append(errStrings, err.Error())
}
resp := c.makeRequest(url, method, headers, body, timeout)
if len(resp.Errors) == 0 {
return resp, nil
}

return r, errors.New(strings.Join(errStrings, ", "))
errStrings := []string{}
for _, err := range resp.Errors {
errStrings = append(errStrings, err.Error())
}

return resp, errors.New(strings.Join(errStrings, ", "))
}

// makeRequest sends a request to the specified URL.
func (c *fiberClient) makeRequest(url, method string, headers M, body []byte) (resp *Response) {
client := fiber.AcquireClient()
var req *fiber.Agent

func (c *fiberClient) makeRequest(url, method string, headers M, body []byte, timeout time.Duration) *Response {
var agent *fiber.Agent
switch method {
case "POST":
req = client.Post(url)
agent = fiber.Post(url)
case "GET":
req = client.Get(url)
agent = fiber.Get(url)
case "DELETE":
req = client.Delete(url)
agent = fiber.Delete(url)
case "PUT":
req = client.Put(url)
agent = fiber.Put(url)
case "PATCH":
req = client.Patch(url)
agent = fiber.Patch(url)
default:
err := fmt.Errorf("unknown method: %s", method)
return NewResponse(500, []error{err}, nil)
}

// skip ssl verification
req.InsecureSkipVerify()
agent.InsecureSkipVerify()
agent.Timeout(timeout)

// add request headers
for k, v := range headers {
req.Add(k, v)
agent.Add(k, v)
}

// add request body
if method == "POST" || method == "PUT" || method == "PATCH" {
req.Body(body)
agent.Body(body)
}

code, content, errs := req.Bytes()
// make request
code, content, errs := agent.Bytes()
if code == 0 {
code = 500
}
Expand Down
20 changes: 20 additions & 0 deletions helpers/address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package helpers

import (
"fmt"
"net"
"time"

"github.com/ochom/gutils/logs"
)

// GetAvailableAddress returns the next available address e.g :8080
func GetAvailableAddress(port int) string {
_, err := net.DialTimeout("tcp", net.JoinHostPort("", fmt.Sprintf("%d", port)), time.Second)
if err == nil {
logs.Warn("[🥵] address :%d is not available trying another port...", port)
return GetAvailableAddress(port + 1)
}

return fmt.Sprintf(":%d", port)
}
27 changes: 27 additions & 0 deletions helpers/address_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package helpers

import "testing"

func TestGetAvailableAddress(t *testing.T) {
type args struct {
port int
}
tests := []struct {
name string
args args
want string
}{
{
name: "Test GetAvailableAddress",
args: args{port: 8080},
want: ":8080",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GetAvailableAddress(tt.args.port); got != tt.want {
t.Errorf("GetAvailableAddress() = %v, want %v", got, tt.want)
}
})
}
}
29 changes: 0 additions & 29 deletions helpers/json.go

This file was deleted.

Loading
Loading