-
Notifications
You must be signed in to change notification settings - Fork 6
/
start.go
62 lines (47 loc) · 1.36 KB
/
start.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"encoding/json"
"github.com/gofiber/fiber"
)
type FruitInterface struct {
Page int
Fruits []string
}
func main() {
app := fiber.New()
// Returns plain text.
app.Get("/", func(c *fiber.Ctx) {
c.Send("Hello, Fiber!")
// navigate to => http://localhost:3000/
})
// Load static files like CSS, Images & JavaScript
app.Static("/public", "./public")
// Returns a local HTML file.
app.Get("/hello", func(c *fiber.Ctx) {
if err := c.SendFile("./templates/hello.html"); err != nil {
c.Next(err)
}
// navigate to => http://localhost:3000/hello
})
// Use parameters
app.Get("/parameter/:value", func(c *fiber.Ctx) {
c.Send("Get request with value: " + c.Params("value"))
// navigate to => http://localhost:3000/parameter/this_is_the_parameter
})
// Use wildcards to design your API.
app.Get("/api/*", func(c *fiber.Ctx) {
c.Send("API path: " + c.Params("*") + " -> do lookups with these values")
// navigate to => http://localhost:3000/api/user/chris
// return serialized JSON.
if c.Params("*") == "fruits" {
response := FruitInterface{
Page: 1,
Fruits: []string{"apple", "peach", "pear"},
}
responseJson, _ := json.Marshal(response)
c.Send(responseJson)
// navigate to => http://localhost:3000/api/fruits
}
})
app.Listen(3000)
}