-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
147 lines (136 loc) · 5.03 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"bufio"
"flag"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
utl "github.com/eensymachines-in/utilities"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
const (
mgoIP = "srvmongo"
dbTimeout = 10 * time.Second // this is when we want to timeout the database connection tru
broker = "mosquitto"
port = 1883
fMqttsecret = "/run/secrets/mqtt_secret"
pages = "/var/www/luminapp/pages" // this has to be loaded into the container from environment
)
var (
// Flog : determines if the direction of log output
Flog bool
// FVerbose : determines the level of log
FVerbose bool
)
// sendIndexHtml : handler for all the request to send back the index.html page
// you can customize this later to have the og:tags modified for SEO and preview links
func sendIndexHtml(c *gin.Context) {
log.Println("We have reached sendIndexHtml")
c.HTML(http.StatusOK, "index.html", gin.H{})
}
// Setting the environment variables here and prepare before the api server runs
func init() {
/*Reading in the command line params, at the entrypoint*/
utl.SetUpLog()
flag.BoolVar(&Flog, "flog", true, "direction of log messages, set false for terminal logging. Default is true")
flag.BoolVar(&FVerbose, "verbose", false, "Determines what level of log messages are to be output, Default is info level")
file, err := os.Open(fMqttsecret)
if err != nil {
log.Warnf("Failed to read encryption secrets, please load those %s", err)
}
defer file.Close()
reader := bufio.NewReader(file)
// ++++++++++++++++++++++++++++++++ reading in the mqtt secret
line, _, err := reader.ReadLine()
if err != nil {
log.Warn("Error reading the auth secret from file")
}
// split the line to read the username and password
if !strings.Contains(string(line), ":") {
// Incase the secret file is not as expected
log.Warn("mqtt_secret: file not in expected format, expected format username:password, without any white space")
}
result := strings.Split(string(line), ":")
if len(result) == 2 {
log.WithFields(log.Fields{
"mqtt_u": result[0],
"mqtt_p": "*******", //result[1],
}).Info("we have now loaded the mqtt credentials")
// Setting the environment variables
os.Setenv("MQTT_U", result[0])
os.Setenv("MQTT_P", result[1])
}
}
func main() {
/*This will setup the logging direction and depth*/
flag.Parse()
logFile := os.Getenv("LOGF")
if logFile != "" {
closeLogFile := utl.CustomLog(Flog, FVerbose, logFile) // Log direction and the level of logging
file, err := os.Open(os.Getenv("LOGF"))
if err != nil {
log.Fatal(err)
}
gin.DisableConsoleColor()
gin.DefaultWriter = io.MultiWriter(file)
defer file.Close()
defer closeLogFile()
}
gin.SetMode(gin.DebugMode)
r := gin.Default()
// server needs access to the index.html and other error pages
// cause on all the routes the main page is to be sent from the server
// SEO and image og meta tags will be modified from the server side
r.LoadHTMLGlob(fmt.Sprintf("%s/*", pages))
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"app": "luminapi",
"logs": logFile,
"verblog": FVerbose,
})
})
r.GET("/", sendIndexHtml)
r.GET("/signup", sendIndexHtml)
r.GET("/about", sendIndexHtml)
r.GET("/admin/accounts", func(c *gin.Context) {
log.Println("Inside accounts list administration..")
c.HTML(http.StatusOK, "index.html", gin.H{})
})
r.GET("/admin/embargo", sendIndexHtml)
r.GET("/admin/devices", sendIndexHtml)
r.GET("/accounts/:email", func(c *gin.Context) {
log.Printf("Email param: %s", c.Param("email"))
c.HTML(http.StatusOK, "index.html", gin.H{})
})
r.GET("/accounts/:email/devices", sendIndexHtml)
r.GET("/schedules/:serial", sendIndexHtml)
r.Use(CORS)
// r.GET("/:email/devices", sendIndexHtml)
/*Admin related tasks here under one group. Check the nginx conf this has been appropriately */
grpAdmin := r.Group("/admin")
grpAdmin.GET("/logs", HndlLogs(os.Getenv("LOGF")))
// +++++++++++++++++++++ logs
// Only device specific logs, GET and POST
logs := r.Group("/logs")
logs.Use(dbConnect())
logs.GET("/:serial", checkIfDeviceReg(true), HndlDeviceLogs())
logs.POST("", devregPayload, checkIfDeviceReg(true), HndlDeviceLogs())
// ++++++++++++ devices
devices := r.Group("/devices")
devices.Use(dbConnect())
devices.POST("", devregPayload, checkIfDeviceReg(false), HandlDevices) // to register new devices
devices.DELETE("/:serial", checkIfDeviceReg(true), HandlDevice) // single device un-register
devices.PATCH("/:serial", checkIfDeviceReg(true), devregPayload, mqttConnect(), HandlDevice) // schedules are updated here
devices.GET("/:serial", checkIfDeviceReg(true), HandlDevice) // GETting the schedules for a device
// a group to facilitate commands to the device from the app
// A command can only be only posted, the nature of the action in
cmds := r.Group("/cmds")
cmds.POST("/:serial", mqttConnect(), HndlCommands)
log.Info("Starting luminapi service ..")
defer log.Warn("Now quitting luminapi service")
log.Fatal(r.Run(":8080"))
}