webserver.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. e := gin.Default()
  81. e.HTMLRender = renderer
  82. routes.Register(e, WEB_ROOT, DOMAIN_NAME, REDIS_PORT, REDIS_ADDR)
  83. if os.Getenv("SSL_MODE") == "ON" {
  84. e.RunTLS(fmt.Sprintf("%s:%s", os.Getenv("HOST_ADDR"), os.Getenv("HOST_PORT")),
  85. os.Getenv(env.CHAIN), os.Getenv(env.KEY))
  86. }
  87. e.Run(fmt.Sprintf("%s:%s", os.Getenv("HOST_ADDR"), os.Getenv("HOST_PORT")))
  88. }