Skip to content

Commit

Permalink
Cors/version (#270)
Browse files Browse the repository at this point in the history
* Enable CORS for version information

* Enable CORS for version information

* Add middleware to version endpoint

* Refactor version into GQL

* tidy
  • Loading branch information
DylanTinianov authored May 26, 2023
1 parent 14fa645 commit 5cbaf74
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 50 deletions.
66 changes: 64 additions & 2 deletions generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
github.com/getsentry/sentry-go v0.13.0
github.com/go-chi/chi v3.3.2+incompatible
github.com/go-chi/httplog v0.2.5
github.com/go-chi/render v1.0.1
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da
github.com/google/uuid v1.3.0
github.com/gorilla/mux v1.8.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,6 @@ github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8=
github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/httplog v0.2.5 h1:S02eG9NTrB/9kk3Q3RA3F6CR2b+v8WzB8IxK+zq3dBo=
github.com/go-chi/httplog v0.2.5/go.mod h1:/pIXuFSrOdc5heKIJRA5Q2mW7cZCI2RySqFZNFoZjKg=
github.com/go-chi/render v1.0.1 h1:4/5tis2cKaNdnv9zFLfXzcquC9HbeZgCnxGnKrltBS8=
github.com/go-chi/render v1.0.1/go.mod h1:pq4Rr7HbnsdaeHagklXub+p6Wd16Af5l9koip1OvJns=
github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
Expand Down
5 changes: 3 additions & 2 deletions model/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/dapperlabs/flow-playground-api/controller"
userErr "github.com/dapperlabs/flow-playground-api/middleware/errors"
"github.com/dapperlabs/flow-playground-api/model"
"github.com/dapperlabs/flow-playground-api/server/version"
"github.com/dapperlabs/flow-playground-api/storage"
"github.com/google/uuid"
"github.com/onflow/cadence"
Expand Down Expand Up @@ -399,9 +400,15 @@ func (r *projectResolver) UpdatedAt(_ context.Context, proj *model.Project) (str
type queryResolver struct{ *Resolver }

func (r *queryResolver) PlaygroundInfo(_ context.Context) (*model.PlaygroundInfo, error) {
emulatorVer, err := version.GetDependencyVersion("github.com/onflow/flow-emulator")
if err != nil {
return nil, err
}

return &model.PlaygroundInfo{
APIVersion: *r.version,
CadenceVersion: *semver.MustParse(cadence.Version),
APIVersion: *r.version,
CadenceVersion: *semver.MustParse(cadence.Version),
EmulatorVersion: *semver.MustParse(emulatorVer),
}, nil
}

Expand Down
1 change: 1 addition & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ scalar Version
type PlaygroundInfo {
apiVersion: Version!
cadenceVersion: Version!
emulatorVersion: Version!
}

type Project {
Expand Down
4 changes: 0 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"fmt"
"github.com/dapperlabs/flow-playground-api/server/config"
"github.com/dapperlabs/flow-playground-api/server/ping"
"github.com/dapperlabs/flow-playground-api/server/version"
"github.com/dapperlabs/flow-playground-api/telemetry"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/otel/sdk/trace"
Expand Down Expand Up @@ -179,14 +178,11 @@ func main() {
errors.Middleware(entry, localHub),
),
)

})

embedsHandler := controller.NewEmbedsHandler(store, conf.PlaygroundBaseURL)
router.Handle("/embed", embedsHandler)

router.HandleFunc("/version", version.Handler)

err := ping.SetPingHandlers(store.Ping)
if err != nil {
log.Fatal(err)
Expand Down
38 changes: 1 addition & 37 deletions server/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,54 +20,18 @@ package version

import (
"errors"
"fmt"
"github.com/Masterminds/semver"
"github.com/dapperlabs/flow-playground-api/build"
"github.com/go-chi/render"
"github.com/icza/bitio"
"github.com/onflow/cadence"
"net/http"
"runtime/debug"
)

func Handler(w http.ResponseWriter, r *http.Request) {
version := struct {
API string
Cadence string
Emulator string
}{
API: "n/a",
Cadence: "n/a",
Emulator: "n/a",
}

apiVer := build.Version()
if apiVer != nil {
version.API = apiVer.String()
}

cadenceVer := semver.MustParse(cadence.Version)
if cadenceVer != nil {
version.Cadence = cadenceVer.String()
}

emulatorVer, err := getDependencyVersion("github.com/onflow/flow-emulator")
if err == nil {
version.Emulator = semver.MustParse(emulatorVer).String()
}

render.JSON(w, r, version)
}

func getDependencyVersion(path string) (string, error) {
func GetDependencyVersion(path string) (string, error) {
_ = bitio.NewReader
bi, ok := debug.ReadBuildInfo()
if !ok {
return "", errors.New("failed to read build info")
}

for _, dep := range bi.Deps {
fmt.Printf("Dep: %+v\n", dep)
if dep.Path == path {
return dep.Version, nil
}
Expand Down

0 comments on commit 5cbaf74

Please sign in to comment.