first commit
This commit is contained in:
47
internal/server/routes.go
Normal file
47
internal/server/routes.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
datapoliklinikHandler "api-poliklinik/pkg/handlers/Poliklinik"
|
||||
datapractitionerHandler "api-poliklinik/pkg/handlers/Practitioner"
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (s *Server) RegisterRoutes() http.Handler {
|
||||
r := gin.Default()
|
||||
|
||||
r.GET("/", s.HelloWorldHandler)
|
||||
|
||||
r.GET("/health", s.healthHandler)
|
||||
|
||||
v1 := r.Group("/api")
|
||||
|
||||
Poliklinik := v1.Group("/poliklinik")
|
||||
{
|
||||
Poliklinik.GET("/getdata/statuspelayanan/:statuspelayanan", datapoliklinikHandler.GetDataPoliklinik)
|
||||
}
|
||||
Practitioner := v1.Group("/practitioner")
|
||||
{
|
||||
Practitioner.GET("/getdata", datapractitionerHandler.GetDataPractitioner)
|
||||
}
|
||||
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,
|
||||
}))
|
||||
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) healthHandler(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, s.db.Health())
|
||||
}
|
||||
32
internal/server/routes_test.go
Normal file
32
internal/server/routes_test.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHelloWorldHandler(t *testing.T) {
|
||||
s := &Server{}
|
||||
r := gin.New()
|
||||
r.GET("/", s.HelloWorldHandler)
|
||||
// Create a test HTTP request
|
||||
req, err := http.NewRequest("GET", "/", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Create a ResponseRecorder to record the response
|
||||
rr := httptest.NewRecorder()
|
||||
// Serve the HTTP request
|
||||
r.ServeHTTP(rr, req)
|
||||
// Check the status code
|
||||
if status := rr.Code; status != http.StatusOK {
|
||||
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusOK)
|
||||
}
|
||||
// Check the response body
|
||||
expected := "{\"message\":\"Hello World\"}"
|
||||
if rr.Body.String() != expected {
|
||||
t.Errorf("Handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
|
||||
}
|
||||
}
|
||||
40
internal/server/server.go
Normal file
40
internal/server/server.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"api-poliklinik/internal/database"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
_ "github.com/joho/godotenv/autoload"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
port int
|
||||
|
||||
db database.Service
|
||||
}
|
||||
|
||||
func NewServer() *http.Server {
|
||||
port, _ := strconv.Atoi(os.Getenv("PORT"))
|
||||
log.Println("port:", port)
|
||||
NewServer := &Server{
|
||||
port: port,
|
||||
|
||||
db: database.New(),
|
||||
}
|
||||
|
||||
// Declare Server config
|
||||
server := &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", NewServer.port),
|
||||
Handler: NewServer.RegisterRoutes(),
|
||||
IdleTimeout: time.Minute,
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 30 * time.Second,
|
||||
}
|
||||
|
||||
return server
|
||||
}
|
||||
Reference in New Issue
Block a user