99 lines
2.7 KiB
Go
99 lines
2.7 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/coder/websocket"
|
|
"github.com/gin-contrib/cors"
|
|
"github.com/gin-gonic/gin"
|
|
"log"
|
|
"net/http"
|
|
LoginHandler "template_blueprint/pkg/handlers/login"
|
|
MenuHandler "template_blueprint/pkg/handlers/menu"
|
|
"time"
|
|
)
|
|
|
|
func (s *Server) RegisterRoutes() http.Handler {
|
|
r := gin.Default()
|
|
//r.Use(cors.New(cors.Config{
|
|
// AllowOrigins: []string{"*"}, // Sesuaikan dengan FE Nuxt
|
|
// AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
|
// AllowHeaders: []string{"Origin", "Content-Type", "Accept", "Authorization"},
|
|
// AllowCredentials: true,
|
|
// MaxAge: 12 * time.Hour,
|
|
//}))
|
|
r.Use(cors.New(cors.Config{
|
|
AllowOrigins: []string{"*"}, // or specific domains like "http://example.com"
|
|
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
|
AllowHeaders: []string{"Origin", "Content-Type"},
|
|
AllowCredentials: true,
|
|
}))
|
|
|
|
r.GET("/", s.HelloWorldHandler)
|
|
|
|
r.GET("/websocket", s.websocketHandler)
|
|
|
|
api := r.Group("/api")
|
|
login := api.Group("/login")
|
|
{
|
|
login.GET("/get", LoginHandler.Getlogin)
|
|
login.GET("/:id", LoginHandler.GetloginbyID)
|
|
}
|
|
keuangan := api.Group("/menu")
|
|
{
|
|
keuangan.GET("/getlist", MenuHandler.Getmenukeuangan)
|
|
keuangan.GET("/role", MenuHandler.Getrole)
|
|
keuangan.GET("/role/type/:id", MenuHandler.GetroleByType)
|
|
keuangan.GET("/id/:id", MenuHandler.GetmenuByID)
|
|
keuangan.GET("/typeuser/id/:id", MenuHandler.GettypeByID)
|
|
keuangan.GET("/type/:id", MenuHandler.GetroleByTypeWithUserInfo)
|
|
keuangan.GET("/type", MenuHandler.Gettype)
|
|
//keuangan.DELETE("/delete/:id", MenuHandler.GetBytypeIDdelete)
|
|
//keuangan.POST("/role/insert", MenuHandler.InsertRoles)
|
|
keuangan.POST("/update/rolemenu", MenuHandler.GabunganApi)
|
|
}
|
|
|
|
// Contoh route
|
|
r.GET("/api/test", func(c *gin.Context) {
|
|
c.JSON(200, gin.H{"message": "CORS berhasil"})
|
|
})
|
|
|
|
// Start server
|
|
r.Run(":8080")
|
|
|
|
return r
|
|
}
|
|
|
|
func (s *Server) HelloWorldHandler(c *gin.Context) {
|
|
resp := make(map[string]string)
|
|
resp["message"] = "Hello World"
|
|
|
|
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)
|
|
}
|
|
}
|