feat: Adding openapi example API
This commit is contained in:
44
app/main.go
44
app/main.go
@@ -1,34 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/cors"
|
||||
"github.com/gofiber/fiber/v2/middleware/helmet"
|
||||
"github.com/gofiber/fiber/v2/middleware/logger"
|
||||
|
||||
database "cloud.etaviaporte.com/api/libs/database"
|
||||
rbac "cloud.etaviaporte.com/api/libs/database/schemas/rbac"
|
||||
"cloud.etaviaporte.com/api/libs/openapi"
|
||||
"cloud.etaviaporte.com/api/libs/services/auth"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var connection = database.Init()
|
||||
app := fiber.New()
|
||||
|
||||
// Prepare a variable to hold the fetched user record.
|
||||
// Replace rbac.User with the actual struct name if different.
|
||||
var user rbac.User
|
||||
app.Use(cors.New(cors.Config{
|
||||
AllowOrigins: "https://api.etaviaporte.com, http://127.0.0.1, http://localhost",
|
||||
AllowHeaders: "Origin, Content-Type, Accept, Authorization",
|
||||
AllowMethods: "GET, POST, HEAD, PUT, DELETE, PATCH",
|
||||
AllowCredentials: true,
|
||||
}))
|
||||
|
||||
// Fetch the user whose primary key is 1.
|
||||
// GORM's First method, when given a numeric id as the second argument, queries by primary key.
|
||||
result := connection.First(&user, 1)
|
||||
app.Use(logger.New(logger.Config{
|
||||
Format: "[${ip}]:${port} ${status} - ${method} ${path}\n",
|
||||
}))
|
||||
|
||||
// result.Error holds any error from the DB operation (including "record not found").
|
||||
// result.RowsAffected tells how many rows were returned.
|
||||
if result.Error != nil {
|
||||
// Handle the error (could be gorm.ErrRecordNotFound or a DB/connection error).
|
||||
// For learning purposes we print the error and exit main.
|
||||
fmt.Printf("failed to fetch user with id=1: %v\n", result.Error)
|
||||
return
|
||||
}
|
||||
app.Use(helmet.New())
|
||||
|
||||
// If no error, user now contains the row from the database.
|
||||
// Print the struct with field names and values to inspect the loaded data.
|
||||
fmt.Printf("User fetched: %+v\n", user)
|
||||
app.Get("/", func(c *fiber.Ctx) error {
|
||||
return c.SendString("Hello, World!")
|
||||
})
|
||||
|
||||
openapi.RegisterHandlers(app, auth.Handler{})
|
||||
|
||||
app.Listen(":3000")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user