webserver.go 3.1 KB

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