优化配置代码

pull/23/head
taoshihan1991 4 years ago
parent 699ad6c1dc
commit f14bdd646e

@ -3,7 +3,7 @@ package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/taoshihan1991/imaptool/config"
"github.com/taoshihan1991/imaptool/common"
"github.com/taoshihan1991/imaptool/models"
"github.com/taoshihan1991/imaptool/tools"
"io/ioutil"
@ -21,8 +21,8 @@ var installCmd = &cobra.Command{
}
func install() {
sqlFile := config.Dir + "go-fly.sql"
isExit, _ := tools.IsFileExist(config.MysqlConf)
sqlFile := common.Dir + "go-fly.sql"
isExit, _ := tools.IsFileExist(common.MysqlConf)
dataExit, _ := tools.IsFileExist(sqlFile)
if !isExit || !dataExit {
fmt.Println("config/mysql.json 数据库配置文件或者数据库文件go-fly.sql不存在")

@ -3,13 +3,13 @@ package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/taoshihan1991/imaptool/config"
"github.com/taoshihan1991/imaptool/common"
)
var versionCmd = &cobra.Command{
Use: "version",
Short: "example:go-fly version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("go-fly " + config.Version)
fmt.Println("go-fly " + common.Version)
},
}

@ -0,0 +1,11 @@
package common
var (
PageSize uint = 10
VisitorPageSize uint = 8
Version string = "0.3.9"
VisitorExpire float64 = 600
Upload string = "static/upload/"
Dir string = "config/"
MysqlConf string = Dir + "mysql.json"
)

@ -0,0 +1,29 @@
package common
import (
"encoding/json"
"github.com/taoshihan1991/imaptool/tools"
"io/ioutil"
)
type Mysql struct {
Server string
Port string
Database string
Username string
Password string
}
func GetMysqlConf() *Mysql {
var mysql = &Mysql{}
isExist, _ := tools.IsFileExist(MysqlConf)
if !isExist {
return mysql
}
info, err := ioutil.ReadFile(MysqlConf)
if err != nil {
return mysql
}
err = json.Unmarshal(info, mysql)
return mysql
}

@ -1,146 +0,0 @@
package config
import (
"encoding/json"
"fmt"
"github.com/taoshihan1991/imaptool/tools"
"io/ioutil"
"os"
)
var (
PageSize uint = 10
VisitorPageSize uint = 8
Version = "0.1.2"
VisitorExpire float64 = 600
GoflyConfig *Config
)
const Dir = "config/"
const AccountConf = Dir + "account.json"
const MysqlConf = Dir + "mysql.json"
const MailConf = Dir + "mail.json"
const MainConf = Dir + "config.json"
func init() {
//配置文件
GoflyConfig = CreateConfig()
}
type Mysql struct {
Server string
Port string
Database string
Username string
Password string
}
type MailServer struct {
Server, Email, Password string
}
type Config struct {
Upload string
NoticeServerJiang bool
}
func CreateConfig() *Config {
var configObj Config
c := &Config{
Upload: "static/upload/",
NoticeServerJiang: false,
}
isExist, _ := tools.IsFileExist(MainConf)
if !isExist {
return c
}
info, err := ioutil.ReadFile(MainConf)
if err != nil {
return c
}
err = json.Unmarshal(info, &configObj)
return &configObj
}
func CreateMailServer() *MailServer {
var imap MailServer
isExist, _ := tools.IsFileExist(MailConf)
if !isExist {
return &imap
}
info, err := ioutil.ReadFile(MailConf)
if err != nil {
return &imap
}
err = json.Unmarshal(info, &imap)
return &imap
}
func CreateMysql() *Mysql {
var mysql Mysql
isExist, _ := tools.IsFileExist(MysqlConf)
if !isExist {
return &mysql
}
info, err := ioutil.ReadFile(MysqlConf)
if err != nil {
return &mysql
}
err = json.Unmarshal(info, &mysql)
return &mysql
}
func GetMysql() map[string]string {
var mysql map[string]string
isExist, _ := tools.IsFileExist(MysqlConf)
if !isExist {
return mysql
}
info, err := ioutil.ReadFile(MysqlConf)
if err != nil {
return mysql
}
err = json.Unmarshal(info, &mysql)
return mysql
}
func GetAccount() map[string]string {
var account map[string]string
isExist, _ := tools.IsFileExist(AccountConf)
if !isExist {
return account
}
info, err := ioutil.ReadFile(AccountConf)
if err != nil {
return account
}
err = json.Unmarshal(info, &account)
return account
}
func GetUserInfo(uid string) map[string]string {
var userInfo map[string]string
userFile := Dir + "sess_" + uid + ".json"
isExist, _ := tools.IsFileExist(userFile)
if !isExist {
return userInfo
}
info, err := ioutil.ReadFile(userFile)
if err != nil {
return userInfo
}
err = json.Unmarshal(info, &userInfo)
return userInfo
}
func SetUserInfo(uid string, info map[string]string) {
userFile := Dir + "sess_" + uid + ".json"
isExist, _ := tools.IsFileExist(Dir)
if !isExist {
os.Mkdir(Dir, os.ModePerm)
}
file, _ := os.OpenFile(userFile, os.O_RDWR|os.O_CREATE, os.ModePerm)
str := "{\r\n"
for k, v := range info {
str += fmt.Sprintf(`"%s":"%s",`, k, v)
}
str += fmt.Sprintf(`"session_id":"%s"%s}`, uid, "\r\n")
file.WriteString(str)
}

@ -1,4 +0,0 @@
{
"Upload":"static/upload/",
"NoticeServerJiang": false
}

@ -1,30 +1,10 @@
package controller
import (
"github.com/taoshihan1991/imaptool/config"
"github.com/taoshihan1991/imaptool/models"
"github.com/taoshihan1991/imaptool/tools"
)
func CheckPass(username string, password string) string {
account := config.GetAccount()
if account == nil {
account = make(map[string]string)
}
if account["Username"] == "" && account["Password"] == "" {
account["Username"] = "admin"
account["Password"] = "admin123"
}
if username == account["Username"] && password == account["Password"] {
sessionId := tools.Md5(username)
info := make(map[string]string)
info["username"] = username
config.SetUserInfo(sessionId, info)
return sessionId
}
return ""
}
func CheckKefuPass(username string, password string) (models.User, models.User_role, bool) {
info := models.FindUser(username)
var uRole models.User_role
@ -35,29 +15,3 @@ func CheckKefuPass(username string, password string) (models.User, models.User_r
return info, uRole, true
}
func AuthLocal(username string, password string) string {
account := config.GetAccount()
if account == nil {
account = make(map[string]string)
}
if account["Username"] == "" && account["Password"] == "" {
account["Username"] = "admin"
account["Password"] = "admin123"
}
if username == account["Username"] && password == account["Password"] {
sessionId := tools.Md5(username)
info := make(map[string]string)
info["username"] = username
config.SetUserInfo(sessionId, info)
return sessionId
}
return ""
}
//验证是否已经登录
func AuthCheck(uid string) map[string]string {
info := config.GetUserInfo(uid)
return info
}

@ -2,7 +2,7 @@ package controller
import (
"github.com/gin-gonic/gin"
"github.com/taoshihan1991/imaptool/config"
"github.com/taoshihan1991/imaptool/common"
"github.com/taoshihan1991/imaptool/models"
"strconv"
)
@ -44,14 +44,14 @@ func GetIpblacks(c *gin.Context) {
page = 1
}
count := models.CountIps(nil, nil)
list := models.FindIps(nil, nil, uint(page), config.VisitorPageSize)
list := models.FindIps(nil, nil, uint(page), common.VisitorPageSize)
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
"result": gin.H{
"list": list,
"count": count,
"pagesize": config.PageSize,
"pagesize": common.PageSize,
},
})
}

@ -3,22 +3,9 @@ package controller
import (
"github.com/gin-gonic/gin"
"github.com/taoshihan1991/imaptool/models"
"github.com/taoshihan1991/imaptool/tmpl"
"github.com/taoshihan1991/imaptool/tools"
"github.com/taoshihan1991/imaptool/ws"
"net/http"
)
func ActionMain(w http.ResponseWriter, r *http.Request) {
sessionId := tools.GetCookie(r, "session_id")
info := AuthCheck(sessionId)
if len(info) == 0 {
http.Redirect(w, r, "/login", 302)
return
}
render := tmpl.NewRender(w)
render.Display("main", render)
}
func MainCheckAuth(c *gin.Context) {
id, _ := c.Get("kefu_id")
userinfo := models.FindUserRole("user.avator,user.name,user.id, role.name role_name", id)

@ -5,7 +5,7 @@ import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/taoshihan1991/imaptool/config"
"github.com/taoshihan1991/imaptool/common"
"github.com/taoshihan1991/imaptool/models"
"github.com/taoshihan1991/imaptool/tools"
"github.com/taoshihan1991/imaptool/ws"
@ -174,7 +174,6 @@ func SendCloseMessageV2(c *gin.Context) {
})
}
func UploadImg(c *gin.Context) {
config := config.CreateConfig()
f, err := c.FormFile("imgfile")
if err != nil {
c.JSON(200, gin.H{
@ -192,12 +191,12 @@ func UploadImg(c *gin.Context) {
})
return
}
isMainUploadExist, _ := tools.IsFileExist(config.Upload)
isMainUploadExist, _ := tools.IsFileExist(common.Upload)
if !isMainUploadExist {
os.Mkdir(config.Upload, os.ModePerm)
os.Mkdir(common.Upload, os.ModePerm)
}
fileName := tools.Md5(fmt.Sprintf("%s%s", f.Filename, time.Now().String()))
fildDir := fmt.Sprintf("%s%d%s/", config.Upload, time.Now().Year(), time.Now().Month().String())
fildDir := fmt.Sprintf("%s%d%s/", common.Upload, time.Now().Year(), time.Now().Month().String())
isExist, _ := tools.IsFileExist(fildDir)
if !isExist {
os.Mkdir(fildDir, os.ModePerm)
@ -222,7 +221,6 @@ func UploadFile(c *gin.Context) {
})
return
}
config := config.CreateConfig()
f, err := c.FormFile("realfile")
if err != nil {
c.JSON(200, gin.H{
@ -242,7 +240,7 @@ func UploadFile(c *gin.Context) {
}
fileName := tools.Md5(fmt.Sprintf("%s%s", f.Filename, time.Now().String()))
fildDir := fmt.Sprintf("%s%d%s/", config.Upload, time.Now().Year(), time.Now().Month().String())
fildDir := fmt.Sprintf("%s%d%s/", common.Upload, time.Now().Year(), time.Now().Month().String())
isExist, _ := tools.IsFileExist(fildDir)
if !isExist {
os.Mkdir(fildDir, os.ModePerm)

@ -4,7 +4,7 @@ import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/taoshihan1991/imaptool/config"
"github.com/taoshihan1991/imaptool/common"
"github.com/taoshihan1991/imaptool/models"
"github.com/taoshihan1991/imaptool/tools"
"github.com/taoshihan1991/imaptool/ws"
@ -159,7 +159,7 @@ func GetVisitors(c *gin.Context) {
page, _ := strconv.Atoi(c.Query("page"))
pagesize, _ := strconv.Atoi(c.Query("pagesize"))
if pagesize == 0 {
pagesize = int(config.VisitorPageSize)
pagesize = int(common.VisitorPageSize)
}
kefuId, _ := c.Get("kefu_name")
vistors := models.FindVisitorsByKefuId(uint(page), uint(pagesize), kefuId.(string))
@ -170,7 +170,7 @@ func GetVisitors(c *gin.Context) {
"result": gin.H{
"list": vistors,
"count": count,
"pagesize": config.PageSize,
"pagesize": common.PageSize,
},
})
}

@ -3,7 +3,7 @@ package models
import (
"fmt"
"github.com/jinzhu/gorm"
"github.com/taoshihan1991/imaptool/config"
"github.com/taoshihan1991/imaptool/common"
"github.com/taoshihan1991/imaptool/tools"
"log"
"time"
@ -19,7 +19,7 @@ type Model struct {
}
func init() {
mysql := config.CreateMysql()
mysql := common.GetMysqlConf()
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", mysql.Username, mysql.Password, mysql.Server, mysql.Port, mysql.Database)
var err error
DB, err = gorm.Open("mysql", dsn)

@ -0,0 +1,2 @@
go-fly.exe server
pause

@ -4,7 +4,7 @@ import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/taoshihan1991/imaptool/config"
"github.com/taoshihan1991/imaptool/common"
"github.com/taoshihan1991/imaptool/models"
"log"
"time"
@ -185,7 +185,7 @@ func cleanVisitorExpire() {
for {
for _, user := range ClientList {
diff := time.Now().Sub(user.UpdateTime).Seconds()
if diff >= config.VisitorExpire {
if diff >= common.VisitorExpire {
msg := TypeMessage{
Type: "auto_close",
Data: user.Id,

Loading…
Cancel
Save