-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
12 changed files
with
132 additions
and
3 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
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
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
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,4 @@ | ||
DB_PORT=6379 | ||
DB_ADDRESS=localhost | ||
DB_PASSWORD= | ||
DB_DATABASE=0 |
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,66 @@ | ||
package database | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"strconv" | ||
"time" | ||
|
||
_ "github.com/joho/godotenv/autoload" | ||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
type Service interface { | ||
Health() map[string]string | ||
} | ||
|
||
type service struct { | ||
db *redis.Client | ||
} | ||
|
||
var ( | ||
address = os.Getenv("DB_ADDRESS") | ||
port = os.Getenv("DB_PORT") | ||
password = os.Getenv("DB_PASSWORD") | ||
database = os.Getenv("DB_DATABASE") | ||
) | ||
|
||
func New() Service { | ||
num, err := strconv.Atoi(database) | ||
if err != nil { | ||
log.Fatalf(fmt.Sprintf("database incorrect %v", err)) | ||
} | ||
|
||
fullAddress := fmt.Sprintf("%s:%s", address, port) | ||
|
||
rdb := redis.NewClient(&redis.Options{ | ||
Addr: fullAddress, | ||
Password: password, | ||
DB: num, | ||
}) | ||
|
||
s := &service{db: rdb} | ||
|
||
return s | ||
} | ||
|
||
func (s *service) Health() map[string]string { | ||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) | ||
defer cancel() | ||
|
||
result, err := s.db.Ping(ctx).Result() | ||
|
||
if err != nil { | ||
log.Fatalf(fmt.Sprintf("db down: %v", err)) | ||
} | ||
|
||
if result != "PONG" { | ||
log.Fatalf(fmt.Sprintf("Unexpected ping response: %s", result)) | ||
} | ||
|
||
return map[string]string{ | ||
"message": "It's healthy", | ||
} | ||
} |
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,21 @@ | ||
package dbdriver | ||
|
||
import ( | ||
_ "embed" | ||
) | ||
|
||
type RedisTemplate struct{} | ||
|
||
//go:embed files/service/redis.tmpl | ||
var redisServiceTemplate []byte | ||
|
||
//go:embed files/env/redis.tmpl | ||
var redisEnvTemplate []byte | ||
|
||
func (r RedisTemplate) Service() []byte { | ||
return redisServiceTemplate | ||
} | ||
|
||
func (r RedisTemplate) Env() []byte { | ||
return redisEnvTemplate | ||
} |
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 @@ | ||
version: '3.8' | ||
|
||
services: | ||
redis: | ||
image: redis:latest | ||
restart: always | ||
ports: | ||
- "${DB_PORT}:6379" |
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,14 @@ | ||
package docker | ||
|
||
import ( | ||
_ "embed" | ||
) | ||
|
||
type RedisDockerTemplate struct{} | ||
|
||
//go:embed files/docker-compose/redis.tmpl | ||
var redisDockerTemplate []byte | ||
|
||
func (r RedisDockerTemplate) Docker() []byte { | ||
return redisDockerTemplate | ||
} |
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 |
---|---|---|
|
@@ -21,3 +21,4 @@ | |
- rustafariandev | ||
- limesten | ||
- Ujstor | ||
- schoolboybru |
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