diff --git a/api/src/main.go b/api/src/main.go index 0695ab038..0abd8b11a 100644 --- a/api/src/main.go +++ b/api/src/main.go @@ -2,7 +2,7 @@ package main import ( "github.com/gofiber/fiber/v2" - // api "mizuserver" + api "mizuserver" "mizuserver/src/pkg/middleware" "mizuserver/src/pkg/routes" "mizuserver/src/pkg/utils" @@ -10,8 +10,8 @@ import ( func main() { // TODO: to generate data - // path := "/Users/roeegadot/Downloads/output2" - // api.TestHarSavingFromFolder(path) + path := "/Users/roeegadot/Downloads/output2" + api.TestHarSavingFromFolder(path) app := fiber.New() diff --git a/api/src/pkg/controllers/entries_controller.go b/api/src/pkg/controllers/entries_controller.go index 78f559e9e..2f64ab921 100644 --- a/api/src/pkg/controllers/entries_controller.go +++ b/api/src/pkg/controllers/entries_controller.go @@ -6,11 +6,14 @@ import ( "github.com/google/martian/har" "mizuserver/src/pkg/database" "mizuserver/src/pkg/models" + "mizuserver/src/pkg/utils" ) func GetEntries(c *fiber.Ctx) error { var baseEntries []models.BaseEntryDetails - database.EntriesCollection.Find(&baseEntries) + database.EntriesTable. + Find(&baseEntries). + Limit(100) return c.Status(fiber.StatusOK).JSON(fiber.Map{ "error": false, @@ -21,17 +24,18 @@ func GetEntries(c *fiber.Ctx) error { func GetEntry(c *fiber.Ctx) error { var entryData models.EntryData - database.EntriesCollection. + database.EntriesTable. 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 - entryObject := json.Unmarshal([]byte(entryData.Entry), &fullEntry) - + unmarshallErr := json.Unmarshal([]byte(entryData.Entry), &fullEntry) + utils.CheckErr(unmarshallErr) return c.Status(fiber.StatusOK).JSON(fiber.Map{ "error": false, - "msg": entryObject, + "msg": fullEntry, }) } diff --git a/api/src/pkg/database/utils.go b/api/src/pkg/database/utils.go index 005c14fe7..1771fcaac 100644 --- a/api/src/pkg/database/utils.go +++ b/api/src/pkg/database/utils.go @@ -11,12 +11,12 @@ const ( ) var ( - DB = initDataBase(DBPath) - EntriesCollection = DB.Table("mizu_entries") + DB = initDataBase(DBPath) + EntriesTable = 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 return temp -} \ No newline at end of file +} diff --git a/api/src/pkg/models/mizuEntry.go b/api/src/pkg/models/mizuEntry.go index 7df0620b5..f6bbe27f4 100644 --- a/api/src/pkg/models/mizuEntry.go +++ b/api/src/pkg/models/mizuEntry.go @@ -6,6 +6,7 @@ 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"` @@ -18,12 +19,12 @@ type MizuEntry struct { } type BaseEntryDetails struct { - Id string `json:"id,omitempty"` - EntryId string `json:"entryId,omitempty"` + 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"` + ServiceName string `json:"serviceName,omitempty" gorm:"column:serviceName"` Path string `json:"path,omitempty"` - StatusCode int `json:"statusCode,omitempty"` + Status int `json:"status,omitempty"` Method string `json:"method,omitempty"` Timestamp int64 `json:"timestamp,omitempty"` } diff --git a/api/src/pkg/routes/not_found_route.go b/api/src/pkg/routes/not_found_route.go index e0fcda923..1aa1e7da7 100644 --- a/api/src/pkg/routes/not_found_route.go +++ b/api/src/pkg/routes/not_found_route.go @@ -3,12 +3,9 @@ package routes import "github.com/gofiber/fiber/v2" // NotFoundRoute func for describe 404 Error route. -func NotFoundRoute(a *fiber.App) { - // Register new special route. - a.Use( - // Anonymous function. +func NotFoundRoute(fiberApp *fiber.App) { + fiberApp.Use( func(c *fiber.Ctx) error { - // Return HTTP 404 status and JSON response. return c.Status(fiber.StatusNotFound).JSON(fiber.Map{ "error": true, "msg": "sorry, endpoint is not found", diff --git a/api/src/pkg/routes/public_routes.go b/api/src/pkg/routes/public_routes.go index 532b96008..04a4d6a1e 100644 --- a/api/src/pkg/routes/public_routes.go +++ b/api/src/pkg/routes/public_routes.go @@ -1,20 +1,14 @@ package routes import ( - "mizuserver/src/pkg/controllers" - "github.com/gofiber/fiber/v2" + "mizuserver/src/pkg/controllers" ) // PublicRoutes func for describe group of public routes. -func PublicRoutes(a *fiber.App) { - //controllers.GenerateData() - - // Create routes group. - route := a.Group("/api") - - // Routes for GET method: - route.Get("/entries", controllers.GetEntries) // get list of all books - route.Get("/entries/:entryId", controllers.GetEntry) // get one book by ID +func PublicRoutes(fiberApp *fiber.App) { + routeGroup := fiberApp.Group("/api") + routeGroup.Get("/entries", controllers.GetEntries) // get entries (base/thin entries) + routeGroup.Get("/entries/:entryId", controllers.GetEntry) // get single (full) entry } diff --git a/api/testing_from_file.go b/api/testing_from_file.go index 98a2fe40c..4c82e6d14 100644 --- a/api/testing_from_file.go +++ b/api/testing_from_file.go @@ -49,7 +49,7 @@ func SaveHarToDb(entry har.Entry, source string) { Source: source, Timestamp: entry.StartedDateTime.Unix(), } - database.EntriesCollection.Create(&mizuEntry) + database.EntriesTable.Create(&mizuEntry) } func getServiceNameFromUrl(inputUrl string) (string, string) {