Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/pcpratheesh/golang-influxdb-example

Sample code snippet for influxdb with golang and fiber
https://github.com/pcpratheesh/golang-influxdb-example

code example fiber go golang influx influxdb influxdb-client snippets time-series time-series-database

Last synced: about 1 month ago
JSON representation

Sample code snippet for influxdb with golang and fiber

Awesome Lists containing this project

README

        

# golang-influx-example
This is a sample respository to leanrn how we can perform influx db operations with an api endpoint.

To get all items from db
```go
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)
})
```

To create new entry to the db

```go
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")
})
```

## configuration
- rename config.sample.yml into config.yml
- change the configuration variables

```yml
server:
host: server host
port: server running port

influxInstance:
host: db host
port: db port
db: database
user: db user
password: db password
```
### Run
```go
go run main.go
```

## References
- InfluxDB : https://docs.influxdata.com/influxdb/v1.8/introduction/get-started/
- InfluxDB Golang Client : https://github.com/influxdata/influxdb-client-go
- Go Fiber : https://github.com/gofiber/fiber