From f6f31e56c497e6e10b59fd13b0896eeae351e41d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=B9=BF?= Date: Wed, 27 Dec 2023 17:22:18 +0800 Subject: [PATCH] add install script --- Makefile | 2 ++ config.json | 34 +++++++++++++++++----------------- gohttpd.service | 13 +++++++++++++ install.sh | 25 +++++++++++++++++++++++++ utils/util.go | 12 ++++++++++-- 5 files changed, 67 insertions(+), 19 deletions(-) create mode 100644 gohttpd.service create mode 100755 install.sh diff --git a/Makefile b/Makefile index b3009e0..d5aa699 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,8 @@ build: go build -o ${BINARY_NAME} cp config.json target/ cp -r adminui target/ + cp gohttpd.service target/ + cp install.sh target/ clean: go clean diff --git a/config.json b/config.json index 7ee5f70..aade76b 100644 --- a/config.json +++ b/config.json @@ -45,87 +45,87 @@ "paths":[ { "path": "/", - "upstreams": ["http://192.168.12.174:9880"], + "upstreams": ["http://localhost:4200"], "directives":["HostSchemas $target"] }, { "path" : "/ssologin", - "upstreams": ["http://192.168.12.174:9880"], + "upstreams": ["http://192.168.12.157:9880"], "directives":["HostSchemas $target"] }, { "path" : "/themes", - "upstreams": ["http://192.168.12.174:9880"], + "upstreams": ["http://192.168.12.157:9880"], "directives":["HostSchemas $target"] }, { "path" : "/security/login", - "upstreams": ["http://192.168.12.174:9880"], + "upstreams": ["http://192.168.12.157:9880"], "directives":["HostSchemas $target"] }, { "path" : "/security/logout", - "upstreams": ["http://192.168.12.174:9880"], + "upstreams": ["http://192.168.12.157:9880"], "directives":["HostSchemas $target"] }, { "path" : "/v1/relational/assets", - "upstreams": ["http://192.168.12.174:9880"], + "upstreams": ["http://192.168.12.157:9880"], "directives":["HostSchemas $target"] }, { "path" : "/platform/version", - "upstreams": ["http://192.168.12.174:9880"], + "upstreams": ["http://192.168.12.157:9880"], "directives":["HostSchemas $target"] }, { "path" : "/v1/patrolrecords", - "upstreams": ["http://192.168.12.174:9880"], + "upstreams": ["http://192.168.12.157:9880"], "directives":["HostSchemas $target"] }, { "path" : "/v1/standingbook", - "upstreams": ["http://192.168.12.174:9880"], + "upstreams": ["http://192.168.12.157:9880"], "directives":["HostSchemas $target"] }, { "path" : "/v1/resource", - "upstreams": ["http://192.168.12.174:9880"], + "upstreams": ["http://192.168.12.157:9880"], "directives":["HostSchemas $target"] }, { "path" : "/v1/config", - "upstreams": ["http://192.168.12.174:9880"], + "upstreams": ["http://192.168.12.157:9880"], "directives":["HostSchemas $target"] }, { "path" : "/v1/workflow", - "upstreams": ["http://192.168.12.174:9880"], + "upstreams": ["http://192.168.12.157:9880"], "directives":["HostSchemas $target"] }, { "path":"/token/check", - "upstreams": ["http://192.168.12.174:9880"], + "upstreams": ["http://192.168.12.157:9880"], "directives":["HostSchemas $target"] }, { "path" : "/v1/messages", - "upstreams": ["http://192.168.12.174:9880"], + "upstreams": ["http://192.168.12.157:9880"], "directives":["HostSchemas $target"] }, { "path" : "/cloudops-state/", - "upstreams": ["http://192.168.12.174:9880"], + "upstreams": ["http://192.168.12.157:9880"], "directives":["HostSchemas $target"] }, { "path":"/socket.io/", - "upstreams": ["http://192.168.12.174:9880"], + "upstreams": ["http://192.168.12.157:9880"], "directives":["HostSchemas $target"] }, { "path" : "/thing", - "upstreams": ["http://192.168.12.174:9880"], + "upstreams": ["http://192.168.12.157:9880"], "directives":[ "HostSchemas $target", "HeaderOrigin", diff --git a/gohttpd.service b/gohttpd.service new file mode 100644 index 0000000..f74671d --- /dev/null +++ b/gohttpd.service @@ -0,0 +1,13 @@ +[Unit] +Description=GoHTTPD Web Server + +[Service] +ExecStart=/usr/local/bin/gohttpd +WorkingDirectory=/usr/local/gohttpd +Environment="GoHTTPD_Home=/usr/local/gohttpd" +Environment="_go_daemon=g_daemon" +Restart=always +RestartSec=5 + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..46047c8 --- /dev/null +++ b/install.sh @@ -0,0 +1,25 @@ +#!/bin/bash +which gohttpd +if [ $? -eq 0 ]; then + echo "gohttpd is already installed." + exit 0 +fi + +#获取当前脚本所在目录 +scriptPath=$( cd `dirname $0` && pwd ) + +# 创建目录/usr/local/gohttpd +mkdir -p /usr/local/gohttpd +# 复制文件到scriptPath下的所有文件到/usr/local/gohttpd +cp -r $scriptPath/* /usr/local/gohttpd/ +# link /usr/local/gohttpd/gohttpd to /usr/local/bin/gohttpd +ln -s /usr/local/gohttpd/gohttpd /usr/local/bin/gohttpd + +# 安装gohttpd.service文件 +cp $scriptPath/gohttpd.service /etc/systemd/system/ +# 启动服务 +systemctl daemon-reload +systemctl enable gohttpd.service +systemctl start gohttpd.service + +echo "gohttpd installed successfully." diff --git a/utils/util.go b/utils/util.go index b39e99e..3d65531 100644 --- a/utils/util.go +++ b/utils/util.go @@ -6,12 +6,20 @@ import ( ) func GetExecDir() string { - + if os.Getenv("GoHTTPD_Home") != "" { + return os.Getenv("GoHTTPD_Home") + } execPath, err := os.Executable() + if err != nil { panic(err) } - return filepath.Dir(execPath) + rpath, err := filepath.EvalSymlinks(execPath) + if err != nil { + panic(err) + } + rpath, _ = filepath.Abs(rpath) + return filepath.Dir(rpath) } func NormalizePath(path string) string {