Skip to content

Commit

Permalink
Merge branch 'develop' into fix/return-maps-in-correct-order
Browse files Browse the repository at this point in the history
  • Loading branch information
Onyxmoon authored Jan 3, 2024
2 parents d5c0abb + f9a566e commit b8ad7c8
Show file tree
Hide file tree
Showing 85 changed files with 8,815 additions and 491 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Price Whisper
![Coverage](https://img.shields.io/badge/Coverage-82.7%25-brightgreen)
![Coverage](https://img.shields.io/badge/Coverage-77.2%25-brightgreen)
![GitHub release (by tag)](https://img.shields.io/github/v/tag/onyxmoon/hsfl-master-ai-cloud-engineering.svg?sort=semver&label=Version&color=4ccc93d)
[![Run tests (http proxy service)](https://github.com/Onyxmoon/hsfl-master-ai-cloud-engineering/actions/workflows/run-tests-http-proxy-service.yml/badge.svg)](https://github.com/Onyxmoon/hsfl-master-ai-cloud-engineering/actions/workflows/run-tests-http-proxy-service.yml)
[![Run tests (product-service)](https://github.com/Onyxmoon/hsfl-master-ai-cloud-engineering/actions/workflows/run-tests-product-service.yml/badge.svg)](https://github.com/Onyxmoon/hsfl-master-ai-cloud-engineering/actions/workflows/run-tests-product-service.yml)
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.dev.native.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ services:
context: ./
dockerfile: ./src/http-proxy-service/Dockerfile
environment:
- PROXY_DOCKER_CONFIG_PATH=./config
- PROXY_DOCKER_CONFIG_FILENAME=proxyConfig.docker.native.yaml
- PROXY_CONFIG_PATH=./config/proxyConfig.docker.native.yaml
ports:
- 8080:8080
depends_on:
Expand All @@ -28,6 +27,7 @@ services:
dockerfile: ./src/product-service/Dockerfile
ports:
- 3003:3003
- 50051:50051

users:
build:
Expand Down
7 changes: 2 additions & 5 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ services:
context: ./
dockerfile: ./src/http-proxy-service/Dockerfile
environment:
- PROXY_DOCKER_CONFIG_PATH=./config
- PROXY_DOCKER_CONFIG_FILENAME=proxyConfig.docker.yaml
- PROXY_CONFIG_PATH=./config/proxyConfig.docker.yaml
ports:
- 8080:8080
depends_on:
Expand All @@ -18,9 +17,7 @@ services:
context: ./
dockerfile: ./src/load-balancer-service/Dockerfile
environment:
- IMAGE=onyxmoon/pw-web-service:latest
- REPLICAS=5
- NETWORK_NAME=dev-network
- LOAD_BALANCER_CONFIG_PATH=./config/config.web-service.dev.yaml
ports:
- 3000:3000
volumes:
Expand Down
20 changes: 2 additions & 18 deletions docker-compose.native.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
services:
proxy:
build:
context: ./
dockerfile: ./src/http-proxy-service/Dockerfile
image: onyxmoon/pw-http-proxy-service:latest
environment:
- PROXY_DOCKER_CONFIG_PATH=./config
- PROXY_DOCKER_CONFIG_FILENAME=proxyConfig.docker.native.yaml
- PROXY_CONFIG_PATH=./config/proxyConfig.docker.native.yaml
ports:
- 80:8080
- 8080:8080
- 443:8443
depends_on:
- web
Expand All @@ -17,9 +13,6 @@ services:
- internal

web:
build:
context: ./
dockerfile: ./src/web-service/Dockerfile
image: onyxmoon/pw-web-service:latest
ports:
- 3000:3000
Expand All @@ -31,29 +24,20 @@ services:
- internal

products:
build:
context: ./
dockerfile: ./src/product-service/Dockerfile
image: onyxmoon/pw-product-service:latest
ports:
- 3003:3003
networks:
- internal

users:
build:
context: ./
dockerfile: ./src/user-service/Dockerfile
image: onyxmoon/pw-user-service:latest
ports:
- 3001:3001
networks:
- internal

shoppinglists:
build:
context: ./
dockerfile: ./src/shoppinglist-service/Dockerfile
image: onyxmoon/pw-shoppinglist-service:latest
ports:
- 3002:3002
Expand Down
10 changes: 3 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ services:
dockerfile: ./src/http-proxy-service/Dockerfile
image: onyxmoon/pw-http-proxy-service:latest
environment:
- PROXY_DOCKER_CONFIG_PATH=./config
- PROXY_DOCKER_CONFIG_FILENAME=proxyConfig.docker.yaml
- PROXY_CONFIG_PATH=./config/proxyConfig.docker.yaml
ports:
- 80:8080
- 443:8443
- 8080:8080
depends_on:
- load-balancer-web
networks:
Expand All @@ -22,9 +20,7 @@ services:
dockerfile: ./src/load-balancer-service/Dockerfile
image: onyxmoon/pw-load-balancer-service:latest
environment:
- IMAGE=onyxmoon/pw-web-service:latest
- REPLICAS=5
- NETWORK_NAME=bridge
- LOAD_BALANCER_CONFIG_PATH=./config/config.web-service.yaml
ports:
- 3000:3000
volumes:
Expand Down
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ use (
src/user-service
src/web-service
src/http-proxy-service
utils/http-stress-test
)
657 changes: 640 additions & 17 deletions go.work.sum

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions lib/router/middleware/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package middleware

import (
router "hsfl.de/group6/hsfl-master-ai-cloud-engineering/lib"
"net/http"
)

func CreateAuthMiddleware() router.Middleware {
return func(w http.ResponseWriter, r *http.Request) *http.Request {
return r
}
}
79 changes: 47 additions & 32 deletions lib/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"regexp"
)

type Middleware = func(w http.ResponseWriter, r *http.Request) *http.Request

type route struct {
method string
pattern *regexp.Regexp
Expand All @@ -15,7 +17,8 @@ type route struct {
}

type Router struct {
routes []route
routes []route
middlewares []Middleware
}

func New() *Router {
Expand All @@ -32,6 +35,9 @@ func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {

if len(matches) > 0 {
r = createRequestContext(r, route.params, matches[1:])
for _, middleware := range router.middlewares {
r = middleware(w, r)
}
route.handler(w, r)
return
}
Expand All @@ -53,7 +59,7 @@ func createRequestContext(r *http.Request, paramKeys []string, paramValues []str
return r.WithContext(ctx)
}

func (router *Router) addRoute(method string, pattern string, handler http.HandlerFunc) {
func (router *Router) addRoute(method string, pattern string, handler http.HandlerFunc, middlewares ...Middleware) {
paramMatcher := regexp.MustCompile(":([a-zA-Z]+)|\\*")
paramMatches := paramMatcher.FindAllStringSubmatch(pattern, -1)

Expand Down Expand Up @@ -83,55 +89,64 @@ func (router *Router) addRoute(method string, pattern string, handler http.Handl
router.routes = append(router.routes, route{
method: method,
pattern: regexp.MustCompile("^" + pattern + "$"),
handler: handler,
params: params,
handler: func(w http.ResponseWriter, r *http.Request) {
for _, middleware := range middlewares {
r = middleware(w, r)
}
handler(w, r)
},
params: params,
})
}

func (router *Router) GET(pattern string, handler http.HandlerFunc) {
router.addRoute(http.MethodGet, pattern, handler)
func (router *Router) GET(pattern string, handler http.HandlerFunc, middlewares ...Middleware) {
router.addRoute(http.MethodGet, pattern, handler, middlewares...)
}

func (router *Router) POST(pattern string, handler http.HandlerFunc, middlewares ...Middleware) {
router.addRoute(http.MethodPost, pattern, handler, middlewares...)
}

func (router *Router) POST(pattern string, handler http.HandlerFunc) {
router.addRoute(http.MethodPost, pattern, handler)
func (router *Router) PUT(pattern string, handler http.HandlerFunc, middlewares ...Middleware) {
router.addRoute(http.MethodPut, pattern, handler, middlewares...)
}

func (router *Router) PUT(pattern string, handler http.HandlerFunc) {
router.addRoute(http.MethodPut, pattern, handler)
func (router *Router) DELETE(pattern string, handler http.HandlerFunc, middlewares ...Middleware) {
router.addRoute(http.MethodDelete, pattern, handler, middlewares...)
}

func (router *Router) DELETE(pattern string, handler http.HandlerFunc) {
router.addRoute(http.MethodDelete, pattern, handler)
func (router *Router) PATCH(pattern string, handler http.HandlerFunc, middlewares ...Middleware) {
router.addRoute(http.MethodPatch, pattern, handler, middlewares...)
}

func (router *Router) PATCH(pattern string, handler http.HandlerFunc) {
router.addRoute(http.MethodPatch, pattern, handler)
func (router *Router) CONNECT(pattern string, handler http.HandlerFunc, middlewares ...Middleware) {
router.addRoute(http.MethodConnect, pattern, handler, middlewares...)
}

func (router *Router) CONNECT(pattern string, handler http.HandlerFunc) {
router.addRoute(http.MethodConnect, pattern, handler)
func (router *Router) HEAD(pattern string, handler http.HandlerFunc, middlewares ...Middleware) {
router.addRoute(http.MethodHead, pattern, handler, middlewares...)
}

func (router *Router) HEAD(pattern string, handler http.HandlerFunc) {
router.addRoute(http.MethodHead, pattern, handler)
func (router *Router) OPTIONS(pattern string, handler http.HandlerFunc, middlewares ...Middleware) {
router.addRoute(http.MethodOptions, pattern, handler, middlewares...)
}

func (router *Router) OPTIONS(pattern string, handler http.HandlerFunc) {
router.addRoute(http.MethodOptions, pattern, handler)
func (router *Router) TRACE(pattern string, handler http.HandlerFunc, middlewares ...Middleware) {
router.addRoute(http.MethodTrace, pattern, handler, middlewares...)
}

func (router *Router) TRACE(pattern string, handler http.HandlerFunc) {
router.addRoute(http.MethodTrace, pattern, handler)
func (router *Router) ALL(pattern string, handler http.HandlerFunc, middlewares ...Middleware) {
router.GET(pattern, handler, middlewares...)
router.POST(pattern, handler, middlewares...)
router.PUT(pattern, handler, middlewares...)
router.DELETE(pattern, handler, middlewares...)
router.PATCH(pattern, handler, middlewares...)
router.CONNECT(pattern, handler, middlewares...)
router.HEAD(pattern, handler, middlewares...)
router.OPTIONS(pattern, handler, middlewares...)
router.TRACE(pattern, handler, middlewares...)
}

func (router *Router) ALL(pattern string, handler http.HandlerFunc) {
router.GET(pattern, handler)
router.POST(pattern, handler)
router.PUT(pattern, handler)
router.DELETE(pattern, handler)
router.PATCH(pattern, handler)
router.CONNECT(pattern, handler)
router.HEAD(pattern, handler)
router.OPTIONS(pattern, handler)
router.TRACE(pattern, handler)
func (router *Router) RegisterMiddleware(middleware Middleware) {
router.middlewares = append(router.middlewares, middleware)
}
48 changes: 48 additions & 0 deletions lib/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,52 @@ func TestRouter(t *testing.T) {
assert.Equal(t, "route", ctx.Value("route"))
assert.Equal(t, "params", ctx.Value("params"))
})

t.Run("should handle simple handler response", func(t *testing.T) {
// given
router := New()
handlerResponse := "Test response"
router.GET("/test/handler", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(handlerResponse))
})

w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/test/handler", nil)

// when
router.ServeHTTP(w, r)

// then
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, handlerResponse, w.Body.String())
})

t.Run("should handle handler response with middleware", func(t *testing.T) {
// given
router := New()
handlerResponse := "Test response"
contextKey := "testKey"
expectedValue := "testValue"
var ctx context.Context
router.GET("/test/context",
func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(handlerResponse))
w.WriteHeader(http.StatusOK)
ctx = r.Context()
},
func(w http.ResponseWriter, r *http.Request) *http.Request {
return r.WithContext(context.WithValue(r.Context(), contextKey, expectedValue))
})

w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/test/context", nil)

// when
router.ServeHTTP(w, r)

// then
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, expectedValue, ctx.Value(contextKey).(string))
assert.Equal(t, handlerResponse, w.Body.String())
})
}
Loading

0 comments on commit b8ad7c8

Please sign in to comment.