mirror of
https://github.com/coaidev/coai.git
synced 2025-05-19 21:10:18 +09:00
feat: auto config file creating and database root user
This commit is contained in:
parent
31eaba784b
commit
b9635352f9
@ -34,7 +34,7 @@ RUN ulimit -n 65535 && \
|
|||||||
echo "ulimit -n 65535" >> /etc/rc.local
|
echo "ulimit -n 65535" >> /etc/rc.local
|
||||||
|
|
||||||
# set go proxy to https://goproxy.cn (open for vps in China Mainland)
|
# set go proxy to https://goproxy.cn (open for vps in China Mainland)
|
||||||
RUN go env -w GOPROXY=https://goproxy.cn,direct
|
# RUN go env -w GOPROXY=https://goproxy.cn,direct
|
||||||
ENV GOOS=linux GOARCH=amd64 GO111MODULE=on CGO_ENABLED=1
|
ENV GOOS=linux GOARCH=amd64 GO111MODULE=on CGO_ENABLED=1
|
||||||
|
|
||||||
# Build backend
|
# Build backend
|
||||||
|
@ -93,6 +93,7 @@
|
|||||||
|
|
||||||
|
|
||||||
## 📦 部署 | Deploy
|
## 📦 部署 | Deploy
|
||||||
|
*部署成功后,管理员账号为 `root`,密码默认为 `123456`*
|
||||||
|
|
||||||
1. 编译安装 (自定义性强)
|
1. 编译安装 (自定义性强)
|
||||||
```shell
|
```shell
|
||||||
|
@ -8,6 +8,9 @@ mysql:
|
|||||||
redis:
|
redis:
|
||||||
host: localhost
|
host: localhost
|
||||||
port: 6379
|
port: 6379
|
||||||
|
db: 0
|
||||||
|
password: ""
|
||||||
|
|
||||||
|
|
||||||
secret: SbitdyN5ZH39cNxSrG3kMNZ1GfiyyQ43
|
secret: SbitdyN5ZH39cNxSrG3kMNZ1GfiyyQ43
|
||||||
|
|
||||||
|
@ -35,6 +35,8 @@ func ConnectMySQL() *sql.DB {
|
|||||||
log.Println(fmt.Sprintf("[connection] connected to mysql server (host: %s)", viper.GetString("mysql.host")))
|
log.Println(fmt.Sprintf("[connection] connected to mysql server (host: %s)", viper.GetString("mysql.host")))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
InitRootUser(db)
|
||||||
|
|
||||||
CreateUserTable(db)
|
CreateUserTable(db)
|
||||||
CreateConversationTable(db)
|
CreateConversationTable(db)
|
||||||
CreateSharingTable(db)
|
CreateSharingTable(db)
|
||||||
@ -50,6 +52,29 @@ func ConnectMySQL() *sql.DB {
|
|||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func InitRootUser(db *sql.DB) {
|
||||||
|
// create root user if totally empty
|
||||||
|
var count int
|
||||||
|
err := db.QueryRow("SELECT COUNT(*) FROM auth").Scan(&count)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if count == 0 {
|
||||||
|
fmt.Println("[service] no user found, creating root user (username: root, password: 123456, email: root@null.com)")
|
||||||
|
_, err := db.Exec(`
|
||||||
|
INSERT INTO auth (username, password, email, is_admin, bind_id, token)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?)
|
||||||
|
`, "root", "123456", "root@null.com", true, 0, "root")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Println(fmt.Sprintf("[service] %d user(s) found, skip creating root user", count))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func CreateUserTable(db *sql.DB) {
|
func CreateUserTable(db *sql.DB) {
|
||||||
_, err := db.Exec(`
|
_, err := db.Exec(`
|
||||||
CREATE TABLE IF NOT EXISTS auth (
|
CREATE TABLE IF NOT EXISTS auth (
|
||||||
|
10
main.go
10
main.go
@ -10,23 +10,21 @@ import (
|
|||||||
"chat/manager"
|
"chat/manager"
|
||||||
"chat/manager/conversation"
|
"chat/manager/conversation"
|
||||||
"chat/middleware"
|
"chat/middleware"
|
||||||
|
"chat/utils"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
viper.SetConfigFile("config.yaml")
|
utils.ReadConf()
|
||||||
if err := viper.ReadInConfig(); err != nil {
|
channel.InitManager()
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
if cli.Run() {
|
if cli.Run() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
channel.InitManager()
|
|
||||||
|
|
||||||
app := gin.New()
|
app := gin.New()
|
||||||
|
|
||||||
worker := middleware.RegisterMiddleware(app)
|
worker := middleware.RegisterMiddleware(app)
|
||||||
defer worker()
|
defer worker()
|
||||||
|
|
||||||
|
@ -1 +1,26 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
)
|
||||||
|
|
||||||
|
var configFile = "config.yaml"
|
||||||
|
var configExampleFile = "config.example.yaml"
|
||||||
|
|
||||||
|
func ReadConf() {
|
||||||
|
viper.SetConfigFile(configFile)
|
||||||
|
|
||||||
|
if !IsFileExist(configFile) {
|
||||||
|
fmt.Println(fmt.Sprintf("[service] config.yaml not found, creating one from template: %s", configExampleFile))
|
||||||
|
if err := CopyFile(configExampleFile, configFile); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := viper.ReadInConfig(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
viper.AutomaticEnv()
|
||||||
|
}
|
||||||
|
38
utils/fs.go
38
utils/fs.go
@ -2,6 +2,7 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@ -25,6 +26,10 @@ func CreateFolderNotExists(path string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func CreateFolderOnFile(file string) string {
|
func CreateFolderOnFile(file string) string {
|
||||||
|
if strings.LastIndex(file, "/") == -1 {
|
||||||
|
return file
|
||||||
|
}
|
||||||
|
|
||||||
return CreateFolderNotExists(file[:strings.LastIndex(file, "/")])
|
return CreateFolderNotExists(file[:strings.LastIndex(file, "/")])
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,3 +67,36 @@ func Walk(path string) []string {
|
|||||||
}
|
}
|
||||||
return files
|
return files
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IsFileExist(path string) bool {
|
||||||
|
_, err := os.Stat(path)
|
||||||
|
return err == nil || os.IsExist(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func CopyFile(src string, dst string) error {
|
||||||
|
in, err := os.Open(src)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer func(in *os.File) {
|
||||||
|
err := in.Close()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
}(in)
|
||||||
|
|
||||||
|
CreateFolderOnFile(dst)
|
||||||
|
out, err := os.Create(dst)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer func(out *os.File) {
|
||||||
|
err := out.Close()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
}(out)
|
||||||
|
|
||||||
|
_, err = io.Copy(out, in)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user