Skip to content

Commit

Permalink
Merge pull request #95 from friendsofgo/v0.4.1
Browse files Browse the repository at this point in the history
V0.4.1
  • Loading branch information
aperezg authored Apr 24, 2021
2 parents 33737ca + 5800178 commit a70bee5
Show file tree
Hide file tree
Showing 23 changed files with 407 additions and 136 deletions.
58 changes: 0 additions & 58 deletions .circleci/config.yml

This file was deleted.

23 changes: 23 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test & Build

on:
push:
paths:
- 'cmd/**'
- 'internal/**'

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
go: ['1.16']
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}
- name: Run Tests...
run: go test -v -vet=off -race ./...
- name: Build...
run: go build -race cmd/killgrave/main.go
27 changes: 27 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Release

on:
push:
tags:
- 'v*.*.*'

jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
if: startsWith(github.ref, 'refs/tags/')
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## v0.4.1(2021/04/24)
* Migration to Github actions and remove the use of Circle CI
* Deprecation go versions before to v1.16
* Run the mock server using TLS
* Support YAML for a configuration and imposters definition
* Put watcher options on the config file
* Not support codecov anymore

## v0.4.0 (2020/01/30)
* The config file option load the imposters path relative on where the config file is
* Upgrade Killgrave to go1.13
Expand Down
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ FROM golang:alpine AS build
LABEL MAINTAINER = 'Friends of Go ([email protected])'

RUN apk add --update git
RUN apk add ca-certificates
WORKDIR /go/src/github.com/friendsofgo/killgrave
COPY . .
RUN export GO111MODULE=on && go mod tidy && TAG=$(git describe --tags --abbrev=0) \
Expand All @@ -12,4 +13,5 @@ RUN export GO111MODULE=on && go mod tidy && TAG=$(git describe --tags --abbrev=0
# Building image with the binary
FROM scratch
COPY --from=build /go/bin/killgrave /go/bin/killgrave
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
ENTRYPOINT ["/go/bin/killgrave"]
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@



<p align="center">
<img src="https://res.cloudinary.com/fogo/image/upload/c_scale,w_350/v1555701634/fogo/projects/gopher-killgrave.png" alt="Golang Killgrave"/>
</p>
Expand All @@ -11,7 +8,6 @@ Killgrave is a simulator for HTTP-based APIs, in simple words a **Mock Server**,

[![CircleCI](https://circleci.com/gh/friendsofgo/killgrave/tree/master.svg?style=svg)](https://circleci.com/gh/friendsofgo/killgrave/tree/master)
[![Version](https://img.shields.io/github/release/friendsofgo/killgrave.svg?style=flat-square)](https://github.com/friendsofgo/killgrave/releases/latest)
[![codecov](https://codecov.io/gh/friendsofgo/killgrave/branch/master/graph/badge.svg)](https://codecov.io/gh/friendsofgo/killgrave)
[![Go Report Card](https://goreportcard.com/badge/github.com/friendsofgo/killgrave)](https://goreportcard.com/report/github.com/friendsofgo/killgrave)
[![Total alerts](https://img.shields.io/lgtm/alerts/g/friendsofgo/killgrave.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/friendsofgo/killgrave/alerts/)
[![FriendsOfGo](https://img.shields.io/badge/powered%20by-Friends%20of%20Go-73D7E2.svg)](https://friendsofgo.tech)
Expand Down Expand Up @@ -152,6 +148,8 @@ $ killgrave -h
directory where your imposters are saved (default "imposters")
-port int
port to run the server (default 3000)
-secure bool
if you run your server using TLS (https)
-proxy-mode string
proxy mode you can choose between (all, missing or none) (default "none")
-proxy-url string
Expand Down Expand Up @@ -184,6 +182,8 @@ cors:
exposed_headers: ["Cache-Control"]
origins: ["*"]
allow_credentials: true
watcher: true
secure: true
```
As you can see, you could configure all the options on a very easy way. Keep in mind that the routes are based on the config file are.
Expand Down Expand Up @@ -211,9 +211,15 @@ Furthermore, the `imposters_path` option in previous version towards reference t
The option `cors` still being optional and its options can be an empty array.
If you want more information about the CORS options, visit the [CORS section](#configure-cors).
The `watcher` configuration field is optional, with this setting you can enable hot-reloads on imposter changes. Disabled by default.
The `secure` configuration field is optional, with this setting you can run your server using TLS options with a dummy certificate, so to make it work with the `HTTPS` protocol. Disabled by default.
The option `proxy` allow to configure the mock on a proxy mode, that means that you could configure an fallback urls, for all
your calls or only the missing ones or none. More information: [Proxy Section](#prepare-killgrave-for-proxy-mode)
## How to use
### Configure CORS
If you want to use `killgrave` on your client application you must consider to configure correctly all about CORS, thus we offer the possibility to configure it as you need through a config file.
Expand Down
12 changes: 9 additions & 3 deletions cmd/killgrave/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
const (
_defaultHost = "localhost"
_defaultPort = 3000
_defaultSecure = false
_defaultImpostersPath = "imposters"
_defaultConfigFile = ""
_defaultProxyMode = killgrave.ProxyNone
Expand All @@ -34,13 +35,15 @@ func main() {
var (
host = flag.String("host", _defaultHost, "if you run your server on a different host")
port = flag.Int("port", _defaultPort, "port to run the server")
secure = flag.Bool("secure", _defaultSecure, "if you run your server using TLS (https)")
imposters = flag.String("imposters", _defaultImpostersPath, "directory where your imposters are saved")
showVersion = flag.Bool("version", false, "show the _version of the application")
configFilePath = flag.String("config", _defaultConfigFile, "path with configuration file")
watcherFlag = flag.Bool("watcher", false, "file watcher, reload the server with each file change")
proxyModeFlag = flag.String("proxy-mode", _defaultProxyMode.String(), "proxy mode you can choose between (all, missing or none)")
proxyURLFlag = flag.String("proxy-url", "", "proxy url, you need to choose a proxy-mode")
)

flag.Parse()

if *showVersion {
Expand All @@ -53,7 +56,9 @@ func main() {
*imposters,
*host,
*port,
*secure,
killgrave.WithProxyConfiguration(*proxyModeFlag, *proxyURLFlag),
killgrave.WithWatcherConfiguration(*watcherFlag),
killgrave.WithConfigFile(*configFilePath),
)
if err != nil {
Expand All @@ -69,7 +74,7 @@ func main() {
srv.Run()

// Initialize and start the file watcher if the watcher option is true
w := runWatcher(*watcherFlag, cfg.ImpostersPath, &srv, cfg.Host, cfg.Port, cfg)
w := runWatcher(cfg.Watcher, cfg.ImpostersPath, &srv, cfg.Host, cfg.Port, cfg)

<-done
close(done)
Expand Down Expand Up @@ -104,12 +109,12 @@ func runServer(host string, port int, cfg killgrave.Config) server.Server {
router := mux.NewRouter()
httpAddr := fmt.Sprintf("%s:%d", host, port)

httpServer := http.Server{
httpServer := &http.Server{
Addr: httpAddr,
Handler: handlers.CORS(server.PrepareAccessControl(cfg.CORS)...)(router),
}

proxyServer, err := server.NewProxy(cfg.Proxy.Url, cfg.Proxy.Mode)
proxyServer, err := server.NewProxy(cfg.Proxy.URL, cfg.Proxy.Mode)
if err != nil {
log.Fatal(err)
}
Expand All @@ -119,6 +124,7 @@ func runServer(host string, port int, cfg killgrave.Config) server.Server {
router,
httpServer,
proxyServer,
cfg.Secure,
)
if err := s.Build(); err != nil {
log.Fatal(err)
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
module github.com/friendsofgo/killgrave

go 1.15
go 1.16

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gorilla/handlers v1.5.0
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/kr/pretty v0.1.0 // indirect
github.com/radovskyb/watcher v1.0.7
github.com/stretchr/testify v1.4.0 // indirect
github.com/stretchr/testify v1.4.0
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonschema v1.2.0
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.3.0
gopkg.in/yaml.v2 v2.4.0
)
11 changes: 4 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/gorilla/handlers v1.5.0 h1:4wjo3sf9azi99c8hTmyaxp9y5S+pFszsy3pP0rAw/lw=
github.com/gorilla/handlers v1.5.0/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
Expand All @@ -28,11 +27,9 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHo
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
18 changes: 15 additions & 3 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type Config struct {
Host string `yaml:"host"`
CORS ConfigCORS `yaml:"cors"`
Proxy ConfigProxy `yaml:"proxy"`
Secure bool `yaml:"secure"`
Watcher bool `yaml:"watcher"`
}

// ConfigCORS representation of section CORS of the yaml
Expand All @@ -29,7 +31,7 @@ type ConfigCORS struct {

// ConfigProxy is a representation of section proxy of the yaml
type ConfigProxy struct {
Url string `yaml:"url"`
URL string `yaml:"url"`
Mode ProxyMode `yaml:"mode"`
}

Expand Down Expand Up @@ -95,11 +97,12 @@ func (p *ProxyMode) UnmarshalYAML(unmarshal func(interface{}) error) error {
type ConfigOpt func(cfg *Config) error

// NewConfig initialize the config
func NewConfig(impostersPath, host string, port int, opts ...ConfigOpt) (Config, error) {
func NewConfig(impostersPath, host string, port int, secure bool, opts ...ConfigOpt) (Config, error) {
cfg := Config{
ImpostersPath: impostersPath,
Host: host,
Port: port,
Secure: secure,
}

for _, opt := range opts {
Expand Down Expand Up @@ -140,8 +143,17 @@ func WithProxyConfiguration(proxyMode, proxyURL string) ConfigOpt {
return func(cfg *Config) error {
mode, _ := StringToProxyMode(proxyMode)
cfg.Proxy.Mode = mode
cfg.Proxy.Url = proxyURL
cfg.Proxy.URL = proxyURL

return nil
}
}

// WithWatcherConfiguration preparing server to do auto-reload
func WithWatcherConfiguration(watcher bool) ConfigOpt {
return func(cfg *Config) error {

cfg.Watcher = watcher
return nil
}
}
10 changes: 9 additions & 1 deletion internal/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ func TestNewConfig(t *testing.T) {

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
got, err := NewConfig("", "", 0, WithProxyConfiguration(ProxyNone.String(), ""), WithConfigFile(tc.input))
got, err := NewConfig(
"",
"",
0,
false,
WithProxyConfiguration(ProxyNone.String(), ""),
WithConfigFile(tc.input),
WithWatcherConfiguration(tc.expected.Watcher))

if err != nil && tc.err == nil {
t.Fatalf("not expected any erros and got %v", err)
Expand Down Expand Up @@ -114,6 +121,7 @@ func validConfig() Config {
ExposedHeaders: []string{"Cache-Control"},
AllowCredentials: true,
},
Watcher: true,
}
}

Expand Down
17 changes: 17 additions & 0 deletions internal/server/http/cert/server.cert
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-----BEGIN CERTIFICATE-----
MIICyTCCAbGgAwIBAgIJAMUQpJT1NlnjMA0GCSqGSIb3DQEBBQUAMBYxFDASBgNV
BAMTC2V4YW1wbGUuY29tMB4XDTIxMDMxMTIzMjgwNFoXDTMxMDMwOTIzMjgwNFow
FjEUMBIGA1UEAxMLZXhhbXBsZS5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAw
ggEKAoIBAQCrPV00/BCLJ8KpFfPxXT3hEHZPy6mG8sPUed5PwzpNzramNNiXwxFl
/xd4rTbTk6D8AaojGT7SkJTgsZYLKcnMRo8wfUFc+eyzNjGWNTT6fBQGdiATFR+S
CSwa0S8WZzpY3WMHaQRpDp+KS8X48kcFOKcJtwRMOHrB7VQqgvDjNQzLpk5Pb7Qy
TPGuHvERXfNR8Tidm74MW6YDJ53MpHQBWU6Snmmvu37nJCg1X5F5FgSADfKiuTJM
za8R0gL8YkOoHusnW+R0pA5DAeQcjOn/WWR3cIjz8/5TyTpUarEGnWEa0zKpNRsi
Toude1PhysKA5OBQgA7MKe7rsJD5nWhBAgMBAAGjGjAYMBYGA1UdEQQPMA2CC2V4
YW1wbGUuY29tMA0GCSqGSIb3DQEBBQUAA4IBAQAb7zACDvTo936OvXNLELZH1a+Q
xY3qaUBgjAqBU4q47jewz8TpLvFksGPOZOODwrCzrVWd/2xeqcrDRfU7ltdIiRQ/
TbmVhgOU8e8nd00bc766rW6uje/pH2gLl1ysl2kVgEyP/SG+MHcZqsUVavMz9K+G
4iQ71p5G4TcjUIqKZi8VtbNXF4l8qwhGRMDEx/796MB8/4L6rHer6pzWFhya0sjI
LvdUhDAIqKViomzAkrLYBNfJAF4aSCxD7MNpQxZFRibaiSzPiO6nhx4L/b+nUnUN
zDRr+3Brd2bo43k8+4rD+UwFvG5ym/N02hVASJYqUryxBJROU5mwD9HSmll/
-----END CERTIFICATE-----
Loading

0 comments on commit a70bee5

Please sign in to comment.