mirror of
https://github.com/coaidev/coai.git
synced 2025-05-26 00:10:15 +09:00
update database: user table, ubscription table, package table, payment logger table
This commit is contained in:
parent
f9f784cc35
commit
cd87ecb085
@ -11,7 +11,14 @@ const router = createRouter({ //@ts-ignore
|
||||
meta: {
|
||||
title: "Chat Nio",
|
||||
},
|
||||
},
|
||||
}, {
|
||||
path: "/login",
|
||||
name: "login",
|
||||
component: () => import("../src/views/LoginView.vue"),
|
||||
meta: {
|
||||
title: "Login | Chat Nio",
|
||||
}
|
||||
}
|
||||
],
|
||||
});
|
||||
|
||||
|
@ -10,3 +10,16 @@ openai:
|
||||
anonymous_endpoint: https://api.openai.com/v1
|
||||
user: sk-xxxxxx
|
||||
user_endpoint: https://api.openai.com/v1
|
||||
|
||||
mysql:
|
||||
host: "localhost"
|
||||
port: 3306
|
||||
user: root
|
||||
password: ...
|
||||
|
||||
db: "chatnio"
|
||||
|
||||
secret: ...
|
||||
auth:
|
||||
access: ...
|
||||
salt: ...
|
||||
|
93
connection/database.go
Normal file
93
connection/database.go
Normal file
@ -0,0 +1,93 @@
|
||||
package connection
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/spf13/viper"
|
||||
"log"
|
||||
)
|
||||
|
||||
var Database *sql.DB
|
||||
|
||||
func ConnectMySQL() *sql.DB {
|
||||
// connect to MySQL
|
||||
Database, err := sql.Open("mysql", fmt.Sprintf(
|
||||
"%s:%s@tcp(%s:%d)/%s",
|
||||
viper.GetString("mysql.user"),
|
||||
viper.GetString("mysql.password"),
|
||||
viper.GetString("mysql.host"),
|
||||
viper.GetInt("mysql.port"),
|
||||
viper.GetString("mysql.db"),
|
||||
))
|
||||
if err != nil {
|
||||
log.Fatalln("Failed to connect to MySQL server: ", err)
|
||||
} else {
|
||||
log.Println("Connected to MySQL server successfully")
|
||||
}
|
||||
|
||||
CreateUserTable(Database)
|
||||
CreateSubscriptionTable(Database)
|
||||
CreatePackageTable(Database)
|
||||
CreatePaymentLogTable(Database)
|
||||
return Database
|
||||
}
|
||||
|
||||
func CreateUserTable(db *sql.DB) {
|
||||
_, err := db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS auth (
|
||||
id INT PRIMARY KEY AUTO_INCREMENT,
|
||||
bind_id INT UNIQUE,
|
||||
username VARCHAR(24) UNIQUE,
|
||||
token VARCHAR(255) NOT NULL,
|
||||
password VARCHAR(64) NOT NULL
|
||||
);
|
||||
`)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func CreatePaymentLogTable(db *sql.DB) {
|
||||
_, err := db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS payment_log (
|
||||
id INT PRIMARY KEY AUTO_INCREMENT,
|
||||
user_id INT,
|
||||
amount DECIMAL(12,2) DEFAULT 0,
|
||||
description VARCHAR(3600),
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
`)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func CreateSubscriptionTable(db *sql.DB) {
|
||||
_, err := db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS subscription (
|
||||
id INT PRIMARY KEY AUTO_INCREMENT,
|
||||
user_id INT,
|
||||
plan_id INT,
|
||||
expired_at DATETIME,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
`)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func CreatePackageTable(db *sql.DB) {
|
||||
_, err := db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS package (
|
||||
id INT PRIMARY KEY AUTO_INCREMENT,
|
||||
user_id INT,
|
||||
money DECIMAL(12,2) DEFAULT 0,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
`)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
1
go.mod
1
go.mod
@ -15,6 +15,7 @@ require (
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.14.0 // indirect
|
||||
github.com/go-redis/redis/v8 v8.11.5 // indirect
|
||||
github.com/go-sql-driver/mysql v1.7.1 // indirect
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
|
2
go.sum
2
go.sum
@ -83,6 +83,8 @@ github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg
|
||||
github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
|
||||
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
|
||||
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
|
||||
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
|
||||
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
|
||||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||
|
4
main.go
4
main.go
@ -14,12 +14,14 @@ func main() {
|
||||
panic(err)
|
||||
}
|
||||
connection.ConnectRedis()
|
||||
connection.ConnectMySQL()
|
||||
|
||||
app := gin.Default()
|
||||
{
|
||||
app.Use(middleware.CORSMiddleware())
|
||||
app.Use(middleware.BuiltinMiddleWare(connection.Database, connection.Cache))
|
||||
app.Use(middleware.ThrottleMiddleware())
|
||||
|
||||
|
||||
app.POST("/api/anonymous", api.AnonymousAPI)
|
||||
}
|
||||
if err := app.Run(":" + viper.GetString("server.port")); err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user