-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
45 lines (36 loc) · 1009 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"log"
"net/http"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/adaptor"
"github.com/jub0bs/cors"
)
func main() {
app := fiber.New()
app.Get("/hello", handleHello) // note: not configured for CORS
// create CORS middleware
corsMw, err := cors.NewMiddleware(cors.Config{
Origins: []string{"https://example.com"},
Methods: []string{http.MethodGet, http.MethodPost},
RequestHeaders: []string{"Authorization"},
})
if err != nil {
log.Fatal(err)
}
corsMw.SetDebug(true) // turn debug mode on (optional)
api := app.Group("/api")
api.Use(adaptor.HTTPMiddleware(corsMw.Wrap))
api.Get("/users", handleUsersGet)
api.Post("/users", handleUsersPost)
log.Fatal(app.Listen(":8080"))
}
func handleHello(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
}
func handleUsersGet(c *fiber.Ctx) error {
return nil // omitted implementation
}
func handleUsersPost(c *fiber.Ctx) error {
return nil // omitted implementation
}