webserver.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "github.com/gin-contrib/multitemplate"
  7. "github.com/gin-gonic/gin"
  8. swaggerFiles "github.com/swaggo/files"
  9. ginSwagger "github.com/swaggo/gin-swagger"
  10. "adeptus-mechanicus.void/git/keiji/docs"
  11. "adeptus-mechanicus.void/git/keiji/pkg/env"
  12. "adeptus-mechanicus.void/git/keiji/pkg/routes"
  13. )
  14. var WEB_ROOT string
  15. var DOMAIN_NAME string
  16. var REDIS_PORT string
  17. var REDIS_ADDR string
  18. func main() {
  19. args := os.Args
  20. err := env.LoadAndVerifyEnv(args[1], env.REQUIRED_VARS)
  21. if err != nil {
  22. log.Fatal("Error when loading env file: ", err)
  23. }
  24. docs.SwaggerInfo.Title = "Webserver Swagger documentation"
  25. docs.SwaggerInfo.Host = "127.0.0.1:8080"
  26. docs.SwaggerInfo.Schemes = []string{"http"}
  27. renderer := multitemplate.NewRenderer()
  28. renderer.AddFromFiles(
  29. "home",
  30. fmt.Sprintf("%s/templates/home.html", WEB_ROOT),
  31. fmt.Sprintf("%s/templates/navigation.html", WEB_ROOT),
  32. fmt.Sprintf("%s/templates/menu.html", WEB_ROOT),
  33. fmt.Sprintf("%s/templates/link.html", WEB_ROOT),
  34. fmt.Sprintf("%s/templates/listing.html", WEB_ROOT),
  35. )
  36. renderer.AddFromFiles(
  37. "blogpost",
  38. fmt.Sprintf("%s/templates/blogpost.html", WEB_ROOT),
  39. fmt.Sprintf("%s/templates/navigation.html", WEB_ROOT),
  40. fmt.Sprintf("%s/templates/menu.html", WEB_ROOT),
  41. fmt.Sprintf("%s/templates/link.html", WEB_ROOT),
  42. )
  43. renderer.AddFromFiles(
  44. "digital_art",
  45. fmt.Sprintf("%s/templates/digital_art.html", WEB_ROOT),
  46. fmt.Sprintf("%s/templates/centered_image.html", WEB_ROOT),
  47. fmt.Sprintf("%s/templates/navigation.html", WEB_ROOT),
  48. fmt.Sprintf("%s/templates/menu.html", WEB_ROOT),
  49. fmt.Sprintf("%s/templates/link.html", WEB_ROOT),
  50. )
  51. renderer.AddFromFiles(
  52. "login",
  53. fmt.Sprintf("%s/templates/login.html", WEB_ROOT),
  54. )
  55. renderer.AddFromFiles(
  56. "admin",
  57. fmt.Sprintf("%s/templates/admin.html", WEB_ROOT),
  58. fmt.Sprintf("%s/templates/menu.html", WEB_ROOT),
  59. fmt.Sprintf("%s/templates/link.html", WEB_ROOT),
  60. fmt.Sprintf("%s/templates/navigation.html", WEB_ROOT),
  61. fmt.Sprintf("%s/templates/listing.html", WEB_ROOT),
  62. )
  63. renderer.AddFromFiles(
  64. "blogpost_editor",
  65. fmt.Sprintf("%s/templates/blogpost_editor.html", WEB_ROOT),
  66. fmt.Sprintf("%s/templates/menu.html", WEB_ROOT),
  67. fmt.Sprintf("%s/templates/link.html", WEB_ROOT),
  68. fmt.Sprintf("%s/templates/navigation.html", WEB_ROOT),
  69. fmt.Sprintf("%s/templates/listing.html", WEB_ROOT),
  70. )
  71. e := gin.Default()
  72. e.HTMLRender = renderer
  73. e.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
  74. routes.Register(e, WEB_ROOT, DOMAIN_NAME, REDIS_PORT, REDIS_ADDR)
  75. e.Run(fmt.Sprintf("%s:%s", os.Getenv("HOST_ADDR"), os.Getenv("HOST_PORT")))
  76. }