-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
> 过程化改结构化结构紧密 - 支持直接 `command` -v [vhost目录] - 支持 `command` -nginx [nginx执行文件]
- Loading branch information
Showing
7 changed files
with
411 additions
and
339 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ | |
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,17 @@ | ||
package main | ||
|
||
type establish struct { | ||
// 站点名称 | ||
Site string | ||
// 站点存储路径 | ||
Path string | ||
// 站点日志路径 | ||
Log string | ||
// 站点日志路径 | ||
Error string | ||
type Configure struct { | ||
NGINX string | ||
VHOST string | ||
ServerName string | ||
RootPath string | ||
LogPath string | ||
ErrorPath string | ||
} | ||
|
||
var Configure establish = establish{ | ||
Site: "", | ||
Path: "", | ||
Log: "", | ||
Error: "", | ||
} | ||
|
||
var Nginx string = ` | ||
server { | ||
# 监听端口义 | ||
listen 80; | ||
# 主机名 | ||
server_name {{.Site}}; # 站点名称 | ||
autoindex on; # 目录索引 | ||
autoindex_exact_size off; # 显示文件大小 | ||
autoindex_localtime on; # 显示本地时间 | ||
charset utf-8; # 字符集 | ||
gzip on; # 开启gzip | ||
# 日志位置定义 | ||
# access_log off; | ||
access_log {{.Log}}/access.log; # 访问日期 | ||
error_log {{.Log}}/error.log; # 错误日志 | ||
# 定义根目录和索引文件 | ||
root {{.Path}}/public; | ||
index index.php index.html index.htm default.php default.html default.htm; | ||
error_page 403 /error_page/403.html; | ||
error_page 404 /error_page/404.html; | ||
error_page 500 /error_page/500.html; | ||
error_page 501 /error_page/501.html; | ||
error_page 502 /error_page/502.html; | ||
error_page 503 /error_page/503.html; | ||
error_page 503 /error_page/503.html; | ||
error_page 504 /error_page/504.html; | ||
location /error_page { | ||
root {{.Path}}; | ||
} | ||
# 图片压缩配置 | ||
location ~*\\.(jpg|jpeg|gif|png|ico|swf)$ { | ||
access_log off; # 关闭图片访问日志 | ||
gzip off; | ||
} | ||
location ~*\\.(css|js|less)$ { | ||
access_log off; # 关闭脚本访问日志 | ||
gzip on; | ||
} | ||
# 加载 rewrite 规则 | ||
#location / { | ||
# if (!-e \$request_filename) { | ||
# rewrite ^(.*)\$ /index.php?s=\$1 last; | ||
# rewrite ^.*$ /index.html last; | ||
# } | ||
#} | ||
location ~ \\.php$ { | ||
fastcgi_pass 127.0.0.1:9000; | ||
fastcgi_index index.php; | ||
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name; | ||
include fastcgi_params; | ||
} | ||
} | ||
` | ||
type StdIn struct { | ||
ServerName string | ||
RootPath string | ||
LogPath string | ||
ErrPath string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,157 +1,46 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"flag" | ||
"fmt" | ||
"html/template" | ||
"os" | ||
) | ||
|
||
// 控制台输出颜色 | ||
var console Console = Console{} | ||
var page Page = Page{} | ||
var configure Configure = Configure{ | ||
NGINX: "/opt/nginx/sbin/nginx", | ||
VHOST: "/opt/nginx/vhost/", | ||
} | ||
var mutual Mutual = Mutual{} | ||
|
||
var ( | ||
VHOST = "/opt/nginx/vhost/" | ||
NGINX = "/opt/nginx/sbin/nginx" | ||
vhost string | ||
nginx string | ||
help bool | ||
) | ||
|
||
var err error | ||
var input = bufio.NewScanner(os.Stdin) | ||
|
||
func main() { | ||
if len(os.Args) <= 1 { | ||
siteName() | ||
path() | ||
errpage() | ||
createErrorPage() | ||
logpath() | ||
create() | ||
} | ||
} | ||
func createErrorPage() { | ||
var ( | ||
file400 string = Configure.Error + "/400.html" | ||
file401 string = Configure.Error + "/401.html" | ||
file403 string = Configure.Error + "/403.html" | ||
file404 string = Configure.Error + "/404.html" | ||
file500 string = Configure.Error + "/500.html" | ||
file501 string = Configure.Error + "/501.html" | ||
file502 string = Configure.Error + "/502.html" | ||
file503 string = Configure.Error + "/503.html" | ||
file504 string = Configure.Error + "/504.html" | ||
) | ||
render(file400, NotFound) | ||
render(file401, Unauthorized) | ||
render(file403, Forbidden) | ||
render(file404, NotFound) | ||
render(file500, InternalServerError) | ||
render(file501, NotImplemented) | ||
render(file502, BadGateway) | ||
render(file503, ServiceUnavailable) | ||
render(file504, GatewayTimeout) | ||
} | ||
func render(file string, content pageContent) { | ||
tpl, err := template.New("errpage").Parse(ErrorPage) | ||
ioFile, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE, 0666) | ||
if err != nil { | ||
fmt.Println(Red("写入错误页面失败")) | ||
} | ||
tpl.Execute(ioFile, content) | ||
defer ioFile.Close() | ||
} | ||
func isFileExist(path string) (bool, error) { | ||
fileInfo, err := os.Stat(path) | ||
|
||
if os.IsNotExist(err) { | ||
return false, nil | ||
} | ||
//我这里判断了如果是0也算不存在 | ||
if fileInfo.Size() == 0 { | ||
return false, nil | ||
} | ||
if err == nil { | ||
return true, nil | ||
} | ||
return false, err | ||
} | ||
|
||
func create() { | ||
var ( | ||
file string | ||
) | ||
tpl, err := template.New("vhost").Parse(Nginx) | ||
if err != nil { | ||
fmt.Println(Red("解析站点配置文件失败")) | ||
} | ||
file = VHOST + "/" + Configure.Site + ".conf" | ||
|
||
fileio, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE, 0666) | ||
|
||
if err != nil { | ||
fmt.Println(Red("读写站点配置文件失败")) | ||
} | ||
err = tpl.Execute(fileio, Configure) | ||
if err != nil { | ||
fmt.Println(Red("创建站点配置文件失败")) | ||
} | ||
defer fileio.Close() | ||
fmt.Println("创建站点", Blue("[OK]")) | ||
fmt.Println(Yellow("现在您可以重启站点")) | ||
} | ||
|
||
func siteName() { | ||
fmt.Print(Green("请输入站点名称:")) | ||
input.Scan() | ||
Configure.Site = input.Text() | ||
flag.StringVar(&vhost, "v", configure.VHOST, "配置 nginx vhost 目录位置") | ||
flag.BoolVar(&help, "h", false, "查看帮助信息") | ||
flag.StringVar(&nginx, "nginx", configure.NGINX, "配置 nginx 可执行文件位置") | ||
flag.Parse() | ||
|
||
if len(Configure.Site) <= 0 { | ||
fmt.Println("站点名称不能为空", Red("[Error]")) | ||
configure.VHOST = vhost | ||
configure.NGINX = nginx | ||
if !mutual.isFileExist(configure.VHOST) { | ||
fmt.Println("虚拟站点配置目录找不到", console.Yellow("[-v vhost_dir]")) | ||
os.Exit(2) | ||
} | ||
} | ||
func path() { | ||
fmt.Print(Green("请输入站点主路径:")) | ||
input.Scan() | ||
Configure.Path = input.Text() | ||
if len(Configure.Path) <= 0 { | ||
fmt.Println("站点主路径不能为空", Red("[Error]")) | ||
os.Exit(2) | ||
} | ||
err = os.MkdirAll(Configure.Path+"/public", os.ModePerm) | ||
if err != nil { | ||
fmt.Println("创建站点目录", Red("[Fail]")) | ||
os.Exit(2) | ||
} | ||
fmt.Println("创建站点目录", Blue("[Ok]")) | ||
} | ||
func errpage() { | ||
fmt.Print(Green("请输入错误页面目录名称:")) | ||
input.Scan() | ||
if len(input.Text()) <= 0 { | ||
fmt.Println("使用默认错误目录", Yellow("[error_page]")) | ||
Configure.Error = Configure.Path + "/error_page" | ||
} else { | ||
Configure.Error = Configure.Path + "/" + input.Text() | ||
} | ||
|
||
err = os.MkdirAll(Configure.Error, os.ModePerm) | ||
if err != nil { | ||
fmt.Println("创建错误页面目录", Configure.Error, Red("[Fail]")) | ||
os.Exit(2) | ||
} | ||
fmt.Println("创建错误页面目录", Configure.Error, Blue("[Ok]")) | ||
} | ||
func logpath() { | ||
|
||
fmt.Print(Green("请输入日志目录名称:")) | ||
input.Scan() | ||
if len(input.Text()) <= 0 { | ||
fmt.Println("创建认日志目录", Yellow("[logs]")) | ||
Configure.Log = Configure.Path + "/logs" | ||
} else { | ||
Configure.Log = Configure.Path + "/" + input.Text() | ||
} | ||
err = os.MkdirAll(Configure.Log, os.ModePerm) | ||
if err != nil { | ||
fmt.Println("创建认日志目录", Configure.Log, Red("[Fail]")) | ||
os.Exit(2) | ||
if help { | ||
flag.PrintDefaults() | ||
} | ||
fmt.Println("创建认日志目录", Configure.Log, Blue("[Ok]")) | ||
mutual.ServerName() | ||
mutual.RootPath() | ||
mutual.ErrorPath() | ||
mutual.ErrorPage() | ||
mutual.LogPath() | ||
mutual.build() | ||
mutual.reloadNginx() | ||
} |
Oops, something went wrong.