-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of requests ingestion (#4)
* start working on ingesting and storing http requests * ingest and store request data * add database tests
- Loading branch information
Showing
12 changed files
with
279 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package postgres | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/adelowo/sdump" | ||
"github.com/uptrace/bun" | ||
) | ||
|
||
type ingestRepository struct { | ||
inner *bun.DB | ||
} | ||
|
||
func NewIngestRepository(db *bun.DB) sdump.IngestRepository { | ||
return &ingestRepository{ | ||
inner: db, | ||
} | ||
} | ||
|
||
func (u *ingestRepository) Create(ctx context.Context, | ||
model *sdump.IngestHTTPRequest, | ||
) error { | ||
_, err := bun.NewInsertQuery(u.inner).Model(model). | ||
Exec(ctx) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//go:build integration | ||
// +build integration | ||
|
||
package postgres | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/adelowo/sdump" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestIngestRepository_Create(t *testing.T) { | ||
client, teardownFunc := setupDatabase(t) | ||
defer teardownFunc() | ||
|
||
ingestStore := NewIngestRepository(client) | ||
|
||
urlStore := NewURLRepositoryTable(client) | ||
|
||
endpoint, err := urlStore.Get(context.Background(), &sdump.FindURLOptions{ | ||
Reference: "cmltfm6g330l5l1vq110", // see fixtures/urls.yml | ||
}) | ||
require.NoError(t, err) | ||
|
||
require.NoError(t, ingestStore.Create(context.Background(), &sdump.IngestHTTPRequest{ | ||
UrlID: endpoint.ID, | ||
Request: sdump.RequestDefinition{ | ||
Body: "{}", | ||
}, | ||
})) | ||
} |
1 change: 1 addition & 0 deletions
1
datastore/postgres/migrations/20240120165926_create_http_ingest_table.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE ingests; |
10 changes: 10 additions & 0 deletions
10
datastore/postgres/migrations/20240120165926_create_http_ingest_table.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CREATE TABLE ingests ( | ||
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(), | ||
url_id uuid NOT NULL REFERENCES urls(id), | ||
|
||
request jsonb NOT NULL DEFAULT '{}'::jsonb, | ||
|
||
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
deleted_at TIMESTAMP WITH TIME ZONE | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package sdump | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"github.com/uptrace/bun" | ||
) | ||
|
||
type RequestDefinition struct { | ||
Body string `mapstructure:"body" json:"body,omitempty"` | ||
Query string `json:"query,omitempty"` | ||
Headers http.Header `json:"headers,omitempty"` | ||
IPAddress net.IP `json:"ip_address,omitempty" bson:"ip_address"` | ||
Size int64 `json:"size,omitempty"` | ||
} | ||
|
||
type IngestHTTPRequest struct { | ||
ID uuid.UUID `bun:"type:uuid,default:uuid_generate_v4()" json:"id,omitempty" mapstructure:"id"` | ||
UrlID uuid.UUID `json:"url_id,omitempty"` | ||
Request RequestDefinition `json:"request,omitempty"` | ||
|
||
// No need to store content type, it will always be application/json | ||
|
||
CreatedAt time.Time `bun:",nullzero,notnull,default:current_timestamp" json:"created_at,omitempty" bson:"created_at" mapstructure:"created_at"` | ||
UpdatedAt time.Time `bun:",nullzero,notnull,default:current_timestamp" json:"updated_at,omitempty" bson:"updated_at" mapstructure:"updated_at"` | ||
DeletedAt *time.Time `bun:",soft_delete,nullzero" json:"-,omitempty" bson:"deleted_at" mapstructure:"deleted_at"` | ||
|
||
bun.BaseModel `bun:"table:ingests"` | ||
} | ||
|
||
type IngestRepository interface { | ||
Create(context.Context, *IngestHTTPRequest) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package util | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
var ( | ||
xForwardedFor = http.CanonicalHeaderKey("X-Forwarded-For") | ||
xRealIP = http.CanonicalHeaderKey("X-Real-IP") | ||
) | ||
|
||
func GetIP(r *http.Request) net.IP { | ||
cloudflareIP := r.Header.Get("CF-Connecting-IP") | ||
if cloudflareIP != "" { | ||
return net.ParseIP(cloudflareIP) | ||
} | ||
|
||
if xff := r.Header.Get(xForwardedFor); xff != "" { | ||
i := strings.Index(xff, ", ") | ||
|
||
if i == -1 { | ||
i = len(xff) | ||
} | ||
|
||
return net.ParseIP(xff[:i]) | ||
} | ||
|
||
if ip := r.Header.Get(xRealIP); ip != "" { | ||
return net.ParseIP(ip) | ||
} | ||
|
||
return net.ParseIP(r.RemoteAddr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters