feat: 增加静态网站

This commit is contained in:
zhengkunwang223 2022-11-21 16:28:51 +08:00 committed by zhengkunwang223
parent 61d7e68262
commit 8099bcb031
9 changed files with 130 additions and 39 deletions

View File

@ -10,5 +10,6 @@ services:
- ./log:/var/log/nginx
- ./conf/conf.d:/etc/nginx/conf.d/
- ./ssl:/etc/nginx/ssl
- ./www:/www/root/

View File

@ -21,7 +21,7 @@ type WebSiteCreate struct {
Alias string `json:"alias" validate:"required"`
Remark string `json:"remark"`
OtherDomains string `json:"otherDomains"`
AppType AppType `json:"appType" validate:"required"`
AppType AppType `json:"appType"`
AppInstall NewAppInstall `json:"appInstall"`
AppID uint `json:"appID"`
AppInstallID uint `json:"appInstallID"`

View File

@ -47,12 +47,18 @@ func (w WebsiteService) CreateWebsite(create dto.WebSiteCreate) error {
Protocol: constant.ProtocolHTTP,
}
if create.AppType == dto.NewApp {
install, err := ServiceGroupApp.Install(create.AppInstall.Name, create.AppInstall.AppDetailId, create.AppInstall.Params)
if err != nil {
if create.Type == "deployment" {
if create.AppType == dto.NewApp {
install, err := ServiceGroupApp.Install(create.AppInstall.Name, create.AppInstall.AppDetailId, create.AppInstall.Params)
if err != nil {
return err
}
website.AppInstallID = install.ID
}
} else {
if err := createStaticHtml(website); err != nil {
return err
}
website.AppInstallID = install.ID
}
tx, ctx := getTxAndContext()

View File

@ -43,6 +43,35 @@ func getDomain(domainStr string, websiteID uint) (model.WebSiteDomain, error) {
return model.WebSiteDomain{}, nil
}
func createStaticHtml(website *model.WebSite) error {
nginxApp, err := appRepo.GetFirst(appRepo.WithKey("nginx"))
if err != nil {
return err
}
nginxInstall, err := appInstallRepo.GetFirst(appInstallRepo.WithAppId(nginxApp.ID))
if err != nil {
return err
}
indexFolder := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "www", website.Alias)
indexPath := path.Join(indexFolder, "index.html")
indexContent := string(nginx_conf.Index)
fileOp := files.NewFileOp()
if !fileOp.Stat(indexFolder) {
if err := fileOp.CreateDir(indexFolder, 0755); err != nil {
return err
}
}
if !fileOp.Stat(indexPath) {
if err := fileOp.CreateFile(indexPath); err != nil {
return err
}
}
if err := fileOp.WriteFile(indexPath, strings.NewReader(indexContent), 0755); err != nil {
return err
}
return nil
}
func configDefaultNginx(website *model.WebSite, domains []model.WebSiteDomain) error {
nginxApp, err := appRepo.GetFirst(appRepo.WithKey("nginx"))
@ -53,14 +82,9 @@ func configDefaultNginx(website *model.WebSite, domains []model.WebSiteDomain) e
if err != nil {
return err
}
appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(website.AppInstallID))
if err != nil {
return err
}
nginxFileName := website.Alias + ".conf"
configPath := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "conf", "conf.d", nginxFileName)
nginxContent := string(nginx_conf.WebsiteDefault)
config := parser.NewStringParser(nginxContent).Parse()
servers := config.FindServers()
@ -74,8 +98,17 @@ func configDefaultNginx(website *model.WebSite, domains []model.WebSiteDomain) e
server.UpdateListen(strconv.Itoa(domain.Port), false)
}
server.UpdateServerName(serverNames)
proxy := fmt.Sprintf("http://127.0.0.1:%d", appInstall.HttpPort)
server.UpdateRootProxy([]string{proxy})
if website.Type == "deployment" {
appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(website.AppInstallID))
if err != nil {
return err
}
proxy := fmt.Sprintf("http://127.0.0.1:%d", appInstall.HttpPort)
server.UpdateRootProxy([]string{proxy})
} else {
server.UpdateRoot(path.Join("/www/root", website.Alias))
server.UpdateRootLocation()
}
config.FilePath = configPath
if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil {

View File

@ -127,26 +127,41 @@ func (s *Server) UpdateServerName(names []string) {
s.UpdateDirectives("server_name", serverNameDirective)
}
func (s *Server) UpdateRootProxy(proxy []string) {
//TODD 根据ID修改
dirs := s.FindDirectives("location")
for _, dir := range dirs {
location, ok := dir.(*Location)
if ok && location.Match == "/" {
newDir := Directive{
Name: "location",
Parameters: []string{"/"},
Block: &Block{},
}
block := &Block{}
block.Directives = append(block.Directives, &Directive{
Name: "proxy_pass",
Parameters: proxy,
})
newDir.Block = block
s.UpdateDirectiveBySecondKey("location", "/", newDir)
}
func (s *Server) UpdateRoot(path string) {
rootDir := Directive{
Name: "root",
Parameters: []string{path},
}
s.UpdateDirectives("root", rootDir)
}
func (s *Server) UpdateRootLocation() {
newDir := Directive{
Name: "location",
Parameters: []string{"/"},
Block: &Block{},
}
block := &Block{}
block.Directives = append(block.Directives, &Directive{
Name: "root",
Parameters: []string{"index.html"},
})
newDir.Block = block
}
func (s *Server) UpdateRootProxy(proxy []string) {
newDir := Directive{
Name: "location",
Parameters: []string{"/"},
Block: &Block{},
}
block := &Block{}
block.Directives = append(block.Directives, &Directive{
Name: "proxy_pass",
Parameters: proxy,
})
newDir.Block = block
s.UpdateDirectiveBySecondKey("location", "/", newDir)
}
func (s *Server) UpdateDirectiveBySecondKey(name string, key string, directive Directive) {

View File

@ -0,0 +1,34 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>恭喜,站点创建成功!</title>
<style>
.container {
width: 60%;
margin: 10% auto 0;
background-color: #f0f0f0;
padding: 2% 5%;
border-radius: 10px
}
ul {
padding-left: 20px;
}
ul li {
line-height: 2.3
}
a {
color: #20a53a
}
</style>
</head>
<body>
<div class="container">
<h1>恭喜, 站点创建成功!</h1>
<h3>这是默认index.html本页面由系统自动生成</h3>
</div>
</body>
</html>

View File

@ -15,3 +15,6 @@ var WebsiteDefault []byte
//go:embed limit.conf
var Limit []byte
//go:embed index.html
var Index []byte

View File

@ -2,6 +2,8 @@ server {
listen 80;
server_name ko.wp-1.com;
index index.php index.html index.htm default.php default.htm default.html;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
@ -10,8 +12,5 @@ server {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
location / {
proxy_pass http://127.0.0.1:8080;
}
access_log /var/log/nginx/nginx-log.log;
}

View File

@ -1,6 +1,6 @@
<template>
<div v-loading="loading">
<el-row :gutter="20">
<el-row :gutter="5">
<el-col :span="12">
<el-input v-model="req.name" @blur="searchByName"></el-input>
</el-col>
@ -12,7 +12,7 @@
<el-col :span="1">
<el-button @click="sync">{{ $t('app.sync') }}</el-button>
</el-col>
<el-col v-for="(app, index) in apps" :key="index" :xs="8" :sm="8" :lg="4">
<el-col v-for="(app, index) in apps" :key="index" :span="6">
<div @click="getAppDetail(app.id)">
<el-card :body-style="{ padding: '0px' }" class="a-card">
<el-row :gutter="24">
@ -123,13 +123,13 @@ onMounted(() => {
padding: 5px;
.icon {
width: 100%;
width: 95%;
height: 80%;
padding: 10%;
margin-top: 5px;
.image {
width: auto;
height: auto;
width: 80%;
height: 80%;
}
}