Skip to content

Commit

Permalink
feat: auto config file creating and database root user
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Dec 24, 2023
1 parent 31eaba7 commit b963535
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ RUN ulimit -n 65535 && \
echo "ulimit -n 65535" >> /etc/rc.local

# 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

# Build backend
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@


## 📦 部署 | Deploy
*部署成功后,管理员账号为 `root`,密码默认为 `123456`*

1. 编译安装 (自定义性强)
```shell
Expand Down
3 changes: 3 additions & 0 deletions config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ mysql:
redis:
host: localhost
port: 6379
db: 0
password: ""


secret: SbitdyN5ZH39cNxSrG3kMNZ1GfiyyQ43

Expand Down
25 changes: 25 additions & 0 deletions connection/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func ConnectMySQL() *sql.DB {
log.Println(fmt.Sprintf("[connection] connected to mysql server (host: %s)", viper.GetString("mysql.host")))
}

InitRootUser(db)

CreateUserTable(db)
CreateConversationTable(db)
CreateSharingTable(db)
Expand All @@ -50,6 +52,29 @@ func ConnectMySQL() *sql.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: [email protected])")
_, err := db.Exec(`
INSERT INTO auth (username, password, email, is_admin, bind_id, token)
VALUES (?, ?, ?, ?, ?, ?)
`, "root", "123456", "[email protected]", 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) {
_, err := db.Exec(`
CREATE TABLE IF NOT EXISTS auth (
Expand Down
10 changes: 4 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,21 @@ import (
"chat/manager"
"chat/manager/conversation"
"chat/middleware"
"chat/utils"
"fmt"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
)

func main() {
viper.SetConfigFile("config.yaml")
if err := viper.ReadInConfig(); err != nil {
panic(err)
}
utils.ReadConf()
channel.InitManager()

if cli.Run() {
return
}
channel.InitManager()

app := gin.New()

worker := middleware.RegisterMiddleware(app)
defer worker()

Expand Down
25 changes: 25 additions & 0 deletions utils/config.go
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
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 changes: 38 additions & 0 deletions utils/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
Expand All @@ -25,6 +26,10 @@ func CreateFolderNotExists(path string) string {
}

func CreateFolderOnFile(file string) string {
if strings.LastIndex(file, "/") == -1 {
return file
}

return CreateFolderNotExists(file[:strings.LastIndex(file, "/")])
}

Expand Down Expand Up @@ -62,3 +67,36 @@ func Walk(path string) []string {
}
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
}

0 comments on commit b963535

Please sign in to comment.