moved middleware into more relevant location. Adding send test notifications handler. making sure that config is available from web handler functions.
This commit is contained in:
@@ -31,7 +31,7 @@ type Notify struct {
|
|||||||
Payload Payload
|
Payload Payload
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Notify) Send(level string, payload interface{}) error {
|
func (n *Notify) Send() error {
|
||||||
//validate that the Payload is populated
|
//validate that the Payload is populated
|
||||||
sendDate := time.Now()
|
sendDate := time.Now()
|
||||||
n.Payload.Date = sendDate.Format(time.RFC3339)
|
n.Payload.Date = sendDate.Format(time.RFC3339)
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/analogj/scrutiny/webapp/backend/pkg/config"
|
||||||
|
dbModels "github.com/analogj/scrutiny/webapp/backend/pkg/models/db"
|
||||||
|
"github.com/analogj/scrutiny/webapp/backend/pkg/notify"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Send test notification
|
||||||
|
func SendTestNotification(c *gin.Context) {
|
||||||
|
appConfig := c.MustGet("CONFIG").(config.Interface)
|
||||||
|
|
||||||
|
testNotify := notify.Notify{
|
||||||
|
Config: appConfig,
|
||||||
|
Payload: notify.Payload{
|
||||||
|
Mailer: os.Args[0],
|
||||||
|
Subject: fmt.Sprintf("Scrutiny SMART error (EmailTest) detected on disk: XXXXX"),
|
||||||
|
FailureType: "EmailTest",
|
||||||
|
Device: "/dev/sda",
|
||||||
|
DeviceType: "ata",
|
||||||
|
DeviceString: "/dev/sda",
|
||||||
|
Message: "TEST EMAIL from smartd for device: /dev/sda",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
err := testNotify.Send()
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"success": false,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
c.JSON(http.StatusOK, dbModels.DeviceWrapper{
|
||||||
|
Success: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/analogj/scrutiny/webapp/backend/pkg/config"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ConfigMiddleware(appConfig config.Interface) gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
c.Set("CONFIG", appConfig)
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
-2
@@ -1,4 +1,4 @@
|
|||||||
package database
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
||||||
)
|
)
|
||||||
|
|
||||||
func DatabaseHandler(dbPath string) gin.HandlerFunc {
|
func DatabaseMiddleware(dbPath string) gin.HandlerFunc {
|
||||||
//var database *gorm.DB
|
//var database *gorm.DB
|
||||||
fmt.Printf("Trying to connect to database stored: %s", dbPath)
|
fmt.Printf("Trying to connect to database stored: %s", dbPath)
|
||||||
database, err := gorm.Open("sqlite3", dbPath)
|
database, err := gorm.Open("sqlite3", dbPath)
|
||||||
@@ -3,8 +3,8 @@ package web
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/analogj/scrutiny/webapp/backend/pkg/config"
|
"github.com/analogj/scrutiny/webapp/backend/pkg/config"
|
||||||
"github.com/analogj/scrutiny/webapp/backend/pkg/database"
|
|
||||||
"github.com/analogj/scrutiny/webapp/backend/pkg/web/handler"
|
"github.com/analogj/scrutiny/webapp/backend/pkg/web/handler"
|
||||||
|
"github.com/analogj/scrutiny/webapp/backend/pkg/web/middleware"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
@@ -16,7 +16,8 @@ type AppEngine struct {
|
|||||||
func (ae *AppEngine) Setup() *gin.Engine {
|
func (ae *AppEngine) Setup() *gin.Engine {
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
r.Use(database.DatabaseHandler(ae.Config.GetString("web.database.location")))
|
r.Use(middleware.DatabaseMiddleware(ae.Config.GetString("web.database.location")))
|
||||||
|
r.Use(middleware.ConfigMiddleware(ae.Config))
|
||||||
|
|
||||||
api := r.Group("/api")
|
api := r.Group("/api")
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user