webserver.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "github.com/gin-contrib/multitemplate"
  7. "github.com/gin-gonic/gin"
  8. "git.aetherial.dev/aeth/keiji/pkg/env"
  9. "git.aetherial.dev/aeth/keiji/pkg/routes"
  10. )
  11. var WEB_ROOT string
  12. var DOMAIN_NAME string
  13. func main() {
  14. args := os.Args
  15. err := env.LoadAndVerifyEnv(args[1], env.REQUIRED_VARS)
  16. if err != nil {
  17. log.Fatal("Error when loading env file: ", err)
  18. }
  19. REDIS_PORT := os.Getenv("REDIS_PORT")
  20. REDIS_ADDR := os.Getenv("REDIS_ADDR")
  21. renderer := multitemplate.NewRenderer()
  22. renderer.AddFromFiles(
  23. "home",
  24. fmt.Sprintf("%s/templates/home.html", WEB_ROOT),
  25. fmt.Sprintf("%s/templates/navigation.html", WEB_ROOT),
  26. fmt.Sprintf("%s/templates/menu.html", WEB_ROOT),
  27. fmt.Sprintf("%s/templates/link.html", WEB_ROOT),
  28. fmt.Sprintf("%s/templates/listing.html", WEB_ROOT),
  29. )
  30. renderer.AddFromFiles(
  31. "blogpost",
  32. fmt.Sprintf("%s/templates/blogpost.html", WEB_ROOT),
  33. fmt.Sprintf("%s/templates/navigation.html", WEB_ROOT),
  34. fmt.Sprintf("%s/templates/menu.html", WEB_ROOT),
  35. fmt.Sprintf("%s/templates/link.html", WEB_ROOT),
  36. )
  37. renderer.AddFromFiles(
  38. "digital_art",
  39. fmt.Sprintf("%s/templates/digital_art.html", WEB_ROOT),
  40. fmt.Sprintf("%s/templates/centered_image.html", WEB_ROOT),
  41. fmt.Sprintf("%s/templates/navigation.html", WEB_ROOT),
  42. fmt.Sprintf("%s/templates/menu.html", WEB_ROOT),
  43. fmt.Sprintf("%s/templates/link.html", WEB_ROOT),
  44. )
  45. renderer.AddFromFiles(
  46. "login",
  47. fmt.Sprintf("%s/templates/login.html", WEB_ROOT),
  48. )
  49. renderer.AddFromFiles(
  50. "admin",
  51. fmt.Sprintf("%s/templates/admin.html", WEB_ROOT),
  52. fmt.Sprintf("%s/templates/menu.html", WEB_ROOT),
  53. fmt.Sprintf("%s/templates/link.html", WEB_ROOT),
  54. fmt.Sprintf("%s/templates/navigation.html", WEB_ROOT),
  55. fmt.Sprintf("%s/templates/listing.html", WEB_ROOT),
  56. fmt.Sprintf("%s/templates/blogpost_editor.html", WEB_ROOT),
  57. )
  58. renderer.AddFromFiles(
  59. "blogpost_editor",
  60. fmt.Sprintf("%s/templates/blogpost_editor.html", WEB_ROOT),
  61. fmt.Sprintf("%s/templates/menu.html", WEB_ROOT),
  62. fmt.Sprintf("%s/templates/link.html", WEB_ROOT),
  63. fmt.Sprintf("%s/templates/upload_status.html", WEB_ROOT),
  64. fmt.Sprintf("%s/templates/navigation.html", WEB_ROOT),
  65. fmt.Sprintf("%s/templates/listing.html", WEB_ROOT),
  66. )
  67. renderer.AddFromFiles(
  68. "new_blogpost",
  69. fmt.Sprintf("%s/templates/new_blogpost.html", WEB_ROOT),
  70. fmt.Sprintf("%s/templates/menu.html", WEB_ROOT),
  71. fmt.Sprintf("%s/templates/link.html", WEB_ROOT),
  72. fmt.Sprintf("%s/templates/upload_status.html", WEB_ROOT),
  73. fmt.Sprintf("%s/templates/navigation.html", WEB_ROOT),
  74. fmt.Sprintf("%s/templates/listing.html", WEB_ROOT),
  75. )
  76. renderer.AddFromFiles(
  77. "upload_status",
  78. fmt.Sprintf("%s/templates/upload_status.html", WEB_ROOT),
  79. )
  80. renderer.AddFromFiles(
  81. "unhandled_error",
  82. fmt.Sprintf("%s/templates/unhandled_error.html", WEB_ROOT),
  83. fmt.Sprintf("%s/templates/menu.html", WEB_ROOT),
  84. fmt.Sprintf("%s/templates/link.html", WEB_ROOT),
  85. fmt.Sprintf("%s/templates/navigation.html", WEB_ROOT),
  86. fmt.Sprintf("%s/templates/listing.html", WEB_ROOT),
  87. )
  88. renderer.AddFromFiles(
  89. "upload",
  90. fmt.Sprintf("%s/templates/upload.html", WEB_ROOT),
  91. fmt.Sprintf("%s/templates/menu.html", WEB_ROOT),
  92. fmt.Sprintf("%s/templates/link.html", WEB_ROOT),
  93. fmt.Sprintf("%s/templates/navigation.html", WEB_ROOT),
  94. fmt.Sprintf("%s/templates/listing.html", WEB_ROOT),
  95. )
  96. e := gin.Default()
  97. e.HTMLRender = renderer
  98. routes.Register(e, WEB_ROOT, DOMAIN_NAME, REDIS_PORT, REDIS_ADDR)
  99. if os.Getenv("SSL_MODE") == "ON" {
  100. e.RunTLS(fmt.Sprintf("%s:%s", os.Getenv("HOST_ADDR"), os.Getenv("HOST_PORT")),
  101. os.Getenv(env.CHAIN), os.Getenv(env.KEY))
  102. }
  103. e.Run(fmt.Sprintf("%s:%s", os.Getenv("HOST_ADDR"), os.Getenv("HOST_PORT")))
  104. }