2014-08-18 17:45:43 +04:00
|
|
|
package conf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2014-08-25 18:17:02 +04:00
|
|
|
_ "github.com/b3log/wide/i18n"
|
2014-08-18 17:51:03 +04:00
|
|
|
"github.com/b3log/wide/util"
|
2014-08-18 17:45:43 +04:00
|
|
|
"github.com/golang/glog"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-08-31 14:50:38 +04:00
|
|
|
type User struct {
|
|
|
|
Name string
|
|
|
|
Password string
|
2014-08-31 09:31:26 +04:00
|
|
|
}
|
|
|
|
|
2014-08-23 19:07:24 +04:00
|
|
|
type conf struct {
|
2014-08-18 17:45:43 +04:00
|
|
|
Server string
|
|
|
|
StaticServer string
|
|
|
|
EditorChannel string
|
|
|
|
OutputChannel string
|
|
|
|
ShellChannel string
|
|
|
|
StaticResourceVersion string
|
|
|
|
ContextPath string
|
|
|
|
StaticPath string
|
|
|
|
RuntimeMode string
|
2014-08-28 06:51:03 +04:00
|
|
|
Repos string
|
|
|
|
UserRepos string
|
2014-08-31 14:50:38 +04:00
|
|
|
Users []User
|
2014-08-18 17:45:43 +04:00
|
|
|
}
|
|
|
|
|
2014-08-23 19:07:24 +04:00
|
|
|
var Wide conf
|
2014-08-31 14:50:38 +04:00
|
|
|
var rawWide conf
|
2014-08-18 17:45:43 +04:00
|
|
|
|
2014-08-31 14:50:38 +04:00
|
|
|
func Save() bool {
|
|
|
|
// 可变部分
|
|
|
|
rawWide.Users = Wide.Users
|
|
|
|
|
|
|
|
// 原始配置文件内容
|
|
|
|
bytes, err := json.MarshalIndent(rawWide, "", " ")
|
|
|
|
|
|
|
|
if nil != err {
|
|
|
|
glog.Error(err)
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = ioutil.WriteFile("conf/wide.json", bytes, 0644); nil != err {
|
|
|
|
glog.Error(err)
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func Load() {
|
2014-08-18 17:45:43 +04:00
|
|
|
bytes, _ := ioutil.ReadFile("conf/wide.json")
|
|
|
|
|
|
|
|
err := json.Unmarshal(bytes, &Wide)
|
|
|
|
if err != nil {
|
|
|
|
glog.Error(err)
|
|
|
|
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
|
2014-08-31 14:50:38 +04:00
|
|
|
// 保存未经变量替换处理的原始配置文件,用于写回时
|
|
|
|
json.Unmarshal(bytes, &rawWide)
|
|
|
|
|
2014-08-18 17:45:43 +04:00
|
|
|
ip, err := util.Net.LocalIP()
|
|
|
|
if err != nil {
|
|
|
|
glog.Error(err)
|
|
|
|
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.Infof("IP [%s]", ip)
|
|
|
|
Wide.Server = strings.Replace(Wide.Server, "{IP}", ip, 1)
|
|
|
|
Wide.StaticServer = strings.Replace(Wide.StaticServer, "{IP}", ip, 1)
|
|
|
|
Wide.EditorChannel = strings.Replace(Wide.EditorChannel, "{IP}", ip, 1)
|
|
|
|
Wide.OutputChannel = strings.Replace(Wide.OutputChannel, "{IP}", ip, 1)
|
|
|
|
Wide.ShellChannel = strings.Replace(Wide.ShellChannel, "{IP}", ip, 1)
|
|
|
|
|
2014-08-28 06:51:03 +04:00
|
|
|
// 获取当前执行路径
|
2014-08-18 17:45:43 +04:00
|
|
|
file, _ := exec.LookPath(os.Args[0])
|
|
|
|
pwd, _ := filepath.Abs(file)
|
|
|
|
pwd = pwd[:strings.LastIndex(pwd, string(os.PathSeparator))]
|
|
|
|
glog.Infof("pwd [%s]", pwd)
|
2014-08-28 06:51:03 +04:00
|
|
|
|
|
|
|
Wide.Repos = strings.Replace(Wide.Repos, "{pwd}", pwd, 1)
|
|
|
|
Wide.UserRepos = strings.Replace(Wide.UserRepos, "{pwd}", pwd, 1)
|
2014-08-18 17:45:43 +04:00
|
|
|
|
|
|
|
glog.Info("Conf: \n" + string(bytes))
|
|
|
|
}
|