package server import ( "fmt" "github.com/coder/websocket" "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" swaggerFiles "github.com/swaggo/files" ginSwagger "github.com/swaggo/gin-swagger" patientHandler "template_blueprint/pkg/handlers/patient" datapoliklinikHandler "template_blueprint/pkg/handlers/poliklinik" dataRetribusi "template_blueprint/pkg/handlers/retribusi" //_ "template_blueprint/docs" //swaggerFiles "github.com/swaggo/files" //ginSwagger "github.com/swaggo/gin-swagger" "io/fs" "log" "net/http" //_ "template_blueprint/cmd/api/docs" "template_blueprint/cmd/web" "time" ) func (s *Server) RegisterRoutes() http.Handler { r := gin.Default() r.Use(cors.New(cors.Config{ AllowOrigins: []string{"*"}, // or specific domains like "http://example.com" AllowMethods: []string{"GET", "POST", "PUT", "DELETE"}, AllowHeaders: []string{"Origin", "Content-Type"}, AllowCredentials: true, })) //r.Use(middleware.CORSConfig()) //r.Use(middleware.ErrorHandler()) //r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) r.GET("/", s.HelloWorldHandler) r.GET("/websocket", s.websocketHandler) staticFiles, _ := fs.Sub(web.Files, "assets") r.StaticFS("/assets", http.FS(staticFiles)) v1 := r.Group("/api") patient := v1.Group("/patient") { patient.POST("/insertpatient", patientHandler.InsertPatient) patient.GET("/getallpatient", patientHandler.GetAllPatient) } Poliklinik := v1.Group("/poliklinik") { Poliklinik.GET("/getdata/statuspelayanan/:statuspelayanan", datapoliklinikHandler.GetDataPoliklinik) } retribusi := v1.Group("/retribusi") { retribusi.GET("/getData/Limit/:limit/Offset/:offset", dataRetribusi.GetDataRetribusi) retribusi.POST("/insertretribusi", dataRetribusi.InsertRetribution) retribusi.PUT("/updateretribusi", dataRetribusi.UpdateRetribution) retribusi.DELETE("/deleteretribusi/:id", dataRetribusi.DeleteDataRetribusi) retribusi.GET("/searchData/:kelompok_obyek", dataRetribusi.SearchDataRetribusi) } //add swagger //url := ginSwagger.URL("http://localhost:8084/swagger/doc.json") //r.GET("/docs/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) //r.GET("/swagger/*any", func(c *gin.Context) { // if c.Request.RequestURI == "/swagger/" { // c.Redirect(302, "/swagger/index.html") // return // } // ginSwagger.WrapHandler(swaggerFiles.Handler, ginSwagger.URL("http://localhost:8084/swagger/doc.json"))(c) //}) r.GET("/docs/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) return r } func (s *Server) HelloWorldHandler(c *gin.Context) { resp := make(map[string]string) resp["message"] = "Hello World 2" c.JSON(http.StatusOK, resp) } func (s *Server) websocketHandler(c *gin.Context) { w := c.Writer r := c.Request socket, err := websocket.Accept(w, r, nil) if err != nil { log.Printf("could not open websocket: %v", err) _, _ = w.Write([]byte("could not open websocket")) w.WriteHeader(http.StatusInternalServerError) return } defer socket.Close(websocket.StatusGoingAway, "server closing websocket") ctx := r.Context() socketCtx := socket.CloseRead(ctx) for { payload := fmt.Sprintf("server timestamp: %d", time.Now().UnixNano()) err := socket.Write(socketCtx, websocket.MessageText, []byte(payload)) if err != nil { break } time.Sleep(time.Second * 2) } }