From 45aa7e0b468354988297c19cb9ce3df48e3c0315 Mon Sep 17 00:00:00 2001 From: shaipeng Date: Mon, 13 Feb 2023 10:14:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E8=AF=BB=E5=8F=96=E6=96=B9=E5=BC=8F=EF=BC=8C?= =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=90=AF=E5=8A=A8=E6=9C=8D=E5=8A=A1=E6=97=B6?= =?UTF-8?q?=E6=8F=90=E7=A4=BA=E6=97=A0=E6=B3=95=E8=AF=BB=E5=8F=96=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=9B?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=97=B6=E9=97=B4=E9=97=AE=E9=A2=98=EF=BC=9B?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=88=9D=E5=A7=8B=E5=8C=96=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=8D=AE=E6=8F=92=E5=85=A5=E5=88=86=E7=B1=BB=E5=80=BC=E6=8A=A5?= =?UTF-8?q?=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/admin/article.go | 2 +- app/controller/admin/comment.go | 2 +- app/controller/admin/controller.go | 2 +- app/controller/admin/link.go | 2 +- app/controller/admin/message.go | 2 +- app/controller/admin/system.go | 2 +- app/controller/admin/upload.go | 2 +- app/controller/admin/user.go | 2 +- app/controller/home/article.go | 2 +- app/controller/home/comment.go | 2 +- app/controller/home/controller.go | 2 +- app/controller/home/message.go | 2 +- app/model/model.go | 2 +- deploy/mysql/init.sql | 28 +-- main.go | 2 +- middleware/jwt.go | 2 +- middleware/log.go | 2 +- pkg/file/file.go | 189 ++++++++++++++++++++ pkg/utils/upload.go | 2 +- pkg/utils/util.go | 156 ++++++++-------- pkg/setting/setting.go => setting/config.go | 57 +++++- setting/config.toml | 2 +- views/defaulted/article/detail.html | 6 +- views/defaulted/common/_blog_sider.html | 2 +- views/green/article/detail.html | 6 +- views/home/article/detail.html | 2 +- 26 files changed, 363 insertions(+), 119 deletions(-) create mode 100644 pkg/file/file.go rename pkg/setting/setting.go => setting/config.go (80%) diff --git a/app/controller/admin/article.go b/app/controller/admin/article.go index 7cb356d..82aef93 100644 --- a/app/controller/admin/article.go +++ b/app/controller/admin/article.go @@ -5,8 +5,8 @@ import ( "gitee.com/jikey/elk-blog/app/service" "gitee.com/jikey/elk-blog/pkg/e" "gitee.com/jikey/elk-blog/pkg/response" - "gitee.com/jikey/elk-blog/pkg/setting" "gitee.com/jikey/elk-blog/pkg/utils" + "gitee.com/jikey/elk-blog/setting" "github.com/flosch/pongo2" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" diff --git a/app/controller/admin/comment.go b/app/controller/admin/comment.go index c75b87d..a37fd93 100644 --- a/app/controller/admin/comment.go +++ b/app/controller/admin/comment.go @@ -5,7 +5,7 @@ import ( "gitee.com/jikey/elk-blog/app/service" "gitee.com/jikey/elk-blog/pkg/e" "gitee.com/jikey/elk-blog/pkg/response" - "gitee.com/jikey/elk-blog/pkg/setting" + "gitee.com/jikey/elk-blog/setting" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" "strconv" diff --git a/app/controller/admin/controller.go b/app/controller/admin/controller.go index 5b1f2a2..f94d32b 100644 --- a/app/controller/admin/controller.go +++ b/app/controller/admin/controller.go @@ -1,7 +1,7 @@ package admin import ( - "gitee.com/jikey/elk-blog/pkg/setting" + "gitee.com/jikey/elk-blog/setting" "github.com/gin-gonic/gin" "reflect" "strings" diff --git a/app/controller/admin/link.go b/app/controller/admin/link.go index 6aca49e..9ae9a66 100644 --- a/app/controller/admin/link.go +++ b/app/controller/admin/link.go @@ -5,7 +5,7 @@ import ( "gitee.com/jikey/elk-blog/app/service" "gitee.com/jikey/elk-blog/pkg/e" "gitee.com/jikey/elk-blog/pkg/response" - "gitee.com/jikey/elk-blog/pkg/setting" + "gitee.com/jikey/elk-blog/setting" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" "strconv" diff --git a/app/controller/admin/message.go b/app/controller/admin/message.go index d3bb25d..ce942d0 100644 --- a/app/controller/admin/message.go +++ b/app/controller/admin/message.go @@ -5,7 +5,7 @@ import ( "gitee.com/jikey/elk-blog/app/service" "gitee.com/jikey/elk-blog/pkg/e" "gitee.com/jikey/elk-blog/pkg/response" - "gitee.com/jikey/elk-blog/pkg/setting" + "gitee.com/jikey/elk-blog/setting" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" "strconv" diff --git a/app/controller/admin/system.go b/app/controller/admin/system.go index df407f9..444032c 100644 --- a/app/controller/admin/system.go +++ b/app/controller/admin/system.go @@ -5,7 +5,7 @@ import ( "gitee.com/jikey/elk-blog/app/service" "gitee.com/jikey/elk-blog/pkg/e" "gitee.com/jikey/elk-blog/pkg/response" - "gitee.com/jikey/elk-blog/pkg/setting" + "gitee.com/jikey/elk-blog/setting" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" "github.com/spf13/viper" diff --git a/app/controller/admin/upload.go b/app/controller/admin/upload.go index 18a124a..dbd3b5b 100644 --- a/app/controller/admin/upload.go +++ b/app/controller/admin/upload.go @@ -4,8 +4,8 @@ import ( "fmt" "gitee.com/jikey/elk-blog/app/model" "gitee.com/jikey/elk-blog/pkg/response" - "gitee.com/jikey/elk-blog/pkg/setting" "gitee.com/jikey/elk-blog/pkg/utils" + "gitee.com/jikey/elk-blog/setting" "github.com/gin-gonic/gin" "github.com/nleeper/goment" "github.com/sirupsen/logrus" diff --git a/app/controller/admin/user.go b/app/controller/admin/user.go index 3c3a040..1379214 100644 --- a/app/controller/admin/user.go +++ b/app/controller/admin/user.go @@ -6,7 +6,7 @@ import ( "gitee.com/jikey/elk-blog/app/service" "gitee.com/jikey/elk-blog/pkg/e" "gitee.com/jikey/elk-blog/pkg/response" - "gitee.com/jikey/elk-blog/pkg/setting" + "gitee.com/jikey/elk-blog/setting" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" "strconv" diff --git a/app/controller/home/article.go b/app/controller/home/article.go index 964fd09..1828091 100644 --- a/app/controller/home/article.go +++ b/app/controller/home/article.go @@ -5,8 +5,8 @@ import ( "gitee.com/jikey/elk-blog/app/model" "gitee.com/jikey/elk-blog/app/service" "gitee.com/jikey/elk-blog/pkg/response" - "gitee.com/jikey/elk-blog/pkg/setting" "gitee.com/jikey/elk-blog/pkg/utils" + "gitee.com/jikey/elk-blog/setting" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" "html/template" diff --git a/app/controller/home/comment.go b/app/controller/home/comment.go index f8f4996..cab0b0d 100644 --- a/app/controller/home/comment.go +++ b/app/controller/home/comment.go @@ -5,7 +5,7 @@ import ( "gitee.com/jikey/elk-blog/app/service" "gitee.com/jikey/elk-blog/pkg/e" "gitee.com/jikey/elk-blog/pkg/response" - "gitee.com/jikey/elk-blog/pkg/setting" + "gitee.com/jikey/elk-blog/setting" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" "strconv" diff --git a/app/controller/home/controller.go b/app/controller/home/controller.go index 9dc4bc8..9f8b3ae 100644 --- a/app/controller/home/controller.go +++ b/app/controller/home/controller.go @@ -2,7 +2,7 @@ package home import ( "gitee.com/jikey/elk-blog/app/service" - "gitee.com/jikey/elk-blog/pkg/setting" + "gitee.com/jikey/elk-blog/setting" "github.com/gin-gonic/gin" "reflect" "strings" diff --git a/app/controller/home/message.go b/app/controller/home/message.go index fb7b601..2dfd28f 100644 --- a/app/controller/home/message.go +++ b/app/controller/home/message.go @@ -5,8 +5,8 @@ import ( "gitee.com/jikey/elk-blog/app/service" "gitee.com/jikey/elk-blog/pkg/e" "gitee.com/jikey/elk-blog/pkg/response" - "gitee.com/jikey/elk-blog/pkg/setting" "gitee.com/jikey/elk-blog/pkg/utils" + "gitee.com/jikey/elk-blog/setting" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" "html/template" diff --git a/app/model/model.go b/app/model/model.go index 7adc88a..332f0ab 100644 --- a/app/model/model.go +++ b/app/model/model.go @@ -2,13 +2,13 @@ package model import ( "fmt" + "gitee.com/jikey/elk-blog/setting" "github.com/spf13/cast" "log" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/mysql" - "gitee.com/jikey/elk-blog/pkg/setting" "time" ) diff --git a/deploy/mysql/init.sql b/deploy/mysql/init.sql index 333cd96..b45992e 100644 --- a/deploy/mysql/init.sql +++ b/deploy/mysql/init.sql @@ -91,20 +91,20 @@ CREATE TABLE `tk_category` ( -- ---------------------------- -- Records of tk_category -- ---------------------------- -INSERT INTO `tk_category` VALUES (1, 0, 0, 1, 'Design', 0, '0', 'Design', '', 1, '设计1', 1, 0); -INSERT INTO `tk_category` VALUES (2, 0, 0, 2, 'About', 0, '0', 'About', '', 1, '关于我们', 1, 0); -INSERT INTO `tk_category` VALUES (3, 0, 0, 3, 'Contact', 0, '0', 'Contact', '', 1, '联系我', 1, 0); -INSERT INTO `tk_category` VALUES (4, 0, 0, 4, 'Blog', 0, '0', 'Blog', '', 1, '博客', 1, 0); -INSERT INTO `tk_category` VALUES (5, 1, 0, 1, 'Design', 0, '0-1', 'GUI444', '', 0, '

ios android

', 1, 1); -INSERT INTO `tk_category` VALUES (6, 1, 0, 1, 'Design', 0, '0-1', 'WEB', '', 0, 'WEB', 1, 4); -INSERT INTO `tk_category` VALUES (7, 1, 0, 1, 'Design', 0, '0-1', 'PAINT', '', 0, 'PAINT', 1, 3); -INSERT INTO `tk_category` VALUES (17, 4, 0, 4, 'Blog', 0, '0-4', '轻慢摄影', '', 0, '轻慢摄影', 0, 0); -INSERT INTO `tk_category` VALUES (14, 4, 0, 4, 'Blog', 0, '0-4', '快乐绘画', '', 0, '快乐绘画', 0, 0); -INSERT INTO `tk_category` VALUES (15, 4, 0, 4, 'Blog', 0, '0-4', '收藏点滴', '', 0, '收藏点滴', 0, 0); -INSERT INTO `tk_category` VALUES (18, 1, 0, 1, 'Design', 0, '0-1', 'App', '', 0, '', 0, 2); -INSERT INTO `tk_category` VALUES (184, 1, 0, 1, 'Design', 0, '0-1', 'logo', '', 0, '', 0, 5); -INSERT INTO `tk_category` VALUES (185, 4, 0, 4, 'Blog', 0, '0-4', '设计理论', '', 0, '设计理论', 0, 0); -INSERT INTO `tk_category` VALUES (188, 4, 0, 4, 'Blog', NULL, '0-4', '好看风云', NULL, NULL, '好看风云', NULL, 6); +INSERT INTO `tk_category` VALUES (1, 0, 0, 1, 'Design', 0, '0', 'Design', '', 1, '设计1', 1, 0, NULL, '2023-01-15 14:18:37'); +INSERT INTO `tk_category` VALUES (2, 0, 0, 2, 'About', 0, '0', 'About', '', 1, '关于我们', 1, 0, NULL, '2023-01-15 14:18:37'); +INSERT INTO `tk_category` VALUES (3, 0, 0, 3, 'Contact', 0, '0', 'Contact', '', 1, '联系我', 1, 0, NULL, '2023-01-15 14:18:37'); +INSERT INTO `tk_category` VALUES (4, 0, 0, 4, 'Blog', 0, '0', 'Blog', '', 1, '博客', 1, 0, NULL, '2023-01-15 14:18:37'); +INSERT INTO `tk_category` VALUES (5, 1, 0, 1, 'Design', 0, '0-1', 'GUI444', '', 0, '

ios android

', 1, 1, NULL, '2023-01-15 14:18:37'); +INSERT INTO `tk_category` VALUES (6, 1, 0, 1, 'Design', 0, '0-1', 'WEB', '', 0, 'WEB', 1, 4, NULL, '2023-01-15 14:18:37'); +INSERT INTO `tk_category` VALUES (7, 1, 0, 1, 'Design', 0, '0-1', 'PAINT', '', 0, 'PAINT', 1, 3, NULL, '2023-01-15 14:18:37'); +INSERT INTO `tk_category` VALUES (17, 4, 0, 4, 'Blog', 0, '0-4', '轻慢摄影', '', 0, '轻慢摄影', 0, 0, NULL, '2023-01-15 14:18:37'); +INSERT INTO `tk_category` VALUES (14, 4, 0, 4, 'Blog', 0, '0-4', '快乐绘画', '', 0, '快乐绘画', 0, 0, NULL, '2023-01-15 14:18:37'); +INSERT INTO `tk_category` VALUES (15, 4, 0, 4, 'Blog', 0, '0-4', '收藏点滴', '', 0, '收藏点滴', 0, 0, NULL, '2023-01-15 14:18:37'); +INSERT INTO `tk_category` VALUES (18, 1, 0, 1, 'Design', 0, '0-1', 'App', '', 0, '', 0, 2, NULL, '2023-01-15 14:18:37'); +INSERT INTO `tk_category` VALUES (184, 1, 0, 1, 'Design', 0, '0-1', 'logo', '', 0, '', 0, 5, NULL, '2023-01-15 14:18:37'); +INSERT INTO `tk_category` VALUES (185, 4, 0, 4, 'Blog', 0, '0-4', '设计理论', '', 0, '设计理论', 0, 0, NULL, '2023-01-15 14:18:37'); +INSERT INTO `tk_category` VALUES (188, 4, 0, 4, 'Blog', NULL, '0-4', '好看风云', NULL, NULL, '好看风云', NULL, 6, NULL, '2023-01-15 14:18:37'); -- ---------------------------- -- Table structure for tk_comment diff --git a/main.go b/main.go index afeae19..9e3d71b 100644 --- a/main.go +++ b/main.go @@ -3,8 +3,8 @@ package main import ( // "gitee.com/jikey/elk-blog/app/service" "gitee.com/jikey/elk-blog/app/model" - "gitee.com/jikey/elk-blog/pkg/setting" "gitee.com/jikey/elk-blog/routers" + "gitee.com/jikey/elk-blog/setting" "github.com/sirupsen/logrus" "time" ) diff --git a/middleware/jwt.go b/middleware/jwt.go index cc1c4bc..64471b5 100644 --- a/middleware/jwt.go +++ b/middleware/jwt.go @@ -4,8 +4,8 @@ import ( "errors" "gitee.com/jikey/elk-blog/pkg/e" "gitee.com/jikey/elk-blog/pkg/response" - "gitee.com/jikey/elk-blog/pkg/setting" "gitee.com/jikey/elk-blog/pkg/utils" + "gitee.com/jikey/elk-blog/setting" "github.com/gin-gonic/gin" jwtgo "github.com/golang-jwt/jwt/v4" "github.com/sirupsen/logrus" diff --git a/middleware/log.go b/middleware/log.go index bc5c694..e412b1e 100644 --- a/middleware/log.go +++ b/middleware/log.go @@ -2,7 +2,7 @@ package middleware import ( "fmt" - "gitee.com/jikey/elk-blog/pkg/setting" + "gitee.com/jikey/elk-blog/setting" "github.com/gin-gonic/gin" rotatelogs "github.com/lestrrat-go/file-rotatelogs" "github.com/rifflock/lfshook" diff --git a/pkg/file/file.go b/pkg/file/file.go new file mode 100644 index 0000000..b91664e --- /dev/null +++ b/pkg/file/file.go @@ -0,0 +1,189 @@ +package file + +import ( + "bytes" + "fmt" + "io" + "os" +) + +var ( + buffSize = 1 << 20 +) + +// ReadLineFromEnd -- +type ReadLineFromEnd struct { + f *os.File + + fileSize int + bwr *bytes.Buffer + lineBuff []byte + swapBuff []byte + + isFirst bool +} + +// IsExists 文件是否存在 +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) + if err != nil { + return nil, err + } + + info, err := f.Stat() + if err != nil { + return nil, err + } + + if info.IsDir() { + return nil, fmt.Errorf("not file") + } + + fileSize := int(info.Size()) + + rd = &ReadLineFromEnd{ + f: f, + fileSize: fileSize, + bwr: bytes.NewBuffer([]byte{}), + lineBuff: make([]byte, 0), + swapBuff: make([]byte, buffSize), + isFirst: true, + } + return rd, nil +} + +// ReadLine 结尾包含'\n' +func (c *ReadLineFromEnd) ReadLine() (line []byte, err error) { + var ok bool + for { + ok, err = c.buff() + if err != nil { + return nil, err + } + if ok { + break + } + } + line, err = c.bwr.ReadBytes('\n') + if err == io.EOF && c.fileSize > 0 { + err = nil + } + return line, err +} + +// Close -- +func (c *ReadLineFromEnd) Close() (err error) { + return c.f.Close() +} + +func (c *ReadLineFromEnd) buff() (ok bool, err error) { + if c.fileSize == 0 { + return true, nil + } + + if c.bwr.Len() >= buffSize { + return true, nil + } + + offset := 0 + if c.fileSize > buffSize { + offset = c.fileSize - buffSize + } + _, err = c.f.Seek(int64(offset), 0) + if err != nil { + return false, err + } + + n, err := c.f.Read(c.swapBuff) + if err != nil && err != io.EOF { + return false, err + } + if c.fileSize < n { + n = c.fileSize + } + if n == 0 { + return true, nil + } + + for { + m := bytes.LastIndex(c.swapBuff[:n], []byte{'\n'}) + if m == -1 { + break + } + if m < n-1 { + err = c.writeLine(c.swapBuff[m+1 : n]) + if err != nil { + return false, err + } + ok = true + } else if m == n-1 && !c.isFirst { + err = c.writeLine(nil) + if err != nil { + return false, err + } + ok = true + } + n = m + if n == 0 { + break + } + } + if n > 0 { + reverseBytes(c.swapBuff[:n]) + c.lineBuff = append(c.lineBuff, c.swapBuff[:n]...) + } + if offset == 0 { + err = c.writeLine(nil) + if err != nil { + return false, err + } + ok = true + } + c.fileSize = offset + if c.isFirst { + c.isFirst = false + } + return ok, nil +} + +func (c *ReadLineFromEnd) writeLine(b []byte) (err error) { + if len(b) > 0 { + _, err = c.bwr.Write(b) + if err != nil { + return err + } + } + if len(c.lineBuff) > 0 { + reverseBytes(c.lineBuff) + _, err = c.bwr.Write(c.lineBuff) + if err != nil { + return err + } + c.lineBuff = c.lineBuff[:0] + } + _, err = c.bwr.Write([]byte{'\n'}) + if err != nil { + return err + } + return nil +} + +func reverseBytes(b []byte) { + n := len(b) + if n <= 1 { + return + } + for i := 0; i < n; i++ { + k := n - 1 + if k != i { + b[i], b[k] = b[k], b[i] + } + n-- + } +} diff --git a/pkg/utils/upload.go b/pkg/utils/upload.go index f8756d1..c8ea749 100644 --- a/pkg/utils/upload.go +++ b/pkg/utils/upload.go @@ -2,7 +2,7 @@ package utils import ( "fmt" - "gitee.com/jikey/elk-blog/pkg/setting" + "gitee.com/jikey/elk-blog/setting" "github.com/aliyun/aliyun-oss-go-sdk/oss" "github.com/gin-gonic/gin" "io" diff --git a/pkg/utils/util.go b/pkg/utils/util.go index 01405bf..ed704ad 100644 --- a/pkg/utils/util.go +++ b/pkg/utils/util.go @@ -1,125 +1,125 @@ package utils import ( - "bytes" - "crypto/md5" - "encoding/hex" - "gitee.com/jikey/elk-blog/pkg/setting" - "github.com/gin-gonic/gin" - mathrand "math/rand" - "net/http" - "reflect" - "strconv" - "time" + "bytes" + "crypto/md5" + "encoding/hex" + "gitee.com/jikey/elk-blog/setting" + "github.com/gin-gonic/gin" + mathrand "math/rand" + "net/http" + "reflect" + "strconv" + "time" ) // IsEnv 检测环境 func IsEnv(eType string) bool { - // return Config.Section("app").Key("env").MustString("") == eType - return setting.Config.App.Env == eType + // return Config.Section("app").Key("env").MustString("") == eType + return setting.Config.App.Env == eType } // IsProd 是否是生产环境 func IsProd() bool { - return IsEnv("production") + return IsEnv("production") } // GetTime 当前时间 func GetTime() (retTime string) { - currentTime := time.Now().Local() - year := strconv.Itoa(currentTime.Year()) - month := strconv.Itoa(int(currentTime.Month())) - day := strconv.Itoa(currentTime.Day()) - hour := strconv.Itoa(currentTime.Hour()) - minute := strconv.Itoa(currentTime.Minute()) - second := strconv.Itoa(currentTime.Second()) + currentTime := time.Now().Local() + year := strconv.Itoa(currentTime.Year()) + month := strconv.Itoa(int(currentTime.Month())) + day := strconv.Itoa(currentTime.Day()) + hour := strconv.Itoa(currentTime.Hour()) + minute := strconv.Itoa(currentTime.Minute()) + second := strconv.Itoa(currentTime.Second()) - retTime = year + "年" + month + "月" + day + "日 " + hour + ":" + minute + ":" + second + retTime = year + "年" + month + "月" + day + "日 " + hour + ":" + minute + ":" + second - return + return } // Empty 类似于 PHP 的 empty() 函数 func Empty(val interface{}) bool { - if val == nil { - return true - } - v := reflect.ValueOf(val) - - switch v.Kind() { - case reflect.String, reflect.Array: - return v.Len() == 0 - case reflect.Map, reflect.Slice: - return v.Len() == 0 || v.IsNil() - case reflect.Bool: - return !v.Bool() - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return v.Int() == 0 - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: - return v.Uint() == 0 - case reflect.Float32, reflect.Float64: - return v.Float() == 0 - case reflect.Interface, reflect.Ptr: - return v.IsNil() - } - - return reflect.DeepEqual(val, reflect.Zero(v.Type()).Interface()) + if val == nil { + return true + } + v := reflect.ValueOf(val) + + switch v.Kind() { + case reflect.String, reflect.Array: + return v.Len() == 0 + case reflect.Map, reflect.Slice: + return v.Len() == 0 || v.IsNil() + case reflect.Bool: + return !v.Bool() + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return v.Int() == 0 + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + return v.Uint() == 0 + case reflect.Float32, reflect.Float64: + return v.Float() == 0 + case reflect.Interface, reflect.Ptr: + return v.IsNil() + } + + return reflect.DeepEqual(val, reflect.Zero(v.Type()).Interface()) } // NowTime 获取当前时间 func NowTime() interface{} { - return time.Now().Format("2006-01-02 15:04:05") + return time.Now().Format("2006-01-02 15:04:05") } type Merge struct { - Sum int - Strings string - SumFloat float64 + Sum int + Strings string + SumFloat float64 } // Interface2Type 实现传任意多个任意类型的参数 // https://github.com/JLynnLee/learnGrammar/blob/1e0f16e5af8ea2ac44e7b5ca93b6af3f0d41ceb1/main.go func (m *Merge) Interface2Type(inter interface{}) { - switch inter.(type) { - case string: - bt := bytes.Buffer{} - bt.WriteString(m.Strings) - bt.WriteString(inter.(string)) - m.Strings = bt.String() - break - case int: - m.Sum += inter.(int) - break - case float64: - m.SumFloat += inter.(float64) - break - } + switch inter.(type) { + case string: + bt := bytes.Buffer{} + bt.WriteString(m.Strings) + bt.WriteString(inter.(string)) + m.Strings = bt.String() + break + case int: + m.Sum += inter.(int) + break + case float64: + m.SumFloat += inter.(float64) + break + } } // IsGet 是否是Gost func IsGet(c *gin.Context) bool { - if c.Request.Method == http.MethodGet { - return true - } - return false + if c.Request.Method == http.MethodGet { + return true + } + return false } // RandomString 生成长度为 length 的随机字符串 func RandomString(length int) string { - mathrand.Seed(time.Now().UnixNano()) - letters := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" - b := make([]byte, length) - for i := range b { - b[i] = letters[mathrand.Intn(len(letters))] - } - - return string(b) + mathrand.Seed(time.Now().UnixNano()) + letters := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + b := make([]byte, length) + for i := range b { + b[i] = letters[mathrand.Intn(len(letters))] + } + + return string(b) } // Md5 返回一个32位md5加密后的字符串 func Md5(str string) string { - h := md5.New() - h.Write([]byte(str)) + h := md5.New() + h.Write([]byte(str)) - return hex.EncodeToString(h.Sum(nil)) + return hex.EncodeToString(h.Sum(nil)) } diff --git a/pkg/setting/setting.go b/setting/config.go similarity index 80% rename from pkg/setting/setting.go rename to setting/config.go index a17c4a7..463ed6d 100644 --- a/pkg/setting/setting.go +++ b/setting/config.go @@ -1,8 +1,13 @@ package setting import ( + "bytes" + _ "embed" "fmt" + "gitee.com/jikey/elk-blog/pkg/file" + "github.com/fsnotify/fsnotify" "github.com/spf13/viper" + "io" "os" "path/filepath" @@ -119,10 +124,60 @@ func init() { SetSite() } -var Config AllConfig +var ( + //go:embed config.toml + devConfig []byte + Config = new(AllConfig) +) // LoadInI 载入配置 func LoadInI() { + var r io.Reader + + r = bytes.NewReader(devConfig) + + viper.SetConfigType("toml") + + if err := viper.ReadConfig(r); err != nil { + panic(err) + } + + if err := viper.Unmarshal(Config); err != nil { + fmt.Println(err) + panic(err) + } + + viper.SetConfigName("config") + viper.AddConfigPath("./setting") + + configFile := "./setting/" + "config.toml" + _, ok := file.IsExists(configFile) + if !ok { + if err := os.MkdirAll(filepath.Dir(configFile), 0766); err != nil { + panic(err) + } + + f, err := os.Create(configFile) + if err != nil { + panic(err) + } + defer f.Close() + + if err := viper.WriteConfig(); err != nil { + panic(err) + } + } + + viper.WatchConfig() + viper.OnConfigChange(func(e fsnotify.Event) { + if err := viper.Unmarshal(Config); err != nil { + panic(err) + } + }) +} + +// LoadInIOld 载入配置 +func LoadInIOld() { var err error path, _ := os.Getwd() cfg := viper.New() diff --git a/setting/config.toml b/setting/config.toml index e269cf3..eacb447 100644 --- a/setting/config.toml +++ b/setting/config.toml @@ -21,7 +21,7 @@ type = 'mysql' host = '127.0.0.1' port = '3306' user = 'root' -password = '88888888' +password = '123456' dbname = 'elk-blog' charset = 'utf8mb4' table_prefix = "tk_" diff --git a/views/defaulted/article/detail.html b/views/defaulted/article/detail.html index 2a7dee8..9664c1d 100644 --- a/views/defaulted/article/detail.html +++ b/views/defaulted/article/detail.html @@ -29,8 +29,8 @@
- {{ data.article.Createtime | slice:"8:10" }} - {{ data.article.Createtime | slice:":7" }} + {{ data.article.CreateTime | slice:"8:10" }} + {{ data.article.CreateTime | slice:":7" }}
@@ -151,7 +151,7 @@

{{ reply.Username }}

- +
diff --git a/views/defaulted/common/_blog_sider.html b/views/defaulted/common/_blog_sider.html index 7674d5d..e3d67c0 100644 --- a/views/defaulted/common/_blog_sider.html +++ b/views/defaulted/common/_blog_sider.html @@ -33,7 +33,7 @@
- {{ data.article.Createtime | slice:"8:10" }} - {{ data.article.Createtime | slice:":7" }} + {{ data.article.CreateTime | slice:"8:10" }} + {{ data.article.CreateTime | slice:":7" }}
@@ -190,7 +190,7 @@

{{ reply.Username }}

- +
diff --git a/views/home/article/detail.html b/views/home/article/detail.html index 2566aa2..0a2d862 100644 --- a/views/home/article/detail.html +++ b/views/home/article/detail.html @@ -132,7 +132,7 @@

{{ reply.Username }}

- +
-- Gitee