mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-06-26 00:04:33 +00:00
Add api build and clean to makefile (files restructure) (#9)
* no message * add clean api command
This commit is contained in:
parent
1d2462562e
commit
0061f7d14a
5
Makefile
5
Makefile
@ -27,7 +27,8 @@ cli: # build CLI
|
|||||||
@(cd cli; echo "building cli" )
|
@(cd cli; echo "building cli" )
|
||||||
|
|
||||||
api: ## build API server
|
api: ## build API server
|
||||||
@(cd api; echo "building api" )
|
@(cd api; go build -o build/apiserver main.go)
|
||||||
|
@ls -l api/build
|
||||||
|
|
||||||
docker: ## build Docker image
|
docker: ## build Docker image
|
||||||
@(echo "building docker image" )
|
@(echo "building docker image" )
|
||||||
@ -43,7 +44,7 @@ clean-ui:
|
|||||||
@(cd ui; rm -rf build ; echo "ui cleanup done" )
|
@(cd ui; rm -rf build ; echo "ui cleanup done" )
|
||||||
|
|
||||||
clean-api:
|
clean-api:
|
||||||
@(echo "API cleanup - NOT IMPLEMENTED YET " )
|
@(cd api; rm -rf build ; echo "api cleanup done" )
|
||||||
|
|
||||||
clean-cli:
|
clean-cli:
|
||||||
@(echo "CLI cleanup - NOT IMPLEMENTED YET " )
|
@(echo "CLI cleanup - NOT IMPLEMENTED YET " )
|
||||||
|
@ -2,27 +2,30 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"mizuserver/src/pkg/inserter"
|
"mizuserver/pkg/middleware"
|
||||||
"mizuserver/src/pkg/middleware"
|
"mizuserver/pkg/routes"
|
||||||
"mizuserver/src/pkg/routes"
|
"mizuserver/pkg/utils"
|
||||||
"mizuserver/src/pkg/utils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
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)
|
||||||
go inserter.StartReadingFiles("/var/up9hars")
|
|
||||||
|
// TODO: disabling this line for now (this should be as part of the MAIN
|
||||||
|
// go inserter.StartReadingFiles("/var/up9hars")
|
||||||
app := fiber.New()
|
app := fiber.New()
|
||||||
|
|
||||||
middleware.FiberMiddleware(app) // Register Fiber's middleware for app.
|
middleware.FiberMiddleware(app) // Register Fiber's middleware for app.
|
||||||
|
|
||||||
|
app.Static("/", "./site")
|
||||||
|
|
||||||
// Simple route to know server is running
|
// Simple route to know server is running
|
||||||
app.Get("/", func(c *fiber.Ctx) error {
|
app.Get("/echo", func(c *fiber.Ctx) error {
|
||||||
return c.SendString("Hello, World 👋!")
|
return c.SendString("Hello, World 👋!")
|
||||||
})
|
})
|
||||||
|
|
||||||
routes.PublicRoutes(app)
|
routes.EntriesRoutes(app)
|
||||||
routes.NotFoundRoute(app)
|
routes.NotFoundRoute(app)
|
||||||
|
|
||||||
utils.StartServer(app)
|
utils.StartServer(app)
|
@ -4,9 +4,9 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/google/martian/har"
|
"github.com/google/martian/har"
|
||||||
"mizuserver/src/pkg/database"
|
"mizuserver/pkg/database"
|
||||||
"mizuserver/src/pkg/models"
|
"mizuserver/pkg/models"
|
||||||
"mizuserver/src/pkg/utils"
|
"mizuserver/pkg/utils"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
@ -3,7 +3,7 @@ package database
|
|||||||
import (
|
import (
|
||||||
"gorm.io/driver/sqlite"
|
"gorm.io/driver/sqlite"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"mizuserver/src/pkg/models"
|
"mizuserver/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
@ -8,9 +8,9 @@ import (
|
|||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"mizuserver/src/pkg/database"
|
"mizuserver/pkg/database"
|
||||||
"mizuserver/src/pkg/models"
|
"mizuserver/pkg/models"
|
||||||
"mizuserver/src/pkg/utils"
|
"mizuserver/pkg/utils"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
@ -19,7 +19,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func IsEmpty(name string) (bool) {
|
func IsEmpty(name string) bool {
|
||||||
f, err := os.Open(name)
|
f, err := os.Open(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
@ -58,7 +58,6 @@ func StartReadingFiles(workingDir string) {
|
|||||||
utils.CheckErr(decErr)
|
utils.CheckErr(decErr)
|
||||||
|
|
||||||
for _, entry := range inputHar.Log.Entries {
|
for _, entry := range inputHar.Log.Entries {
|
||||||
fmt.Printf("Entry inserted")
|
|
||||||
SaveHarToDb(*entry, "")
|
SaveHarToDb(*entry, "")
|
||||||
}
|
}
|
||||||
rmErr := os.Remove(inputFilePath)
|
rmErr := os.Remove(inputFilePath)
|
@ -2,11 +2,11 @@ package routes
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"mizuserver/src/pkg/controllers"
|
"mizuserver/pkg/controllers"
|
||||||
)
|
)
|
||||||
|
|
||||||
// PublicRoutes func for describe group of public routes.
|
// EntriesRoutes func for describe group of public routes.
|
||||||
func PublicRoutes(fiberApp *fiber.App) {
|
func EntriesRoutes(fiberApp *fiber.App) {
|
||||||
routeGroup := fiberApp.Group("/api")
|
routeGroup := fiberApp.Group("/api")
|
||||||
|
|
||||||
routeGroup.Get("/entries", controllers.GetEntries) // get entries (base/thin entries)
|
routeGroup.Get("/entries", controllers.GetEntries) // get entries (base/thin entries)
|
Binary file not shown.
@ -1,55 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"gorm.io/driver/sqlite"
|
|
||||||
_ "gorm.io/driver/sqlite"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
type User struct {
|
|
||||||
gorm.Model // this will add "id", "created updated inserted times"
|
|
||||||
|
|
||||||
Name string
|
|
||||||
Age int
|
|
||||||
Birthday time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
func main () {
|
|
||||||
// github.com/mattn/go-sqlite3
|
|
||||||
db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{})
|
|
||||||
if err != nil{
|
|
||||||
panic("ff")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run migrations
|
|
||||||
migErr := db.AutoMigrate(&User{})
|
|
||||||
if migErr != nil {
|
|
||||||
panic("Cannot run migration")
|
|
||||||
}
|
|
||||||
|
|
||||||
user := User{Name: "Jinzhu", Age: 18, Birthday: time.Now()}
|
|
||||||
db.Create(&user) // pass pointer of data to Create
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//func main() {
|
|
||||||
// app := fiber.New()
|
|
||||||
//
|
|
||||||
// c := make(chan os.Signal, 1)
|
|
||||||
// signal.Notify(c, os.Interrupt)
|
|
||||||
// go func() {
|
|
||||||
// _ = <-c
|
|
||||||
// fmt.Println("Gracefully shutting down...")
|
|
||||||
// _ = app.Shutdown()
|
|
||||||
// }()
|
|
||||||
//
|
|
||||||
// if err := app.Listen(":3000"); err != nil {
|
|
||||||
// log.Panic(err)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// fmt.Println("Running cleanup tasks...")
|
|
||||||
// // Your cleanup tasks go here
|
|
||||||
//}
|
|
@ -1,59 +0,0 @@
|
|||||||
package api
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"github.com/google/martian/har"
|
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
||||||
"mizuserver/src/pkg/database"
|
|
||||||
"mizuserver/src/pkg/models"
|
|
||||||
"mizuserver/src/pkg/utils"
|
|
||||||
"net/url"
|
|
||||||
"os"
|
|
||||||
"path"
|
|
||||||
"sort"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestHarSavingFromFolder(inputDir string) {
|
|
||||||
dir, _ := os.Open(inputDir)
|
|
||||||
dirFiles, _ := dir.Readdir(-1)
|
|
||||||
sort.Sort(utils.ByModTime(dirFiles))
|
|
||||||
|
|
||||||
for _, fileInfo := range dirFiles {
|
|
||||||
inputFilePath := path.Join(inputDir, fileInfo.Name())
|
|
||||||
file, err := os.Open(inputFilePath)
|
|
||||||
utils.CheckErr(err)
|
|
||||||
|
|
||||||
var inputHar har.HAR
|
|
||||||
decErr := json.NewDecoder(bufio.NewReader(file)).Decode(&inputHar)
|
|
||||||
utils.CheckErr(decErr)
|
|
||||||
|
|
||||||
for _, entry := range inputHar.Log.Entries {
|
|
||||||
SaveHarToDb(*entry, "")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
Service: serviceName,
|
|
||||||
Url: entry.Request.URL,
|
|
||||||
Path: urlPath,
|
|
||||||
Method: entry.Request.Method,
|
|
||||||
Status: entry.Response.Status,
|
|
||||||
Source: source,
|
|
||||||
Timestamp: entry.StartedDateTime.Unix(),
|
|
||||||
}
|
|
||||||
database.GetEntriesTable().Create(&mizuEntry)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getServiceNameFromUrl(inputUrl string) (string, string) {
|
|
||||||
parsed, err := url.Parse(inputUrl)
|
|
||||||
utils.CheckErr(err)
|
|
||||||
return fmt.Sprintf("%s://%s", parsed.Scheme, parsed.Host), parsed.Path
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user