Skip to content

Commit

Permalink
Fix configuration reading of database file
Browse files Browse the repository at this point in the history
chore(deps): bump github.com/ordishs/gocore from 1.0.54 to 1.0.55

Bumps [github.com/ordishs/gocore](https://github.com/ordishs/gocore) from 1.0.54 to 1.0.55.
- [Commits](ordishs/gocore@v1.0.54...v1.0.55)

---
updated-dependencies:
- dependency-name: github.com/ordishs/gocore
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

chore(deps): bump github.com/libsv/go-p2p from 0.1.5 to 0.1.6

Bumps [github.com/libsv/go-p2p](https://github.com/libsv/go-p2p) from 0.1.5 to 0.1.6.
- [Commits](libsv/go-p2p@v0.1.5...v0.1.6)

---
updated-dependencies:
- dependency-name: github.com/libsv/go-p2p
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

chore(deps): bump github.com/multiformats/go-multiaddr

Bumps [github.com/multiformats/go-multiaddr](https://github.com/multiformats/go-multiaddr) from 0.12.0 to 0.12.1.
- [Release notes](https://github.com/multiformats/go-multiaddr/releases)
- [Commits](multiformats/go-multiaddr@v0.12.0...v0.12.1)

---
updated-dependencies:
- dependency-name: github.com/multiformats/go-multiaddr
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

chore(deps): bump github.com/ordishs/gocore from 1.0.55 to 1.0.57

Bumps [github.com/ordishs/gocore](https://github.com/ordishs/gocore) from 1.0.55 to 1.0.57.
- [Commits](ordishs/gocore@v1.0.55...v1.0.57)

---
updated-dependencies:
- dependency-name: github.com/ordishs/gocore
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Add support for freezing and confiscation (#12)

* Support for confiscation

* Freezing is working

* Fixes for linter

* Fix go mod

Make p2p topic name configurable

Don't fail on unprocessed alerts

Include default alert_system topic in config

Update kubernetes files and include image workflow

chore(deps): bump github.com/99designs/gqlgen from 0.17.42 to 0.17.43

Bumps [github.com/99designs/gqlgen](https://github.com/99designs/gqlgen) from 0.17.42 to 0.17.43.
- [Release notes](https://github.com/99designs/gqlgen/releases)
- [Changelog](https://github.com/99designs/gqlgen/blob/master/CHANGELOG.md)
- [Commits](99designs/gqlgen@v0.17.42...v0.17.43)

---
updated-dependencies:
- dependency-name: github.com/99designs/gqlgen
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Fix image workflow

Change actions for multi-platform support

chore(deps): bump github.com/mrz1836/go-datastore from 0.5.9 to 0.5.10

Bumps [github.com/mrz1836/go-datastore](https://github.com/mrz1836/go-datastore) from 0.5.9 to 0.5.10.
- [Release notes](https://github.com/mrz1836/go-datastore/releases)
- [Changelog](https://github.com/mrz1836/go-datastore/blob/master/.goreleaser.yml)
- [Commits](mrz1836/go-datastore@v0.5.9...v0.5.10)

---
updated-dependencies:
- dependency-name: github.com/mrz1836/go-datastore
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

chore(deps): bump actions/setup-go from 3 to 5

Bumps [actions/setup-go](https://github.com/actions/setup-go) from 3 to 5.
- [Release notes](https://github.com/actions/setup-go/releases)
- [Commits](actions/setup-go@v3...v5)

---
updated-dependencies:
- dependency-name: actions/setup-go
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

chore(deps): bump actions/cache from 3 to 4

Bumps [actions/cache](https://github.com/actions/cache) from 3 to 4.
- [Release notes](https://github.com/actions/cache/releases)
- [Changelog](https://github.com/actions/cache/blob/main/RELEASES.md)
- [Commits](actions/cache@v3...v4)

---
updated-dependencies:
- dependency-name: actions/cache
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

Make peer discovery a constant process (#18)

* Make peer discovery a constant process

* Fix config

* Fix comments

Make invalidate block run on sync

chore(deps): bump github.com/libsv/go-p2p from 0.1.6 to 0.1.7

Bumps [github.com/libsv/go-p2p](https://github.com/libsv/go-p2p) from 0.1.6 to 0.1.7.
- [Commits](libsv/go-p2p@v0.1.6...v0.1.7)

---
updated-dependencies:
- dependency-name: github.com/libsv/go-p2p
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Update logging of connection details

Add STN environment

chore(deps): bump github.com/libsv/go-p2p from 0.1.7 to 0.1.8

Bumps [github.com/libsv/go-p2p](https://github.com/libsv/go-p2p) from 0.1.7 to 0.1.8.
- [Commits](libsv/go-p2p@v0.1.7...v0.1.8)

---
updated-dependencies:
- dependency-name: github.com/libsv/go-p2p
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Process all alerts on sync

Add timeout to sync process (#25)

chore(deps): bump github.com/libsv/go-p2p from 0.1.8 to 0.1.9

Bumps [github.com/libsv/go-p2p](https://github.com/libsv/go-p2p) from 0.1.8 to 0.1.9.
- [Commits](libsv/go-p2p@v0.1.8...v0.1.9)

---
updated-dependencies:
- dependency-name: github.com/libsv/go-p2p
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

chore(deps): bump github.com/mrz1836/go-datastore from 0.5.10 to 0.5.11

Bumps [github.com/mrz1836/go-datastore](https://github.com/mrz1836/go-datastore) from 0.5.10 to 0.5.11.
- [Release notes](https://github.com/mrz1836/go-datastore/releases)
- [Changelog](https://github.com/mrz1836/go-datastore/blob/master/.goreleaser.yml)
- [Commits](mrz1836/go-datastore@v0.5.10...v0.5.11)

---
updated-dependencies:
- dependency-name: github.com/mrz1836/go-datastore
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

chore(deps): bump github.com/multiformats/go-multiaddr

Bumps [github.com/multiformats/go-multiaddr](https://github.com/multiformats/go-multiaddr) from 0.12.1 to 0.12.2.
- [Release notes](https://github.com/multiformats/go-multiaddr/releases)
- [Commits](multiformats/go-multiaddr@v0.12.1...v0.12.2)

---
updated-dependencies:
- dependency-name: github.com/multiformats/go-multiaddr
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

chore(deps): bump github.com/mrz1836/go-api-router from 0.7.0 to 0.7.1

Bumps [github.com/mrz1836/go-api-router](https://github.com/mrz1836/go-api-router) from 0.7.0 to 0.7.1.
- [Release notes](https://github.com/mrz1836/go-api-router/releases)
- [Changelog](https://github.com/mrz1836/go-api-router/blob/master/.goreleaser.yml)
- [Commits](mrz1836/go-api-router@v0.7.0...v0.7.1)

---
updated-dependencies:
- dependency-name: github.com/mrz1836/go-api-router
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Add alert processing retry cron (#31)

* Add alert processing retry cron

* Fix ReadRaw func

Move connection wait

chore(deps): bump github.com/mrz1836/go-datastore from 0.5.11 to 0.5.12

Bumps [github.com/mrz1836/go-datastore](https://github.com/mrz1836/go-datastore) from 0.5.11 to 0.5.12.
- [Release notes](https://github.com/mrz1836/go-datastore/releases)
- [Changelog](https://github.com/mrz1836/go-datastore/blob/master/.goreleaser.yml)
- [Commits](mrz1836/go-datastore@v0.5.11...v0.5.12)

---
updated-dependencies:
- dependency-name: github.com/mrz1836/go-datastore
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Changes to support confiscation

Fix CI system for confiscation

Add basic status page

Add support for unfreeze

Clean up webhook message

Upgraded deps

First attempt at creating binaries on release

Enabled CGO, added a few more config settings

Fix deprecated notice on goreleaser

Path to the main.go file

Attempt to add flags for cgo

Fix the flags

Disable darwin for now until linux works

Remove flags for now

Attempt to turn CGO off for now

Attempt to fix the archive issue

Some more formatting for webhook messages

Add basic config

Disable webhook tests for now since DB calls are made

chore(deps): bump gorm.io/gorm from 1.25.6 to 1.25.7

Bumps [gorm.io/gorm](https://github.com/go-gorm/gorm) from 1.25.6 to 1.25.7.
- [Release notes](https://github.com/go-gorm/gorm/releases)
- [Commits](go-gorm/gorm@v1.25.6...v1.25.7)

---
updated-dependencies:
- dependency-name: gorm.io/gorm
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

chore(deps): bump gorm.io/driver/sqlite from 1.5.4 to 1.5.5

Bumps [gorm.io/driver/sqlite](https://github.com/go-gorm/sqlite) from 1.5.4 to 1.5.5.
- [Commits](go-gorm/sqlite@v1.5.4...v1.5.5)

---
updated-dependencies:
- dependency-name: gorm.io/driver/sqlite
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

feat: Add support for reading bitcoin configuration file (#40)

Move to a new topic for STN

chore(deps): bump github.com/mrz1836/go-datastore from 0.5.12 to 0.5.13

Bumps [github.com/mrz1836/go-datastore](https://github.com/mrz1836/go-datastore) from 0.5.12 to 0.5.13.
- [Release notes](https://github.com/mrz1836/go-datastore/releases)
- [Changelog](https://github.com/mrz1836/go-datastore/blob/master/.goreleaser.yml)
- [Commits](mrz1836/go-datastore@v0.5.12...v0.5.13)

---
updated-dependencies:
- dependency-name: github.com/mrz1836/go-datastore
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Error out if RPC credentials aren't valid (#44)

Implement quit channel for dht initialization (#45)

* Implement quit channel for dht initialization

* More graceful shutdowns

Add more readable alerts to API
  • Loading branch information
galt-tr committed Feb 15, 2024
1 parent be0c7de commit 5ea998e
Show file tree
Hide file tree
Showing 51 changed files with 1,871 additions and 490 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Build and push OCI image to Docker Hub

on:
push:
tags:
- '*'

jobs:
image:
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: "./go.mod"

- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Extract metadata (tags, labels)
id: meta
uses: docker/metadata-action@v5
with:
images: bsvb/alert-key

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build and push image
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.19
go-version: 1.21
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5.0.0
with:
distribution: goreleaser
version: latest
args: release --rm-dist --debug
args: release --clean --debug
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
#- name: Syndicate to GoDocs
# run: make godocs
# run: make godocs
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4
- name: Cache code
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: |
~/go/pkg/mod # Module download cache
Expand Down
38 changes: 35 additions & 3 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,40 @@ changelog:
# ---------------------------
# Builder
# ---------------------------
build:
skip: true
builds:
- env:
- CGO_ENABLED=0 # Required for SQLite
- GO111MODULE=on
#- CGO_CFLAGS=-Wno-error=unused-command-line-argument
main: ./cmd/
binary: "alert_system"
goarch:
- amd64
- arm64
goos:
- darwin
- linux
goarm:
- 6
- 7
goamd64:
- v2
- v3
mod_timestamp: "{{ .CommitTimestamp }}"
ldflags:
- -s -w -X github.com/bitcoin-sv/{{ .ProjectName }}/cmd.Version={{ .Version }}

# ---------------------------
# Archives + Checksums
# ---------------------------
archives:
- wrap_in_directory: true
format: tar.gz
name_template: '{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}_{{ .Version }}'

checksum:
name_template: "{{ .ProjectName }}_checksums.txt"
algorithm: sha256

# ---------------------------
# GitHub Release
Expand Down Expand Up @@ -68,4 +100,4 @@ announce:
# Defaults to `{{ .GitURL }}/releases/tag/{{ .Tag }}`
# url_template: 'https://github.com/bitcoin-sv/{{ .ProjectName }}/releases/tag/{{ .Tag }}'
# Defaults to `{{ .ProjectName }} {{ .Tag }} is out!`
title_template: '{{ .ProjectName }} {{ .Tag }} is out!'
title_template: '{{ .ProjectName }} {{ .Tag }} is out!'
2 changes: 1 addition & 1 deletion .make/common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ install-releaser: ## Install the GoReleaser application
release:: ## Full production release (creates release in GitHub)
@echo "releasing..."
@test $(github_token)
@export GITHUB_TOKEN=$(github_token) && goreleaser --rm-dist
@export GITHUB_TOKEN=$(github_token) && goreleaser --clean

.PHONY: release-test
release-test: ## Full production test release (everything except deploy)
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ WORKDIR /
RUN mkdir /.bitcoin
RUN touch /.bitcoin/alert_system_private_key
COPY --from=builder /opt/app-root/src/alert-system .
COPY example_settings_local.conf settings_local.conf
USER 65534:65534
ENV ALERT_SYSTEM_ENVIRONMENT=local
CMD ["/alert-system"]
50 changes: 24 additions & 26 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
Open BSV License version 4
Open BSV License Version 5 – granted by BSV Association, authorised licensee

Copyright (c) 2023 BSV Blockchain Association ("BA")
For the purposes of this license, the definitions below have the following meanings:
“Bitcoin Protocol” means the protocol implementation, cryptographic rules, network protocols, and consensus mechanisms in the Bitcoin White Paper as described here https://protocol.bsvblockchain.org.
“Bitcoin White Paper” means the paper entitled ‘Bitcoin: A Peer-to-Peer Electronic Cash System’ published by ‘Satoshi Nakamoto’ in October 2008.
“BSV Blockchains” means:
(a) the Bitcoin blockchain containing block height #556767 with the hash "000000000000000001d956714215d96ffc00e0afda4cd0a96c96f8d802b1662b" and that contains the longest honest persistent chain of blocks which has been produced in a manner which is consistent with the rules set forth in the Network Access Rules; and
(b) the test blockchains that contain the longest honest persistent chains of blocks which has been produced in a manner which is consistent with the rules set forth in the Network Access Rules.
“Network Access Rules” or “Rules” means the set of rules regulating the relationship between BSV Association and the nodes on BSV based on the Bitcoin Protocol rules and those set out in the Bitcoin White Paper, and available here https://bsvblockchain.org/network-access-rules.
“Software” means the software the subject of this licence, including any/all intellectual property rights therein and associated documentation files.
BSV Association grants permission, free of charge and on a non-exclusive and revocable basis, to any person obtaining a copy of 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:
1 - The text “© BSV Association,” and this license shall be included in all copies or substantial portions of the Software.
2 - The Software, and any software that is derived from the Software or parts thereof, must only be used on the BSV Blockchains.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES REGARDING ENTITLEMENT, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS THEREOF 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.
Version 0.1.1 of the Bitcoin SV software, and prior versions of software upon which it was based, were licensed under the MIT License, which is included below.
The MIT License (MIT)
Copyright (c) 2009-2010 Satoshi Nakamoto
Copyright (c) 2009-2015 Bitcoin Developers
Copyright (c) 2009-2017 The Bitcoin Core developers
Copyright (c) 2017 The Bitcoin ABC developers
Copyright (c) 2018 Bitcoin Association for BSV
Copyright (c) 2023 BSV Association

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:

1 - The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

2 - The Software, and any software that is derived from the Software or parts thereof,
can only be used on the Bitcoin SV blockchains. The Bitcoin SV blockchains are defined,
for purposes of this license, as the Bitcoin blockchain containing block height #556767
with the hash "000000000000000001d956714215d96ffc00e0afda4cd0a96c96f8d802b1662b" and
that contains the longest persistent chain of blocks accepted by this Software and which are valid under the rules set forth in the Bitcoin white paper (S. Nakamoto, Bitcoin: A Peer-to-Peer Electronic Cash System, posted online October 2008) and the latest version of this Software available in this repository or another repository designated by BA,
as well as the test blockchains that contain the longest persistent chains of blocks accepted by this Software and which are valid under the rules set forth in the Bitcoin whitepaper (S. Nakamoto, Bitcoin: A Peer-to-Peer Electronic Cash System, posted online October 2008) and the latest version of this Software available in this repository, or another repository designated by BA.

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.
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.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ Configuration files can be found in the [config](app/config/envs) directory.

<br/>

## Container Environment
**Note:** to use a custom settings file, it needs to be mounted and the appropriate environment variables set. Running it as below will run an ephemeral database but the container should sync up from the peers on the network on startup.
### podman
```
$ podman run -u root -e P2P_PORT=9908 -e P2P_IP=0.0.0.0 --expose 9908 docker.io/galtbv/alert-system:0.0.2
```

## Documentation
View the generated [documentation](https://pkg.go.dev/github.com/bitcoin-sv/alert-system)

Expand Down
77 changes: 77 additions & 0 deletions app/api/base/alert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package base

import (
"encoding/hex"
"encoding/json"
"errors"
"net/http"
"strconv"

"github.com/bitcoin-sv/alert-system/app/webhook"

"github.com/bitcoin-sv/alert-system/app"
"github.com/bitcoin-sv/alert-system/app/models"
"github.com/bitcoin-sv/alert-system/app/models/model"
"github.com/julienschmidt/httprouter"
apirouter "github.com/mrz1836/go-api-router"
)

// alerts will return the saved
func (a *Action) alert(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
// Read params
params := apirouter.GetParams(req)
if params == nil {
apiError := apirouter.ErrorFromRequest(req, "parameters is nil", "no parameters specified", http.StatusBadRequest, http.StatusBadRequest, "")
apirouter.ReturnResponse(w, req, apiError.Code, apiError)
return
}
idStr := params.GetString("sequence")
if idStr == "" {
apiError := apirouter.ErrorFromRequest(req, "missing sequence param", "missing sequence param", http.StatusBadRequest, http.StatusBadRequest, "")
apirouter.ReturnResponse(w, req, apiError.Code, apiError)
return
}
sequenceNumber, err := strconv.Atoi(idStr)
if err != nil {
apiError := apirouter.ErrorFromRequest(req, "sequence is invalid", "sequence is invalid", http.StatusBadRequest, http.StatusBadRequest, "")
apirouter.ReturnResponse(w, req, apiError.Code, apiError)
return
}

// Get alert
alertModel, err := models.GetAlertMessageBySequenceNumber(req.Context(), uint32(sequenceNumber), model.WithAllDependencies(a.Config))
if err != nil {
app.APIErrorResponse(w, req, http.StatusInternalServerError, err)
return
} else if alertModel == nil {
app.APIErrorResponse(w, req, http.StatusNotFound, errors.New("alert not found"))
return
}
err = alertModel.ReadRaw()
if err != nil {
app.APIErrorResponse(w, req, http.StatusInternalServerError, errors.New("alert faile"))
return
}
am := alertModel.ProcessAlertMessage()
if am == nil {
app.APIErrorResponse(w, req, http.StatusInternalServerError, errors.New("alert not valid type"))
return
}
err = am.Read(alertModel.GetRawMessage())
if err != nil {
app.APIErrorResponse(w, req, http.StatusInternalServerError, err)
return
}
p := webhook.Payload{
AlertType: alertModel.GetAlertType(),
Sequence: alertModel.SequenceNumber,
Raw: hex.EncodeToString(alertModel.GetRawData()),
Text: am.MessageString(),
}
// Return the response
_ = apirouter.ReturnJSONEncode(
w,
http.StatusOK,
json.NewEncoder(w),
p, []string{"sequence", "raw", "text", "alert_type"})
}
43 changes: 43 additions & 0 deletions app/api/base/alerts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package base

import (
"encoding/json"
"errors"
"net/http"

"github.com/bitcoin-sv/alert-system/app"
"github.com/bitcoin-sv/alert-system/app/models"
"github.com/bitcoin-sv/alert-system/app/models/model"
"github.com/julienschmidt/httprouter"
apirouter "github.com/mrz1836/go-api-router"
)

// AlertsResponse is the response for the alerts endpoint
type AlertsResponse struct {
Alerts []*models.AlertMessage `json:"alerts"`
LatestSequence uint32 `json:"latest_sequence"`
}

// alerts will return the saved
func (a *Action) alerts(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {

// Get all alerts
alerts, err := models.GetAllAlerts(req.Context(), nil, model.WithAllDependencies(a.Config))
if err != nil {
app.APIErrorResponse(w, req, http.StatusBadRequest, err)
return
} else if alerts == nil {
app.APIErrorResponse(w, req, http.StatusNotFound, errors.New("alert not found"))
return
}

// Return the response
_ = apirouter.ReturnJSONEncode(
w,
http.StatusOK,
json.NewEncoder(w),
AlertsResponse{
Alerts: alerts,
LatestSequence: alerts[len(alerts)-1].SequenceNumber,
}, []string{"alerts", "latest_sequence"})
}
Loading

0 comments on commit 5ea998e

Please sign in to comment.