Compare commits
10 Commits
0fef07cfae
...
d25c3eed54
Author | SHA1 | Date |
---|---|---|
Matthew | d25c3eed54 | |
Matthew | 30e0c47a08 | |
Matthew | e1d10687b2 | |
Matthew | b897a8e331 | |
Matthew | 6dcee20a1c | |
Matthew | 167d1bb067 | |
Matthew | 61d93d1cc9 | |
Matthew | 50072202ca | |
Matthew | 5489020ba1 | |
Matthew | 29588e0829 |
|
@ -1,2 +1,2 @@
|
|||
# IDE Configuration Files
|
||||
.idea
|
||||
.idea/
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
|
@ -0,0 +1,37 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
RedisURL string `json:"redisURL"`
|
||||
MongoDBURL string `json:"mongoDBURL"`
|
||||
ServiceAddress string `json:"serviceAddress"`
|
||||
WebServerPort int `json:"webServerPort"`
|
||||
}
|
||||
|
||||
func LoadConfig(file string) (*Config, error) {
|
||||
// Open the config file
|
||||
configFile, err := os.Open(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func(configFile *os.File) {
|
||||
err := configFile.Close()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}(configFile)
|
||||
|
||||
// Parse the JSON file into a Config struct
|
||||
var config Config
|
||||
decoder := json.NewDecoder(configFile)
|
||||
err = decoder.Decode(&config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &config, nil
|
||||
}
|
11
go.mod
11
go.mod
|
@ -7,4 +7,15 @@ require github.com/redis/go-redis/v9 v9.6.1
|
|||
require (
|
||||
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/golang/snappy v0.0.4 // indirect
|
||||
github.com/klauspost/compress v1.13.6 // indirect
|
||||
github.com/montanaflynn/stats v0.7.1 // indirect
|
||||
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
|
||||
github.com/xdg-go/scram v1.1.2 // indirect
|
||||
github.com/xdg-go/stringprep v1.0.4 // indirect
|
||||
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
|
||||
go.mongodb.org/mongo-driver v1.17.1 // indirect
|
||||
golang.org/x/crypto v0.26.0 // indirect
|
||||
golang.org/x/sync v0.8.0 // indirect
|
||||
golang.org/x/text v0.17.0 // indirect
|
||||
)
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
package cache
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/gob"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func StoreCacheGob[Data any](ctx context.Context, rdb *redis.Client, key string, data Data) error {
|
||||
var buffer bytes.Buffer
|
||||
encoder := gob.NewEncoder(&buffer)
|
||||
|
||||
// Serialize struct to Gob
|
||||
err := encoder.Encode(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Store data in Redis
|
||||
return rdb.Set(ctx, key, buffer.Bytes(), 0).Err()
|
||||
}
|
||||
|
||||
func GetCacheGob[Data any](ctx context.Context, rdb *redis.Client, key string) (Data, error) {
|
||||
var data Data
|
||||
|
||||
binData, err := rdb.Get(ctx, key).Bytes()
|
||||
if err != nil {
|
||||
return data, err
|
||||
}
|
||||
|
||||
buffer := bytes.NewBuffer(binData)
|
||||
decoder := gob.NewDecoder(buffer)
|
||||
|
||||
// Deserialize Gob binary back to struct
|
||||
err = decoder.Decode(&data)
|
||||
if err != nil {
|
||||
return data, err
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.libraryofcode.org/engineering/bces/config"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"log"
|
||||
)
|
||||
|
||||
// NewRedisConnection takes a Config parameter and connects to the RedisURL provided in the config. It returns an instance of Redis Client.
|
||||
// This function will exit the program if it cannot parse the URL properly nor connect successfully, and therefore doesn't return an error.
|
||||
func NewRedisConnection(config config.Config) *redis.Client {
|
||||
redisConnectionOptions, err := redis.ParseURL(config.RedisURL)
|
||||
if err != nil {
|
||||
log.Fatalf("Redis Connection URL is not valid or cannot parse given Connection URL: %s.\n\n%s", config.RedisURL, err)
|
||||
}
|
||||
redisClient := redis.NewClient(redisConnectionOptions)
|
||||
|
||||
redisPingResponse, err := redisClient.Ping(context.Background()).Result()
|
||||
if err != nil {
|
||||
log.Fatalf("Could not ping Redis.\n\n%s", err)
|
||||
}
|
||||
log.Printf("Connected to Redis DB at <%s>: %s", config.RedisURL, redisPingResponse)
|
||||
|
||||
return redisClient
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package cache
|
||||
|
||||
import "time"
|
||||
|
||||
type CachedReport struct {
|
||||
ReportID string
|
||||
UserID string
|
||||
GeneratedAt time.Time
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package db
|
||||
|
||||
type HardInquiryDB struct {
|
||||
ID string
|
||||
UserID string
|
||||
VendorID string
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package db
|
||||
|
||||
type Vendor struct {
|
||||
ID string `bson:"vendorID"`
|
||||
Name string `bson:"name"`
|
||||
PermissionLevel int `bson:"permissionLevel"`
|
||||
Key string `bson:"key"`
|
||||
}
|
Loading…
Reference in New Issue