Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiht committed Aug 29, 2019
0 parents commit a5be587
Show file tree
Hide file tree
Showing 528 changed files with 417,179 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.go]
indent_size = unset
indent_style = tab

[Makefile]
indent_size = unset
indent_style = tab
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM golang:buster AS build
ARG VERSION=snapshot
WORKDIR /go/src/github.com/Thiht/go-mock-server
COPY . .
RUN make version=$VERSION build

FROM debian:buster
RUN apt-get update && \
apt-get install -y ca-certificates && \
rm -rf /var/lib/apt/lists/*
COPY --from=build /go/src/github.com/Thiht/go-mock-server/build/go-mock-server /opt/go-mock-server
WORKDIR /opt
EXPOSE 8080 8081
CMD ["/opt/go-mock-server"]
149 changes: 149 additions & 0 deletions Gopkg.lock

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

34 changes: 34 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[[constraint]]
name = "github.com/labstack/echo"
version = "4.1.10"

[prune]
go-tests = true
unused-packages = true
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Thibaut Rousseau

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
APPNAME:=$(shell basename $(shell go list))
VERSION?=snapshot
COMMIT:=$(shell git rev-parse --verify HEAD)
DATE:=$(shell date +%FT%T%z)

GO_LDFLAGS+=-X main.appName=$(APPNAME)
GO_LDFLAGS+=-X main.buildVersion=$(VERSION)
GO_LDFLAGS+=-X main.buildCommit=$(COMMIT)
GO_LDFLAGS+=-X main.buildDate=$(DATE)
GO_LDFLAGS:=-ldflags="$(GO_LDFLAGS)"

REFLEX=$(GOPATH)/bin/reflex
$(REFLEX):
go get github.com/cespare/reflex

.PHONY: start
start: $(REFLEX)
reflex --start-service \
--decoration='none' \
--regex='\.go$$' \
--inverse-regex='^vendor/' \
-- go run $(GO_LDFLAGS) *.go

.PHONY: build
build:
go build $(GO_LDFLAGS) -o build/$(APPNAME)

.PHONY: clean
clean:
rm -rf ./build
91 changes: 91 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package main

import (
"os"
"strconv"
"strings"

"github.com/labstack/echo"
"github.com/namsral/flag"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)

var (
appName, buildVersion, buildCommit, buildDate string // nolint
)

type config struct {
logLevel string
configListenPort int
mockServerListenPort int
}

func parseConfig() (c config) {
// Use a prefix for environment variables
flag.CommandLine = flag.NewFlagSetWithEnvPrefix(os.Args[0], "GOMS", flag.ExitOnError)

flag.StringVar(&c.logLevel, "log-level", "info", "")
flag.IntVar(&c.configListenPort, "config-listen-port", 8081, "")
flag.IntVar(&c.mockServerListenPort, "mock-server-listen-port", 8080, "")

flag.Parse()
return
}

func setupLogger(logLevel string) {
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})

level, err := log.ParseLevel(logLevel)
if err != nil {
log.WithError(err).WithField("level", level).Warn("Invalid log level, fallback to info")
level = log.InfoLevel
}
log.WithField("level", level).Info("Setting log level")
log.SetLevel(level)
}

func setupServer(mockServerListenPort, configListenPort int) {
mockServer := NewMockServer()
mockServer.Start(mockServerListenPort)

e := echo.New()
e.HideBanner = true
e.HidePort = true

e.POST("/mocks", func(c echo.Context) error {
req := c.Request()
var mocks []MockRoute
if err := c.Bind(&mocks); err != nil {
if err != echo.ErrUnsupportedMediaType {
return err
}

// echo doesn't support YAML yet
if strings.HasPrefix(req.Header.Get(echo.HeaderContentType), "application/x-yaml") {
if err := yaml.NewDecoder(req.Body).Decode(&mocks); err != nil {
return err
}
}
}

for _, mock := range mocks {
mockServer.AddRoute(mock)
}

return nil
})

log.WithField("port", configListenPort).Info("Starting config server")
if err := e.Start(":" + strconv.Itoa(configListenPort)); err != nil {
log.WithError(err).Fatal("Config server execution failed")
}
}

func main() {
c := parseConfig()
setupLogger(c.logLevel)
setupServer(c.mockServerListenPort, c.configListenPort)
}
Loading

0 comments on commit a5be587

Please sign in to comment.