diff --git a/api/src/pkg/controllers/entries_controller.go b/api/src/pkg/controllers/entries_controller.go index 2f64ab921..194a311a5 100644 --- a/api/src/pkg/controllers/entries_controller.go +++ b/api/src/pkg/controllers/entries_controller.go @@ -7,13 +7,32 @@ import ( "mizuserver/src/pkg/database" "mizuserver/src/pkg/models" "mizuserver/src/pkg/utils" + "strconv" ) func GetEntries(c *fiber.Ctx) error { - var baseEntries []models.BaseEntryDetails - database.EntriesTable. - Find(&baseEntries). - Limit(100) + limit, e := strconv.Atoi(c.Query("limit", "100")) + utils.CheckErr(e) + + 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{ "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 - database.EntriesTable. + database.GetEntriesTable(). Select("entry"). Where(map[string]string{"entryId": c.Params("entryId")}). First(&entryData) - // TODO: check why don't we get entry here var fullEntry har.Entry unmarshallErr := json.Unmarshal([]byte(entryData.Entry), &fullEntry) utils.CheckErr(unmarshallErr) diff --git a/api/src/pkg/database/utils.go b/api/src/pkg/database/utils.go index 1771fcaac..4a52eedd0 100644 --- a/api/src/pkg/database/utils.go +++ b/api/src/pkg/database/utils.go @@ -12,9 +12,12 @@ const ( var ( DB = initDataBase(DBPath) - EntriesTable = DB.Table("mizu_entries") ) +func GetEntriesTable() *gorm.DB{ + return DB.Table("mizu_entries") +} + func initDataBase(databasePath string) *gorm.DB { temp, _ := gorm.Open(sqlite.Open(databasePath), &gorm.Config{}) _ = temp.AutoMigrate(&models.MizuEntry{}) // this will ensure table is created diff --git a/api/src/pkg/models/mizuEntry.go b/api/src/pkg/models/mizuEntry.go index f6bbe27f4..89745fdf7 100644 --- a/api/src/pkg/models/mizuEntry.go +++ b/api/src/pkg/models/mizuEntry.go @@ -6,30 +6,27 @@ import ( type MizuEntry struct { gorm.Model - // TODO: map id to _id? - Entry string `json:"entry,omitempty" gorm:"column:entry"` - EntryId string `json:"entryId" gorm:"column:entryId"` - Url string `json:"url" gorm:"column:url"` - Method string `json:"method" gorm:"column:method"` - Status int `json:"status" gorm:"column:status"` - Source string `json:"source" gorm:"column:source"` - ServiceName string `json:"serviceName" gorm:"column:serviceName"` - Timestamp int64 `json:"timestamp" gorm:"column:timestamp"` - Path string `json:"path" gorm:"column:path"` + Entry string `json:"entry,omitempty" gorm:"column:entry"` + EntryId string `json:"entryId" gorm:"column:entryId"` + Url string `json:"url" gorm:"column:url"` + Method string `json:"method" gorm:"column:method"` + Status int `json:"status" gorm:"column:status"` + Source string `json:"source" gorm:"column:source"` + Service string `json:"service" gorm:"column:service"` + Timestamp int64 `json:"timestamp" gorm:"column:timestamp"` + Path string `json:"path" gorm:"column:path"` } type BaseEntryDetails struct { - Id string `json:"id,omitempty"` // TODO: can be removed - EntryId string `json:"entryId,omitempty" gorm:"column:entryId"` - Url string `json:"url,omitempty"` - ServiceName string `json:"serviceName,omitempty" gorm:"column:serviceName"` - Path string `json:"path,omitempty"` - Status int `json:"status,omitempty"` - Method string `json:"method,omitempty"` - Timestamp int64 `json:"timestamp,omitempty"` + Id string `json:"id,omitempty"` + Url string `json:"url,omitempty"` + Service string `json:"service,omitempty"` + Path string `json:"path,omitempty"` + Status int `json:"status,omitempty"` + Method string `json:"method,omitempty"` + Timestamp int64 `json:"timestamp,omitempty"` } - type EntryData struct { Entry string `json:"entry,omitempty"` -} \ No newline at end of file +} diff --git a/api/src/pkg/utils/utils.go b/api/src/pkg/utils/utils.go index 108fe3b89..789756bcb 100644 --- a/api/src/pkg/utils/utils.go +++ b/api/src/pkg/utils/utils.go @@ -32,6 +32,7 @@ func StartServer(app *fiber.App) { func CheckErr(e error) { if e != nil { - panic(e) + log.Printf("%v", e) + //panic(e) } } \ No newline at end of file diff --git a/api/testing_from_file.go b/api/testing_from_file.go index 4c82e6d14..fff207e64 100644 --- a/api/testing_from_file.go +++ b/api/testing_from_file.go @@ -39,17 +39,17 @@ func SaveHarToDb(entry har.Entry, source string) { entryBytes, _ := json.Marshal(entry) serviceName, urlPath := getServiceNameFromUrl(entry.Request.URL) mizuEntry := models.MizuEntry{ - EntryId: primitive.NewObjectID().Hex(), - Entry: string(entryBytes), // simple way to store it and not convert to bytes - ServiceName: serviceName, - Url: entry.Request.URL, - Path: urlPath, - Method: entry.Request.Method, - Status: entry.Response.Status, - Source: source, - Timestamp: entry.StartedDateTime.Unix(), + EntryId: primitive.NewObjectID().Hex(), + Entry: string(entryBytes), // simple way to store it and not convert to bytes + Service: serviceName, + Url: entry.Request.URL, + Path: urlPath, + Method: entry.Request.Method, + Status: entry.Response.Status, + Source: source, + Timestamp: entry.StartedDateTime.Unix(), } - database.EntriesTable.Create(&mizuEntry) + database.GetEntriesTable().Create(&mizuEntry) } func getServiceNameFromUrl(inputUrl string) (string, string) {