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.

86 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
  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.Bind(
  40. channel.NewV1(),
  41. subscription.NewV1(),
  42. live.NewV1(),
  43. reservation.NewV1(),
  44. )
  45. })
  46. // 定义一个路由组,路径为/mainpage
  47. s.Group("/mainpage", func(group *ghttp.RouterGroup) {
  48. // 添加中间件,用于处理响应
  49. //group.Middleware(ghttp.MiddlewareHandlerResponse)
  50. group.Middleware(MiddlewareCORS)
  51. // 绑定路由,将mainPage.NewMainPage()绑定到该路由组
  52. //group.Bind(
  53. // mainPage.NewMainPage(),
  54. //)
  55. group.GET("/get-shows", mainPage.NewMainPage().GetShows)
  56. group.GET("/get-videos", mainPage.NewMainPage().GetVideos)
  57. })
  58. // 定义一个路由组,路径为/clubpage
  59. s.Group("/clubpage", func(group *ghttp.RouterGroup) {
  60. // 添加中间件,用于处理响应
  61. //group.Middleware(ghttp.MiddlewareHandlerResponse)
  62. group.Middleware(MiddlewareCORS)
  63. // 绑定路由,将mainPage.NewMainPage()绑定到该路由组
  64. //group.Bind(
  65. // clubPage.NewClubPage(),
  66. //)
  67. group.GET("/get-clubs", clubPage.NewClubPage().GetClub)
  68. group.GET("/get-club-shows", clubPage.NewClubPage().GetClubShows)
  69. })
  70. //开启静态资源
  71. s.SetServerRoot("resource/public")
  72. s.SetOpenApiPath("/api.json") //启用OpenAPIv3,并设置OpenAPI文档的路径
  73. s.SetSwaggerPath("/swagger") //启用内置的Swagger接口文档UI,并设置Swagger文档的路径
  74. s.Run()
  75. return nil
  76. },
  77. }
  78. )