diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index b5bf592d..00000000 --- a/Dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM ubuntu -WORKDIR /app -EXPOSE 9000 diff --git a/install.sh b/install.sh index 9b809863..f2e05ee4 100644 --- a/install.sh +++ b/install.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash # Data Path -# DataPath=/usr/local/etc/nginx-ui +DataPath=/usr/local/etc/nginx-ui # Bin Path # BinPath=/usr/local/bin/nginx-ui # Service Path @@ -37,13 +37,13 @@ identify_the_operating_system_and_architecture() { if [[ "$(uname)" == 'Linux' ]]; then case "$(uname -m)" in 'i386' | 'i686') - MACHINE='32' + MACHINE='386' ;; 'amd64' | 'x86_64') - MACHINE='64' + MACHINE='amd64' ;; 'armv8' | 'aarch64') - MACHINE='arm64-v8a' + MACHINE='arm64' ;; *) echo "error: The architecture is not supported." @@ -102,13 +102,13 @@ install_software() { } 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 - 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" 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.' return 1 fi @@ -116,7 +116,8 @@ download() { } 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.' "rm" -r "$TMP_DIRECTORY" echo "removed: $TMP_DIRECTORY" @@ -131,7 +132,21 @@ install_bin() { } 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() { @@ -163,7 +178,9 @@ main() { # TMP TMP_DIRECTORY="$(mktemp -d)" # 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' @@ -173,6 +190,8 @@ main() { install_bin install_service + mkdir DataPath + start_nginx_ui stop_nginx_ui diff --git a/main.go b/main.go index 9954b570..ae820fc9 100644 --- a/main.go +++ b/main.go @@ -1,68 +1,71 @@ package main import ( - "context" - "flag" - "github.com/0xJacky/Nginx-UI/server/model" - "github.com/0xJacky/Nginx-UI/server/router" - "github.com/0xJacky/Nginx-UI/server/settings" - tool2 "github.com/0xJacky/Nginx-UI/server/tool" - "log" - "mime" - "net/http" - "os/signal" - "syscall" - "time" + "context" + "flag" + "github.com/0xJacky/Nginx-UI/server/model" + "github.com/0xJacky/Nginx-UI/server/router" + "github.com/0xJacky/Nginx-UI/server/settings" + tool2 "github.com/0xJacky/Nginx-UI/server/tool" + "github.com/gin-gonic/gin" + "log" + "mime" + "net/http" + "os/signal" + "syscall" + "time" ) func main() { - // Create context that listens for the interrupt signal from the OS. - ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) - defer stop() + // Create context that listens for the interrupt signal from the OS. + ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + defer stop() - // Hack: fix wrong Content Type of .js file on some OS platforms - // See https://github.com/golang/go/issues/32350 - _ = mime.AddExtensionType(".js", "text/javascript; charset=utf-8") + // Hack: fix wrong Content Type of .js file on some OS platforms + // See https://github.com/golang/go/issues/32350 + _ = mime.AddExtensionType(".js", "text/javascript; charset=utf-8") - var confPath string - flag.StringVar(&confPath, "config", "app.ini", "Specify the configuration file") - flag.Parse() + var confPath string + flag.StringVar(&confPath, "config", "app.ini", "Specify the configuration file") + flag.Parse() - settings.Init(confPath) - log.Printf("nginx config dir path: %s", tool2.GetNginxConfPath("")) - if "" != settings.ServerSettings.JwtSecret { - model.Init() - go tool2.AutoCert() - } + gin.SetMode(settings.ServerSettings.RunMode) + + settings.Init(confPath) + log.Printf("nginx config dir path: %s", tool2.GetNginxConfPath("")) + if "" != settings.ServerSettings.JwtSecret { + model.Init() + go tool2.AutoCert() + } - srv := &http.Server{ - Addr: ":" + settings.ServerSettings.HttpPort, - Handler: router.InitRouter(), - } + srv := &http.Server{ + Addr: ":" + settings.ServerSettings.HttpPort, + Handler: router.InitRouter(), + } - // Initializing the server in a goroutine so that - // it won't block the graceful shutdown handling below - go func() { - if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { - log.Fatalf("listen: %s\n", err) - } - }() + // Initializing the server in a goroutine so that + // it won't block the graceful shutdown handling below + go func() { + if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { + log.Fatalf("listen: %s\n", err) + } + }() - // Listen for the interrupt signal. - <-ctx.Done() + // Listen for the interrupt signal. + <-ctx.Done() - // Restore default behavior on the interrupt signal and notify user of shutdown. - stop() - log.Println("shutting down gracefully, press Ctrl+C again to force") + // Restore default behavior on the interrupt signal and notify user of shutdown. + stop() + 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 request it is currently handling - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - if err := srv.Shutdown(ctx); err != nil { - log.Fatal("Server forced to shutdown: ", err) - } + // The context is used to inform the server it has 5 seconds to finish + // the request it is currently handling + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + if err := srv.Shutdown(ctx); err != nil { + log.Fatal("Server forced to shutdown: ", err) + } - log.Println("Server exiting") + log.Println("Server exiting") } diff --git a/nginx-ui b/nginx-ui deleted file mode 100755 index 2f20ba80..00000000 Binary files a/nginx-ui and /dev/null differ diff --git a/nginx-ui.service b/nginx-ui.service index ae5ade2a..39c00b17 100644 --- a/nginx-ui.service +++ b/nginx-ui.service @@ -2,10 +2,11 @@ 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