Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: activity functions #2

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
/config/conf.yaml
/docs/思.md
/test
/volumes
8 changes: 7 additions & 1 deletion config/conf-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ kafka:
addr: addr

jwt:
key: secret-key-for-jwt
key: secret-key-for-jwt

imgbed:
accessKey: your-access-key
secretKey: your-secret-key
bucket: bucket-name
imgUrl: img-store-url
2 changes: 1 addition & 1 deletion config/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func Init() error {
flag.Parse()
viper.SetConfigFile(ConfPath)

viper.SetDefault("mysql.dsn", "root:114514@tcp(127.0.0.1:3306)/EventGlide?charset=utf8mb4&parseTime=True&loc=Local")
viper.SetDefault("mysql.dsn", "root:123456@tcp(127.0.0.1:3306)/EventGlide?charset=utf8mb4&parseTime=True&loc=Local")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Avoid hardcoding database credentials.

The MySQL password should not be hardcoded in the source code. Instead, load it from environment variables or a secure configuration file.

Apply this diff to use environment variables:

-viper.SetDefault("mysql.dsn", "root:123456@tcp(127.0.0.1:3306)/EventGlide?charset=utf8mb4&parseTime=True&loc=Local")
+viper.SetDefault("mysql.dsn", fmt.Sprintf("root:%s@tcp(127.0.0.1:3306)/EventGlide?charset=utf8mb4&parseTime=True&loc=Local", os.Getenv("MYSQL_PASSWORD")))

Add these imports:

import (
    "fmt"
    "os"
)

viper.SetDefault("mysql.maxIdleConns", 10)
viper.SetDefault("mysql.maxOpenConns", 100)
viper.SetDefault("redis.addr", "127.0.0.1:6379")
Expand Down
50 changes: 50 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
services:
kafka:
image: docker.io/bitnami/kafka:3.9
container_name: kafka_EG
ports:
- "9092:9092"
volumes:
- ./volumes/kafka:/bitnami
environment:
# KRaft settings
- KAFKA_CFG_NODE_ID=0
- KAFKA_CFG_PROCESS_ROLES=controller,broker
- KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=0@kafka:9093
# Listeners
- KAFKA_CFG_LISTENERS=PLAINTEXT://:9092,CONTROLLER://:9093
- KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://:9092
- KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
- KAFKA_CFG_CONTROLLER_LISTENER_NAMES=CONTROLLER
- KAFKA_CFG_INTER_BROKER_LISTENER_NAME=PLAINTEXT

zookeeper:
container_name: zookeeper_EG
image: docker.io/bitnami/zookeeper:3.9
ports:
- '2181:2181'
volumes:
- ./volumes/zookeeper:/bitnami/zookeeper
environment:
- ALLOW_ANONYMOUS_LOGIN=yes

db:
image: mysql:8.0
container_name: mysql_EG
restart: always
environment:
MYSQL_ROOT_PASSWORD: 114514
ports:
- "3306:3306"
volumes:
- ./volumes/mysql:/var/lib/mysql

Comment on lines +31 to +41
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Critical: Address MySQL security concerns and configuration.

  1. Move sensitive data to environment variables
  2. Create specific database and user instead of using root
  3. Configure character set and collation
  4. Use named volumes
 db:
   image: mysql:8.0
   container_name: mysql_EG
   restart: always
   environment:
-    MYSQL_ROOT_PASSWORD: 114514
+    MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
+    MYSQL_DATABASE: ${MYSQL_DATABASE}
+    MYSQL_USER: ${MYSQL_USER}
+    MYSQL_PASSWORD: ${MYSQL_PASSWORD}
+    MYSQL_CHARACTER_SET_SERVER: utf8mb4
+    MYSQL_COLLATION_SERVER: utf8mb4_unicode_ci
   ports:
     - "3306:3306"
   volumes:
-    - ./volumes/mysql:/var/lib/mysql
+    - mysql_data:/var/lib/mysql
+  healthcheck:
+    test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
+    interval: 30s
+    timeout: 10s
+    retries: 3

Create a .env file (add to .gitignore):

MYSQL_ROOT_PASSWORD=your_secure_password
MYSQL_DATABASE=app_db
MYSQL_USER=app_user
MYSQL_PASSWORD=app_password

Add at the end of the file:

volumes:
  mysql_data:
    driver: local

redis:
image: redis:7.0
container_name: redis_EG
restart: always
ports:
- "6379:6379"
volumes:
- ./volumes/redis:/data

Comment on lines +42 to +50
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance Redis security and performance configuration.

Consider the following improvements:

  1. Add password protection
  2. Set memory limits
  3. Configure persistence properly
  4. Add healthcheck
 redis:
   image: redis:7.0
   container_name: redis_EG
   restart: always
+  command: redis-server --requirepass ${REDIS_PASSWORD} --maxmemory 512mb --maxmemory-policy allkeys-lru
   ports:
     - "6379:6379"
   volumes:
-    - ./volumes/redis:/data
+    - redis_data:/data
+  healthcheck:
+    test: ["CMD", "redis-cli", "ping"]
+    interval: 30s
+    timeout: 10s
+    retries: 3

Add to .env file:

REDIS_PASSWORD=your_secure_password

Add at the end of the file:

volumes:
  redis_data:
    driver: local

16 changes: 14 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ module github.com/raiki02/EG
go 1.23.2

require (
github.com/IBM/sarama v1.45.0
github.com/gin-contrib/cors v1.7.3
github.com/gin-gonic/gin v1.10.0
github.com/golang-jwt/jwt/v4 v4.5.1
github.com/google/uuid v1.4.0
github.com/qiniu/go-sdk/v7 v7.25.2
github.com/redis/go-redis/v9 v9.7.0
github.com/spf13/viper v1.19.0
github.com/swaggo/swag v1.16.4
Expand All @@ -15,10 +17,12 @@ require (
)

require (
github.com/IBM/sarama v1.45.0 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/alex-ant/gomath v0.0.0-20160516115720-89013a210a82 // indirect
github.com/bytedance/sonic v1.12.6 // indirect
github.com/bytedance/sonic/loader v0.2.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
Expand All @@ -31,6 +35,7 @@ require (
github.com/eapache/queue v1.1.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.7 // indirect
github.com/gammazero/toposort v0.1.1 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.6 // indirect
Expand All @@ -39,9 +44,12 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.23.0 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/goccy/go-json v0.10.4 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/subcommands v1.2.0 // indirect
github.com/google/wire v0.6.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
Expand All @@ -66,6 +74,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pierrec/lz4/v4 v4.1.22 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
Expand All @@ -81,12 +90,15 @@ require (
golang.org/x/arch v0.12.0 // indirect
golang.org/x/crypto v0.32.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/net v0.34.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.29.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/protobuf v1.36.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
modernc.org/fileutil v1.0.0 // indirect
)
Loading