Skip to content

Commit

Permalink
upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
xinliangnote committed Apr 10, 2021
1 parent 8a6a343 commit 3e1ef9c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
29 changes: 28 additions & 1 deletion assets/templates/gencode/gencode_init.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<link href="bootstrap/js/jquery-confirm/jquery-confirm.min.css" rel="stylesheet">
<link href="bootstrap/css/materialdesignicons.min.css" rel="stylesheet">
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="bootstrap/css/style.min.css" rel="stylesheet">
Expand Down Expand Up @@ -46,6 +47,7 @@

<script type="text/javascript" src="bootstrap/js/jquery.min.js"></script>
<script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="bootstrap/js/jquery-confirm/jquery-confirm.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#btnOk").click(function(){
Expand All @@ -58,9 +60,34 @@
$("#resultDiv").text(data);
$("#btnLoading").hide();
$("#btnOk").show();

if (getQueryString("init")) {
$.alert({
title: '操作成功',
icon: 'mdi mdi-check-decagram',
type: 'green',
content: '初始化完成。',
buttons: {
okay: {
text: '系统首页',
action: function () {
location.href = "/";
}
}
}
});
}
})

})
});

function getQueryString(name) {
let reg = new RegExp('(?:(?:&|\\?)' + name + '=([^&]*))|(?:/' + name + '/([^/]*))', 'i');
let r = window.location.href.match(reg);
if (r != null)
return decodeURI(r[1] || r[2]);
return null;
}
})
</script>
</body>
Expand Down
8 changes: 8 additions & 0 deletions cmd/init/db/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"log"
"os"
"strings"

"github.com/xinliangnote/go-gin-api/cmd/init/db/mysql"
Expand Down Expand Up @@ -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()

Expand Down
4 changes: 4 additions & 0 deletions configs/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
10 changes: 9 additions & 1 deletion internal/router/router.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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"
"github.com/xinliangnote/go-gin-api/internal/pkg/grpc"
"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"
Expand All @@ -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),
Expand Down
6 changes: 6 additions & 0 deletions pkg/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3e1ef9c

Please sign in to comment.