utils.go 4.0 KB

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