Skip to content

Commit

Permalink
Redis (#177)
Browse files Browse the repository at this point in the history
Redis
  • Loading branch information
schoolboybru authored Feb 7, 2024
1 parent a69aa16 commit 2c56ad0
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/generate-linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
framework:
[chi, gin, fiber, gorilla/mux, httprouter, standard-library, echo]
driver:
[mysql, postgres, sqlite, mongo, none]
[mysql, postgres, sqlite, mongo, redis, none]
goVersion: ["1.20"]
runs-on: ubuntu-latest
steps:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Choose from a variety of supported database drivers:
- [Postgres](https://github.com/jackc/pgx/)
- [Sqlite](https://github.com/mattn/go-sqlite3)
- [Mongo](https://go.mongodb.org/mongo-driver)
- [Redis](https://github.com/redis/go-redis)

<a id="advanced-features"></a>
<h2>
Expand Down
3 changes: 2 additions & 1 deletion cmd/flags/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ const (
Postgres Database = "postgres"
Sqlite Database = "sqlite"
Mongo Database = "mongo"
Redis Database = "redis"
None Database = "none"
)

var AllowedDBDrivers = []string{string(MySql), string(Postgres), string(Sqlite), string(Mongo), string(None)}
var AllowedDBDrivers = []string{string(MySql), string(Postgres), string(Sqlite), string(Mongo), string(Redis), string(None)}

func (f Database) String() string {
return string(f)
Expand Down
9 changes: 9 additions & 0 deletions cmd/program/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ var (
mysqlDriver = []string{"github.com/go-sql-driver/mysql"}
postgresDriver = []string{"github.com/lib/pq"}
sqliteDriver = []string{"github.com/mattn/go-sqlite3"}
redisDriver = []string{"github.com/redis/go-redis/v9"}
mongoDriver = []string{"go.mongodb.org/mongo-driver"}

godotenvPackage = []string{"github.com/joho/godotenv"}
Expand Down Expand Up @@ -183,6 +184,10 @@ func (p *Project) createDBDriverMap() {
packageName: mongoDriver,
templater: dbdriver.MongoTemplate{},
}
p.DBDriverMap[flags.Redis] = Driver{
packageName: redisDriver,
templater: dbdriver.RedisTemplate{},
}
}

func (p *Project) createDockerMap() {
Expand All @@ -200,6 +205,10 @@ func (p *Project) createDockerMap() {
packageName: []string{},
templater: docker.MongoDockerTemplate{},
}
p.DockerMap[flags.Redis] = Docker{
packageName: []string{},
templater: docker.RedisDockerTemplate{},
}
}

// CreateMainFile creates the project folders and files,
Expand Down
3 changes: 3 additions & 0 deletions cmd/steps/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ func InitSteps(projectType flags.Framework, databaseType flags.Database) *Steps
{
Title: "Mongo",
Desc: "The MongoDB supported driver for Go."},
{
Title: "Redis",
Desc: "Redis driver for Go."},
{
Title: "None",
Desc: "Choose this option if you don't wish to install a specific database driver."},
Expand Down
4 changes: 4 additions & 0 deletions cmd/template/dbdriver/files/env/redis.tmpl
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
66 changes: 66 additions & 0 deletions cmd/template/dbdriver/files/service/redis.tmpl
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",
}
}
21 changes: 21 additions & 0 deletions cmd/template/dbdriver/redis.go
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
}
8 changes: 8 additions & 0 deletions cmd/template/docker/files/docker-compose/redis.tmpl
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"
14 changes: 14 additions & 0 deletions cmd/template/docker/redis.go
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
}
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
- rustafariandev
- limesten
- Ujstor
- schoolboybru
3 changes: 2 additions & 1 deletion docs/docs/db-drivers.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ To extend the project with database functionality, users can choose from a varie
2. [Postgres](https://github.com/jackc/pgx/): Facilitates connectivity to PostgreSQL databases.
3. [Sqlite](https://github.com/mattn/go-sqlite3): Suitable for projects requiring a lightweight, self-contained database.
4. [Mongo](https://go.mongodb.org/mongo-driver): Provides necessary tools for connecting and interacting with MongoDB databases.
5. [Redis](https://github.com/redis/go-redis): Provides tools for connectiong and interacting with Redis

## Updated Project Structure

Expand Down Expand Up @@ -38,4 +39,4 @@ Users can select the desired database driver based on their project's specific n

To facilitate quick setup and testing, a `docker-compose.yml` file is provided. This file defines a service for the chosen database system with the necessary environment variables. Running `docker-compose up` will quickly spin up a containerized instance of the database, allowing users to test their application against a real database server.

This Docker Compose approach simplifies the process of setting up a database for development or testing purposes, providing a convenient and reproducible environment for the project.
This Docker Compose approach simplifies the process of setting up a database for development or testing purposes, providing a convenient and reproducible environment for the project.

0 comments on commit 2c56ad0

Please sign in to comment.