Skip to content

Commit

Permalink
fix install script
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJacky committed Feb 21, 2022
1 parent f097acb commit c8732dd
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 65 deletions.
3 changes: 0 additions & 3 deletions Dockerfile

This file was deleted.

39 changes: 29 additions & 10 deletions install.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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."
Expand Down Expand Up @@ -102,21 +102,22 @@ 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
return 0
}

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"
Expand All @@ -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() {
Expand Down Expand Up @@ -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'

Expand All @@ -173,6 +190,8 @@ main() {
install_bin
install_service

mkdir DataPath

start_nginx_ui
stop_nginx_ui

Expand Down
105 changes: 54 additions & 51 deletions main.go
Original file line number Diff line number Diff line change
@@ -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")

}
Binary file removed nginx-ui
Binary file not shown.
3 changes: 2 additions & 1 deletion nginx-ui.service
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c8732dd

Please sign in to comment.