Compare commits

...

10 Commits

Author SHA1 Message Date
Matthew d25c3eed54
go modules update 2024-10-23 19:44:28 -04:00
Matthew 30e0c47a08
hard inquiry struct 2024-10-23 19:44:11 -04:00
Matthew e1d10687b2
cache creation move 2024-10-23 19:44:06 -04:00
Matthew b897a8e331
vendor struct 2024-10-23 19:44:01 -04:00
Matthew 6dcee20a1c
report struct 2024-10-23 19:43:57 -04:00
Matthew 167d1bb067
config loading 2024-10-23 19:43:52 -04:00
Matthew 61d93d1cc9
functions for manipulating gobs 2024-10-23 19:43:44 -04:00
Matthew 50072202ca
.gitignore 2024-10-23 19:43:30 -04:00
Matthew 5489020ba1
main file for internal/db package 2024-10-04 17:50:08 -04:00
Matthew 29588e0829
main file for config package 2024-10-04 17:50:03 -04:00
9 changed files with 141 additions and 9 deletions

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
# IDE Configuration Files # IDE Configuration Files
.idea .idea/

8
.idea/.gitignore vendored
View File

@ -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

37
config/main.go Normal file
View File

@ -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
View File

@ -7,4 +7,15 @@ require github.com/redis/go-redis/v9 v9.6.1
require ( require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // 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
) )

42
internal/cache/gob.go vendored Normal file
View File

@ -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
}

26
internal/cache/main.go vendored Normal file
View File

@ -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
}

9
internal/cache/report.go vendored Normal file
View File

@ -0,0 +1,9 @@
package cache
import "time"
type CachedReport struct {
ReportID string
UserID string
GeneratedAt time.Time
}

View File

@ -0,0 +1,7 @@
package db
type HardInquiryDB struct {
ID string
UserID string
VendorID string
}

8
internal/db/Vendor.go Normal file
View File

@ -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"`
}