no message

This commit is contained in:
Roee Gadot 2021-04-27 09:45:40 +03:00
parent 3728ee08d6
commit d715473088
5 changed files with 58 additions and 39 deletions

View File

@ -7,13 +7,32 @@ import (
"mizuserver/src/pkg/database" "mizuserver/src/pkg/database"
"mizuserver/src/pkg/models" "mizuserver/src/pkg/models"
"mizuserver/src/pkg/utils" "mizuserver/src/pkg/utils"
"strconv"
) )
func GetEntries(c *fiber.Ctx) error { func GetEntries(c *fiber.Ctx) error {
var baseEntries []models.BaseEntryDetails limit, e := strconv.Atoi(c.Query("limit", "100"))
database.EntriesTable. utils.CheckErr(e)
Find(&baseEntries).
Limit(100) var entries []models.MizuEntry
database.GetEntriesTable().
Omit("entry"). // remove the "big" entry field
Limit(limit).
Find(&entries)
// Convert to base entries
baseEntries := make([]models.BaseEntryDetails, 0)
for _, entry := range entries {
baseEntries = append(baseEntries, models.BaseEntryDetails{
Id: entry.EntryId,
Url: entry.Url,
Service: entry.Service,
Path: entry.Path,
Status: entry.Status,
Method: entry.Method,
Timestamp: entry.Timestamp,
})
}
return c.Status(fiber.StatusOK).JSON(fiber.Map{ return c.Status(fiber.StatusOK).JSON(fiber.Map{
"error": false, "error": false,
@ -21,15 +40,14 @@ func GetEntries(c *fiber.Ctx) error {
}) })
} }
func GetEntry(c *fiber.Ctx) error {
func GetEntry(c *fiber.Ctx) error {
var entryData models.EntryData var entryData models.EntryData
database.EntriesTable. database.GetEntriesTable().
Select("entry"). Select("entry").
Where(map[string]string{"entryId": c.Params("entryId")}). Where(map[string]string{"entryId": c.Params("entryId")}).
First(&entryData) First(&entryData)
// TODO: check why don't we get entry here
var fullEntry har.Entry var fullEntry har.Entry
unmarshallErr := json.Unmarshal([]byte(entryData.Entry), &fullEntry) unmarshallErr := json.Unmarshal([]byte(entryData.Entry), &fullEntry)
utils.CheckErr(unmarshallErr) utils.CheckErr(unmarshallErr)

View File

@ -12,9 +12,12 @@ const (
var ( var (
DB = initDataBase(DBPath) DB = initDataBase(DBPath)
EntriesTable = DB.Table("mizu_entries")
) )
func GetEntriesTable() *gorm.DB{
return DB.Table("mizu_entries")
}
func initDataBase(databasePath string) *gorm.DB { func initDataBase(databasePath string) *gorm.DB {
temp, _ := gorm.Open(sqlite.Open(databasePath), &gorm.Config{}) temp, _ := gorm.Open(sqlite.Open(databasePath), &gorm.Config{})
_ = temp.AutoMigrate(&models.MizuEntry{}) // this will ensure table is created _ = temp.AutoMigrate(&models.MizuEntry{}) // this will ensure table is created

View File

@ -6,30 +6,27 @@ import (
type MizuEntry struct { type MizuEntry struct {
gorm.Model gorm.Model
// TODO: map id to _id? Entry string `json:"entry,omitempty" gorm:"column:entry"`
Entry string `json:"entry,omitempty" gorm:"column:entry"` EntryId string `json:"entryId" gorm:"column:entryId"`
EntryId string `json:"entryId" gorm:"column:entryId"` Url string `json:"url" gorm:"column:url"`
Url string `json:"url" gorm:"column:url"` Method string `json:"method" gorm:"column:method"`
Method string `json:"method" gorm:"column:method"` Status int `json:"status" gorm:"column:status"`
Status int `json:"status" gorm:"column:status"` Source string `json:"source" gorm:"column:source"`
Source string `json:"source" gorm:"column:source"` Service string `json:"service" gorm:"column:service"`
ServiceName string `json:"serviceName" gorm:"column:serviceName"` Timestamp int64 `json:"timestamp" gorm:"column:timestamp"`
Timestamp int64 `json:"timestamp" gorm:"column:timestamp"` Path string `json:"path" gorm:"column:path"`
Path string `json:"path" gorm:"column:path"`
} }
type BaseEntryDetails struct { type BaseEntryDetails struct {
Id string `json:"id,omitempty"` // TODO: can be removed Id string `json:"id,omitempty"`
EntryId string `json:"entryId,omitempty" gorm:"column:entryId"` Url string `json:"url,omitempty"`
Url string `json:"url,omitempty"` Service string `json:"service,omitempty"`
ServiceName string `json:"serviceName,omitempty" gorm:"column:serviceName"` Path string `json:"path,omitempty"`
Path string `json:"path,omitempty"` Status int `json:"status,omitempty"`
Status int `json:"status,omitempty"` Method string `json:"method,omitempty"`
Method string `json:"method,omitempty"` Timestamp int64 `json:"timestamp,omitempty"`
Timestamp int64 `json:"timestamp,omitempty"`
} }
type EntryData struct { type EntryData struct {
Entry string `json:"entry,omitempty"` Entry string `json:"entry,omitempty"`
} }

View File

@ -32,6 +32,7 @@ func StartServer(app *fiber.App) {
func CheckErr(e error) { func CheckErr(e error) {
if e != nil { if e != nil {
panic(e) log.Printf("%v", e)
//panic(e)
} }
} }

View File

@ -39,17 +39,17 @@ func SaveHarToDb(entry har.Entry, source string) {
entryBytes, _ := json.Marshal(entry) entryBytes, _ := json.Marshal(entry)
serviceName, urlPath := getServiceNameFromUrl(entry.Request.URL) serviceName, urlPath := getServiceNameFromUrl(entry.Request.URL)
mizuEntry := models.MizuEntry{ mizuEntry := models.MizuEntry{
EntryId: primitive.NewObjectID().Hex(), EntryId: primitive.NewObjectID().Hex(),
Entry: string(entryBytes), // simple way to store it and not convert to bytes Entry: string(entryBytes), // simple way to store it and not convert to bytes
ServiceName: serviceName, Service: serviceName,
Url: entry.Request.URL, Url: entry.Request.URL,
Path: urlPath, Path: urlPath,
Method: entry.Request.Method, Method: entry.Request.Method,
Status: entry.Response.Status, Status: entry.Response.Status,
Source: source, Source: source,
Timestamp: entry.StartedDateTime.Unix(), Timestamp: entry.StartedDateTime.Unix(),
} }
database.EntriesTable.Create(&mizuEntry) database.GetEntriesTable().Create(&mizuEntry)
} }
func getServiceNameFromUrl(inputUrl string) (string, string) { func getServiceNameFromUrl(inputUrl string) (string, string) {