You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-fly/config/config.go

144 lines
2.8 KiB

package config
import (
"encoding/json"
"fmt"
"github.com/taoshihan1991/imaptool/tools"
"io/ioutil"
"os"
)
var(
PageSize uint=10
5 years ago
VisitorPageSize uint=8
Version = "0.1.2"
GoflyConfig *Config
)
const Dir = "config/"
5 years ago
const AccountConf = Dir + "account.json"
const MysqlConf = Dir + "mysql.json"
5 years ago
const MailConf = Dir + "mail.json"
const LangConf=Dir+"language.json"
const MainConf = Dir + "config.json"
const WeixinToken=""
const ServerJiang=""
func init(){
//配置文件
GoflyConfig=CreateConfig()
}
5 years ago
type Mysql struct{
Server string
Port string
Database string
Username string
Password string
}
5 years ago
type MailServer struct {
Server, Email, Password string
}
5 years ago
type Config struct {
Upload string
NoticeServerJiang bool
5 years ago
}
func CreateConfig()*Config{
var configObj Config
5 years ago
c:=&Config{
Upload: "static/upload/",
NoticeServerJiang:false,
5 years ago
}
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
5 years ago
}
5 years ago
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
}
5 years ago
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
}
5 years ago
func GetAccount() map[string]string {
var account map[string]string
5 years ago
isExist, _ := tools.IsFileExist(AccountConf)
if !isExist {
return account
}
5 years ago
info, err := ioutil.ReadFile(AccountConf)
if err != nil {
return account
}
5 years ago
err = json.Unmarshal(info, &account)
return account
}
5 years ago
func GetUserInfo(uid string) map[string]string {
var userInfo map[string]string
5 years ago
userFile := Dir + "sess_" + uid + ".json"
isExist, _ := tools.IsFileExist(userFile)
if !isExist {
return userInfo
}
5 years ago
info, err := ioutil.ReadFile(userFile)
if err != nil {
return userInfo
}
5 years ago
err = json.Unmarshal(info, &userInfo)
return userInfo
}
5 years ago
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)
5 years ago
str := "{\r\n"
for k, v := range info {
str += fmt.Sprintf(`"%s":"%s",`, k, v)
}
5 years ago
str += fmt.Sprintf(`"session_id":"%s"%s}`, uid, "\r\n")
file.WriteString(str)
5 years ago
}