diff --git a/assets/templates/gencode/gencode_init.html b/assets/templates/gencode/gencode_init.html index ae81223..a56f50f 100644 --- a/assets/templates/gencode/gencode_init.html +++ b/assets/templates/gencode/gencode_init.html @@ -3,6 +3,7 @@ + @@ -46,6 +47,7 @@ + diff --git a/cmd/init/db/main.go b/cmd/init/db/main.go index 61b6ea7..0765b06 100644 --- a/cmd/init/db/main.go +++ b/cmd/init/db/main.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "log" + "os" "strings" "github.com/xinliangnote/go-gin-api/cmd/init/db/mysql" @@ -99,6 +100,13 @@ func main() { } fmt.Println("create admin table data success") + // 生成已完成 init 的标识,生成 init_db.lock + f, err := os.Create("cmd/init/db/init_db.lock") + if err != nil { + log.Fatal("create init_db.lock err: ", err.Error()) + } + defer f.Close() + // 完成事务 tx.Commit() diff --git a/configs/configs.go b/configs/configs.go index 79db8c0..b9770d2 100644 --- a/configs/configs.go +++ b/configs/configs.go @@ -94,3 +94,7 @@ func ProjectPort() string { func ProjectLogFile() string { return fmt.Sprintf("./logs/%s-access.log", ProjectName()) } + +func InitDBLockFile() string { + return "cmd/init/db/init_db.lock" +} diff --git a/internal/router/router.go b/internal/router/router.go index d632fa1..525bbad 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -1,6 +1,7 @@ package router import ( + "github.com/xinliangnote/go-gin-api/configs" "github.com/xinliangnote/go-gin-api/internal/pkg/cache" "github.com/xinliangnote/go-gin-api/internal/pkg/core" "github.com/xinliangnote/go-gin-api/internal/pkg/db" @@ -8,6 +9,7 @@ import ( "github.com/xinliangnote/go-gin-api/internal/pkg/metrics" "github.com/xinliangnote/go-gin-api/internal/pkg/notify" "github.com/xinliangnote/go-gin-api/internal/router/middleware" + "github.com/xinliangnote/go-gin-api/pkg/file" "github.com/pkg/errors" "go.uber.org/zap" @@ -23,13 +25,19 @@ type resource struct { } func NewHTTPMux(logger *zap.Logger, db db.Repo, cache cache.Repo, grpConn grpc.ClientConn) (core.Mux, error) { + var openBrowserUri = "http://127.0.0.1:9999" + + _, ok := file.IsExists(configs.InitDBLockFile()) + if !ok { + openBrowserUri = "http://127.0.0.1:9999/init?init=db" + } if logger == nil { return nil, errors.New("logger required") } mux, err := core.New(logger, - core.WithEnableOpenBrowser("http://127.0.0.1:9999"), + core.WithEnableOpenBrowser(openBrowserUri), core.WithEnableCors(), core.WithEnableRate(), core.WithPanicNotify(notify.OnPanicNotify), diff --git a/pkg/file/file.go b/pkg/file/file.go index 150b3a4..9bee285 100644 --- a/pkg/file/file.go +++ b/pkg/file/file.go @@ -23,6 +23,12 @@ type ReadLineFromEnd struct { isFirst bool } +// Exists +func IsExists(path string) (os.FileInfo, bool) { + f, err := os.Stat(path) + return f, err == nil || os.IsExist(err) +} + // NewReadLineFromEnd func NewReadLineFromEnd(filename string) (rd *ReadLineFromEnd, err error) { f, err := os.Open(filename)