Skip to content

Commit

Permalink
域名管理
Browse files Browse the repository at this point in the history
  • Loading branch information
donknap committed Mar 5, 2024
1 parent 3b29d0b commit d668854
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 108 deletions.
154 changes: 154 additions & 0 deletions app/application/http/controller/domain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package controller

import (
"embed"
"errors"
"github.com/donknap/dpanel/common/dao"
"github.com/donknap/dpanel/common/entity"
"github.com/donknap/dpanel/common/service/docker"
"github.com/gin-gonic/gin"
"github.com/we7coreteam/w7-rangine-go-support/src/facade"
"github.com/we7coreteam/w7-rangine-go/src/http/controller"
"html/template"
"os"
)

var (
confRootPath = "/Users/renchao/Workspace/data/dpanel/nginx/proxy_host"
)

type Domain struct {
controller.Abstract
}

func (self Domain) Create(http *gin.Context) {
type ParamsValidate struct {
ContainerId string `json:"containerId" binding:"required"`
ServerName string `json:"serverName" binding:"required"`
Schema string `json:"schema" binding:"omitempty,oneof=http https"`
Port int32 `json:"port" binding:"required"`
EnableBlockCommonExploits bool `json:"enableBlockCommonExploits"`
EnableAssetCache bool `json:"enableAssetCache"`
EnableWs bool `json:"enableWs"`
ExtraNginx string `json:"extraNginx"`
}
params := ParamsValidate{}
if !self.Validate(http, &params) {
return
}
containerRow, err := docker.Sdk.Client.ContainerInspect(docker.Sdk.Ctx, params.ContainerId)
if err != nil {
self.JsonResponseWithError(http, err, 500)
return
}
siteDomainRow, _ := dao.SiteDomain.Where(dao.SiteDomain.ServerName.Eq(params.ServerName)).First()
if siteDomainRow != nil {
self.JsonResponseWithError(http, errors.New("域名已经存在"), 500)
return
}

var asset embed.FS
err = facade.GetContainer().NamedResolve(&asset, "asset")
if err != nil {
self.JsonResponseWithError(http, err, 500)
return
}
vhostFile, err := os.OpenFile(confRootPath+"/"+params.ServerName+".conf", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0666)
defer vhostFile.Close()

type tplParams struct {
ServerAddress string
Port int32
ServerName string
EnableBlockCommonExploits bool
EnableAssetCache bool
EnableWs bool
ExtraNginx string
}
parser, err := template.ParseFS(asset, "asset/nginx/*.tpl")
err = parser.ExecuteTemplate(vhostFile, "vhost.tpl", tplParams{
ServerAddress: containerRow.NetworkSettings.DefaultNetworkSettings.IPAddress,
Port: params.Port,
ServerName: params.ServerName,
EnableBlockCommonExploits: params.EnableBlockCommonExploits,
EnableWs: params.EnableWs,
EnableAssetCache: params.EnableAssetCache,
ExtraNginx: params.ExtraNginx,
})
if err != nil {
self.JsonResponseWithError(http, err, 500)
return
}

err = dao.SiteDomain.Create(&entity.SiteDomain{
ServerName: params.ServerName,
Port: params.Port,
ContainerID: containerRow.ID,
Schema: params.Schema,
})
if err != nil {
self.JsonResponseWithError(http, err, 500)
return
}

self.JsonSuccessResponse(http)
return
}

func (self Domain) GetList(http *gin.Context) {
type ParamsValidate struct {
ServerName string `json:"serverName"`
Port int32 `json:"port"`
Page int `json:"page,default=1" binding:"omitempty,gt=0"`
PageSize int `json:"pageSize" binding:"omitempty"`
}
params := ParamsValidate{}
if !self.Validate(http, &params) {
return
}
if params.Page < 1 {
params.Page = 1
}
if params.PageSize < 1 {
params.PageSize = 10
}

query := dao.SiteDomain.Order(dao.SiteDomain.ID.Desc())
if params.ServerName != "" {
query = query.Where(dao.SiteDomain.ServerName.Like("%" + params.ServerName + "%"))
}
if params.Port > 0 {
query = query.Where(dao.SiteDomain.Port.Eq(params.Port))
}
list, total, _ := query.FindByPage((params.Page-1)*params.PageSize, params.PageSize)

self.JsonResponseWithoutError(http, gin.H{
"total": total,
"page": params.Page,
"list": list,
})
return
}

func (self Domain) Delete(http *gin.Context) {
type ParamsValidate struct {
Id []int32 `json:"id" binding:"required"`
}
params := ParamsValidate{}
if !self.Validate(http, &params) {
return
}
list, _ := dao.SiteDomain.Where(dao.SiteDomain.ID.In(params.Id...)).Find()
for _, item := range list {
confFile := confRootPath + "/" + item.ServerName + ".conf"
_ = os.Remove(confFile)
}
_, err := dao.SiteDomain.Where(dao.SiteDomain.ID.In(params.Id...)).Delete()
if err != nil {
self.JsonResponseWithError(http, err, 500)
return
}
self.JsonSuccessResponse(http)
return

}
61 changes: 0 additions & 61 deletions app/application/http/controller/site.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package controller

import (
"embed"
"errors"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
Expand All @@ -13,13 +12,10 @@ import (
"github.com/donknap/dpanel/common/service/docker"
"github.com/donknap/dpanel/common/service/notice"
"github.com/gin-gonic/gin"
"github.com/we7coreteam/w7-rangine-go-support/src/facade"
"github.com/we7coreteam/w7-rangine-go/src/http/controller"
"gorm.io/gorm"
"html/template"
"log/slog"
"net"
"os"
)

type Site struct {
Expand Down Expand Up @@ -281,60 +277,3 @@ func (self Site) Delete(http *gin.Context) {
}
return
}

func (self Site) CreateDomain(http *gin.Context) {
type ParamsValidate struct {
ContainerId string `json:"containerId" binding:"required"`
Domain string `json:"domain" binding:"required"`
Schema string `json:"schema" binding:"omitempty,oneof=http https"`
Port int `json:"port" binding:"required"`
EnableBlockCommonExploits bool `json:"enableBlockCommonExploits"`
EnableAssetCache bool `json:"enableAssetCache"`
EnableWs bool `json:"enableWs"`
ExtraNginx string `json:"extraNginx"`
}
params := ParamsValidate{}
if !self.Validate(http, &params) {
return
}
containerRow, err := docker.Sdk.Client.ContainerInspect(docker.Sdk.Ctx, params.ContainerId)
if err != nil {
self.JsonResponseWithError(http, err, 500)
return
}

var asset embed.FS
err = facade.GetContainer().NamedResolve(&asset, "asset")
if err != nil {
self.JsonResponseWithError(http, err, 500)
return
}
confRootPath := "/Users/renchao/Workspace/data/dpanel/nginx/proxy_host"
vhostFile, err := os.OpenFile(confRootPath+"/"+params.Domain+".conf", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0666)
defer vhostFile.Close()

type tplParams struct {
ServerAddress string
Port int
ServerName string
EnableBlockCommonExploits bool
EnableAssetCache bool
EnableWs bool
ExtraNginx string
}
parser, err := template.ParseFS(asset, "asset/nginx/*.tpl")
err = parser.ExecuteTemplate(vhostFile, "vhost.tpl", tplParams{
ServerAddress: containerRow.NetworkSettings.DefaultNetworkSettings.IPAddress,
Port: params.Port,
ServerName: params.Domain,
EnableBlockCommonExploits: params.EnableBlockCommonExploits,
EnableWs: params.EnableWs,
EnableAssetCache: params.EnableAssetCache,
ExtraNginx: params.ExtraNginx,
})
if err != nil {
self.JsonResponseWithError(http, err, 500)
return
}

}
4 changes: 3 additions & 1 deletion app/application/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ func (provider *Provider) Register(httpServer *http_server.Server) {
cors.POST("/app/site/get-list", controller.Site{}.GetList)
cors.POST("/app/site/get-detail", controller.Site{}.GetDetail)
cors.POST("/app/site/delete", controller.Site{}.Delete)
cors.POST("/app/site/create-domain", controller.Site{}.CreateDomain)
cors.POST("/app/site/create-domain", controller.Domain{}.Create)
cors.POST("/app/site/get-domain-list", controller.Domain{}.GetList)
cors.POST("/app/site/delete-domain", controller.Domain{}.Delete)

// 容器相关
cors.POST("/app/container/status", controller.Container{}.Status)
Expand Down
100 changes: 54 additions & 46 deletions common/dao/gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions database/gen-model.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ relation:
- table: ims_registry
- table: ims_event
- table: ims_notice
- table: ims_site_domain
Binary file modified dpanel.db
Binary file not shown.

0 comments on commit d668854

Please sign in to comment.