utils.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package utils
  2. import (
  3. "html/template"
  4. "net/http"
  5. "os"
  6. "strings"
  7. "golang-fave/assets"
  8. "golang-fave/consts"
  9. )
  10. func IsFileExists(filename string) bool {
  11. if _, err := os.Stat(filename); !os.IsNotExist(err) {
  12. if err == nil {
  13. return true
  14. }
  15. }
  16. return false
  17. }
  18. func IsDir(filename string) bool {
  19. if st, err := os.Stat(filename); !os.IsNotExist(err) {
  20. if err == nil {
  21. if st.Mode().IsDir() {
  22. return true
  23. }
  24. }
  25. }
  26. return false
  27. }
  28. func IsHostDirExists(path string) bool {
  29. if IsFileExists(path) && IsDir(path) {
  30. return true
  31. }
  32. return false
  33. }
  34. func FixPath(path string) string {
  35. newPath := strings.TrimSpace(path)
  36. if len(newPath) <= 0 {
  37. return newPath
  38. }
  39. if newPath[len(newPath)-1] == '/' || newPath[len(newPath)-1] == '\\' {
  40. newPath = newPath[0 : len(newPath)-2]
  41. }
  42. return newPath
  43. }
  44. func ExtractHostPort(host string, https bool) (string, string) {
  45. h := host
  46. p := "80"
  47. if https {
  48. p = "443"
  49. }
  50. i := strings.Index(h, ":")
  51. if i > -1 {
  52. p = h[i+1:]
  53. h = h[0:i]
  54. }
  55. return h, p
  56. }
  57. func GetAssetsUrl(filename string) string {
  58. return "/" + filename + "?v=" + consts.AssetsVersion
  59. }
  60. func GetTmplSystemData() consts.TmplSystem {
  61. return consts.TmplSystem{
  62. PathIcoFav: GetAssetsUrl(consts.AssetsSysFaveIco),
  63. PathSvgLogo: GetAssetsUrl(consts.AssetsSysLogoSvg),
  64. PathCssStyles: GetAssetsUrl(consts.AssetsSysStylesCss),
  65. PathCssCpStyles: GetAssetsUrl(consts.AssetsCpStylesCss),
  66. PathCssBootstrap: GetAssetsUrl(consts.AssetsBootstrapCss),
  67. PathJsJquery: GetAssetsUrl(consts.AssetsJqueryJs),
  68. PathJsPopper: GetAssetsUrl(consts.AssetsPopperJs),
  69. PathJsBootstrap: GetAssetsUrl(consts.AssetsBootstrapJs),
  70. PathJsCpScripts: GetAssetsUrl(consts.AssetsCpScriptsJs),
  71. }
  72. }
  73. func GetTmplError(err error) consts.TmplError {
  74. return consts.TmplError{
  75. ErrorMessage: err.Error(),
  76. }
  77. }
  78. func SystemErrorPage(w http.ResponseWriter, err error) {
  79. if tmpl, errr := template.New("template").Parse(string(assets.TmplPageErrorEngine)); errr == nil {
  80. w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  81. w.WriteHeader(http.StatusInternalServerError)
  82. w.Header().Set("Content-Type", "text/html")
  83. tmpl.Execute(w, consts.TmplData{
  84. System: GetTmplSystemData(),
  85. Data: GetTmplError(err),
  86. })
  87. return
  88. }
  89. w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  90. w.WriteHeader(http.StatusInternalServerError)
  91. w.Header().Set("Content-Type", "text/html")
  92. w.Write([]byte("<h1>Critical engine error</h1>"))
  93. w.Write([]byte("<h2>" + err.Error() + "</h2>"))
  94. }