Sphinx-Tribes is a decentralized message broker for public groups within the Sphinx ecosystem. This README covers the setup and configuration of the Sphinx-Tribes backend.
- Prerequisites
- Setup
- Optional Features
- Testing and Mocking
- Backend API Data Validations
- Contributing
- License
- Docker
- Go language environment
- PostgreSQL database
- Redis instance (optional)
- Relay server access (optional)
Clone the Sphinx-Tribes repository:
git clone https://github.com/Vayras/sphinx-tribes.git
Navigate to the cloned directory and build the Docker image:
docker build --no-cache -t sphinx-tribes .
docker tag sphinx-tribes sphinxlightning/sphinx-tribes:x
docker push sphinxlightning/sphinx-tribes:x
Create a .env
file in the project root with the required environment variables.
Set up a PostgreSQL database and execute the provided SQL scripts to create necessary tables.
Build and run the Golang backend:
go build .
./sphinx-tribes
Configure Redis by adding the REDIS_URL
or other relevant variables to your .env
file.
RDS_HOSTNAME =
RDS_PORT =
RDS_DB_NAME =
RDS_USERNAME =
RDS_PASSWORD =
For invoice creation and keysend payment, add RELAY_URL
and RELAY_AUTH_KEY
.
Requires a running Relay. Enable it with MEME_URL
.
Add public keys to SUPER_ADMINS
in your .env
file.
Add STAKWORK_KEY
for YouTube video downloads.
Run unit tests with coverage:
- Have Docker installed on your machine
- Run Docker
- Spin up a Postgres DB container before the test with this command
docker compose -f docker/testdb-docker-compose.yml -p test_db up -d
- Change the rdHost
rdsHost := "172.17.0.1"
variable value indb/test_config.go
to your127.0.0.1
// you may need to install cover with this command first
go get golang.org/x/tools/cmd/cover
// run test
RELAY_AUTH_KEY=TEST go test ./... -tags mock -race -v -coverprofile=coverage.out && ./cover-check.sh coverage.out <min coverage amount>
// To get code coverage in html format do the following after running the code above
go tool cover -html="coverage.out"
- Drop the Postgres DB container after the test with this command
docker compose -f docker/testdb-docker-compose.yml -p test_db down
- Change the rdHost variable value in
db/test_config.go
to the default value for github workflowrdsHost := "172.17.0.1"
Use mockery for interface mocking.
There are multiple options to install mockery. Use any one of the following to download.
Use the release page link mockery releases to download the artifact for your respective device.
If you have go already installed on your device you can use the go install command to download mockery.
go install github.com/vektra/mockery/v2@v2.39.1
If you are on mac you can use homebrew to download mockery
brew install mockery
brew upgrade mockery
- Update the corresponding interface with the function signature, for example if you are adding new function to the
database
structure make sure the interface filedb/interface.go
is updated with the function signature. - run the command
mockery
to update the mocks.
- Add the new entry in the
.mockery.yml
file like this
with-expecter: true
dir: "mocks"
packages:
github.com/stakwork/sphinx-tribes/db:
interfaces:
Database:
github.com/stakwork/sphinx-tribes/*your-package-name*:
interfaces:
*your-interface-1*:
*your-interface-2*:
- run the command
mockery
to update the mocks.
We are currently using gopkg.in/go-playground/validator.v9
for validation, to validate a struct add the validate
property to it
type Workspace struct {
Name string `gorm:"unique;not null" json:"name" validate:"required"`
Website string `json:"website" validate:"omitempty,uri"`
Github string `json:"github" validate:"omitempty,uri"`
Description string `json:"description" validate:"omitempty,lte=200"`
}
Then handle the validation errors in the request handler
err = db.Validate.Struct(org)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
msg := fmt.Sprintf("Error: did not pass validation test : %s", err)
json.NewEncoder(w).Encode(msg)
return
}
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests.
This project is licensed under the [LICENSE NAME] - see the LICENSE.md file for details.