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

Merge changes to move server into its own package #265

Merged
merged 15 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from 13 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: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
bindata.go linguist-generated
spec/bindata.go linguist-generated
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ build:
check-gofmt:
scripts/check_gofmt.sh

# This command is overcomplicated because Golint's `./...` doesn't filter
# `vendor/` (unlike every other Go command).
lint:
go list ./... | xargs -I{} -n1 sh -c 'golint -set_exit_status {} || exit 255'
staticcheck

test:
go test ./...
Expand Down
77 changes: 18 additions & 59 deletions bindata.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ project_name: stripe-mock

before:
hooks:
- go install golang.org/x/lint/golint@latest
- go get -u honnef.co/go/tools/cmd/staticcheck@latest
- go install github.com/go-bindata/go-bindata/...@latest
- go generate

Expand Down
88 changes: 10 additions & 78 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
//go:generate go-bindata -mode 444 -modtime 1 cert/cert.pem cert/key.pem openapi/openapi/fixtures3.json openapi/openapi/spec3.json
//go:generate go-bindata -mode 0444 -modtime 1 cert/cert.pem cert/key.pem
akalinin-stripe marked this conversation as resolved.
Show resolved Hide resolved
//go:generate go-bindata -o server/bindata.go -pkg server -mode 0444 -modtime 1 openapi/openapi/fixtures3.json openapi/openapi/spec3.json
akalinin-stripe marked this conversation as resolved.
Show resolved Hide resolved

package main

import (
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/stripe/stripe-mock/spec"
"github.com/stripe/stripe-mock/server"
)

const defaultPortHTTP = 12111
Expand Down Expand Up @@ -78,25 +75,22 @@ func main() {
abort(fmt.Sprintf("Invalid options: %v", err))
}

server.Version = version

// 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 := getSpec(options.specPath)
stripeSpec, err := server.LoadSpec(options.specPath)
if err != nil {
abort(err.Error())
}

fixtures, err := getFixtures(options.fixturesPath)
fixtures, err := server.LoadFixtures(options.fixturesPath)
if err != nil {
abort(err.Error())
}

stub := StubServer{
fixtures: fixtures,
spec: stripeSpec,
strictVersionCheck: options.strictVersionCheck,
}
err = stub.initializeRouter()
stub, err := server.NewStubServer(fixtures, stripeSpec, options.strictVersionCheck, verbose)
if err != nil {
abort(fmt.Sprintf("Error initializing router: %v\n", err))
}
Expand All @@ -106,7 +100,7 @@ func main() {

// Deduplicates doubled slashes in paths. e.g. `//v1/charges` becomes
// `/v1/charges`.
handler := &DoubleSlashFixHandler{httpMux}
handler := &server.DoubleSlashFixHandler{Mux: httpMux}

httpListener, err := options.getHTTPListener()
if err != nil {
Expand Down Expand Up @@ -344,7 +338,7 @@ func (o *options) getNonSecureHTTPSListener() (net.Listener, error) {
//

func abort(message string) {
fmt.Fprintf(os.Stderr, message)
fmt.Fprint(os.Stderr, message)
os.Exit(1)
}

Expand All @@ -364,34 +358,6 @@ func getTLSCertificate() (tls.Certificate, error) {
return tls.X509KeyPair(cert, key)
}

func getFixtures(fixturesPath string) (*spec.Fixtures, error) {
var data []byte
var err error

if fixturesPath == "" {
// And do the same for fixtures
data, err = Asset("openapi/openapi/fixtures3.json")
} else {
if !isJSONFile(fixturesPath) {
return nil, fmt.Errorf("Fixtures should come from a JSON file")
}

data, err = ioutil.ReadFile(fixturesPath)
}

if err != nil {
return nil, fmt.Errorf("error loading fixtures: %v", err)
}

var fixtures spec.Fixtures
err = json.Unmarshal(data, &fixtures)
if err != nil {
return nil, fmt.Errorf("error decoding spec: %v", err)
}

return &fixtures, nil
}

func getPortListener(addr string, protocol string) (net.Listener, error) {
listener, err := net.Listen("tcp", addr)
if err != nil {
Expand All @@ -417,33 +383,6 @@ func getPortListenerDefault(defaultPort int, protocol string) (net.Listener, err
return getPortListener(fmt.Sprintf(":%v", defaultPort), protocol)
}

func getSpec(specPath string) (*spec.Spec, error) {
var data []byte
var err error

if specPath == "" {
// 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")
}

data, err = ioutil.ReadFile(specPath)
}
if err != nil {
return nil, fmt.Errorf("error loading spec: %v", err)
}

var stripeSpec spec.Spec
err = json.Unmarshal(data, &stripeSpec)
if err != nil {
return nil, fmt.Errorf("error decoding spec: %v", err)
}

return &stripeSpec, nil
}

func getUnixSocketListener(unixSocket, protocol string) (net.Listener, error) {
listener, err := net.Listen("unix", unixSocket)
if err != nil {
Expand All @@ -453,10 +392,3 @@ func getUnixSocketListener(unixSocket, protocol string) (net.Listener, error) {
fmt.Printf("Listening for %s on Unix socket: %s\n", protocol, unixSocket)
return listener, nil
}

// isJSONFile judges based on a file's extension whether it's a JSON file. It's
// used to return a better error message if the user points to an unsupported
// file.
func isJSONFile(path string) bool {
return strings.ToLower(filepath.Ext(path)) == ".json"
}
Loading