bill-server-go/internal/infrastructure/fiber.go

88 lines
2.6 KiB
Go
Raw Normal View History

2023-05-05 20:27:33 +08:00
package infrastructure
import (
2023-05-11 21:00:29 +08:00
"bill-go-fiber/internal/bill"
2023-06-01 19:40:48 +08:00
"bill-go-fiber/internal/label"
2023-05-05 20:27:33 +08:00
"bill-go-fiber/internal/misc"
"fmt"
"log"
_ "github.com/go-sql-driver/mysql"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/compress"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/etag"
"github.com/gofiber/fiber/v2/middleware/favicon"
"github.com/gofiber/fiber/v2/middleware/limiter"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/fiber/v2/middleware/requestid"
)
// Run our Fiber webserver.
func Run() {
// Try to connect to our database as the initial part.
2023-06-01 17:55:41 +08:00
//mariadb, err := ConnectToMariaDB()
sqlite, err := ConnectToSqlite()
2023-05-05 20:27:33 +08:00
if err != nil {
log.Fatal("Database connection error: $s", err)
}
// Creates a new Fiber instance.
app := fiber.New(fiber.Config{
AppName: "Docker MariaDB Clean Arch",
ServerHeader: "Fiber",
})
// Use global middlewares.
app.Use(cors.New())
app.Use(compress.New())
app.Use(etag.New())
app.Use(favicon.New())
app.Use(limiter.New(limiter.Config{
Max: 100,
LimitReached: func(c *fiber.Ctx) error {
return c.Status(fiber.StatusTooManyRequests).JSON(&fiber.Map{
"status": "fail",
"message": "You have requested too many in a single time-frame! Please wait another minute!",
})
},
}))
app.Use(logger.New())
app.Use(recover.New())
app.Use(requestid.New())
// Create repositories.
2023-05-11 21:00:29 +08:00
//cityRepository := city.NewCityRepository(mariadb)
//userRepository := user.NewUserRepository(mariadb)
2023-06-01 18:07:58 +08:00
billRepository := bill.NewBillRepository(sqlite)
2023-06-01 19:40:48 +08:00
labelRepository := label.NewLabelRepository(sqlite)
2023-05-05 20:27:33 +08:00
// Create all of our services.
2023-05-11 21:00:29 +08:00
//cityService := city.NewCityService(cityRepository)
//userService := user.NewUserService(userRepository)
billService := bill.NewBillService(billRepository)
2023-06-01 19:40:48 +08:00
labelService := label.NewLabelService(labelRepository)
2023-05-05 20:27:33 +08:00
// Prepare our endpoints for the API.
misc.NewMiscHandler(app.Group("/api/v1"))
2023-05-11 21:00:29 +08:00
//auth.NewAuthHandler(app.Group("/api/v1/auth"))
//city.NewCityHandler(app.Group("/api/v1/cities"), cityService)
//user.NewUserHandler(app.Group("/api/v1/users"), userService)
bill.NewBillHandler(app.Group("/api/v1/bills"), billService)
2023-06-01 19:40:48 +08:00
label.NewLabelHandler(app.Group("/api/v1/labels"), labelService)
2023-05-05 20:27:33 +08:00
// Prepare an endpoint for 'Not Found'.
app.All("*", func(c *fiber.Ctx) error {
errorMessage := fmt.Sprintf("Route '%s' does not exist in this API!", c.OriginalURL())
return c.Status(fiber.StatusNotFound).JSON(&fiber.Map{
"status": "fail",
"message": errorMessage,
})
})
// Listen to port 8080.
log.Fatal(app.Listen(":8080"))
}