-
Notifications
You must be signed in to change notification settings - Fork 0
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 #1 from FriendlyCaptcha/initial-version
Initial version
- Loading branch information
Showing
19 changed files
with
847 additions
and
1 deletion.
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,28 @@ | ||
name: goreleaser | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
- main | ||
|
||
jobs: | ||
goreleaser: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
with: | ||
fetch-depth: 0 | ||
- name: Set up Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: "^1.21" | ||
- name: Run GoReleaser | ||
uses: goreleaser/goreleaser-action@v5 | ||
with: | ||
distribution: goreleaser | ||
version: latest | ||
args: release --clean | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
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,24 @@ | ||
name: "test hareply" | ||
|
||
on: | ||
pull_request: | ||
push: | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
go-version: | ||
- 1.21.x | ||
steps: | ||
- name: checkout | ||
uses: actions/checkout@v4 | ||
- name: install | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: '${{ matrix.go-version }}' | ||
- name: vet | ||
run: go vet ./... | ||
- name: test | ||
run: go test -v -race ./... |
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 |
---|---|---|
|
@@ -19,3 +19,5 @@ | |
|
||
# Go workspace file | ||
go.work | ||
|
||
agentstate |
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,9 @@ | ||
|
||
|
||
Copyright 2024 Friendly Captcha GmbH | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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 |
---|---|---|
@@ -1,2 +1,33 @@ | ||
# hareply | ||
HAProxy agent-check responder | ||
`hareply` is a tool that replies to [HAProxy's `agent-check`](https://www.haproxy.com/documentation/haproxy-configuration-tutorials/service-reliability/health-checks/#agent-checks) that replies with the contents of a given file. | ||
|
||
## CLI usage | ||
|
||
```shell | ||
# Print help | ||
hareply -h | ||
|
||
# Print help for serve command | ||
hareply serve -h | ||
|
||
# Print version information | ||
hareply version | ||
|
||
# Serve from port 8020 and a specific path. | ||
hareply serve -f /some/path/to/agentstate -p 8020 | ||
|
||
# Serve from port 8442 and filepath `agentstate` (the defaults). | ||
hareply serve | ||
``` | ||
|
||
## As a library | ||
`hareply` can also be used as a library, see the godoc for the `hareply` package. | ||
|
||
## Error handling | ||
|
||
* The response "`agentstate`" file is read on startup, if that fails the program will exit. | ||
* The file is read again on any TCP connection, if that fails the last known file contents are used. | ||
* If the value in the file is not a valid response for `agent-check`, the last valid response is returned instead. | ||
|
||
## License | ||
[MIT](./LICENSE.md) [🎶](https://suno.com/song/da6d4a83-1001-4694-8c28-648a6e8bad0a). |
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,47 @@ | ||
// Package buildinfo implements build information, injected by goreleaser. | ||
package buildinfo | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
) | ||
|
||
// Fields injected by goreleaser. | ||
// | ||
//nolint:gochecknoglobals // These are injected by goreleaser. | ||
var ( | ||
version = "<unknown>" | ||
commitDate = "date unknown" | ||
commit = "" | ||
) | ||
|
||
// Version returns the version of the application, or "<unknown>" if it is not | ||
// set. This is injected by goreleaser. | ||
func Version() string { | ||
return version | ||
} | ||
|
||
// CommitDate returns the date of the commit, or "date unknown" if it is not | ||
// set. This is injected by goreleaser. | ||
func CommitDate() string { | ||
return commitDate | ||
} | ||
|
||
// Commit returns the commit hash, or "" if it is not set. This is injected by | ||
// goreleaser. | ||
func Commit() string { | ||
return commit | ||
} | ||
|
||
// Target returns the target operating system that this binary was compiled for, | ||
// or "" if it is not set. | ||
func Target() string { | ||
return runtime.GOOS | ||
} | ||
|
||
// FullVersion returns the full version string, including the version, commit | ||
// hash, and date. | ||
func FullVersion() string { | ||
return fmt.Sprintf("%s %s/%s %s (%s) %s", | ||
version, runtime.GOOS, runtime.GOARCH, runtime.Version(), commitDate, commit) | ||
} |
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,80 @@ | ||
// Package cmd contains the CLI implementation for hareply. | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
"log/slog" | ||
|
||
"github.com/alecthomas/kong" | ||
"github.com/friendlycaptcha/hareply/buildinfo" | ||
"github.com/friendlycaptcha/hareply/hareply" | ||
) | ||
|
||
// Config represents the configuration for the hareply CLI. | ||
type Config struct { | ||
Debug bool `help:"Enable debug mode."` | ||
|
||
Serve struct { | ||
Host string `help:"Host to listen on." default:""` | ||
Port int `help:"Port to reply on." short:"p" default:"8442"` | ||
File string `help:"Path to read response from." short:"f" default:"agentstate"` | ||
} `cmd:"" help:"Start hareply TCP responder service."` | ||
|
||
Version kong.VersionFlag | ||
} | ||
|
||
// CLI runs the hareply CLI. | ||
func CLI(ctx context.Context, w io.Writer, args []string, opts ...kong.Option) error { | ||
cli := Config{} | ||
|
||
opts = append(opts, | ||
kong.Name("hareply"), | ||
kong.Description("A simple TCP server that replies with a response read from a file."), | ||
kong.DefaultEnvars("HAREPLY_"), | ||
kong.Vars{"version": buildinfo.FullVersion()}, | ||
) | ||
|
||
kcli, err := kong.New(&cli, opts...) | ||
if err != nil { | ||
return fmt.Errorf("error creating CLI parser: %w", err) | ||
} | ||
|
||
kctx, err := kcli.Parse(args) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
logger := setupLogger(w, cli.Debug) | ||
|
||
switch kctx.Command() { | ||
case "serve": | ||
app, err := hareply.New(cli.Serve.File, | ||
hareply.WithPort(cli.Serve.Port), | ||
hareply.WithHost(cli.Serve.Host), | ||
hareply.WithLogger(logger), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
return app.Serve(ctx) | ||
case "version": | ||
fmt.Fprintln(w, buildinfo.FullVersion()) | ||
return nil | ||
default: | ||
return fmt.Errorf("unknown command: %s", kctx.Command()) | ||
} | ||
} | ||
|
||
func setupLogger(w io.Writer, debug bool) *slog.Logger { | ||
lvl := slog.LevelInfo | ||
if debug { | ||
lvl = slog.LevelDebug | ||
} | ||
logger := slog.New(slog.NewJSONHandler(w, &slog.HandlerOptions{ | ||
Level: lvl, | ||
})) | ||
return logger | ||
} |
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 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/alecthomas/kong" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/friendlycaptcha/hareply/testhelper" | ||
) | ||
|
||
func TestCLI(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("serve happy", func(t *testing.T) { | ||
t.Parallel() | ||
f := testhelper.SetupAgentStateFile(t, "up 50%\n") | ||
port := "8302" // Arbitrary port, we could also use 0 and read it from the stdout of the CLI command. | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) | ||
defer cancel() | ||
go func() { | ||
out := &bytes.Buffer{} | ||
err := CLI(ctx, out, []string{"serve", "-f", f, "-p", port}, kong.Exit(func(c int) { | ||
if c != 0 { | ||
require.Fail(t, "unexpected exit code", "exit code: %d", c) | ||
} | ||
})) | ||
require.NoError(t, err) | ||
}() | ||
|
||
time.Sleep(50 * time.Millisecond) | ||
resp := testhelper.DialAndGetResponse(t, "localhost:"+port) | ||
require.Equal(t, "up 50%\n", resp) | ||
}) | ||
|
||
t.Run("unknown command", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
err := CLI(context.Background(), nil, []string{"unknown-command"}) | ||
require.Error(t, 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,14 @@ | ||
module github.com/friendlycaptcha/hareply | ||
|
||
go 1.21.4 | ||
|
||
require ( | ||
github.com/alecthomas/kong v0.9.0 | ||
github.com/stretchr/testify v1.9.0 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
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,18 @@ | ||
github.com/alecthomas/assert/v2 v2.6.0 h1:o3WJwILtexrEUk3cUVal3oiQY2tfgr/FHWiz/v2n4FU= | ||
github.com/alecthomas/assert/v2 v2.6.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k= | ||
github.com/alecthomas/kong v0.9.0 h1:G5diXxc85KvoV2f0ZRVuMsi45IrBgx9zDNGNj165aPA= | ||
github.com/alecthomas/kong v0.9.0/go.mod h1:Y47y5gKfHp1hDc7CH7OeXgLIpp+Q2m1Ni0L5s3bI8Os= | ||
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc= | ||
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= | ||
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= | ||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
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,22 @@ | ||
project_name: hareply | ||
builds: | ||
- binary: hareply | ||
goos: | ||
- linux | ||
- darwin | ||
- windows | ||
goarch: | ||
- amd64 | ||
- arm64 | ||
ldflags: | ||
- -s -w -X github.com/friendlycaptcha/hareply/buildinfo.version={{.Version}} -X github.com/friendlycaptcha/hareply/buildinfo.commit={{.Commit}} -X github.com/friendlycaptcha/hareply/buildinfo.commitDate={{.CommitDate}} | ||
archives: | ||
- id: hareply-archive | ||
name_template: >- | ||
{{ .ProjectName }}_ | ||
{{- .Tag }}_ | ||
{{- .Os }}_ | ||
{{- .Arch}} | ||
format_overrides: | ||
- goos: windows | ||
format: zip |
Oops, something went wrong.