some refactor

This commit is contained in:
Roee Gadot 2021-04-25 14:49:47 +03:00
parent d0119d6046
commit 3728ee08d6
7 changed files with 28 additions and 32 deletions

View File

@ -2,7 +2,7 @@ package main
import ( import (
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
// api "mizuserver" api "mizuserver"
"mizuserver/src/pkg/middleware" "mizuserver/src/pkg/middleware"
"mizuserver/src/pkg/routes" "mizuserver/src/pkg/routes"
"mizuserver/src/pkg/utils" "mizuserver/src/pkg/utils"
@ -10,8 +10,8 @@ import (
func main() { func main() {
// TODO: to generate data // TODO: to generate data
// path := "/Users/roeegadot/Downloads/output2" path := "/Users/roeegadot/Downloads/output2"
// api.TestHarSavingFromFolder(path) api.TestHarSavingFromFolder(path)
app := fiber.New() app := fiber.New()

View File

@ -6,11 +6,14 @@ import (
"github.com/google/martian/har" "github.com/google/martian/har"
"mizuserver/src/pkg/database" "mizuserver/src/pkg/database"
"mizuserver/src/pkg/models" "mizuserver/src/pkg/models"
"mizuserver/src/pkg/utils"
) )
func GetEntries(c *fiber.Ctx) error { func GetEntries(c *fiber.Ctx) error {
var baseEntries []models.BaseEntryDetails var baseEntries []models.BaseEntryDetails
database.EntriesCollection.Find(&baseEntries) database.EntriesTable.
Find(&baseEntries).
Limit(100)
return c.Status(fiber.StatusOK).JSON(fiber.Map{ return c.Status(fiber.StatusOK).JSON(fiber.Map{
"error": false, "error": false,
@ -21,17 +24,18 @@ 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.EntriesCollection. database.EntriesTable.
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
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{ return c.Status(fiber.StatusOK).JSON(fiber.Map{
"error": false, "error": false,
"msg": entryObject, "msg": fullEntry,
}) })
} }

View File

@ -11,12 +11,12 @@ const (
) )
var ( var (
DB = initDataBase(DBPath) DB = initDataBase(DBPath)
EntriesCollection = DB.Table("mizu_entries") EntriesTable = 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
return temp return temp
} }

View File

@ -6,6 +6,7 @@ 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"`
@ -18,12 +19,12 @@ type MizuEntry struct {
} }
type BaseEntryDetails struct { type BaseEntryDetails struct {
Id string `json:"id,omitempty"` Id string `json:"id,omitempty"` // TODO: can be removed
EntryId string `json:"entryId,omitempty"` EntryId string `json:"entryId,omitempty" gorm:"column:entryId"`
Url string `json:"url,omitempty"` Url string `json:"url,omitempty"`
ServiceName string `json:"serviceName,omitempty"` ServiceName string `json:"serviceName,omitempty" gorm:"column:serviceName"`
Path string `json:"path,omitempty"` Path string `json:"path,omitempty"`
StatusCode int `json:"statusCode,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"`
} }

View File

@ -3,12 +3,9 @@ package routes
import "github.com/gofiber/fiber/v2" import "github.com/gofiber/fiber/v2"
// NotFoundRoute func for describe 404 Error route. // NotFoundRoute func for describe 404 Error route.
func NotFoundRoute(a *fiber.App) { func NotFoundRoute(fiberApp *fiber.App) {
// Register new special route. fiberApp.Use(
a.Use(
// Anonymous function.
func(c *fiber.Ctx) error { func(c *fiber.Ctx) error {
// Return HTTP 404 status and JSON response.
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{ return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
"error": true, "error": true,
"msg": "sorry, endpoint is not found", "msg": "sorry, endpoint is not found",

View File

@ -1,20 +1,14 @@
package routes package routes
import ( import (
"mizuserver/src/pkg/controllers"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"mizuserver/src/pkg/controllers"
) )
// PublicRoutes func for describe group of public routes. // PublicRoutes func for describe group of public routes.
func PublicRoutes(a *fiber.App) { func PublicRoutes(fiberApp *fiber.App) {
//controllers.GenerateData() routeGroup := fiberApp.Group("/api")
// 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
routeGroup.Get("/entries", controllers.GetEntries) // get entries (base/thin entries)
routeGroup.Get("/entries/:entryId", controllers.GetEntry) // get single (full) entry
} }

View File

@ -49,7 +49,7 @@ func SaveHarToDb(entry har.Entry, source string) {
Source: source, Source: source,
Timestamp: entry.StartedDateTime.Unix(), Timestamp: entry.StartedDateTime.Unix(),
} }
database.EntriesCollection.Create(&mizuEntry) database.EntriesTable.Create(&mizuEntry)
} }
func getServiceNameFromUrl(inputUrl string) (string, string) { func getServiceNameFromUrl(inputUrl string) (string, string) {