Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert go:embed #291

Merged
merged 1 commit into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.env
.envrc
.idea
/TODO.md
/stripe-mock
/dist/
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,23 @@ Run the test suite:
go test ./...
```

### Updating OpenAPI
### Binary data & updating OpenAPI

Update the `openapi/` submodule to update the bundled OpenAPI specification.
The project uses [go-bindata] to bundle OpenAPI and fixture data into
`bindata.go` so that it's automatically included with built executables.
Rebuild it with:

``` sh
# Make sure you have the go-bindata executable (it's not vendored into this
# repository).
go get -u github.com/go-bindata/go-bindata/...

# Drop into the openapi/ Git submodule and update it (you may have to commit a
# change).
pushd openapi/ && git pull origin master && popd

# Generates `bindata.go`.
go generate
```

## Dependencies
Expand Down
269 changes: 269 additions & 0 deletions bindata.go

Large diffs are not rendered by default.

37 changes: 19 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//go:generate go-bindata -mode 0444 -modtime 1 cert/cert.pem cert/key.pem
//go:generate go-bindata -o server/bindata.go -pkg server -mode 0444 -modtime 1 openapi/openapi/fixtures3.json openapi/openapi/spec3.json

package main

import (
"crypto/tls"
_ "embed"
"flag"
"fmt"
"net"
Expand All @@ -13,18 +15,6 @@ import (
"github.com/stripe/stripe-mock/server"
)

//go:embed cert/key.pem
var EmbeddedCertKey []byte

//go:embed cert/cert.pem
var EmbeddedCertCert []byte

//go:embed openapi/openapi/fixtures3.json
var EmbeddedOpenAPIFixtures []byte

//go:embed openapi/openapi/spec3.json
var EmbeddedOpenAPISpec []byte

const defaultPortHTTP = 12111
const defaultPortHTTPS = 12112

Expand Down Expand Up @@ -90,12 +80,12 @@ func main() {
// For both spec and fixtures stripe-mock will by default load data from
// internal assets compiled into the binary, but either one can be
// overridden with a -spec or -fixtures argument and a path to a file.
stripeSpec, err := server.LoadSpec(EmbeddedOpenAPISpec, options.specPath)
stripeSpec, err := server.LoadSpec(options.specPath)
if err != nil {
abort(err.Error())
}

fixtures, err := server.LoadFixtures(EmbeddedOpenAPIFixtures, options.fixturesPath)
fixtures, err := server.LoadFixtures(options.fixturesPath)
if err != nil {
abort(err.Error())
}
Expand Down Expand Up @@ -143,7 +133,7 @@ func main() {
// arguments, but it won't start if HTTP is explicitly requested and HTTPS
// is not).
if httpsListener != nil {
// Our self-signed certificate is bundled up using go:embed so that
// Our self-signed certificate is bundled up using go-bindata so that
// it stays easy to distribute stripe-mock as a standalone binary with
// no other dependencies.
certificate, err := getTLSCertificate()
Expand Down Expand Up @@ -352,9 +342,20 @@ func abort(message string) {
os.Exit(1)
}

// getTLSCertificate reads a certificate and key embedded into the binary
// getTLSCertificate reads a certificate and key from the assets built by
// go-bindata.
func getTLSCertificate() (tls.Certificate, error) {
return tls.X509KeyPair(EmbeddedCertCert, EmbeddedCertKey)
cert, err := Asset("cert/cert.pem")
if err != nil {
return tls.Certificate{}, err
}

key, err := Asset("cert/key.pem")
if err != nil {
return tls.Certificate{}, err
}

return tls.X509KeyPair(cert, key)
}

func getPortListener(addr string, protocol string) (net.Listener, error) {
Expand Down
271 changes: 271 additions & 0 deletions server/bindata.go

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ type ResponseError struct {
// LoadFixtures load fixtures from a JSON file
//
// If path is empty, fixtures are loaded from internal embedded assets.
func LoadFixtures(embeddedFixtures []byte, fixturesPath string) (*spec.Fixtures, error) {
func LoadFixtures(fixturesPath string) (*spec.Fixtures, error) {
var data []byte
var err error

if fixturesPath == "" {
data = embeddedFixtures
data, err = Asset("openapi/openapi/fixtures3.json")
} else {
if !isJSONFile(fixturesPath) {
return nil, fmt.Errorf("Fixtures should come from a JSON file")
Expand All @@ -173,13 +173,13 @@ func LoadFixtures(embeddedFixtures []byte, fixturesPath string) (*spec.Fixtures,
// LoadSpec loads OpenAPI spec from a JSON file
//
// If path is empty, the spec is loaded from internal embedded assets.
func LoadSpec(embeddedSpec []byte, specPath string) (*spec.Spec, error) {
func LoadSpec(specPath string) (*spec.Spec, error) {
var data []byte
var err error

if specPath == "" {
// Use the embedded spec
data = embeddedSpec
// Load the spec information from go-bindata
data, err = Asset("openapi/openapi/spec3.json")
} else {
if !isJSONFile(specPath) {
return nil, fmt.Errorf("spec should come from a JSON file")
Expand Down
13 changes: 3 additions & 10 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"path"
"runtime"
"testing"

assert "github.com/stretchr/testify/require"
Expand Down Expand Up @@ -44,13 +42,8 @@ func init() {
}

func initRealSpec() {
// Determine test file name and paths to openapi specs
_, testFile, _, _ := runtime.Caller(0)
specFile := path.Join(path.Dir(testFile), "../openapi/openapi/spec3.json")
fixtureFile := path.Join(path.Dir(testFile), "../openapi/openapi/fixtures3.json")

// Load the spec information
data, err := ioutil.ReadFile(specFile)
// Load the spec information from go-bindata
data, err := Asset("openapi/openapi/spec3.json")
if err != nil {
panic(err)
}
Expand All @@ -64,7 +57,7 @@ func initRealSpec() {
spec.GetComponentsForValidation(&realSpec.Components)

// And do the same for fixtures
data, err = ioutil.ReadFile(fixtureFile)
data, err = Asset("openapi/openapi/fixtures3.json")
if err != nil {
panic(err)
}
Expand Down