-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
182 additions
and
163 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
root = "." | ||
testdata_dir = "testdata" | ||
tmp_dir = "tmp" | ||
|
||
[build] | ||
args_bin = [] | ||
bin = "./tmp/tiktok" | ||
cmd = "go build -o ./tmp/tiktok ." | ||
delay = 0 | ||
exclude_dir = ["assets", "tmp", "vendor", "testdata", "apk", "docs"] | ||
exclude_file = [] | ||
exclude_regex = ["_test.go"] | ||
exclude_unchanged = false | ||
follow_symlink = false | ||
full_bin = "" | ||
include_dir = [] | ||
include_ext = ["go", "tpl", "tmpl", "html"] | ||
include_file = [] | ||
kill_delay = "0s" | ||
log = "build-errors.log" | ||
poll = false | ||
poll_interval = 0 | ||
rerun = false | ||
rerun_delay = 500 | ||
send_interrupt = false | ||
stop_on_error = false | ||
|
||
[color] | ||
app = "" | ||
build = "yellow" | ||
main = "magenta" | ||
runner = "green" | ||
watcher = "cyan" | ||
|
||
[log] | ||
main_only = false | ||
time = false | ||
|
||
[misc] | ||
clean_on_exit = false | ||
|
||
[screen] | ||
clear_on_rebuild = false | ||
keep_scroll = true |
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 |
---|---|---|
|
@@ -181,3 +181,6 @@ $RECYCLE.BIN/ | |
|
||
# Windows shortcuts | ||
*.lnk | ||
|
||
# Air | ||
tmp |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
GO ?= go | ||
AIR ?= air | ||
|
||
.DEFAULT_GOAL := default | ||
|
||
TAGS ?= | ||
|
||
default: help | ||
|
||
.PHONY: build | ||
build: ## build tiktok binary file | ||
${GO} build -o tiktok . | ||
|
||
.PHONY: watch | ||
watch: ## live reload | ||
${AIR} server | ||
|
||
.PHONY: test | ||
test: ## go test | ||
${GO} test -v $$(${GO} list ./... | grep -v /models/gen) | ||
|
||
.PHONY: clear | ||
clear: ## clear project | ||
-rm -rf ./tmp tiktok | ||
|
||
|
||
.PHONY: help | ||
help: Makefile ## print Makefile help information. | ||
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<TARGETS>\033[0m\n\n\033[35mTargets:\033[0m\n"} /^[0-9A-Za-z._-]+:.*?##/ { printf " \033[36m%-45s\033[0m %s\n", $$1, $$2 } /^\$$\([0-9A-Za-z_-]+\):.*?##/ { gsub("_","-", $$1); printf " \033[36m%-45s\033[0m %s\n", tolower(substr($$1, 3, length($$1)-7)), $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' Makefile #$(MAKEFILE_LIST) |
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
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,24 +1,23 @@ | ||
server: | ||
port: 8080 | ||
mode: prod | ||
|
||
log: | ||
level: debug | ||
mode: | ||
- console | ||
- file | ||
path: ./log | ||
|
||
database: | ||
mysql: | ||
host: 127.0.0.1 | ||
port: 3306 | ||
username: root | ||
password: 123456 | ||
database: tiktok | ||
mysql: | ||
host: 127.0.0.1 | ||
port: 3306 | ||
username: root | ||
password: 123456 | ||
database: tiktok | ||
|
||
cache: | ||
redis: | ||
host: 127.0.0.1 | ||
port: 6379 | ||
password: 123456 | ||
db: 0 | ||
redis: | ||
host: 127.0.0.1 | ||
port: 6379 | ||
password: 123456 | ||
db: 0 |
This file was deleted.
Oops, something went wrong.
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,96 +1,67 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/fsnotify/fsnotify" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
type Server struct { | ||
Port int | ||
Mode string | ||
} | ||
|
||
type Log struct { | ||
Level string | ||
Mode []string | ||
Path string | ||
} | ||
|
||
type MySQL struct { | ||
Host string | ||
Port int | ||
Username string | ||
Password string | ||
Database string | ||
} | ||
|
||
type DataBase struct { | ||
*MySQL | ||
} | ||
|
||
type Redis struct { | ||
Host string | ||
Port int | ||
Password string | ||
DB int | ||
} | ||
|
||
type Cache struct { | ||
*Redis | ||
} | ||
var config *viper.Viper | ||
|
||
func InitConfig() { | ||
viper.SetDefault("server", map[string]interface{}{ | ||
config = viper.New() | ||
config.SetDefault("server", map[string]interface{}{ | ||
"port": 8080, | ||
"mode": "prod", | ||
}) | ||
|
||
viper.SetDefault("log", map[string]interface{}{ | ||
config.SetDefault("log", map[string]interface{}{ | ||
"level": "debug", | ||
"mode": []string{"console", "file"}, | ||
"path": "./log", | ||
}) | ||
|
||
viper.SetDefault("mysql", map[string]interface{}{ | ||
config.SetDefault("mysql", map[string]interface{}{ | ||
"host": "127.0.0.1", | ||
"port": 3306, | ||
"username": "root", | ||
"password": "123456", | ||
"database": "demo", | ||
}) | ||
|
||
viper.SetDefault("redis", map[string]interface{}{ | ||
config.SetDefault("redis", map[string]interface{}{ | ||
"host": "127.0.0.1", | ||
"port": 6379, | ||
"password": "123456", | ||
"database": 0, | ||
}) | ||
|
||
//viper.SetConfigFile("./conf/tiktok.yml") | ||
viper.SetConfigName("tiktok") | ||
viper.AddConfigPath("./conf/") | ||
viper.AddConfigPath("/etc/tiktok/") | ||
viper.AddConfigPath("$HOME/.tiktok/") | ||
viper.AddConfigPath("./") | ||
viper.SetConfigType("yml") | ||
config.SetConfigName("tiktok") | ||
config.AddConfigPath("./conf/") | ||
config.AddConfigPath("./") | ||
config.AddConfigPath("$HOME/.tiktok/") | ||
config.AddConfigPath("/etc/tiktok/") | ||
config.SetConfigType("yml") | ||
|
||
if err := viper.ReadInConfig(); err != nil { | ||
if _, ok := err.(viper.ConfigFileNotFoundError); ok { | ||
if err := config.ReadInConfig(); err != nil { | ||
var configFileNotFoundError viper.ConfigFileNotFoundError | ||
if errors.As(err, &configFileNotFoundError) { | ||
// 配置文件未找到错误 | ||
fmt.Println("Config file not found!") | ||
} else { | ||
// 配置文件被找到,但产生了另外的错误 | ||
fmt.Printf("some error: %v\n", err) | ||
} | ||
} | ||
|
||
viper.WatchConfig() | ||
viper.WatchConfig() | ||
viper.OnConfigChange(func(e fsnotify.Event) { | ||
config.WatchConfig() | ||
config.WatchConfig() | ||
config.OnConfigChange(func(e fsnotify.Event) { | ||
// 配置文件发生变更之后会调用的回调函数 | ||
fmt.Println("Config file changed:", e.Name) | ||
}) | ||
|
||
fmt.Println(viper.Get("database.mysql.host")) | ||
fmt.Println(config.Get("mysql")) | ||
} | ||
|
||
func Get(key string) interface{} { | ||
return config.Get(key) | ||
} |
Oops, something went wrong.