-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
main.go
57 lines (46 loc) · 1.64 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/_examples/routing/party-controller/pkg/weatherapi"
)
// Example of usage of Party Controllers.
// The method of zero-performance cost at serve-time, APIs run as fast as common Iris handlers.
func main() {
app := iris.New()
// Define a group under /api request path.
api := app.Party("/api")
// Register one or more dependencies.
api.RegisterDependency(weatherapi.NewClient(weatherapi.Options{
APIKey: "{YOUR_API_KEY}",
}))
// Register a party controller under the "/weather" sub request path.
api.PartyConfigure("/weather", new(WeatherController))
// Start the local server at 8080 port.
app.Listen(":8080")
}
// Just like the MVC controllers, route group(aka Party) controller's
// fields are injected by the parent or current party's RegisterDependency method.
//
// This controller structure could be live to another sub-package of our application as well.
type WeatherController struct {
Client *weatherapi.Client // This is automatically injected by .RegisterDependency.
}
func (api *WeatherController) Configure(r iris.Party) {
// Register routes under /api/weather.
r.Get("/current", api.getCurrentData)
}
// Normal Iris Handler.
func (api *WeatherController) getCurrentData(ctx iris.Context) {
city := ctx.URLParamDefault("city", "Athens")
// Call the controller's "Client"'s GetCurrentByCity method
// to retrieve data from external provider and push them to our clients.
data, err := api.Client.GetCurrentByCity(ctx, city)
if err != nil {
ctx.StatusCode(iris.StatusBadRequest)
ctx.JSON(iris.Map{
"error": err.Error(),
})
return
}
ctx.JSON(data)
}