Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
41 changes: 40 additions & 1 deletion dev-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ cecho() {
# Get directory path
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

#=-----------------------------------=#
#= Environment Variables & Overrides =#
#=-----------------------------------=#

export DEFAULTS_PATH=$DIR/data/defaults.yml
export FAVICON_PATH=$DIR/data/favicon.ico
export CONFIG_PATH=$DIR/.dev/config.yml
Expand All @@ -52,4 +56,39 @@ if [ -f "$DIR/dev.local.env" ]; then
source "$DIR/dev.local.env"
fi

cecho "${GREEN}Successfully loaded development environment!${END}"
cecho "${GREEN}Successfully loaded development environment!${END}"

#=-----------------------------------=#
#= Mock server =#
#=-----------------------------------=#

MOCK_BIN="/tmp/mockserver-$MOCK_PORT"
MOCK_PID="/tmp/mockserver-$MOCK_PORT.pid"
MOCK_PORT="8881"

# Kill mockserver if still running
if [ -f "$MOCK_PID" ]; then
OLD_PID=$(cat "$MOCK_PID")
if ps -p "$OLD_PID" > /dev/null 2>&1; then
cecho "${YELLOW}Stopping previous Mock server (PID $OLD_PID)${END}"
kill "$OLD_PID"
sleep 1
fi
fi

# Build mockserver
go build -o "$MOCK_BIN" utils/mockserver/mockserver.go

set +m

if ! nc -z 127.0.0.1 "$MOCK_PORT" >/dev/null 2>&1; then
$MOCK_BIN > "$DIR/.dev/mock.log" 2>&1 &
NEW_PID=$!
disown

echo "$NEW_PID" > "$MOCK_PID"

echo "Mock server started at http://127.0.0.1:$MOCK_PORT"
else
echo "There is already a Service running at http://127.0.0.1:$MOCK_PORT"
fi
2 changes: 2 additions & 0 deletions internals/proxy/middlewares/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,5 +282,7 @@ func (chain *AuthChain) Eval(w http.ResponseWriter, req *http.Request, tokens []
}
}

logger.Warn("Client failed to provide any auth")

return "", err
}
18 changes: 16 additions & 2 deletions internals/proxy/middlewares/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,24 @@ func getConfigByReq(req *http.Request) *structure.CONFIG {
return getConfig(getToken(req))
}

func getConfig(token string) *structure.CONFIG {
func getConfigWithoutDefaultByReq(req *http.Request) *structure.CONFIG {
return getConfigWithoutDefault(getToken(req))
}

func getConfigWithoutDefault(token string) *structure.CONFIG {
conf, exists := config.ENV.CONFIGS[token]

if !exists || conf == nil {
if !exists {
return nil
}

return conf
}

func getConfig(token string) *structure.CONFIG {
conf := getConfigWithoutDefault(token)

if conf == nil {
conf = config.DEFAULT
}

Expand Down
8 changes: 6 additions & 2 deletions internals/proxy/middlewares/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ var InternalMiddlewareLogger Middleware = Middleware{

func middlewareLoggerHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
conf := getConfigByReq(req)
conf := getConfigWithoutDefaultByReq(req)

logLevel := conf.SERVICE.LOG_LEVEL
var logLevel string

if conf != nil {
logLevel = conf.SERVICE.LOG_LEVEL
}

l := logger.Get()

Expand Down
9 changes: 9 additions & 0 deletions internals/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ func Create(targetUrl string) Proxy {
}

proxy := httputil.NewSingleHostReverseProxy(url)
proxy.ModifyResponse = func(res *http.Response) error {
res.Header.Set("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0, private, proxy-revalidate")
res.Header.Set("Pragma", "no-cache")
res.Header.Set("Expires", "0")
res.Header.Set("Vary", "*")
res.Header.Set("Referrer-Policy", "no-referrer")

return nil
}

w := &ioutils.InterceptWriter{
Writer: &bytes.Buffer{},
Expand Down
58 changes: 58 additions & 0 deletions utils/mockserver/mockserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"fmt"
"net"
"net/http"
"os"
"strings"

l "github.com/codeshelldev/gotl/pkg/logger"
"github.com/codeshelldev/gotl/pkg/request"
)

func main() {
logLevel := os.Getenv("LOG_LEVEL")

if strings.TrimSpace(logLevel) == "" {
logLevel = "info"
}

port := os.Getenv("PORT")

if strings.TrimSpace(port) == "" {
port = "8881"
}

logger, err := l.NewWithDefaults(logLevel)

http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
body, err := request.GetReqBody(req)

if err != nil {
http.Error(w, "Bad Request: invalid body", http.StatusBadRequest)
return
}

ip, _, _ := net.SplitHostPort(req.RemoteAddr)

if body.Empty {
logger.Info(ip, " ", req.Method, " ", req.URL.Path, " ", req.URL.RawQuery)
} else {
logger.Info(ip, " ", req.Method, " ", req.URL.Path, " ", req.URL.RawQuery, body.Raw)
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

fmt.Fprint(w, `{"message":"Hello from mock endpoint"}`)
})

logger.Info("Mock server running at http://127.0.0.1:", port)

err = http.ListenAndServe("127.0.0.1:" + port, nil)

if err != nil {
logger.Fatal("Error starting Mock server: ", err.Error())
}
}