-
Notifications
You must be signed in to change notification settings - Fork 15
/
shared_routes.go
71 lines (62 loc) · 2.39 KB
/
shared_routes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package server
import (
"net/http"
"github.com/ashirt-ops/ashirt-server/backend/contentstore"
"github.com/ashirt-ops/ashirt-server/backend/database"
"github.com/ashirt-ops/ashirt-server/backend/helpers"
"github.com/ashirt-ops/ashirt-server/backend/server/middleware"
"github.com/ashirt-ops/ashirt-server/backend/services"
"github.com/go-chi/chi/v5"
)
func bindSharedRoutes(r chi.Router, db *database.Connection, contentStore contentstore.Store) {
route(r, "GET", "/operations", jsonHandler(func(r *http.Request) (interface{}, error) {
return services.ListOperations(r.Context(), db)
}))
route(r, "POST", "/operations", jsonHandler(func(r *http.Request) (interface{}, error) {
dr := dissectJSONRequest(r)
i := services.CreateOperationInput{
Slug: dr.FromBody("slug").Required().AsString(),
Name: dr.FromBody("name").Required().AsString(),
OwnerID: middleware.UserID(r.Context()),
}
if dr.Error != nil {
return nil, dr.Error
}
return services.CreateOperation(r.Context(), db, i)
}))
route(r, "GET", "/operations/{operation_slug}/tags", jsonHandler(func(r *http.Request) (interface{}, error) {
dr := dissectJSONRequest(r)
i := services.ListTagsForOperationInput{
OperationSlug: dr.FromURL("operation_slug").Required().AsString(),
}
return services.ListTagsForOperation(r.Context(), db, i)
}))
route(r, "POST", "/operations/{operation_slug}/tags", jsonHandler(func(r *http.Request) (interface{}, error) {
dr := dissectJSONRequest(r)
i := services.CreateTagInput{
Name: dr.FromBody("name").Required().AsString(),
ColorName: dr.FromBody("colorName").AsString(),
OperationSlug: dr.FromURL("operation_slug").Required().AsString(),
Description: dr.FromBody("description").AsStringPtr(),
}
if dr.Error != nil {
return nil, dr.Error
}
return services.CreateTag(r.Context(), db, i)
}))
route(r, "GET", "/operations/{operation_slug}/evidence", jsonHandler(func(r *http.Request) (interface{}, error) {
dr := dissectJSONRequest(r)
timelineFilters, err := helpers.ParseTimelineQuery(dr.FromQuery("query").AsString())
if err != nil {
return nil, err
}
i := services.ListEvidenceForOperationInput{
OperationSlug: dr.FromURL("operation_slug").Required().AsString(),
Filters: timelineFilters,
}
if dr.Error != nil {
return nil, dr.Error
}
return services.ListEvidenceForOperation(r.Context(), db, contentStore, i)
}))
}