-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a363b19
commit b6df6c2
Showing
7 changed files
with
182 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
// handle the short --> long URL redirection | ||
func RedirectHandler(w http.ResponseWriter, r *http.Request) { | ||
http.Error(w, "Not implemented", http.StatusNotImplemented) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package database | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/go-redis/redis/v8" | ||
) | ||
|
||
// RedisClient is a struct that encapsulates the Redis client. | ||
type RedisClient struct { | ||
client *redis.Client | ||
ctx context.Context | ||
} | ||
|
||
// NewRedisClient initializes a new Redis client. | ||
func NewRedisClient(addr, password string, db int) *RedisClient { | ||
rdb := redis.NewClient(&redis.Options{ | ||
Addr: addr, | ||
Password: password, | ||
DB: db, // Use default DB | ||
}) | ||
return &RedisClient{ | ||
client: rdb, | ||
ctx: context.Background(), | ||
} | ||
} | ||
|
||
// Ping checks the connection to the Redis server. | ||
func (r *RedisClient) Ping() error { | ||
pong, err := r.client.Ping(r.ctx).Result() | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("Connected to Redis: %s\n", pong) | ||
return nil | ||
} | ||
|
||
// Set stores a key-value pair in Redis. | ||
func (r *RedisClient) Set(key, value string) error { | ||
err := r.client.Set(r.ctx, key, value, 0).Err() | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("Key '%s' set successfully!\n", key) | ||
return nil | ||
} | ||
|
||
// Get retrieves the value of a given key from Redis. | ||
func (r *RedisClient) Get(key string) (string, error) { | ||
val, err := r.client.Get(r.ctx, key).Result() | ||
if err != nil { | ||
return "", err | ||
} | ||
return val, nil | ||
} | ||
|
||
// Close closes the Redis client connection. | ||
func (r *RedisClient) Close() { | ||
err := r.client.Close() | ||
if err != nil { | ||
fmt.Printf("Error closing Redis connection: %v\n", err) | ||
} else { | ||
fmt.Println("Redis connection closed.") | ||
} | ||
} | ||
|
||
// // Main function | ||
// func main() { | ||
|
||
// // Test connection | ||
// if err := redisClient.Ping(); err != nil { | ||
// fmt.Printf("Could not connect to Redis: %v\n", err) | ||
// return | ||
// } | ||
|
||
// // Set a key-value pair | ||
// if err := redisClient.Set("mykey", "myvalue"); err != nil { | ||
// fmt.Printf("Error setting key: %v\n", err) | ||
// return | ||
// } | ||
|
||
// // Get the value of a key | ||
// value, err := redisClient.Get("mykey") | ||
// if err != nil { | ||
// fmt.Printf("Error getting key: %v\n", err) | ||
// return | ||
// } | ||
// fmt.Printf("Retrieved value: %s\n", value) | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
module github.com/SartajBhuvaji | ||
|
||
go 1.23.2 | ||
|
||
require ( | ||
github.com/cespare/xxhash/v2 v2.1.2 // indirect | ||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect | ||
github.com/go-redis/redis/v8 v8.11.5 // indirect | ||
github.com/joho/godotenv v1.5.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= | ||
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/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/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= | ||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package utils | ||
|
||
// ReverseString reverses a given string | ||
func ReverseString(s string) string { | ||
runes := []rune(s) | ||
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 { | ||
runes[i], runes[j] = runes[j], runes[i] | ||
} | ||
return string(runes) | ||
} |