You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.6 KiB

7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
  1. package cmd
  2. import (
  3. "context"
  4. "practice_Go/internal/controller/channel"
  5. "practice_Go/internal/controller/clubPage"
  6. "practice_Go/internal/controller/live"
  7. "practice_Go/internal/controller/reservation"
  8. "practice_Go/internal/controller/subscription"
  9. "github.com/gogf/gf/v2/frame/g"
  10. "github.com/gogf/gf/v2/net/ghttp"
  11. "practice_Go/internal/controller/mainPage"
  12. "github.com/gogf/gf/v2/os/gcmd"
  13. )
  14. /*自定义的允许跨域请求中间件*/
  15. func MiddlewareCORS(r *ghttp.Request) {
  16. /* 没有起到应起到的作用,还是所有的都可以访问
  17. //自定义授权跨域的Origin
  18. corsOptions := r.Response.DefaultCORSOptions()
  19. corsOptions.AllowDomain = []string{"localhost"} //数组中的为允许访问的
  20. if !r.Response.CORSAllowedOrigin(corsOptions) {
  21. r.Response.WriteStatus(http.StatusForbidden)
  22. return
  23. }
  24. r.Response.CORS(corsOptions)
  25. r.Middleware.Next()*/
  26. //默认的跨域设置
  27. r.Response.CORSDefault()
  28. r.Middleware.Next()
  29. }
  30. var (
  31. Main = gcmd.Command{
  32. Name: "main",
  33. Usage: "main",
  34. Brief: "start http server",
  35. Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
  36. s := g.Server()
  37. s.Group("/", func(group *ghttp.RouterGroup) {
  38. //group.Middleware(ghttp.MiddlewareHandlerResponse)
  39. group.Middleware(MiddlewareCORS)
  40. group.Bind(
  41. channel.NewV1(),
  42. subscription.NewV1(),
  43. live.NewV1(),
  44. reservation.NewV1(),
  45. )
  46. })
  47. // 定义一个路由组,路径为/mainpage
  48. s.Group("/mainpage", func(group *ghttp.RouterGroup) {
  49. // 添加中间件,用于处理响应
  50. //group.Middleware(ghttp.MiddlewareHandlerResponse)
  51. group.Middleware(MiddlewareCORS)
  52. // 绑定路由,将mainPage.NewMainPage()绑定到该路由组
  53. //group.Bind(
  54. // mainPage.NewMainPage(),
  55. //)
  56. group.GET("/get-shows", mainPage.NewMainPage().GetShows)
  57. group.GET("/get-videos", mainPage.NewMainPage().GetVideos)
  58. })
  59. // 定义一个路由组,路径为/clubpage
  60. s.Group("/clubpage", func(group *ghttp.RouterGroup) {
  61. // 添加中间件,用于处理响应
  62. //group.Middleware(ghttp.MiddlewareHandlerResponse)
  63. group.Middleware(MiddlewareCORS)
  64. // 绑定路由,将mainPage.NewMainPage()绑定到该路由组
  65. //group.Bind(
  66. // clubPage.NewClubPage(),
  67. //)
  68. group.GET("/get-clubs", clubPage.NewClubPage().GetClub)
  69. group.GET("/get-club-shows", clubPage.NewClubPage().GetClubShows)
  70. })
  71. //开启静态资源
  72. s.SetServerRoot("resource/public")
  73. s.SetOpenApiPath("/api.json") //启用OpenAPIv3,并设置OpenAPI文档的路径
  74. s.SetSwaggerPath("/swagger") //启用内置的Swagger接口文档UI,并设置Swagger文档的路径
  75. s.Run()
  76. return nil
  77. },
  78. }
  79. )