Skip to content

Commit

Permalink
Merge pull request #376 from 0xJacky/docker/s6-overlay
Browse files Browse the repository at this point in the history
Enhance nginx ui docker image
  • Loading branch information
0xJacky authored May 8, 2024
2 parents 8623e1e + e0366f9 commit 1ac1d15
Show file tree
Hide file tree
Showing 86 changed files with 5,241 additions and 5,171 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
database.db
tmp
node_modules
.pnpm-store
app.ini
dist
*.exe
Expand Down
31 changes: 21 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,28 @@ FROM --platform=$TARGETPLATFORM uozi/nginx-ui-base:latest
ARG TARGETOS
ARG TARGETARCH
ARG TARGETVARIANT
WORKDIR /app
EXPOSE 80 443

COPY resources/docker/start.sh /app/start.sh
COPY resources/docker/nginx.conf /usr/etc/nginx/nginx.conf
COPY resources/docker/nginx-ui.conf /usr/etc/nginx/conf.d/nginx-ui.conf
COPY resources/docker/nginx-ui.conf /etc/nginx/conf.d/nginx-ui.conf
COPY nginx-ui-$TARGETOS-$TARGETARCH$TARGETVARIANT/nginx-ui /app/nginx-ui
ENV NGINX_UI_OFFICIAL_DOCKER=true

RUN cd /app && chmod a+x /app/start.sh \
&& rm -f /etc/nginx/conf.d/default.conf \
&& rm -f /usr/etc/nginx/conf.d/default.conf
# register nginx-ui service
COPY resources/docker/nginx-ui.run /etc/s6-overlay/s6-rc.d/nginx-ui/run
RUN echo 'longrun' > /etc/s6-overlay/s6-rc.d/nginx-ui/type && \
touch /etc/s6-overlay/s6-rc.d/user/contents.d/nginx-ui

ENTRYPOINT ["./start.sh"]
# copy nginx config
COPY resources/docker/nginx.conf /usr/local/etc/nginx/nginx.conf
COPY resources/docker/nginx-ui.conf /usr/local/etc/nginx/conf.d/nginx-ui.conf

# copy nginx-ui executable binary
COPY nginx-ui-$TARGETOS-$TARGETARCH$TARGETVARIANT/nginx-ui /usr/local/bin/nginx-ui

# remove default nginx config
RUN rm -f /etc/nginx/conf.d/default.conf \
&& rm -f /usr/local/etc/nginx/conf.d/default.conf

# recreate access.log and error.log
RUN rm -f /var/log/nginx/access.log && \
touch /var/log/nginx/access.log && \
rm -f /var/log/nginx/error.log && \
touch /var/log/nginx/error.log
2 changes: 1 addition & 1 deletion README-es.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ pnpm build
Primero compile la interfaz y luego ejecute el siguiente comando en el directorio raíz del proyecto.

```shell
go build -o nginx-ui -v main.go
go build -tags=jsoniter -ldflags "$LD_FLAGS -X 'github.com/0xJacky/Nginx-UI/settings.buildTime=$(date +%s)'" -o nginx-ui -v main.go
```

## Script para Linux
Expand Down
2 changes: 1 addition & 1 deletion README-vi_VN.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ pnpm build
Vui lòng build Frontend trước, sau đó thực hiện lệnh sau trong thư mục gốc của dự án.

```shell
go build -o nginx-ui -v main.go
go build -tags=jsoniter -ldflags "$LD_FLAGS -X 'github.com/0xJacky/Nginx-UI/settings.buildTime=$(date +%s)'" -o nginx-ui -v main.go
```

## Tập lệnh cho Linux
Expand Down
2 changes: 1 addition & 1 deletion README-zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ pnpm build
请先完成前端编译,再回到项目的根目录执行以下命令。

```shell
go build -o nginx-ui -v main.go
go build -tags=jsoniter -ldflags "$LD_FLAGS -X 'github.com/0xJacky/Nginx-UI/settings.buildTime=$(date +%s)'" -o nginx-ui -v main.go
```

## Linux 安装脚本
Expand Down
2 changes: 1 addition & 1 deletion README-zh_TW.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ pnpm build
請先完成前端編譯,再回到專案的根目錄執行以下命令。

```shell
go build -o nginx-ui -v main.go
go build -tags=jsoniter -ldflags "$LD_FLAGS -X 'github.com/0xJacky/Nginx-UI/settings.buildTime=$(date +%s)'" -o nginx-ui -v main.go
```

## Linux 安裝指令
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ pnpm build
Please build the app first, and then execute the following command in the project root directory.

```shell
go build -o nginx-ui -v main.go
go build -tags=jsoniter -ldflags "$LD_FLAGS -X 'github.com/0xJacky/Nginx-UI/settings.buildTime=$(date +%s)'" -o nginx-ui -v main.go
```

## Script for Linux
Expand Down
122 changes: 37 additions & 85 deletions api/cluster/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package cluster
import (
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/internal/analytic"
"github.com/0xJacky/Nginx-UI/internal/environment"
"github.com/0xJacky/Nginx-UI/internal/cluster"
"github.com/0xJacky/Nginx-UI/internal/cosy"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/0xJacky/Nginx-UI/settings"
"github.com/gin-gonic/gin"
"github.com/spf13/cast"
"net/http"
"regexp"
)

func GetEnvironment(c *gin.Context) {
Expand All @@ -27,117 +28,68 @@ func GetEnvironment(c *gin.Context) {
}

func GetEnvironmentList(c *gin.Context) {
data, err := environment.RetrieveEnvironmentList()
if err != nil {
api.ErrHandler(c, err)
return
}
c.JSON(http.StatusOK, gin.H{
"data": data,
})
}

type EnvironmentManageJson struct {
Name string `json:"name" binding:"required"`
URL string `json:"url" binding:"required"`
Token string `json:"token" binding:"required"`
OperationSync bool `json:"operation_sync"`
SyncApiRegex string `json:"sync_api_regex"`
}

func validateRegex(data EnvironmentManageJson) error {
if data.OperationSync {
_, err := regexp.Compile(data.SyncApiRegex)
return err
}
return nil
cosy.Core[model.Environment](c).
SetFussy("name").
SetEqual("enabled").
SetTransformer(func(m *model.Environment) any {
return analytic.GetNode(m)
}).PagingList()
}

func AddEnvironment(c *gin.Context) {
var json EnvironmentManageJson
if !api.BindAndValid(c, &json) {
return
}
if err := validateRegex(json); err != nil {
api.ErrHandler(c, err)
return
}

env := model.Environment{
Name: json.Name,
URL: json.URL,
Token: json.Token,
OperationSync: json.OperationSync,
SyncApiRegex: json.SyncApiRegex,
}

envQuery := query.Environment

err := envQuery.Create(&env)
if err != nil {
api.ErrHandler(c, err)
return
}

go analytic.RestartRetrieveNodesStatus()

c.JSON(http.StatusOK, env)
cosy.Core[model.Environment](c).SetValidRules(gin.H{
"name": "required",
"url": "required,url",
"token": "required",
"enabled": "omitempty,boolean",
}).ExecutedHook(func(c *cosy.Ctx[model.Environment]) {
go analytic.RestartRetrieveNodesStatus()
}).Create()
}

func EditEnvironment(c *gin.Context) {
id := cast.ToInt(c.Param("id"))

var json EnvironmentManageJson
if !api.BindAndValid(c, &json) {
return
}
if err := validateRegex(json); err != nil {
api.ErrHandler(c, err)
return
}
cosy.Core[model.Environment](c).SetValidRules(gin.H{
"name": "required",
"url": "required,url",
"token": "required",
"enabled": "omitempty,boolean",
}).ExecutedHook(func(c *cosy.Ctx[model.Environment]) {
go analytic.RestartRetrieveNodesStatus()
}).Modify()
}

func DeleteEnvironment(c *gin.Context) {
id := cast.ToInt(c.Param("id"))
envQuery := query.Environment

env, err := envQuery.FirstByID(id)
if err != nil {
api.ErrHandler(c, err)
return
}

_, err = envQuery.Where(envQuery.ID.Eq(env.ID)).Updates(&model.Environment{
Name: json.Name,
URL: json.URL,
Token: json.Token,
OperationSync: json.OperationSync,
SyncApiRegex: json.SyncApiRegex,
})

err = envQuery.DeleteByID(env.ID)
if err != nil {
api.ErrHandler(c, err)
return
}

go analytic.RestartRetrieveNodesStatus()

GetEnvironment(c)
c.JSON(http.StatusNoContent, nil)
}

func DeleteEnvironment(c *gin.Context) {
id := cast.ToInt(c.Param("id"))
envQuery := query.Environment

env, err := envQuery.FirstByID(id)
if err != nil {
api.ErrHandler(c, err)
return
}
err = envQuery.DeleteByID(env.ID)
func LoadEnvironmentFromSettings(c *gin.Context) {
err := settings.ReloadCluster()
if err != nil {
api.ErrHandler(c, err)
return
}

cluster.RegisterPredefinedNodes()

go analytic.RestartRetrieveNodesStatus()

c.JSON(http.StatusNoContent, nil)
c.JSON(http.StatusOK, gin.H{
"message": "ok",
})
}
1 change: 1 addition & 0 deletions api/cluster/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "github.com/gin-gonic/gin"
func InitRouter(r *gin.RouterGroup) {
// Environment
r.GET("environments", GetEnvironmentList)
r.POST("environments/load_from_settings", LoadEnvironmentFromSettings)
envGroup := r.Group("environment")
{
envGroup.GET("/:id", GetEnvironment)
Expand Down
22 changes: 19 additions & 3 deletions api/nginx/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/gin-gonic/gin"
"net/http"
"os"
)

func Reload(c *gin.Context) {
Expand All @@ -23,9 +24,24 @@ func Test(c *gin.Context) {
}

func Restart(c *gin.Context) {
output := nginx.Restart()
c.JSON(http.StatusOK, gin.H{
"message": output,
"level": nginx.GetLogLevel(output),
"message": "ok",
})
go nginx.Restart()
}

func Status(c *gin.Context) {
pidPath := nginx.GetPIDPath()
lastOutput := nginx.GetLastOutput()

running := true
if fileInfo, err := os.Stat(pidPath); err != nil || fileInfo.Size() == 0 { // fileInfo.Size() == 0 no process id
running = false
}

c.JSON(http.StatusOK, gin.H{
"running": running,
"message": lastOutput,
"level": nginx.GetLogLevel(lastOutput),
})
}
Loading

0 comments on commit 1ac1d15

Please sign in to comment.