feat:支持后台模式与网络守护

This commit is contained in:
Mmx
2021-07-11 12:52:56 +08:00
parent e3f668c100
commit 257a9b7034
8 changed files with 192 additions and 86 deletions

View File

@@ -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