utils.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package utils
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "html/template"
  6. "net/http"
  7. "os"
  8. "regexp"
  9. "strings"
  10. "golang-fave/assets"
  11. "golang-fave/consts"
  12. )
  13. func IsFileExists(filename string) bool {
  14. if _, err := os.Stat(filename); !os.IsNotExist(err) {
  15. if err == nil {
  16. return true
  17. }
  18. }
  19. return false
  20. }
  21. func IsDir(filename string) bool {
  22. if st, err := os.Stat(filename); !os.IsNotExist(err) {
  23. if err == nil {
  24. if st.Mode().IsDir() {
  25. return true
  26. }
  27. }
  28. }
  29. return false
  30. }
  31. func IsDirExists(path string) bool {
  32. if IsFileExists(path) && IsDir(path) {
  33. return true
  34. }
  35. return false
  36. }
  37. func IsValidEmail(email string) bool {
  38. regexpe := regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`)
  39. return regexpe.MatchString(email)
  40. }
  41. func FixPath(path string) string {
  42. newPath := strings.TrimSpace(path)
  43. if len(newPath) <= 0 {
  44. return newPath
  45. }
  46. if newPath[len(newPath)-1] == '/' || newPath[len(newPath)-1] == '\\' {
  47. newPath = newPath[0 : len(newPath)-2]
  48. }
  49. return newPath
  50. }
  51. func ExtractHostPort(host string, https bool) (string, string) {
  52. h := host
  53. p := "80"
  54. if https {
  55. p = "443"
  56. }
  57. i := strings.Index(h, ":")
  58. if i > -1 {
  59. p = h[i+1:]
  60. h = h[0:i]
  61. }
  62. return h, p
  63. }
  64. func GetAssetsUrl(filename string) string {
  65. return "/" + filename + "?v=" + consts.AssetsVersion
  66. }
  67. func GetTmplSystemData() consts.TmplSystem {
  68. return consts.TmplSystem{
  69. PathIcoFav: GetAssetsUrl(consts.AssetsSysFaveIco),
  70. PathSvgLogo: GetAssetsUrl(consts.AssetsSysLogoSvg),
  71. PathCssStyles: GetAssetsUrl(consts.AssetsSysStylesCss),
  72. PathCssCpStyles: GetAssetsUrl(consts.AssetsCpStylesCss),
  73. PathCssBootstrap: GetAssetsUrl(consts.AssetsBootstrapCss),
  74. PathJsJquery: GetAssetsUrl(consts.AssetsJqueryJs),
  75. PathJsPopper: GetAssetsUrl(consts.AssetsPopperJs),
  76. PathJsBootstrap: GetAssetsUrl(consts.AssetsBootstrapJs),
  77. PathJsCpScripts: GetAssetsUrl(consts.AssetsCpScriptsJs),
  78. }
  79. }
  80. func GetTmplError(err error) consts.TmplError {
  81. return consts.TmplError{
  82. ErrorMessage: err.Error(),
  83. }
  84. }
  85. func GetMd5(str string) string {
  86. hasher := md5.New()
  87. hasher.Write([]byte(str))
  88. return hex.EncodeToString(hasher.Sum(nil))
  89. }
  90. func SystemRenderTemplate(w http.ResponseWriter, c []byte, d interface{}) {
  91. tmpl, err := template.New("template").Parse(string(c))
  92. if err != nil {
  93. SystemErrorPageEngine(w, err)
  94. return
  95. }
  96. w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  97. w.Header().Set("Content-Type", "text/html")
  98. tmpl.Execute(w, consts.TmplData{
  99. System: GetTmplSystemData(),
  100. Data: d,
  101. })
  102. }
  103. func SystemErrorPageEngine(w http.ResponseWriter, err error) {
  104. if tmpl, e := template.New("template").Parse(string(assets.TmplPageErrorEngine)); e == nil {
  105. w.WriteHeader(http.StatusInternalServerError)
  106. w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  107. w.Header().Set("Content-Type", "text/html")
  108. tmpl.Execute(w, consts.TmplData{
  109. System: GetTmplSystemData(),
  110. Data: GetTmplError(err),
  111. })
  112. return
  113. }
  114. w.WriteHeader(http.StatusInternalServerError)
  115. w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  116. w.Header().Set("Content-Type", "text/html")
  117. w.Write([]byte("<h1>Critical engine error</h1>"))
  118. w.Write([]byte("<h2>" + err.Error() + "</h2>"))
  119. }
  120. func SystemErrorPage404(w http.ResponseWriter) {
  121. tmpl, err := template.New("template").Parse(string(assets.TmplPageError404))
  122. if err != nil {
  123. SystemErrorPageEngine(w, err)
  124. return
  125. }
  126. w.WriteHeader(http.StatusNotFound)
  127. w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  128. w.Header().Set("Content-Type", "text/html")
  129. tmpl.Execute(w, consts.TmplData{
  130. System: GetTmplSystemData(),
  131. Data: nil,
  132. })
  133. }
  134. func UrlToArray(url string) []string {
  135. url_buff := url
  136. if len(url_buff) >= 1 && url_buff[:1] == "/" {
  137. url_buff = url_buff[1:]
  138. }
  139. if len(url_buff) >= 1 && url_buff[len(url_buff)-1:] == "/" {
  140. url_buff = url_buff[:len(url_buff)-1]
  141. }
  142. if url_buff == "" {
  143. return []string{}
  144. } else {
  145. return strings.Split(url_buff, "/")
  146. }
  147. }