Skip to content

Commit 930c8e2

Browse files
author
jp
committed
split up the backend
1 parent a51cc87 commit 930c8e2

27 files changed

+277
-112
lines changed

backend/backend.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package backend
22

3-
import "soarca-gui/models/reporting"
3+
import "soarca-gui/models/reporter"
44

5-
type Backend interface {
6-
GetReports() ([]reporting.PlaybookExecutionReport, error)
7-
GetReportsById(string) (reporting.PlaybookExecutionReport, error)
5+
type Report interface {
6+
GetReports() ([]reporter.PlaybookExecutionReport, error)
7+
GetReportsById(string) (reporter.PlaybookExecutionReport, error)
8+
}
9+
10+
type Status interface {
811
GetPongFromStatus() (string, error)
912
}

backend/soarca/helper.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package soarca
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io"
7+
"net/http"
8+
)
9+
10+
func fetchToJson(client http.Client, url string, target interface{}) error {
11+
body, err := fetch(client, url)
12+
if err != nil {
13+
return err
14+
}
15+
err = json.Unmarshal(body, target)
16+
if err != nil {
17+
return fmt.Errorf("failed to unmarshal JSON object: %w", err)
18+
}
19+
return nil
20+
}
21+
22+
func fetch(client http.Client, url string) ([]byte, error) {
23+
response, err := client.Get(url)
24+
if err != nil {
25+
return []byte{}, fmt.Errorf("failed to make GET request: %w", err)
26+
}
27+
defer response.Body.Close()
28+
29+
if response.StatusCode != http.StatusOK {
30+
return []byte{}, fmt.Errorf("unexpected status code: %d", response.StatusCode)
31+
}
32+
33+
body, err := io.ReadAll(response.Body)
34+
if err != nil {
35+
return []byte{}, fmt.Errorf("failed to read response body: %w", err)
36+
}
37+
38+
if len(body) == 0 {
39+
return []byte{}, fmt.Errorf("empty response body")
40+
}
41+
return body, nil
42+
}

backend/soarca/report.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package soarca
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
"soarca-gui/models/reporter"
8+
)
9+
10+
const (
11+
statusPingPath = "/status/ping"
12+
reporterPath = "/reporter"
13+
reporterApiPathId = "/reporter/:id"
14+
)
15+
16+
type Report struct {
17+
Host string
18+
client http.Client
19+
}
20+
21+
func NewReporter(host string, client http.Client) *Report {
22+
return &Report{Host: host, client: client}
23+
}
24+
25+
func (report *Report) GetReports() ([]reporter.PlaybookExecutionReport, error) {
26+
url := fmt.Sprintf("%s%s", report.Host, reporterPath)
27+
28+
var reportings []reporter.PlaybookExecutionReport
29+
err := fetchToJson(report.client, url, &reportings)
30+
if err != nil {
31+
return nil, err
32+
}
33+
34+
return reportings, nil
35+
}
36+
37+
func (report *Report) GetReportsById(Id string) (reporter.PlaybookExecutionReport, error) {
38+
url := fmt.Sprintf("%s%s/%s", report.Host, reporterApiPathId, Id)
39+
var returnReport reporter.PlaybookExecutionReport
40+
41+
err := fetchToJson(report.client, url, &returnReport)
42+
if err != nil {
43+
return reporter.PlaybookExecutionReport{}, err
44+
}
45+
return returnReport, nil
46+
}

backend/soarca/soarca.go

Lines changed: 0 additions & 93 deletions
This file was deleted.

backend/soarca/status.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package soarca
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
type Status struct {
9+
Host string
10+
client http.Client
11+
}
12+
13+
func NewStatus(host string, client http.Client) *Status {
14+
return &Status{Host: host, client: client}
15+
}
16+
17+
func (status *Status) GetPongFromStatus() (string, error) {
18+
url := fmt.Sprintf("%s%s", status.Host, statusPingPath)
19+
20+
body, err := fetch(status.client, url)
21+
if err != nil {
22+
return "", fmt.Errorf("failed to read response body: %w", err)
23+
}
24+
25+
return string(body), nil
26+
}

handlers/reporting.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import (
1414
)
1515

1616
type reportingHandler struct {
17-
backend backend.Backend
17+
reporter backend.Report
1818
}
1919

20-
func NewReportingHandler(backend backend.Backend) reportingHandler {
21-
return reportingHandler{backend: backend}
20+
func NewReportingHandler(backend backend.Report) reportingHandler {
21+
return reportingHandler{reporter: backend}
2222
}
2323

2424
func ReportingDashboardHandler(context *gin.Context) {
@@ -49,7 +49,7 @@ func (r *reportingHandler) ReportingIndexHandler(context *gin.Context) {
4949
}
5050

5151
func (r *reportingHandler) ReportingTableCardHandler(context *gin.Context) {
52-
reports, _ := r.backend.GetReports()
52+
reports, _ := r.reporter.GetReports()
5353
var rows []table.ReportingDataTableRow
5454

5555
for _, report := range reports {

handlers/status.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import (
1111
)
1212

1313
type statusHandler struct {
14-
backend backend.Backend
14+
status backend.Status
1515
}
1616

17-
func NewStatusHandler(backend backend.Backend) statusHandler {
18-
return statusHandler{backend: backend}
17+
func NewStatusHandler(backend backend.Status) statusHandler {
18+
return statusHandler{status: backend}
1919
}
2020

2121
func (s *statusHandler) HealthComponentHandler(context *gin.Context) {
22-
response, err := s.backend.GetPongFromStatus()
22+
response, err := s.status.GetPongFromStatus()
2323
indicatorData := miscellaneous.HealthIndicatorData{Loaded: true}
2424

2525
switch {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package reporting
1+
package reporter
22

33
import (
44
"time"

routes/routes.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"net/http"
55

66
"soarca-gui/backend"
7-
soarca "soarca-gui/backend/soarca"
7+
"soarca-gui/backend/soarca"
88
"soarca-gui/handlers"
99
"soarca-gui/public"
1010
"soarca-gui/utils"
@@ -18,12 +18,14 @@ func Setup(app *gin.Engine) {
1818
ctx.Redirect(http.StatusTemporaryRedirect, "/404-page")
1919
})
2020

21-
backend := soarca.New(utils.GetEnv("SOARCA_URI", "http://localhost:8080"), http.Client{})
21+
reporter := soarca.NewReporter(utils.GetEnv("SOARCA_URI", "http://localhost:8080"), http.Client{})
22+
status := soarca.NewStatus(utils.GetEnv("SOARCA_URI", "http://localhost:8080"), http.Client{})
23+
2224
publicRoutes := app.Group("/")
2325

2426
PublicRoutes(publicRoutes)
25-
ReportingRoutes(backend, publicRoutes)
26-
StatusRoutes(backend, publicRoutes)
27+
ReportingRoutes(reporter, publicRoutes)
28+
StatusRoutes(status, publicRoutes)
2729
SettingsRoutes(publicRoutes)
2830
}
2931

@@ -41,7 +43,7 @@ func PublicRoutes(app *gin.RouterGroup) {
4143
publicRoute.StaticFS("/public", public.GetPublicAssetsFileSystem())
4244
}
4345

44-
func ReportingRoutes(backend backend.Backend, app *gin.RouterGroup) {
46+
func ReportingRoutes(backend backend.Report, app *gin.RouterGroup) {
4547
reportingHandlers := handlers.NewReportingHandler(backend)
4648

4749
reportingRoute := app.Group("/reporting")
@@ -52,7 +54,7 @@ func ReportingRoutes(backend backend.Backend, app *gin.RouterGroup) {
5254
}
5355
}
5456

55-
func StatusRoutes(backend backend.Backend, app *gin.RouterGroup) {
57+
func StatusRoutes(backend backend.Status, app *gin.RouterGroup) {
5658
statusHandlers := handlers.NewStatusHandler(backend)
5759

5860
statusRoute := app.Group("/status")

views/auth/login_templ.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<body class=\"min-h-screen bg-gray-100 dark:bg-slate-800 flex flex-col justify-center sm:py-12 font-family-sans-serif\"><div class=\"p-10 xs:p-0 mx-auto md:w-full md:max-w-md\"><a href=\"
2+
\" class=\"text-white text-3xl font-semibold uppercase hover:text-gray-300\"><img src=\"
3+
\" class=\"w-30 py-3 md:py-0 g-image\"></a><div class=\"bg-white shadow w-full rounded-lg divide-y divide-gray-200\"><div id=\"extra-information\" class=\"px-5 py-7\"></div><div class=\"bg-white shadow w-full rounded-lg divide-y divide-gray-200\">
4+
</div><div class=\"py-5\"><div class=\"grid grid-cols-2 gap-1\"><div class=\"text-center sm:text-left whitespace-nowrap\"><button class=\"transition duration-200 mx-5 px-5 py-4 cursor-pointer font-normal text-sm rounded-lg text-gray-500 hover:bg-gray-100 focus:outline-none focus:bg-gray-200 focus:ring-2 focus:ring-gray-400 focus:ring-opacity-50 ring-inset\"><svg xmlns=\"http://www.w3.org/2000/svg\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" class=\"w-4 h-4 inline-block align-text-top\"><path stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M8 11V7a4 4 0 118 0m-4 8v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2z\"></path></svg> <span class=\"inline-block ml-1\">Forgot Password</span></button></div><div class=\"text-center sm:text-right whitespace-nowrap\"><button class=\"transition duration-200 mx-5 px-5 py-4 cursor-pointer font-normal text-sm rounded-lg text-gray-500 hover:bg-gray-100 focus:outline-none focus:bg-gray-200 focus:ring-2 focus:ring-gray-400 focus:ring-opacity-50 ring-inset\"><svg xmlns=\"http://www.w3.org/2000/svg\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" class=\"w-4 h-4 inline-block align-text-bottom\t\"><path stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M18.364 5.636l-3.536 3.536m0 5.656l3.536 3.536M9.172 9.172L5.636 5.636m3.536 9.192l-3.536 3.536M21 12a9 9 0 11-18 0 9 9 0 0118 0zm-5 0a4 4 0 11-8 0 4 4 0 018 0z\"></path></svg> <span class=\"inline-block ml-1\">Help</span></button></div></div></div></div><div class=\"py-5\"><div class=\"grid grid-cols-2 gap-1\"><div class=\"text-center sm:text-left whitespace-nowrap\"><button class=\"transition duration-200 mx-5 px-5 py-4 cursor-pointer font-normal text-sm rounded-lg text-gray-500 hover:bg-gray-200 focus:outline-none focus:bg-gray-300 focus:ring-2 focus:ring-gray-400 focus:ring-opacity-50 ring-inset\"><svg xmlns=\"http://www.w3.org/2000/svg\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" class=\"w-4 h-4 inline-block align-text-top\"><path stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M10 19l-7-7m0 0l7-7m-7 7h18\"></path></svg> <span class=\"inline-block ml-1\">Back to SOARCA.com</span></button></div></div></div></div></body>
5+
<form hx-post=\"/login\" class=\"px-5 py-7\" method=\"post\" hx-target=\"#extra-information\" hx-swap=\"innerHTML\"><label name=\"email\" type=\"email\" class=\"border-none font-semibold text-sm text-gray-600 pb-1 block\">E-mail</label> <input id=\"email\" name=\"email\" type=\"email\" required class=\"border rounded-lg px-3 py-2 mt-1 mb-5 text-sm w-full\"> <label name=\"password\" type=\"password\" class=\"border-none font-semibold text-sm text-gray-600 pb-1 block\">Password</label> <input id=\"password\" name=\"password\" type=\"password\" required class=\"border rounded-lg px-3 py-2 mt-1 mb-5 text-sm w-full\"> <button class=\"transition duration-200 bg-blue-500 hover:bg-blue-600 focus:bg-blue-700 focus:shadow-sm focus:ring-4 focus:ring-blue-500 focus:ring-opacity-50 text-white w-full py-2.5 rounded-lg text-sm shadow-sm hover:shadow-md font-semibold text-center inline-block\"><span class=\"inline-block mr-2\">Login</span> <svg xmlns=\"http://www.w3.org/2000/svg\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" class=\"w-4 h-4 inline-block\"><path stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M17 8l4 4m0 0l-4 4m4-4H3\"></path></svg></button></form>

0 commit comments

Comments
 (0)