webserver.go 3.9 KB

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