-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from rocket-pool/net-socket
Updates from Betas
- Loading branch information
Showing
18 changed files
with
571 additions
and
116 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Taken from https://github.com/marketplace/actions/block-fixup-commit-merge?version=v2.0.0 | ||
# Updated to use newer ubuntu and checkout action | ||
name: Git Checks | ||
|
||
on: [pull_request] | ||
|
||
jobs: | ||
block-fixup: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Block Fixup Commit Merge | ||
uses: 13rac1/[email protected] |
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,46 @@ | ||
# Taken from https://github.com/golangci/golangci-lint-action | ||
name: golangci-lint | ||
on: | ||
push: | ||
tags: | ||
- v* | ||
branches: | ||
- main | ||
pull_request: | ||
permissions: | ||
contents: read | ||
# Optional: allow read access to pull request. Use with `only-new-issues` option. | ||
# pull-requests: read | ||
jobs: | ||
golangci: | ||
name: lint | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/setup-go@v4 | ||
with: | ||
go-version: 1.21.8 | ||
- uses: actions/checkout@v3 | ||
- name: golangci-lint | ||
uses: golangci/golangci-lint-action@v4 | ||
with: | ||
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version | ||
version: latest | ||
|
||
# Optional: working directory, useful for monorepos | ||
# working-directory: somedir | ||
|
||
# Optional: golangci-lint command line arguments. | ||
# args: --issues-exit-code=0 | ||
|
||
# Optional: show only new issues if it's a pull request. The default value is `false`. | ||
# only-new-issues: true | ||
|
||
# Optional: if set to true then the all caching functionality will be complete disabled, | ||
# takes precedence over all other caching options. | ||
# skip-cache: true | ||
|
||
# Optional: if set to true then the action don't cache or restore ~/go/pkg. | ||
# skip-pkg-cache: true | ||
|
||
# Optional: if set to true then the action don't cache or restore ~/.cache/go-build. | ||
# skip-build-cache: true |
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,19 @@ | ||
name: NMC Unit Tests | ||
on: | ||
push: | ||
tags: | ||
- v* | ||
branches: | ||
- main | ||
pull_request: | ||
permissions: | ||
contents: read | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-go@v4 | ||
with: | ||
go-version: 1.21.8 | ||
- run: go test ./... |
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 was deleted.
Oops, something went wrong.
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,67 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"net" | ||
"net/http" | ||
"net/http/httptrace" | ||
"net/url" | ||
) | ||
|
||
// The context passed into a requester | ||
type NetworkRequesterContext struct { | ||
// The base address and route for API calls | ||
apiUrl *url.URL | ||
|
||
// An HTTP client for sending requests | ||
client *http.Client | ||
|
||
// Logger to print debug messages to | ||
logger *slog.Logger | ||
|
||
// Tracer for HTTP requests | ||
tracer *httptrace.ClientTrace | ||
} | ||
|
||
// Creates a new API client requester context for network-based | ||
// traceOpts is optional. If nil, it will not be used. | ||
func NewNetworkRequesterContext(apiUrl *url.URL, log *slog.Logger, tracer *httptrace.ClientTrace) *NetworkRequesterContext { | ||
requesterContext := &NetworkRequesterContext{ | ||
apiUrl: apiUrl, | ||
logger: log, | ||
tracer: tracer, | ||
client: &http.Client{ | ||
Transport: &http.Transport{ | ||
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { | ||
return net.Dial("tcp", apiUrl.Host) | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
return requesterContext | ||
} | ||
|
||
// Get the base of the address used for submitting server requests | ||
func (r *NetworkRequesterContext) GetAddressBase() string { | ||
return r.apiUrl.String() | ||
} | ||
|
||
// Get the logger for the context | ||
func (r *NetworkRequesterContext) GetLogger() *slog.Logger { | ||
return r.logger | ||
} | ||
|
||
// Set the logger for the context | ||
func (r *NetworkRequesterContext) SetLogger(logger *slog.Logger) { | ||
r.logger = logger | ||
} | ||
|
||
// Send an HTTP request to the server | ||
func (r *NetworkRequesterContext) SendRequest(request *http.Request) (*http.Response, error) { | ||
if r.tracer != nil { | ||
request = request.WithContext(httptrace.WithClientTrace(request.Context(), r.tracer)) | ||
} | ||
return r.client.Do(request) | ||
} |
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,5 @@ | ||
package client | ||
|
||
const ( | ||
jsonContentType string = "application/json" | ||
) |
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 @@ | ||
package client | ||
|
||
import ( | ||
"log/slog" | ||
"net/http" | ||
) | ||
|
||
// IRequester is an interface for making HTTP requests to a specific subroute on the NMC server | ||
type IRequester interface { | ||
// The human-readable requester name (for logging) | ||
GetName() string | ||
|
||
// The name of the subroute to send requests to | ||
GetRoute() string | ||
|
||
// Context to hold settings and utilities the requester should use | ||
GetContext() IRequesterContext | ||
} | ||
|
||
// IRequester is an interface for making HTTP requests to a specific subroute on the NMC server | ||
type IRequesterContext interface { | ||
// Get the base of the address used for submitting server requests | ||
GetAddressBase() string | ||
|
||
// Get the logger for the context | ||
GetLogger() *slog.Logger | ||
|
||
// Set the logger for the context | ||
SetLogger(*slog.Logger) | ||
|
||
// Send an HTTP request to the server | ||
SendRequest(request *http.Request) (*http.Response, error) | ||
} |
Oops, something went wrong.