辅助编译功能优化

This commit is contained in:
zhucheer 2019-12-27 16:50:37 +08:00
parent 684acd829f
commit 346879f774
4 changed files with 116 additions and 6 deletions

View File

@ -30,6 +30,12 @@ func CaptchaImgShow(c *app.Context, n int, size ...int) error {
return c.ResponseWrite(imgByte)
}
func CaptchaVerify(code string) {
func CaptchaVerify(c *app.Context, code string)bool {
codeMd5:=utils.Md5ToString(code)
sessionCode:= c.Session().Get(captchaSessionName)
if codeMd5 == sessionCode{
return true
}
return false
}

108
main.go
View File

@ -13,6 +13,9 @@ import (
"runtime"
"strings"
"time"
"errors"
"path/filepath"
"io"
)
func main() {
@ -48,6 +51,10 @@ func buildProject() {
buildProjectWithWindows()
}
//移动静态文件
CopyPath("config", "build/config")
CopyPath("storage", "build/storage")
}
// windows下编译打包
@ -61,9 +68,7 @@ func buildProjectWithWindows() {
}
projectName := pwdArr[len(pwdArr)-1]
ret, _ := execShell( fmt.Sprintf("go build -o build/%s.exe", projectName))
fmt.Println(projectName, ret)
execShell( fmt.Sprintf("go build -o build/%s.exe", projectName))
}
func createProject(projectName string) {
@ -188,6 +193,103 @@ func dirDot() string {
}
}
//拷贝文件
func CopyFile(src, dst string) bool {
if len(src) == 0 || len(dst) == 0 {
return false
}
srcFile, e := os.OpenFile(src, os.O_RDONLY, os.ModePerm)
if e != nil {
panic(e)
}
defer srcFile.Close()
dst = strings.Replace(dst, "\\", "/", -1)
dstPathArr := strings.Split(dst, "/")
dstPathArr = dstPathArr[0 : len(dstPathArr)-1]
dstPath := strings.Join(dstPathArr, "/")
dstFileInfo := GetFileInfo(dstPath)
if dstFileInfo == nil {
if e := os.MkdirAll(dstPath, os.ModePerm); e != nil {
panic(e)
}
}
//这里要把O_TRUNC 加上,否则会出现新旧文件内容出现重叠现象
dstFile, e := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_RDWR, os.ModePerm)
if e != nil {
panic(e)
}
defer dstFile.Close()
//fileInfo, e := srcFile.Stat()
//fileInfo.Size() > 1024
//byteBuffer := make([]byte, 10)
if _, e := io.Copy(dstFile, srcFile); e != nil {
panic(e)
} else {
return true
}
}
//拷贝目录
func CopyPath(src, dst string) bool {
srcFileInfo := GetFileInfo(src)
if srcFileInfo == nil || !srcFileInfo.IsDir() {
return false
}
err := filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil {
panic(err)
}
relationPath := strings.Replace(path, src, "/", -1)
dstPath := strings.TrimRight(strings.TrimRight(dst, "/"), "\\") + relationPath
if !info.IsDir() {
if CopyFile(path, dstPath) {
return nil
} else {
return errors.New(path + " copy fail")
}
} else {
if _, err := os.Stat(dstPath); err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(dstPath, os.ModePerm); err != nil {
panic(err)
return err
} else {
return nil
}
} else {
panic(err)
return err
}
} else {
return nil
}
}
})
if err != nil {
panic(err)
}
return true
}
//判断文件或目录是否存在
func GetFileInfo(src string) os.FileInfo {
if fileInfo, e := os.Stat(src); e != nil {
if os.IsNotExist(e) {
return nil
}
return nil
} else {
return fileInfo
}
}
func getProjectPath(subdirs ...string) string {
basepath := getGoPath()

View File

@ -8,7 +8,7 @@
[app.logger]
level = "INFO"
type = "text"
path = "./logs"
path = ""
syncInterval = 2
[app.session]
isOpen = true

View File

@ -55,8 +55,10 @@ func VerifyImg(c *app.Context) error {
fmt.Println(code)
ret:= captcha.CaptchaVerify(c, code)
return c.ToJson(map[string]interface{}{
"result": "upload success",
"result": ret,
})
}