Skip to content

Commit

Permalink
完善服务端 install 启动参数
Browse files Browse the repository at this point in the history
  • Loading branch information
yzy613 committed Apr 20, 2020
1 parent ad673f9 commit 42130c2
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 47 deletions.
83 changes: 67 additions & 16 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package common
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
)

const (
LocalVersion = "0.1.2"
RootServer = "https://yzyweb.cn/ddns"
RootServer = "https://yzyweb.cn/ddns"
)

func IsDirExistAndCreate(dirPath string) (err error) {
Expand All @@ -24,33 +25,83 @@ func IsDirExistAndCreate(dirPath string) (err error) {
return
}

func CopyFile(srcPath, dstPath string) (err error) {
srcFile, err := os.Open(srcPath)
if err != nil {
return
}
defer srcFile.Close()
dirSplit := strings.Split(dstPath, "/")
dirPath := ""
if dirPathLen := len(dirSplit); dirPathLen > 1 {
switch dirSplit[0] {
case ".":
dirSplit = dirSplit[1:]
dirPath = "./"
case "":
dirSplit = dirSplit[1:]
dirPath = "/"
}
if dirPathLen := len(dirSplit); dirPathLen > 1 {
for i := 0; i < dirPathLen-1; i++ {
dirPath = dirPath + dirSplit[i] + "/"
}
err = os.MkdirAll(dirPath, 0777)
if err != nil {
return
}
}
}
dstFile, err := os.OpenFile(dstPath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0744)
if err != nil {
return
}
defer dstFile.Close()
buf := make([]byte, 1024)
for {
n, err := srcFile.Read(buf)
if err != nil {
if err == io.EOF {
break
} else {
return err
}
}
n, err = dstFile.Write(buf[:n])
if err != nil {
return err
}
}
return
}

func LoadAndUnmarshal(filePath string, dst interface{}) error {
_, getErr := os.Stat(filePath)
if getErr != nil || os.IsExist(getErr) {
_, getErr = os.OpenFile(filePath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0664)
if getErr != nil {
return getErr
_, err := os.Stat(filePath)
if err != nil || os.IsExist(err) {
_, err = os.OpenFile(filePath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0664)
if err != nil {
return err
}
}
jsonContent, getErr := ioutil.ReadFile(filePath)
if getErr != nil {
return getErr
jsonContent, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
getErr = json.Unmarshal(jsonContent, &dst)
if getErr != nil {
return getErr
err = json.Unmarshal(jsonContent, &dst)
if err != nil {
return err
}
return nil
}

func MarshalAndSave(content interface{}, filePath string) error {
func MarshalAndSave(content interface{}, filePath string) (err error) {
jsonContent, err := json.Marshal(content)
if err != nil {
return err
return
}
err = ioutil.WriteFile(filePath, jsonContent, 0666)
if err != nil {
return err
return
}
return nil
}
Expand Down Expand Up @@ -121,4 +172,4 @@ func DecodeIPv6(srcIP string) (dstIP string) {
dstIP = srcIP
}
return
}
}
7 changes: 6 additions & 1 deletion main-code/server/ddns-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ func main() {

// 加载配置
conf := server.ServerConf{}
getErr := common.IsDirExistAndCreate("./conf")
var getErr error
if server.IsWindows() == true {
getErr = common.IsDirExistAndCreate("./conf/")
} else {
getErr = common.IsDirExistAndCreate(server.ConfPath)
}
if getErr != nil {
fmt.Println(getErr)
return
Expand Down
32 changes: 2 additions & 30 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"ddns/common"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
Expand Down Expand Up @@ -94,39 +93,11 @@ func Install() {
fmt.Println("Windows 暂不支持安装到系统")
} else {
// 复制文件到工作目录
srcFile, getErr := os.Open("./ddns-server")
getErr := common.CopyFile("./ddns-server", WorkPath+"ddns-server")
if getErr != nil {
fmt.Println(getErr)
return
}
defer srcFile.Close()
getErr = os.MkdirAll(WorkPath, 0755)
if getErr != nil {
fmt.Println(getErr)
}
dstFile, getErr := os.OpenFile(WorkPath+"ddns-server", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0744)
if getErr != nil {
fmt.Println(getErr)
return
}
defer dstFile.Close()
buf := make([]byte, 1024)
for {
n, getErr := srcFile.Read(buf)
if getErr != nil {
if getErr == io.EOF {
break
} else {
fmt.Println(getErr)
return
}
}
n, getErr = dstFile.Write(buf[:n])
if getErr != nil {
fmt.Println(getErr)
return
}
}

// 注册系统服务
getErr = ioutil.WriteFile("/etc/systemd/system/ddns-server.service", serviceContent, 0664)
Expand All @@ -148,5 +119,6 @@ func Uninstall() {
return
}
fmt.Println("卸载服务成功")
fmt.Println("\n若要完全删除,请移步到 /opt/ddns 进行完全删除")
}
}

0 comments on commit 42130c2

Please sign in to comment.