diff --git a/.gitignore b/.gitignore index 66fd13c..180e910 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ # Dependency directories (remove the comment below to include it) # vendor/ +.DS_Store diff --git a/configure.go b/configure.go index c90f49b..02e1a3a 100644 --- a/configure.go +++ b/configure.go @@ -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 +} \ No newline at end of file diff --git a/console.go b/console.go index fd7dba8..ce59ec5 100644 --- a/console.go +++ b/console.go @@ -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) } \ No newline at end of file diff --git a/main.go b/main.go index 4836b12..1e5b593 100644 --- a/main.go +++ b/main.go @@ -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() } diff --git a/mutual.go b/mutual.go new file mode 100644 index 0000000..4029615 --- /dev/null +++ b/mutual.go @@ -0,0 +1,130 @@ +package main + +import ( + "bufio" + "fmt" + "html/template" + "os" + "os/exec" +) + +var input = bufio.NewScanner(os.Stdin) + +type Mutual struct{} + +func (this *Mutual) isFileExist(path string) (bool) { + //fileInfo, err := os.Stat(path) + _, err := os.Stat(path) + if os.IsNotExist(err) { + return false + } + //我这里判断了如果是0也算不存在 + //if fileInfo.Size() == 0 { + // return false + //} + if err == nil { + return true + } + return false +} +func (this *Mutual) build() { + var file string + tpl, err := template.New("vhost").Parse(Vhost) + if err != nil { + fmt.Println("点配置文件", console.Red("[Fail]")) + os.Exit(2) + } + file = configure.VHOST + "/" + configure.ServerName + ".conf" + ioFile, ioErr := os.OpenFile(file, os.O_RDWR|os.O_CREATE, 0666) + if ioErr != nil { + fmt.Println("点配置文件", console.Red("[Fail]")) + } + err = tpl.Execute(ioFile, StdIn{ + ServerName: configure.ServerName, + RootPath: configure.RootPath, + LogPath: configure.LogPath, + ErrPath: configure.ErrorPath, + }) + defer ioFile.Close() +} +func (this *Mutual) ErrorPage() { + page.BadRequest(configure.RootPath + configure.ErrorPath + "/400.html") + page.Unauthorized(configure.RootPath + configure.ErrorPath + "/401.html") + page.Forbidden(configure.RootPath + configure.ErrorPath + "/403.html") + page.NotFound(configure.RootPath + configure.ErrorPath + "/404.html") + page.InternalServerError(configure.RootPath + configure.ErrorPath + "/500.html") + page.NotImplemented(configure.RootPath + configure.ErrorPath + "/501.html") + page.BadRequest(configure.RootPath + configure.ErrorPath + "/502.html") + page.ServiceUnavailable(configure.RootPath + configure.ErrorPath + "/503.html") + page.GatewayTimeout(configure.RootPath + configure.ErrorPath + "/504.html") +} +func (this *Mutual) ServerName() { + fmt.Print(console.Green("请输入站点名称: ")) + input.Scan() + configure.ServerName = input.Text() + if len(configure.ServerName) <= 0 { + fmt.Println("站点名称不能为空", console.Red("[Error]")) + os.Exit(2) + } +} +func (this *Mutual) RootPath() { + fmt.Print(console.Green("请输入站点主路径:")) + input.Scan() + configure.RootPath = input.Text() + if len(configure.RootPath) <= 0 { + fmt.Println("站点主路径不能为空", console.Red("[Error]")) + os.Exit(2) + } + err := os.MkdirAll(configure.RootPath+"/public", os.ModePerm) + if err != nil { + fmt.Println("创建站点目录", console.Red("[Fail]")) + os.Exit(2) + } + fmt.Println("创建站点目录", console.Blue("[Ok]")) +} +func (this *Mutual) LogPath() { + fmt.Print(console.Green("请输入日志目录名称:")) + input.Scan() + if len(input.Text()) <= 0 { + fmt.Println("创建认日志目录", console.Yellow("[logs]")) + configure.LogPath = configure.RootPath + "/logs" + } else { + configure.LogPath = configure.RootPath + "/" + input.Text() + } + err := os.MkdirAll(configure.LogPath, os.ModePerm) + if err != nil { + fmt.Println("创建认日志目录", configure.LogPath, console.Red("[Fail]")) + os.Exit(2) + } + fmt.Println("创建认日志目录", configure.LogPath, console.Blue("[Ok]")) +} +func (this *Mutual) ErrorPath() { + fmt.Print(console.Green("请输入错误页面目录名称:")) + input.Scan() + if len(input.Text()) <= 0 { + fmt.Println("使用默认错误目录", console.Yellow("[error_page]")) + configure.ErrorPath = "/error_page" + } else { + configure.ErrorPath = "/" + input.Text() + } + err := os.MkdirAll(configure.RootPath + configure.ErrorPath, os.ModePerm) + if err != nil { + fmt.Println("创建错误页面目录", configure.ErrorPath, console.Red("[Fail]")) + os.Exit(2) + } + fmt.Println("创建错误页面目录", configure.ErrorPath, console.Blue("[Ok]")) +} +func (this *Mutual) reloadNginx() { + out, err := exec.Command( configure.NGINX, "-t" ).Output() + if err != nil { + fmt.Println("检查nginx配置", console.Red("[Error]")) + } + fmt.Println(console.White("检查结果如下:")) + os.Stdout.Write(out) + out, err = exec.Command(configure.NGINX, "-s", "reload").Output() + if err != nil { + fmt.Println("重新启动Nginx", console.Red("[Fail]")) + } + os.Stdout.Write(out) + fmt.Println("重新启动Nginx", console.Green("[Success]")) +} \ No newline at end of file diff --git a/page.go b/page.go index cb31806..994a378 100644 --- a/page.go +++ b/page.go @@ -1,117 +1,117 @@ package main -type pageContent struct { +import ( + "fmt" + "html/template" + "os" +) + +type Content struct { Title string Keywords string Code int Message string } -var ( - BadRequest pageContent = pageContent{ - Title: "400 Bad Request", - Keywords: "Bad Request", - Code: 400, - Message: "400 Bad Request", +type Page struct{} + +func (this *Page) render(file string, content Content) { + tpl, err := template.New("page").Parse(ErrorPage) + ioFile, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE, 0666) + if err != nil { + fmt.Println(console.Red("写入错误页面失败")) + os.Exit(2) } - Unauthorized pageContent = pageContent{ - Title: "401 Unauthorized", + tpl.Execute(ioFile, content) + defer ioFile.Close() + fmt.Println("创建页面[ ", console.Red(file), " ]成功") +} + +// 400 +func (this *Page) BadRequest(file string) { + this.render(file, Content{ + Title: "400 Bad Request", + Keywords: "Bad Request", + Code: 400, + Message: "400 Bad Request", + }) +} + +// 401 +func (this *Page) Unauthorized(file string) { + this.render(file, Content{ + Title: "401 Unauthorized", Keywords: "Unauthorized", - Code: 401, - Message: "401 Unauthorized", - } - Forbidden pageContent = pageContent{ - Title: "403 Forbidden", + Code: 401, + Message: "401 Unauthorized", + }) +} + +// 403 +func (this *Page) Forbidden(file string) { + this.render(file, Content{ + Title: "403 Forbidden", Keywords: "Forbidden", - Code: 403, - Message: "403 Forbidden", - } - NotFound pageContent = pageContent{ - Title: "404 Not Found", + Code: 403, + Message: "403 Forbidden", + }) +} + +// 404 +func (this *Page) NotFound(file string) { + this.render(file, Content{ + Title: "404 Not Found", Keywords: "Not Found", - Code: 404, - Message: "404 Not Found", - } - InternalServerError = pageContent{ - Title: "500 Internal Server Error", + Code: 404, + Message: "404 Not Found", + }) +} + +// 500 +func (this *Page) InternalServerError(file string) { + this.render(file, Content{ + Title: "500 Internal Server Error", Keywords: "Internal Server Error", - Code: 500, - Message: "500 Internal Server Error", - } - NotImplemented = pageContent{ - Title: "501 Not Implemented", - Keywords: "Not Implemented", - Code: 501, - Message: "501 Not Implemented", - } - BadGateway = pageContent{ - Title: "502 Bad Gateway", - Keywords: "Bad Gateway", - Code: 502, - Message: "502 Bad Gateway", - } - ServiceUnavailable = pageContent{ - Title: "503 Service Unavailable", - Keywords: "Service Unavailable", - Code: 503, - Message: "503 Service Unavailable", - } - GatewayTimeout = pageContent{ - Title: "504 Gateway Timeout", - Keywords: "Gateway Timeout", - Code: 504, - Message: "504 Gateway Timeout", - } -) + Code: 500, + Message: "500 Internal Server Error", + }) +} -var ErrorPage string = ` - - +// 501 +func (this *Page) NotImplemented(file string) { + this.render(file, Content{ + Title: "501 Not Implemented", + Keywords: "Not Implemented", + Code: 501, + Message: "501 Not Implemented", + }) +} -
-{{.Message}}
- - -{{.Message}}
+ + +