utils.go 4.2 KB

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