Merge pull request 'davila-dev' (#1) from davila-dev into main

Reviewed-on: ahdan.zeeda.0103/template_mongopostgre#1
This commit is contained in:
2025-08-13 07:53:48 +00:00
12 changed files with 1879 additions and 68 deletions
+55
View File
@@ -0,0 +1,55 @@
package middleware
import (
"net/http"
"template_blueprint/pkg/models"
"github.com/gin-gonic/gin"
)
// ErrorHandler handles errors globally
func ErrorHandler() gin.HandlerFunc {
return func(c *gin.Context) {
c.Next()
if len(c.Errors) > 0 {
err := c.Errors.Last()
status := http.StatusInternalServerError
// Determine status code based on error type
switch err.Type {
case gin.ErrorTypeBind:
status = http.StatusBadRequest
case gin.ErrorTypeRender:
status = http.StatusUnprocessableEntity
case gin.ErrorTypePrivate:
status = http.StatusInternalServerError
}
response := models.ErrorResponse{
Error: "internal_error",
Message: err.Error(),
Code: status,
}
c.JSON(status, response)
}
}
}
// CORS middleware configuration
func CORSConfig() gin.HandlerFunc {
return gin.HandlerFunc(func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, PATCH")
c.Header("Access-Control-Allow-Headers", "Origin, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
})
}
+34 -5
View File
@@ -5,25 +5,35 @@ import (
"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"
patientHandler "template_blueprint/pkg/handlers/patient"
datapoliklinikHandler "template_blueprint/pkg/handlers/poliklinik"
"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)
@@ -43,13 +53,32 @@ func (s *Server) RegisterRoutes() http.Handler {
{
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"
resp["message"] = "Hello World 2"
c.JSON(http.StatusOK, resp)
}