diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index eeb5c8798c..f306094eff 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -1,13 +1,15 @@ # Triggers a new deployment when a release starting with "fe1-" is created. -name: Release and deployment of fe1-web +name: Release and deployment on: release: types: [published] jobs: - deploy: + deploy_fe1: + name: Release and deploy fe1-web + if: ${{ startsWith(github.event.release.tag_name, 'fe1-') }} runs-on: ubuntu-latest @@ -68,4 +70,46 @@ jobs: chmod 600 ./deploy_key rsync -chav --delete \ -e 'ssh -p ${{secrets.FE1_DEPLOY_PORT}} -i ./deploy_key -o StrictHostKeyChecking=no' \ - ./web-build/ ${{env.dest}} \ No newline at end of file + ./web-build/ ${{env.dest}} + + deploy_be1: + name: Release and deploy be1-go + + if: ${{ startsWith(github.event.release.tag_name, 'be1-') }} + runs-on: ubuntu-latest + + env: + base_folder: be1-go + + defaults: + run: + working-directory: ./${{ env.base_folder }} + + steps: + - uses: actions/checkout@v3 + + - name: Use go >= 1.19 + uses: actions/setup-go@v3 + with: + go-version: ">=1.19" + + - name: build + run: | + make build + + - name: Upload release + uses: softprops/action-gh-release@v1 + if: startsWith(github.ref, 'refs/tags/') + with: + files: ${{ env.base_folder }}/pop-* + + # Use rsync to deploy the new website + - name: Sync + env: + dest: "popdemo@${{secrets.POPDEMO_DEPLOY_ADDR}}:be1-go/" + run: | + echo "${{secrets.POPDEMO_DEPLOY_KEY}}" > deploy_key + chmod 600 ./deploy_key + rsync -chav --delete \ + -e 'ssh -p ${{secrets.POPDEMO_DEPLOY_PORT}} -i ./deploy_key -o StrictHostKeyChecking=no' \ + ./pop ${{env.dest}} \ No newline at end of file diff --git a/.gitignore b/.gitignore index ba00a15847..68751929cb 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,7 @@ # Go backend executable be1-go/pop +be1-go/pop-* be1-go/validation/protocol # Karate files diff --git a/be1-go/Makefile b/be1-go/Makefile index 9be30ce009..7eae9263b2 100644 --- a/be1-go/Makefile +++ b/be1-go/Makefile @@ -1,7 +1,18 @@ +version=$(shell git describe --abbrev=0 --tags || echo '0.0.0') +versionFlag="popstellar.Version=$(version)" +versionFile=$(shell echo $(version) | tr . _) +timeFlag="popstellar.BuildTime=$(shell date +'%d/%m/%y_%H:%M')" +shortsha=$(shell git rev-parse --short HEAD) +shaFlag="popstellar.ShortSHA=$(shortsha)" + .PHONY: test build: protocol - go build -o pop ./cli + go build -ldflags="-X $(versionFlag) -X $(timeFlag) -X $(shaFlag)" -o pop ./cli + GOOS=linux GOARCH=amd64 go build -ldflags="-X $(versionFlag) -X $(timeFlag) -X $(shaFlag)" -o pop-linux-amd64-$(versionFile) ./cli + GOOS=darwin GOARCH=amd64 go build -ldflags="-X $(versionFlag) -X $(timeFlag) -X $(shaFlag)" -o pop-darwin-amd64-$(versionFile) ./cli + GOOS=darwin GOARCH=arm64 go build -ldflags="-X $(versionFlag) -X $(timeFlag) -X $(shaFlag)" -o pop-darwin-arm64-$(versionFile) ./cli + GOOS=windows GOARCH=amd64 go build -ldflags="-X $(versionFlag) -X $(timeFlag) -X $(shaFlag)" -o pop-windows-amd64-$(versionFile) ./cli lint: @go install honnef.co/go/tools/cmd/staticcheck@latest diff --git a/be1-go/mod.go b/be1-go/mod.go index cacaa4aedd..6ef8df3301 100644 --- a/be1-go/mod.go +++ b/be1-go/mod.go @@ -17,6 +17,21 @@ import ( "github.com/rs/zerolog" ) +// Version contains the current or build version. This variable can be changed +// at build time with: +// +// go build -ldflags="-X 'popstellar.Version=v1.0.0'" +// +// Version should be fetched from git: `git describe --tags` +var Version = "unknown" + +// BuildTime indicates the time at which the binary has been built. Must be set +// as with Version. +var BuildTime = "unknown" + +// ShortSHA is the short SHA commit id. Must be set as with Version. +var ShortSHA = "unknown" + // EnvLogLevel is the name of the environment variable to change the logging // level. const EnvLogLevel = "LLVL" diff --git a/be1-go/network/server.go b/be1-go/network/server.go index 692cf7e517..f8b7711dd0 100644 --- a/be1-go/network/server.go +++ b/be1-go/network/server.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/http" + "popstellar" "popstellar/hub" "popstellar/network/socket" "sync" @@ -18,6 +19,8 @@ type key int const requestIDKey key = 0 +const infoPath = "/infos" + var upgrader = websocket.Upgrader{ ReadBufferSize: 1024, WriteBufferSize: 1024, @@ -63,6 +66,8 @@ func NewServer(hub hub.Hub, addr string, port int, st socket.SocketType, log zer mux := http.NewServeMux() mux.HandleFunc(path, server.ServeHTTP) + mux.HandleFunc(infoPath, server.infoHandler) + nextRequestID := func() string { return fmt.Sprintf("%d", time.Now().UnixNano()) } @@ -135,6 +140,28 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } +func (s *Server) infoHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + http.Error(w, "not supported", http.StatusMethodNotAllowed) + return + } + + w.Header().Set("Content-Type", "application/json") + + fmtStr := `{ + "version": "%s", + "commit": "%s", + "buildTime": "%s", + "hubType": "%s", + "socketType": "%s" +}` + + resp := fmt.Sprintf(fmtStr, popstellar.Version, popstellar.ShortSHA, + popstellar.BuildTime, s.hub.Type(), s.st) + + w.Write([]byte(resp)) +} + // Shutdown shuts the HTTP server, signals the read and write pumps to // close and waits for them to finish. func (s *Server) Shutdown() error { diff --git a/be1-go/network/server_test.go b/be1-go/network/server_test.go index 496062b5d9..eaca70d11f 100644 --- a/be1-go/network/server_test.go +++ b/be1-go/network/server_test.go @@ -2,6 +2,8 @@ package network import ( "io" + "net/http" + "net/http/httptest" "popstellar/crypto" "popstellar/hub" "popstellar/hub/standard_hub" @@ -25,3 +27,37 @@ func TestServerStartAndShutdown(t *testing.T) { require.NoError(t, err) <-srv.Stopped } + +func TestInfoHandler(t *testing.T) { + s := Server{ + hub: fakeHub{}, + st: "fake", + } + + rr := httptest.NewRecorder() + s.infoHandler(rr, &http.Request{Method: http.MethodGet}) + + res, err := io.ReadAll(rr.Body) + require.NoError(t, err) + + expected := `{ + "version": "unknown", + "commit": "unknown", + "buildTime": "unknown", + "hubType": "fake", + "socketType": "fake" +}` + + require.Equal(t, expected, string(res)) +} + +// ----------------------------------------------------------------------------- +// Utility functions + +type fakeHub struct { + hub.Hub +} + +func (fakeHub) Type() hub.HubType { + return "fake" +}