From 369d1bc40fcdc49df94f6b936d8bca582ff3061e Mon Sep 17 00:00:00 2001 From: dong <1278815766@qq.com> Date: Fri, 27 Dec 2024 19:41:54 +0800 Subject: [PATCH] =?UTF-8?q?redis=20=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 14 +++ .idea/1.Internal Network Penetration.iml | 9 -- .idea/modules.xml | 8 -- .idea/vcs.xml | 6 -- .idea/workspace.xml | 103 ----------------------- .vscode/launch.json | 15 ---- .vscode/settings.json | 3 - Dockerfile | 35 ++++++++ config/redis.go | 49 +++++++++++ docker-compose.yml | 19 +++++ main.go | 10 ++- servers.go | 45 ++-------- users.go | 30 +++---- 13 files changed, 144 insertions(+), 202 deletions(-) create mode 100644 .gitignore delete mode 100644 .idea/1.Internal Network Penetration.iml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml delete mode 100644 .idea/workspace.xml delete mode 100644 .vscode/launch.json delete mode 100644 .vscode/settings.json create mode 100644 Dockerfile create mode 100644 config/redis.go create mode 100644 docker-compose.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d4a49e6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +/.idea/ +/.idea_modules/ +/.gradle/ +/build/ + +# Logs +logs/ +*.log + +# Caches +*.exe + +# folder +Run \ No newline at end of file diff --git a/.idea/1.Internal Network Penetration.iml b/.idea/1.Internal Network Penetration.iml deleted file mode 100644 index 338a266..0000000 --- a/.idea/1.Internal Network Penetration.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index a1b8186..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1dd..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index bbaef37..0000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,103 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - true - - \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index f2b112f..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - // 使用 IntelliSense 了解相关属性。 - // 悬停以查看现有属性的描述。 - // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "name": "Launch Package", - "type": "go", - "request": "launch", - "mode": "auto", - "program": "${fileDirname}" - } - ] -} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 359cd76..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "dotnet.preferCSharpExtension": true -} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..66cf400 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,35 @@ +# 使用官方Go镜像作为构建环境 +FROM golang:1.23-alpine AS builder + +# 设置工作目录 +WORKDIR /app + +# 复制go mod和sum文件 +COPY go.mod go.sum ./ + +# 下载依赖 +RUN go mod download + +# 复制源代码 +COPY . . + +# 构建应用 +RUN CGO_ENABLED=0 GOOS=linux go build -o main . + +# 使用轻量级的alpine作为运行环境 +FROM alpine:latest + +# 安装必要的CA证书 +RUN apk --no-cache add ca-certificates + +WORKDIR /root/ + +# 从builder阶段复制编译好的应用 +COPY --from=builder /app/main . +COPY .env . + +# 暴露应用端口(根据实际需要修改) +EXPOSE 8080 + +# 运行应用 +CMD ["./main"] diff --git a/config/redis.go b/config/redis.go new file mode 100644 index 0000000..f2f7b53 --- /dev/null +++ b/config/redis.go @@ -0,0 +1,49 @@ +package config + +import ( + "fmt" + "os" + + "github.com/redis/go-redis/v9" +) + +// Redis配置结构 +type RedisConfig struct { + Host string + Port string + Password string +} + +// 获取Redis配置 +func GetRedisConfig() *RedisConfig { + config := &RedisConfig{ + Host: os.Getenv("REDIS_HOST"), + Port: os.Getenv("REDIS_PORT"), + Password: os.Getenv("REDIS_PASSWORD"), + } + + // 设置默认值 + if config.Host == "" { + config.Host = "localhost" + } + if config.Port == "" { + config.Port = "6379" + } + if config.Password == "" { + config.Password = "" + } + + return config +} + +// 创建Redis客户端 +func NewRedisClient(db int) (*redis.Client, error) { + config := GetRedisConfig() + client := redis.NewClient(&redis.Options{ + Addr: fmt.Sprintf("%s:%s", config.Host, config.Port), + Password: config.Password, + DB: db, + }) + + return client, nil +} diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..db1b7ab --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,19 @@ +version: '3.8' + +services: + app: + build: . + ports: + - "8080:8080" + environment: + - REDIS_HOST=localhost + - REDIS_PORT=6379 + - REDIS_PASSWORD=*f2iPMa#N42!M + extra_hosts: + - "host.docker.internal:host-gateway" + networks: + - app-network + +networks: + app-network: + driver: bridge diff --git a/main.go b/main.go index bc879a7..e6b70d5 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,8 @@ package main import ( "Go_Test/config" "context" + "fmt" + "github.com/redis/go-redis/v9" ) @@ -25,12 +27,14 @@ var ( ) func main() { - // 初始化数据库 - err := InitRedis() + var err error + // 初始化Redis客户端 + RedisClient, err = config.NewRedisClient(0) if err != nil { + fmt.Printf("Failed to initialize Redis: %v\n", err) return } + defer RedisClient.Close() Connect() - } diff --git a/servers.go b/servers.go index ed041bc..bd58222 100644 --- a/servers.go +++ b/servers.go @@ -2,36 +2,16 @@ package main import ( "fmt" - "Go_Test/config" - - "github.com/redis/go-redis/v9" ) // InitRedis 初始化Redis连接 func InitRedis() error { - - // 创建Redis客户端连接 - RedisClient = redis.NewClient(&redis.Options{ - Addr: "localhost:6379", - Password: "123456", - DB: 0, - }) - /* - // 测试连接 - _, err := RedisClient.Ping(ctx).Result() - if err != nil { - return fmt.Errorf("redis连接失败: %v", err) - } - fmt.Println("redis连接成功") - */ - // 获取所有 Frp 服务器信息 - GetAllFrpServerInfo() - GetFrpServerInfo() - - // 显示所有服务器信息 - //ShowServerInfo() - + var err error + RedisClient, err = config.NewRedisClient(0) + if err != nil { + return fmt.Errorf("failed to initialize Redis: %v", err) + } return nil } @@ -105,18 +85,3 @@ func GetAllFrpServerInfo() { }) } } - -/* -// 显示服务器信息 -func ShowServerInfo() { - // 获取当前时间 - currentTime := time.Now().Format("2006-01-02 15:04:05") - fmt.Println("当前时间:", currentTime) - - // 打印服务器信息 - for i := 1; i <= GetFrpServerNum(); i++ { - fmt.Printf("Frp %d Server Info: \n", i) - fmt.Printf("Host: %s\nPort: %s\nToken: %s\n", ServerConfig[i-1].Host, ServerConfig[i-1].Port, ServerConfig[i-1].Token) - } -} -*/ diff --git a/users.go b/users.go index 71f2ae6..09d9ec4 100644 --- a/users.go +++ b/users.go @@ -3,27 +3,27 @@ package main import ( "Go_Test/config" "fmt" - "github.com/redis/go-redis/v9" "time" ) +// ConnectRedis 连接Redis +func ConnectRedis() error { + var err error + RedisClient, err = config.NewRedisClient(1) + if err != nil { + return fmt.Errorf("failed to connect to Redis: %v", err) + } + return nil +} + // InitUser 初始化用户连接 func InitUser(username string, password string) error { - // 创建Redis客户端连接 - RedisClient = redis.NewClient(&redis.Options{ - Addr: "localhost:6379", - Password: "123456", - DB: 1, - }) - /* - // 测试连接 - _, err := RedisClient.Ping(ctx).Result() - if err != nil { - return fmt.Errorf("redis连接失败: %v", err) - } - fmt.Println("redis连接成功") - */ + // 连接Redis + err := ConnectRedis() + if err != nil { + return err + } SaveUserToRedis(GetUsernameAndPassword(username, password))