-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
54 lines (44 loc) · 1.15 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
package main
import (
"log"
"github.com/gofiber/fiber/v2"
"github.com/pcpratheesh/golang-influxdb-example/config"
"github.com/pcpratheesh/golang-influxdb-example/influxdb"
)
func main() {
cfg, err := config.LoadConfiguration()
if err != nil {
panic(err)
}
// initiate the db instance and connect
influxInstance := influxdb.NewInfluxDBInstance(cfg.InfluxInstance)
err = influxInstance.Connect()
if err != nil {
panic(err)
}
defer influxInstance.Close()
log.Println("Successfully connect to influxdb ...")
// create database if not exists
influxInstance.Create()
app := fiber.New()
// get data
app.Get("/get", func(c *fiber.Ctx) error {
data, err := influxInstance.GetAllItems()
if err != nil {
return c.Status(fiber.ErrBadGateway.Code).JSON(err.Error())
}
return c.JSON(data)
})
// create new data
app.Post("/create", func(c *fiber.Ctx) error {
err := influxInstance.InsertSample()
if err != nil {
return c.Status(fiber.ErrBadGateway.Code).JSON(err.Error())
}
return c.JSON("successfully inserted")
})
log.Println("Start listening...")
if err := app.Listen(":" + cfg.Server.Port); err != nil {
log.Fatal(err)
}
}