Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update #6

Merged
merged 9 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions .air.toml
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,6 @@ $RECYCLE.BIN/

# Windows shortcuts
*.lnk

# Air
tmp
29 changes: 29 additions & 0 deletions Makefile
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)
6 changes: 3 additions & 3 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package cmd

import "github.com/urfave/cli/v2"

func NewApp() *cli.App {
app := cli.NewApp()
func NewApp() *cli.App { //nolint:typecheck
app := cli.NewApp() //nolint:typecheck
app.EnableBashCompletion = true

// 子命令集
subCmdWithConfig := []*cli.Command{
subCmdWithConfig := []*cli.Command{ //nolint:typecheck
CmdWeb,
CmdGen,
}
Expand Down
10 changes: 7 additions & 3 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import (
"biu-x.org/TikTok/models/user"
"biu-x.org/TikTok/modules/config"
"fmt"
"github.com/urfave/cli/v2"
"gorm.io/driver/mysql"
"gorm.io/gen"
Expand All @@ -15,20 +17,22 @@
}

// CmdGen 子命令
var CmdGen = &cli.Command{
var CmdGen = &cli.Command{ //nolint:typecheck
Name: "gen",
Usage: "gen gorm code",
Description: `GEN: Friendly & Safer GORM powered by Code Generation.`,
Action: runGen,
}

func runGen(ctx *cli.Context) error {
func runGen(ctx *cli.Context) error { //nolint:typecheck
g := gen.NewGenerator(gen.Config{
OutPath: "./models/gen",
Mode: gen.WithoutContext | gen.WithDefaultQuery | gen.WithQueryInterface, // generate mode
})

db, err := gorm.Open(mysql.Open("root:@(127.0.0.1:3306)/demo?charset=utf8mb4&parseTime=True&loc=Local"))
url := fmt.Sprintf("%v:%v@(%v:%v)/%v", config.Get("mysql.username"), config.Get("mysql.password"), config.Get("mysql.host"), config.Get("mysql.port"), config.Get("mysql.database"))

Check failure on line 33 in cmd/gen.go

View workflow job for this annotation

GitHub Actions / lint

line is 181 characters (lll)

db, err := gorm.Open(mysql.Open(url + "?charset=utf8mb4&parseTime=True&loc=Local"))
if err != nil {
return err
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ import (
"biu-x.org/TikTok/modules/config"
"biu-x.org/TikTok/routers"
"fmt"
"github.com/spf13/viper"
"github.com/urfave/cli/v2"
)

// CmdWeb api 子命令
var CmdWeb = &cli.Command{
var CmdWeb = &cli.Command{ //nolint:typecheck
Name: "server",
Usage: "Start TikTok api server",
Description: `Star TikTok api server`,
Action: runWeb,
Flags: []cli.Flag{
&cli.StringFlag{
&cli.StringFlag{ //nolint:typecheck
Name: "port",
Aliases: []string{"p"},
Value: "3000",
Expand All @@ -24,9 +23,9 @@ var CmdWeb = &cli.Command{
},
}

func runWeb(ctx *cli.Context) error {
func runWeb(ctx *cli.Context) error { //nolint:typecheck
config.InitConfig()
fmt.Println(viper.Get("server.port"))
fmt.Println(config.Get("redis"))
routers.Init()
return nil
}
25 changes: 12 additions & 13 deletions conf/tiktok.yml
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
54 changes: 0 additions & 54 deletions models/db/db.go

This file was deleted.

77 changes: 24 additions & 53 deletions modules/config/config.go
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)
}
Loading
Loading