feat:支持后台模式与网络守护
This commit is contained in:
29
util/file.go
29
util/file.go
@@ -13,7 +13,11 @@ type file struct{}
|
||||
var File file
|
||||
|
||||
func (a *file) Exists(path string) bool {
|
||||
_, err := os.Stat(a.GetRootPath() + path)
|
||||
root, err := a.GetRootPath()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
_, err = os.Stat(root + path)
|
||||
if err != nil {
|
||||
if os.IsExist(err) {
|
||||
return true
|
||||
@@ -24,7 +28,11 @@ func (a *file) Exists(path string) bool {
|
||||
}
|
||||
|
||||
func (a *file) Read(path string) ([]byte, error) {
|
||||
return ioutil.ReadFile(a.GetRootPath() + path)
|
||||
root, err := a.GetRootPath()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ioutil.ReadFile(root + path)
|
||||
}
|
||||
|
||||
func (a *file) ReadJson(path string, receiver interface{}) error {
|
||||
@@ -36,7 +44,11 @@ func (a *file) ReadJson(path string, receiver interface{}) error {
|
||||
}
|
||||
|
||||
func (a *file) Write(path string, data []byte) error {
|
||||
return ioutil.WriteFile(a.GetRootPath()+path, data, 700)
|
||||
root, err := a.GetRootPath()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ioutil.WriteFile(root+path, data, 700)
|
||||
}
|
||||
|
||||
func (a *file) WriteJson(path string, receiver interface{}) error {
|
||||
@@ -47,16 +59,21 @@ func (a *file) WriteJson(path string, receiver interface{}) error {
|
||||
return a.Write(path, data)
|
||||
}
|
||||
|
||||
func (*file) GetRootPath() string {
|
||||
func (*file) GetRootPath() (string, error) {
|
||||
t, err := os.Executable()
|
||||
if err != nil {
|
||||
ErrHandler(err)
|
||||
return "", err
|
||||
}
|
||||
return filepath.Dir(t) + "/"
|
||||
return filepath.Dir(t) + "/", nil
|
||||
}
|
||||
|
||||
func (a *file) Add(path string, c string) error {
|
||||
file, err := os.OpenFile(a.GetRootPath()+path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 700)
|
||||
root, err := a.GetRootPath()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
file, err := os.OpenFile(root+path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 700)
|
||||
defer file.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -39,6 +39,9 @@ func (c *loG) genTimeStamp() {
|
||||
}
|
||||
|
||||
func (c *loG) Println(a ...interface{}) {
|
||||
if !global.Status.Output {
|
||||
return
|
||||
}
|
||||
c.genTimeStamp()
|
||||
if global.Config.Settings.DemoMode {
|
||||
c.WriteLog("Login-"+c.timeStamp+".log", a...)
|
||||
@@ -47,6 +50,9 @@ func (c *loG) Println(a ...interface{}) {
|
||||
}
|
||||
|
||||
func (c *loG) Fatalln(a ...interface{}) {
|
||||
if !global.Status.Output {
|
||||
return
|
||||
}
|
||||
c.genTimeStamp()
|
||||
c.WriteLog("LoginError-"+c.timeStamp+".log", a...)
|
||||
log.Fatalln(a...)
|
||||
|
||||
13
util/util.go
13
util/util.go
@@ -52,13 +52,14 @@ func Sha1(content string) string {
|
||||
}
|
||||
|
||||
func ErrHandler(err error) {
|
||||
if err != nil {
|
||||
Log.Println("运行出错,状态异常")
|
||||
if global.Config.Settings.DemoMode {
|
||||
Log.Fatalln(err)
|
||||
}
|
||||
os.Exit(1)
|
||||
if !global.Status.Output {
|
||||
return
|
||||
}
|
||||
Log.Println("运行出错,状态异常")
|
||||
if global.Config.Settings.DemoMode {
|
||||
Log.Fatalln(err)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func NetDailEr() func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
|
||||
Reference in New Issue
Block a user