main.go 1.17 KB
package main

import (
	"log"
	"ui.ac.id/dtd/go-webserver-boilerplate/internal/domain"

	"github.com/gofiber/fiber/v3"
	"github.com/gofiber/fiber/v3/middleware/healthcheck"
	"github.com/gofiber/fiber/v3/middleware/logger"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
	"ui.ac.id/dtd/go-webserver-boilerplate/internal/handler"
	"ui.ac.id/dtd/go-webserver-boilerplate/internal/repository/postgresql"
	"ui.ac.id/dtd/go-webserver-boilerplate/internal/service"
)

func main() {
	config := domain.InitConfig()
	db, err := gorm.Open(postgres.Open(config.DatabaseUrl), &gorm.Config{})

	if err != nil {
		log.Fatal(err)
	}

	userRepository := postgresql.NewPostgresUserRepository(db)
	userService := service.NewUserService(userRepository)
	userHandler := handler.NewUserHandler(userService)

	app := fiber.New(fiber.Config{
		ErrorHandler: handler.ErrorHandler,
	})

	app.Use(logger.New())
	app.Get("/", healthcheck.NewHealthChecker())

	//api group
	api := app.Group("/api")

	//v1 api
	v1 := api.Group("/v1")

	//user group
	user := v1.Group("/user")
	user.Get("/:id", userHandler.GetUserDetail)
	user.Get("/", userHandler.GetUserList)

	err = app.Listen(":" + config.Port)

	if err != nil {
		return
	}
}