-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (58 loc) · 2.19 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"log"
"os"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/srik007/sensor-api/infrastructure/cache"
"github.com/srik007/sensor-api/infrastructure/persistance"
"github.com/srik007/sensor-api/interfaces"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
_ "github.com/srik007/sensor-api/docs"
)
func init() {
if err := godotenv.Load(); err != nil {
log.Println("no env gotten")
}
}
// @title Underwater Sensor API
// @version 1.0
// @description A Under water sensor api to monitor the sensor data and generate aggrates of the data.
// @host localhost:8080
// @BasePath /api/v1
func main() {
dbdriver := os.Getenv("DB_DRIVER")
host := os.Getenv("DB_HOST")
password := os.Getenv("DB_PASSWORD")
user := os.Getenv("DB_USER")
dbname := os.Getenv("DB_NAME")
port := os.Getenv("DB_PORT")
cacheUrl := os.Getenv("REDIS_URL")
services, err := persistance.NewRepositories(dbdriver, user, password, port, host, dbname)
cahce := cache.NewCache(cacheUrl)
if err != nil {
panic(err)
}
defer services.Close()
if os.Getenv("APP_ENV") != "test" {
services.Automigrate()
}
sensorHandler := interfaces.NewSensorHandler(services.SensorRepository, services.SensorGroupRepository, services.DataStore, cahce)
r := gin.Default()
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
v1 := r.Group("/api/v1")
{
v1.POST("/create-metadata", sensorHandler.CreateMetadata)
v1.POST("/schedule", sensorHandler.ScheduleJob)
v1.GET("/group/:groupName/transparency", sensorHandler.CollectAverageTransparencyUnderGroup)
v1.GET("/group/:groupName/temparature", sensorHandler.CollectAverageTemparatureUnderGroup)
v1.GET("/group/:groupName/species", sensorHandler.CollectSpeciesUnderGroup)
v1.GET("/group/:groupName/species/top/:topN", sensorHandler.CollectTopNSpeciesUnderGroup)
v1.GET("/region/temparature/min", sensorHandler.CalculateMinTemparatureInsideARegion)
v1.GET("/region/temparature/max", sensorHandler.CalculateMaxTemparatureInsideARegion)
v1.GET("/sensor/:codeName/temparature/average", sensorHandler.CalculateAverageTemparatureBySensor)
}
app_port := os.Getenv("PORT")
r.Run(":" + app_port)
}