glogcenter/glc/www/html/static_file_handle.go
2022-06-30 19:42:12 +08:00

67 lines
1.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package html
import (
"glc/cmn"
"glc/gweb"
"glc/ldb/conf"
"glc/www"
"log"
"os"
)
// [/]重定向到[/glc/]
func RedirectToHomeController(req *gweb.HttpRequest) *gweb.HttpResult {
defer req.Redirect(conf.GetContextPath() + "/")
return nil
}
// 响应请求[/glc/]读取index.html返回
func HomeIndexHtmlController(req *gweb.HttpRequest) *gweb.HttpResult {
file, err := www.Static.ReadFile("web/dist/index.html")
if err != nil && os.IsNotExist(err) {
req.ResponseData(404, "text/html", cmn.StringToBytes("not found"))
} else {
req.ResponseData(200, "text/html", file)
}
return nil
}
// 响应 *.html/*.css/*.js/*.png 等静态文件请求
func StaticFileController(req *gweb.HttpRequest) *gweb.HttpResult {
urlPath := req.RequestUrlPath()
contentType := getContentType(urlPath)
file, err := www.Static.ReadFile(getStaticFilePath(urlPath))
if err != nil && os.IsNotExist(err) {
req.ResponseData(404, contentType, cmn.StringToBytes("not found"))
} else {
req.ResponseData(200, contentType, file)
}
return nil
}
// urlPath如[/glc/assets/index.f0b375ee.js]
func getStaticFilePath(urlPath string) string {
path := cmn.SubStringRune(urlPath, len(conf.GetContextPath()), len(urlPath))
return "web/dist" + path
}
func getContentType(urlPath string) string {
if cmn.EndwithsRune(urlPath, ".html") {
return "text/html"
} else if cmn.EndwithsRune(urlPath, ".css") {
return "text/css"
} else if cmn.EndwithsRune(urlPath, ".js") {
return "application/x-javascript"
} else if cmn.EndwithsRune(urlPath, ".png") {
return "image/png"
} else if cmn.EndwithsRune(urlPath, ".ico") {
return "image/x-icon"
} else {
log.Println("未识别出ContentType按text/html处理", urlPath)
return "text/html"
}
}