Skip to content

Commit

Permalink
added readme and updated to use of tls as well
Browse files Browse the repository at this point in the history
  • Loading branch information
s0s01qp committed Jul 25, 2021
1 parent aaabdae commit 977e523
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 11 deletions.
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Moxy

[![GoDoc](https://godoc.org/github.com/sinhashubham95/moxy?status.svg)](https://pkg.go.dev/github.com/sinhashubham95/moxy)
[![Release](https://img.shields.io/github/v/release/sinhashubham95/moxy?sort=semver)](https://github.com/sinhashubham95/moxy/releases)
[![Report](https://goreportcard.com/badge/github.com/sinhashubham95/moxy)](https://goreportcard.com/report/github.com/sinhashubham95/moxy)
[![Coverage Status](https://coveralls.io/repos/github/sinhashubham95/moxy/badge.svg?branch=master)](https://coveralls.io/github/sinhashubham95/moxy?branch=master)
[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go#server-applications)

Moxy is a simple mocker and proxy application server. Here you can create mock endpoints as well as proxy requests in case no mock exists for the endpoint.

## How it works

![Moxy Architecture Diagram](./moxy.png)

## Features

### Small, Pragmatic and Easy to Use

- Dockerized

- Compiled

- Easily configurable via Environment Variables

- Self-contained, does not require Go or any other dependency, just run the binary or the container

### File-based Persistence

- No heavy database involved.

- Saves the mock endpoints in files.

### Security

- TLS can be enabled by providing your own SSL/TLS Certificates.

### Reliability

- Uses [Go Actuator](https://github.com/sinhashubham95/go-actuator)

- Fully Tested, Unit, Functional & Linted & 0 Race Conditions Detected.

### Customizations

- Mock endpoints created are highly customizable.

- Application port can be configured via the environment variable.

- Database file path can be configured via the environment variable.

## Project Versioning

Moxy uses [semantic versioning](http://semver.org/). No API changes will be introduced in the minor and patch version changes. New minor versions might add additional features to the API.
25 changes: 17 additions & 8 deletions commons/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@ import "fmt"

// Common Constants
const (
ActuatorPrefix = "/actuator"
MoxyPrefix = "/moxy"
PersistencePath = "persistence-path"
PersistenceDefaultValue = "persistence.db"
PersistenceUsage = "persistence path is the path of the file which acts as the persistence layer for this application"
Port = "port"
PortDefaultValue = 8080
PortUsage = "port is the port number on which the application is running"
ActuatorPrefix = "/actuator"
CertFilePath = "cert-file"
CertFilePathDefaultValue = ""
CertFilePathUsage = "cert file path is the path of the certificate file"
KeyFilePath = "key-file"
KeyFilePathDefaultValue = ""
KeyFilePathUsage = "key file path is the path of the key file"
MoxyPrefix = "/moxy"
PersistencePath = "persistence-path"
PersistenceDefaultValue = "persistence.db"
PersistenceUsage = "persistence path is the path of the file which acts as the persistence layer for this application"
Port = "port"
PortDefaultValue = 8080
PortUsage = "port is the port number on which the application is running"
TLSEnabled = "tls"
TLSEnabledDefaultValue = false
TLSEnabledUsage = "tls enabled tells whether the application is running on HTTPS or not"
)

// Endpoint Paths
Expand Down
18 changes: 18 additions & 0 deletions flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import (
)

var (
certFilePath = flag.String(commons.CertFilePath, commons.CertFilePathDefaultValue, commons.CertFilePathUsage)
keyFilePath = flag.String(commons.KeyFilePath, commons.KeyFilePathDefaultValue, commons.KeyFilePathUsage)
persistencePath = flag.String(commons.PersistencePath, commons.PersistenceDefaultValue, commons.PersistenceUsage)
port = flag.Int(commons.Port, commons.PortDefaultValue, commons.PortUsage)
tlsEnabled = flag.Bool(commons.TLSEnabled, commons.TLSEnabledDefaultValue, commons.TLSEnabledUsage)

// dummy flags
_ = flag.String(actuatorCommons.Env, actuatorCommons.EnvDefaultValue, actuatorCommons.EnvUsage)
Expand All @@ -21,6 +24,16 @@ func init() {
flag.Parse()
}

// CertFilePath is the path to the certificate file
func CertFilePath() string {
return *certFilePath
}

// KeyFilePath is the path to the key file
func KeyFilePath() string {
return *keyFilePath
}

// PersistencePath is the path of the persistence database file
func PersistencePath() string {
return *persistencePath
Expand All @@ -30,3 +43,8 @@ func PersistencePath() string {
func Port() int {
return *port
}

// TLSEnabled tells whether the application should start on HTTPS
func TLSEnabled() bool {
return *tlsEnabled
}
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
)

func main() {
err := fasthttp.ListenAndServe(fmt.Sprintf(":%d", flags.Port()), getFastHTTPHandler())
if err != nil {
return
if flags.TLSEnabled() {
_ = fasthttp.ListenAndServeTLS(fmt.Sprintf(":%d", flags.Port()), flags.CertFilePath(),
flags.KeyFilePath(), getFastHTTPHandler())
} else {
_ = fasthttp.ListenAndServe(fmt.Sprintf(":%d", flags.Port()), getFastHTTPHandler())
}
}
3 changes: 3 additions & 0 deletions models/mockrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ type MockRequest struct {
ResponseBody interface{} `json:"responseBody"`
}

// Clean is used to clean the request path
func (r *MockRequest) Clean() {
if r.Path != "" {
r.Path = filepath.Clean(r.Path)
}
}

// Validate is used to validate the request
func (r *MockRequest) Validate() error {
if r.Tag == "" {
// empty tag - which is not allowed because it is the context of the mock
Expand All @@ -50,6 +52,7 @@ func (r *MockRequest) Validate() error {
return nil
}

// Default is used to set default values for the missing request fields
func (r *MockRequest) Default() {
if r.ResponseStatus == 0 {
r.ResponseStatus = http.StatusOK
Expand Down
1 change: 1 addition & 0 deletions models/unmockrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type UnMockRequest struct {
Path string `json:"path"`
}

// Clean is used to clean the request path
func (r *UnMockRequest) Clean() {
if r.Path != "" {
r.Path = filepath.Clean(r.Path)
Expand Down
Binary file added moxy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions persistence/entities/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ func (m *Mock) Key() ([]byte, error) {
})
}

// Encode is used to get the encoded bytes for this mock instance
func (m *Mock) Encode() ([]byte, error) {
return commons.EncodeJSON(m)
}

// Decode is used to decode the bytes to this mock instance
func (m *Mock) Decode(b []byte) error {
return commons.DecodeJSON(b, m)
}

0 comments on commit 977e523

Please sign in to comment.