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 14 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.idea
/config/conf.yaml
/docs/思.md
/test
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
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
)
89 changes: 79 additions & 10 deletions go.sum

Large diffs are not rendered by default.

40 changes: 16 additions & 24 deletions internal/cache/redis.go
Copy link
Contributor

Choose a reason for hiding this comment

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

从go语言的接口规范来看一般接口在调用方定义,因为不管底层实现如何,调用方需要的方法大多数情况只是实现的方法,这种在被调用方定义的方式耦合度比较高。可以看看去年陈诚分享的文章

Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,51 @@ package cache

import (
"context"
"github.com/raiki02/EG/internal/dao"
"github.com/raiki02/EG/internal/model"
"github.com/raiki02/EG/tools"
"github.com/redis/go-redis/v9"
"time"
)

// 先找缓存,缓存没有再找数据库
// TODO: scpoe: 活动,帖子,jwt
type CacheHdl interface {
Get(ctx context.Context, key string) ([]model.Activity, error)
Set(ctx context.Context, key string, val []model.Activity) error
Del(ctx context.Context, key string) error
Get(context.Context, string) ([]interface{}, error)
Set(context.Context, string, []interface{}) error
Del(context.Context, string) error
}

// 操作顺序 router -> controller -> cache -> dao
type Cache struct {
rdb *redis.Client
//操作评论的点赞和评论
cd dao.CommentDAOHdl
}

func NewCache(rdb *redis.Client, cd dao.CommentDAOHdl) CacheHdl {
return &Cache{rdb: rdb, cd: cd}
func NewCache(rdb *redis.Client) *Cache {
return &Cache{
rdb: rdb,
}
}

func (c *Cache) Get(ctx context.Context, key string) ([]model.Activity, error) {
key = "activity:" + key
func (c *Cache) Get(ctx context.Context, key string) ([]interface{}, error) {
b, err := c.rdb.Get(ctx, key).Bytes()
if err != nil {
return nil, err
}
return jsonTOstrs(b), nil
return jsonTOstrus(b), nil
}

func (c *Cache) Set(ctx context.Context, key string, val []model.Activity) error {
key = "activity:" + key
return c.rdb.Set(ctx, key, strsTOjson(val), 48*time.Hour).Err()
func (c *Cache) Set(ctx context.Context, key string, val []interface{}) error {
return c.rdb.Set(ctx, key, Tojson(val), 48*time.Hour).Err()
}

func (c *Cache) Del(ctx context.Context, key string) error {
return c.rdb.Del(ctx, key).Err()
}

// redis的val不能存结构体,转换成json格式储存
func strsTOjson(as []model.Activity) []byte {
return []byte(tools.Marshal(as))
func Tojson(in []interface{}) []byte {
return []byte(tools.Marshal(in))
}

func jsonTOstrs(b []byte) []model.Activity {
res := tools.Unmarshal(b, []model.Activity{})
as, ok := res.([]model.Activity)
if !ok {
return nil
}
return as
func jsonTOstrus(b []byte) (out []interface{}) {
return nil
Comment on lines +50 to +51
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Deserialization function is incomplete.
Returning nil breaks retrieval functionality. Implement JSON unmarshaling to restore the original data:

Example fix:

 func jsonTOstrus(b []byte) (out []interface{}) {
-   return nil
+   err := json.Unmarshal(b, &out)
+   if err != nil {
+       return nil
+   }
+   return out
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func jsonTOstrus(b []byte) (out []interface{}) {
return nil
func jsonTOstrus(b []byte) (out []interface{}) {
err := json.Unmarshal(b, &out)
if err != nil {
return nil
}
return out
}

}
Loading