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 (
"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()

View File

@ -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,
})
}

View File

@ -11,8 +11,8 @@ const (
)
var (
DB = initDataBase(DBPath)
EntriesCollection = DB.Table("mizu_entries")
DB = initDataBase(DBPath)
EntriesTable = DB.Table("mizu_entries")
)
func initDataBase(databasePath string) *gorm.DB {

View File

@ -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"`
}

View File

@ -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",

View File

@ -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
}

View File

@ -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) {