mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-06-23 14:58:44 +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" )
|
||||
|
||||
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
|
||||
@(echo "building docker image" )
|
||||
@ -43,7 +44,7 @@ clean-ui:
|
||||
@(cd ui; rm -rf build ; echo "ui cleanup done" )
|
||||
|
||||
clean-api:
|
||||
@(echo "API cleanup - NOT IMPLEMENTED YET " )
|
||||
@(cd api; rm -rf build ; echo "api cleanup done" )
|
||||
|
||||
clean-cli:
|
||||
@(echo "CLI cleanup - NOT IMPLEMENTED YET " )
|
||||
|
@ -2,27 +2,30 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"mizuserver/src/pkg/inserter"
|
||||
"mizuserver/src/pkg/middleware"
|
||||
"mizuserver/src/pkg/routes"
|
||||
"mizuserver/src/pkg/utils"
|
||||
"mizuserver/pkg/middleware"
|
||||
"mizuserver/pkg/routes"
|
||||
"mizuserver/pkg/utils"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// TODO: to generate data
|
||||
//path := "/Users/roeegadot/Downloads/output2"
|
||||
//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()
|
||||
|
||||
middleware.FiberMiddleware(app) // Register Fiber's middleware for app.
|
||||
|
||||
app.Static("/", "./site")
|
||||
|
||||
// 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 👋!")
|
||||
})
|
||||
|
||||
routes.PublicRoutes(app)
|
||||
routes.EntriesRoutes(app)
|
||||
routes.NotFoundRoute(app)
|
||||
|
||||
utils.StartServer(app)
|
@ -4,9 +4,9 @@ import (
|
||||
"encoding/json"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/martian/har"
|
||||
"mizuserver/src/pkg/database"
|
||||
"mizuserver/src/pkg/models"
|
||||
"mizuserver/src/pkg/utils"
|
||||
"mizuserver/pkg/database"
|
||||
"mizuserver/pkg/models"
|
||||
"mizuserver/pkg/utils"
|
||||
"strconv"
|
||||
)
|
||||
|
@ -3,7 +3,7 @@ package database
|
||||
import (
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"mizuserver/src/pkg/models"
|
||||
"mizuserver/pkg/models"
|
||||
)
|
||||
|
||||
const (
|
@ -8,9 +8,9 @@ import (
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"io"
|
||||
"io/fs"
|
||||
"mizuserver/src/pkg/database"
|
||||
"mizuserver/src/pkg/models"
|
||||
"mizuserver/src/pkg/utils"
|
||||
"mizuserver/pkg/database"
|
||||
"mizuserver/pkg/models"
|
||||
"mizuserver/pkg/utils"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
@ -19,7 +19,7 @@ import (
|
||||
)
|
||||
|
||||
|
||||
func IsEmpty(name string) (bool) {
|
||||
func IsEmpty(name string) bool {
|
||||
f, err := os.Open(name)
|
||||
if err != nil {
|
||||
return false
|
||||
@ -58,7 +58,6 @@ func StartReadingFiles(workingDir string) {
|
||||
utils.CheckErr(decErr)
|
||||
|
||||
for _, entry := range inputHar.Log.Entries {
|
||||
fmt.Printf("Entry inserted")
|
||||
SaveHarToDb(*entry, "")
|
||||
}
|
||||
rmErr := os.Remove(inputFilePath)
|
@ -2,11 +2,11 @@ package routes
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"mizuserver/src/pkg/controllers"
|
||||
"mizuserver/pkg/controllers"
|
||||
)
|
||||
|
||||
// PublicRoutes func for describe group of public routes.
|
||||
func PublicRoutes(fiberApp *fiber.App) {
|
||||
// EntriesRoutes func for describe group of public routes.
|
||||
func EntriesRoutes(fiberApp *fiber.App) {
|
||||
routeGroup := fiberApp.Group("/api")
|
||||
|
||||
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