-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: auto config file creating and database root user
- Loading branch information
1 parent
31eaba7
commit b963535
Showing
7 changed files
with
97 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,6 +93,7 @@ | |
|
||
|
||
## 📦 部署 | Deploy | ||
*部署成功后,管理员账号为 `root`,密码默认为 `123456`* | ||
|
||
1. 编译安装 (自定义性强) | ||
```shell | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,9 @@ mysql: | |
redis: | ||
host: localhost | ||
port: 6379 | ||
db: 0 | ||
password: "" | ||
|
||
|
||
secret: SbitdyN5ZH39cNxSrG3kMNZ1GfiyyQ43 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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 ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters