Skip to content

Commit

Permalink
修改版本
Browse files Browse the repository at this point in the history
> 过程化改结构化结构紧密
- 支持直接 `command` -v [vhost目录]
- 支持 `command` -nginx [nginx执行文件]
  • Loading branch information
lampking committed May 28, 2020
1 parent 715bfa2 commit b371c0f
Show file tree
Hide file tree
Showing 7 changed files with 411 additions and 339 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@

# Dependency directories (remove the comment below to include it)
# vendor/
.DS_Store
91 changes: 13 additions & 78 deletions configure.go
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
}
40 changes: 21 additions & 19 deletions console.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,34 @@ const (
textWhite
)

func Black(str string) string {
return textColor(textBlack, str)
type Console struct {}

func (this *Console) textColor(color int, str string) string {
return fmt.Sprintf("\x1b[0;%dm%s\x1b[0m", color, str)
}

func Red(str string) string {
return textColor(textRed, str)
func (this *Console) Black(str string) string {
return this.textColor(textBlack, str)
}
func Yellow(str string) string {
return textColor(textYellow, str)

func (this *Console) Red(str string) string {
return this.textColor(textRed, str)
}
func Green(str string) string {
return textColor(textGreen, str)
func (this *Console) Yellow(str string) string {
return this.textColor(textYellow, str)
}
func Cyan(str string) string {
return textColor(textCyan, str)
func (this *Console) Green(str string) string {
return this.textColor(textGreen, str)
}
func Blue(str string) string {
return textColor(textBlue, str)
func (this *Console) Cyan(str string) string {
return this.textColor(textCyan, str)
}
func Purple(str string) string {
return textColor(textPurple, str)
func (this *Console) Blue(str string) string {
return this.textColor(textBlue, str)
}
func White(str string) string {
return textColor(textWhite, str)
func (this *Console) Purple(str string) string {
return this.textColor(textPurple, str)
}

func textColor(color int, str string) string {
return fmt.Sprintf("\x1b[0;%dm%s\x1b[0m", color, str)
func (this *Console) White(str string) string {
return this.textColor(textWhite, str)
}
171 changes: 30 additions & 141 deletions main.go
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()
}
Loading

0 comments on commit b371c0f

Please sign in to comment.