Skip to content

Commit 616677d

Browse files
committed
Initial release of APIMock
0 parents  commit 616677d

27 files changed

Lines changed: 1633 additions & 0 deletions

.dockerignore

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
2+
# Created by https://www.toptal.com/developers/gitignore/api/go,visualstudiocode,macos,windows,linux
3+
# Edit at https://www.toptal.com/developers/gitignore?templates=go,visualstudiocode,macos,windows,linux
4+
5+
### Go ###
6+
# Binaries for programs and plugins
7+
*.exe
8+
*.exe~
9+
*.dll
10+
*.so
11+
*.dylib
12+
13+
# Test binary, built with `go test -c`
14+
*.test
15+
16+
# Output of the go coverage tool, specifically when used with LiteIDE
17+
*.out
18+
19+
### Go Patch ###
20+
/vendor/
21+
/Godeps/
22+
23+
### Linux ###
24+
*~
25+
26+
# temporary files which can be created if a process still has a handle open of a deleted file
27+
.fuse_hidden*
28+
29+
# KDE directory preferences
30+
.directory
31+
32+
# Linux trash folder which might appear on any partition or disk
33+
.Trash-*
34+
35+
# .nfs files are created when an open file is removed but is still being accessed
36+
.nfs*
37+
38+
### macOS ###
39+
# General
40+
.DS_Store
41+
.AppleDouble
42+
.LSOverride
43+
44+
# Icon must end with two \r
45+
Icon
46+
47+
# Thumbnails
48+
._*
49+
50+
# Files that might appear in the root of a volume
51+
.DocumentRevisions-V100
52+
.fseventsd
53+
.Spotlight-V100
54+
.TemporaryItems
55+
.Trashes
56+
.VolumeIcon.icns
57+
.com.apple.timemachine.donotpresent
58+
59+
# Directories potentially created on remote AFP share
60+
.AppleDB
61+
.AppleDesktop
62+
Network Trash Folder
63+
Temporary Items
64+
.apdisk
65+
66+
### VisualStudioCode ###
67+
.vscode/*
68+
!.vscode/settings.json
69+
!.vscode/tasks.json
70+
!.vscode/launch.json
71+
!.vscode/extensions.json
72+
73+
### VisualStudioCode Patch ###
74+
# Ignore all local history of files
75+
.history
76+
77+
### Windows ###
78+
# Windows thumbnail cache files
79+
Thumbs.db
80+
ehthumbs.db
81+
ehthumbs_vista.db
82+
83+
# Dump file
84+
*.stackdump
85+
86+
# Folder config file
87+
[Dd]esktop.ini
88+
89+
# Recycle Bin used on file shares
90+
$RECYCLE.BIN/
91+
92+
# Windows Installer files
93+
*.cab
94+
*.msi
95+
*.msix
96+
*.msm
97+
*.msp
98+
99+
# Windows shortcuts
100+
*.lnk
101+
102+
# End of https://www.toptal.com/developers/gitignore/api/go,visualstudiocode,macos,windows,linux
103+
104+
# Misc
105+
106+
.air.conf
107+
/air
108+
/generate-cert.sh

.gitignore

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
2+
# Created by https://www.toptal.com/developers/gitignore/api/go,visualstudiocode,macos,windows,linux
3+
# Edit at https://www.toptal.com/developers/gitignore?templates=go,visualstudiocode,macos,windows,linux
4+
5+
### Go ###
6+
# Binaries for programs and plugins
7+
*.exe
8+
*.exe~
9+
*.dll
10+
*.so
11+
*.dylib
12+
13+
# Test binary, built with `go test -c`
14+
*.test
15+
16+
# Output of the go coverage tool, specifically when used with LiteIDE
17+
*.out
18+
19+
### Go Patch ###
20+
/vendor/
21+
/Godeps/
22+
23+
### Linux ###
24+
*~
25+
26+
# temporary files which can be created if a process still has a handle open of a deleted file
27+
.fuse_hidden*
28+
29+
# KDE directory preferences
30+
.directory
31+
32+
# Linux trash folder which might appear on any partition or disk
33+
.Trash-*
34+
35+
# .nfs files are created when an open file is removed but is still being accessed
36+
.nfs*
37+
38+
### macOS ###
39+
# General
40+
.DS_Store
41+
.AppleDouble
42+
.LSOverride
43+
44+
# Icon must end with two \r
45+
Icon
46+
47+
# Thumbnails
48+
._*
49+
50+
# Files that might appear in the root of a volume
51+
.DocumentRevisions-V100
52+
.fseventsd
53+
.Spotlight-V100
54+
.TemporaryItems
55+
.Trashes
56+
.VolumeIcon.icns
57+
.com.apple.timemachine.donotpresent
58+
59+
# Directories potentially created on remote AFP share
60+
.AppleDB
61+
.AppleDesktop
62+
Network Trash Folder
63+
Temporary Items
64+
.apdisk
65+
66+
### VisualStudioCode ###
67+
.vscode/*
68+
!.vscode/settings.json
69+
!.vscode/tasks.json
70+
!.vscode/launch.json
71+
!.vscode/extensions.json
72+
73+
### VisualStudioCode Patch ###
74+
# Ignore all local history of files
75+
.history
76+
77+
### Windows ###
78+
# Windows thumbnail cache files
79+
Thumbs.db
80+
ehthumbs.db
81+
ehthumbs_vista.db
82+
83+
# Dump file
84+
*.stackdump
85+
86+
# Folder config file
87+
[Dd]esktop.ini
88+
89+
# Recycle Bin used on file shares
90+
$RECYCLE.BIN/
91+
92+
# Windows Installer files
93+
*.cab
94+
*.msi
95+
*.msix
96+
*.msm
97+
*.msp
98+
99+
# Windows shortcuts
100+
*.lnk
101+
102+
# End of https://www.toptal.com/developers/gitignore/api/go,visualstudiocode,macos,windows,linux
103+
104+
# Misc
105+
106+
.air.conf
107+
/air
108+
/generate-cert.sh

.vscode/launch.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Proxy",
9+
"type": "go",
10+
"request": "launch",
11+
"mode": "debug",
12+
"program": "${workspaceFolder}/main.go",
13+
"env": {},
14+
"args": [
15+
"proxy",
16+
]
17+
},
18+
{
19+
"name": "Server",
20+
"type": "go",
21+
"request": "launch",
22+
"mode": "debug",
23+
"program": "${workspaceFolder}/main.go",
24+
"env": {},
25+
"args": [
26+
"server"
27+
]
28+
}
29+
]
30+
}%

Dockerfile.proxy

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM golang:alpine AS build-env
2+
RUN apk --no-cache add build-base git gcc
3+
ADD . /src
4+
RUN cd /src && go build -o apimock
5+
6+
7+
FROM alpine
8+
WORKDIR /app
9+
COPY --from=build-env /src/apimock /app/
10+
11+
EXPOSE 8080
12+
13+
RUN mkdir -p /app/mocks
14+
15+
CMD ["/app/apimock", "proxy", "-a", ":8888", "--k8s"]

build-proxy-docker.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
4+
5+
cd $DIR
6+
7+
docker build -f Dockerfile.proxy -t bmaynard/apimock-proxy-kubernetes:latest .
8+
docker push bmaynard/apimock-proxy-kubernetes:latest

cmd/root.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/spf13/cobra"
8+
9+
homedir "github.com/mitchellh/go-homedir"
10+
"github.com/spf13/viper"
11+
12+
c "github.com/bmaynard/apimock/pkg/cmd"
13+
"github.com/bmaynard/apimock/pkg/config"
14+
l "github.com/bmaynard/apimock/pkg/utils/logger"
15+
)
16+
17+
var (
18+
cfgFile string
19+
)
20+
21+
func NewApiMockApp() *cobra.Command {
22+
cobra.OnInitialize(initConfig)
23+
24+
var rootCmd = &cobra.Command{
25+
Use: "apimock",
26+
Short: "API Mock server",
27+
Long: `Run an API Mock server as well as the ability to record real requests to mock later`,
28+
}
29+
30+
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.apimock.yaml)")
31+
rootCmd.AddCommand(c.NewCmdProxyServer())
32+
rootCmd.AddCommand(c.NewCmdMockServer())
33+
34+
return rootCmd
35+
}
36+
37+
// initConfig reads in config file and ENV variables if set.
38+
func initConfig() {
39+
if cfgFile != "" {
40+
// Use config file from the flag.
41+
viper.SetConfigFile(cfgFile)
42+
} else {
43+
// Find home directory.
44+
home, err := homedir.Dir()
45+
if err != nil {
46+
fmt.Println(err)
47+
os.Exit(1)
48+
}
49+
50+
// Search config in home directory with name ".apimock" (without extension).
51+
viper.AddConfigPath(home)
52+
viper.SetConfigName(".apimock")
53+
}
54+
55+
viper.AutomaticEnv() // read in environment variables that match
56+
57+
// If a config file is found, read it in.
58+
if err := viper.ReadInConfig(); err == nil {
59+
fmt.Println("Using config file:", viper.ConfigFileUsed())
60+
}
61+
62+
err := viper.Unmarshal(&config.Configuration)
63+
64+
if err != nil {
65+
l.Log.Errorf("unable to decode into struct, %v", err)
66+
}
67+
}

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/bmaynard/apimock
2+
3+
go 1.14
4+
5+
require (
6+
github.com/gorilla/mux v1.7.4
7+
github.com/mitchellh/go-homedir v1.1.0
8+
github.com/sirupsen/logrus v1.2.0
9+
github.com/spf13/cobra v1.0.0
10+
github.com/spf13/viper v1.7.0
11+
)

0 commit comments

Comments
 (0)