fix install script

This commit is contained in:
0xJacky 2022-02-21 14:03:46 +08:00
parent f097acb3e9
commit c8732ddb08
5 changed files with 85 additions and 65 deletions

View file

@ -1,3 +0,0 @@
FROM ubuntu
WORKDIR /app
EXPOSE 9000

View file

@ -1,7 +1,7 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Data Path # Data Path
# DataPath=/usr/local/etc/nginx-ui DataPath=/usr/local/etc/nginx-ui
# Bin Path # Bin Path
# BinPath=/usr/local/bin/nginx-ui # BinPath=/usr/local/bin/nginx-ui
# Service Path # Service Path
@ -37,13 +37,13 @@ identify_the_operating_system_and_architecture() {
if [[ "$(uname)" == 'Linux' ]]; then if [[ "$(uname)" == 'Linux' ]]; then
case "$(uname -m)" in case "$(uname -m)" in
'i386' | 'i686') 'i386' | 'i686')
MACHINE='32' MACHINE='386'
;; ;;
'amd64' | 'x86_64') 'amd64' | 'x86_64')
MACHINE='64' MACHINE='amd64'
;; ;;
'armv8' | 'aarch64') 'armv8' | 'aarch64')
MACHINE='arm64-v8a' MACHINE='arm64'
;; ;;
*) *)
echo "error: The architecture is not supported." echo "error: The architecture is not supported."
@ -102,13 +102,13 @@ install_software() {
} }
download() { download() {
LATEST_RELEASE=$(curl -L -s -H 'Accept: application/json' "$PROXY"https://github.com/0xJacky/nginx-ui/releases/latest) LATEST_RELEASE=$(curl -L -s --insecure -H 'Accept: application/json' "$PROXY"https://api.github.com/repos/0xJacky/nginx-ui/releases/latest)
# shellcheck disable=SC2001 # shellcheck disable=SC2001
LATEST_VERSION=$(echo "$LATEST_RELEASE" | sed -e 's/.*"tag_name":"\([^"]*\)".*/\1/') LATEST_VERSION=$(echo "$LATEST_RELEASE" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
DOWNLOAD_LINK=$PROXY"https://github.com/0xJacky/nginx-ui/releases/download/$LATEST_VERSION/nginx-ui-linux-$MACHINE.tar.gz" DOWNLOAD_LINK=$PROXY"https://github.com/0xJacky/nginx-ui/releases/download/$LATEST_VERSION/nginx-ui-linux-$MACHINE.tar.gz"
echo "Downloading NginxUI archive: $DOWNLOAD_LINK" echo "Downloading NginxUI archive: $DOWNLOAD_LINK"
if ! curl -x "${PROXY}" -R -H 'Cache-Control: no-cache' -o "$TAR_FILE" "$DOWNLOAD_LINK"; then if ! curl --insecure -R -H 'Cache-Control: no-cache' -L "$DOWNLOAD_LINK" > "$TAR_FILE"; then
echo 'error: Download failed! Please check your network or try again.' echo 'error: Download failed! Please check your network or try again.'
return 1 return 1
fi fi
@ -116,7 +116,8 @@ download() {
} }
decompression() { decompression() {
if ! unzip -q "$1" -d "$TMP_DIRECTORY"; then echo "$1"
if ! tar -zxvf "$1" -C "$TMP_DIRECTORY"; then
echo 'error: Nginx UI decompression failed.' echo 'error: Nginx UI decompression failed.'
"rm" -r "$TMP_DIRECTORY" "rm" -r "$TMP_DIRECTORY"
echo "removed: $TMP_DIRECTORY" echo "removed: $TMP_DIRECTORY"
@ -131,7 +132,21 @@ install_bin() {
} }
install_service() { install_service() {
install -m 644 "${TMP_DIRECTORY}/nginx-ui.service" "$ServicePath" cat > "$ServicePath" << EOF
[Unit]
Description=Yet another WebUI for Nginx
Documentation=https://github.com/0xJacky/nginx-ui
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/nginx-ui --config /usr/local/etc/nginx-ui/app.ini
Restart=on-failure
TimeoutStopSec=5
KillMode=mixed
[Install]
WantedBy=multi-user.target
EOF
chmod 644 ServicePath
} }
start_nginx_ui() { start_nginx_ui() {
@ -163,7 +178,9 @@ main() {
# TMP # TMP
TMP_DIRECTORY="$(mktemp -d)" TMP_DIRECTORY="$(mktemp -d)"
# Tar # Tar
TAR_FILE="${TMP_DIRECTORY}/nginx-ui-linux-$ARCH.tar.gz" TAR_FILE="${TMP_DIRECTORY}/nginx-ui-linux-$MACHINE.tar.gz"
identify_the_operating_system_and_architecture
install_software 'curl' 'curl' install_software 'curl' 'curl'
@ -173,6 +190,8 @@ main() {
install_bin install_bin
install_service install_service
mkdir DataPath
start_nginx_ui start_nginx_ui
stop_nginx_ui stop_nginx_ui

105
main.go
View file

@ -1,68 +1,71 @@
package main package main
import ( import (
"context" "context"
"flag" "flag"
"github.com/0xJacky/Nginx-UI/server/model" "github.com/0xJacky/Nginx-UI/server/model"
"github.com/0xJacky/Nginx-UI/server/router" "github.com/0xJacky/Nginx-UI/server/router"
"github.com/0xJacky/Nginx-UI/server/settings" "github.com/0xJacky/Nginx-UI/server/settings"
tool2 "github.com/0xJacky/Nginx-UI/server/tool" tool2 "github.com/0xJacky/Nginx-UI/server/tool"
"log" "github.com/gin-gonic/gin"
"mime" "log"
"net/http" "mime"
"os/signal" "net/http"
"syscall" "os/signal"
"time" "syscall"
"time"
) )
func main() { func main() {
// Create context that listens for the interrupt signal from the OS. // Create context that listens for the interrupt signal from the OS.
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop() defer stop()
// Hack: fix wrong Content Type of .js file on some OS platforms // Hack: fix wrong Content Type of .js file on some OS platforms
// See https://github.com/golang/go/issues/32350 // See https://github.com/golang/go/issues/32350
_ = mime.AddExtensionType(".js", "text/javascript; charset=utf-8") _ = mime.AddExtensionType(".js", "text/javascript; charset=utf-8")
var confPath string var confPath string
flag.StringVar(&confPath, "config", "app.ini", "Specify the configuration file") flag.StringVar(&confPath, "config", "app.ini", "Specify the configuration file")
flag.Parse() flag.Parse()
settings.Init(confPath) gin.SetMode(settings.ServerSettings.RunMode)
log.Printf("nginx config dir path: %s", tool2.GetNginxConfPath(""))
if "" != settings.ServerSettings.JwtSecret { settings.Init(confPath)
model.Init() log.Printf("nginx config dir path: %s", tool2.GetNginxConfPath(""))
go tool2.AutoCert() if "" != settings.ServerSettings.JwtSecret {
} model.Init()
go tool2.AutoCert()
}
srv := &http.Server{ srv := &http.Server{
Addr: ":" + settings.ServerSettings.HttpPort, Addr: ":" + settings.ServerSettings.HttpPort,
Handler: router.InitRouter(), Handler: router.InitRouter(),
} }
// Initializing the server in a goroutine so that // Initializing the server in a goroutine so that
// it won't block the graceful shutdown handling below // it won't block the graceful shutdown handling below
go func() { go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err) log.Fatalf("listen: %s\n", err)
} }
}() }()
// Listen for the interrupt signal. // Listen for the interrupt signal.
<-ctx.Done() <-ctx.Done()
// Restore default behavior on the interrupt signal and notify user of shutdown. // Restore default behavior on the interrupt signal and notify user of shutdown.
stop() stop()
log.Println("shutting down gracefully, press Ctrl+C again to force") log.Println("shutting down gracefully, press Ctrl+C again to force")
// The context is used to inform the server it has 5 seconds to finish // The context is used to inform the server it has 5 seconds to finish
// the request it is currently handling // the request it is currently handling
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel() defer cancel()
if err := srv.Shutdown(ctx); err != nil { if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server forced to shutdown: ", err) log.Fatal("Server forced to shutdown: ", err)
} }
log.Println("Server exiting") log.Println("Server exiting")
} }

BIN
nginx-ui

Binary file not shown.

View file

@ -2,10 +2,11 @@
Description=Yet another WebUI for Nginx Description=Yet another WebUI for Nginx
Documentation=https://github.com/0xJacky/nginx-ui Documentation=https://github.com/0xJacky/nginx-ui
After=network.target After=network.target
[Service] [Service]
Type=simple Type=simple
ExecStart=/usr/local/bin/nginx-ui --config /usr/local/etc/nginx-ui/app.ini ExecStart=/usr/local/bin/nginx-ui --config /usr/local/etc/nginx-ui/app.ini
Restart=on-failure Restart=on-failure
TimeoutStopSec=5 TimeoutStopSec=5
KillMode=mixed KillMode=mixed
[Install]
WantedBy=multi-user.target