Skip to content

Commit

Permalink
Merge pull request #60 from Onyxmoon/feature/proxy
Browse files Browse the repository at this point in the history
Feature/proxy
  • Loading branch information
Onyxmoon authored Nov 3, 2023
2 parents 725c30b + c76ce51 commit 44047a2
Show file tree
Hide file tree
Showing 8 changed files with 836 additions and 12 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-81.0%25-brightgreen)
![Coverage](https://img.shields.io/badge/Coverage-78.5%25-brightgreen)
[![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)
[![Run tests (shoppinglist-service)](https://github.com/Onyxmoon/hsfl-master-ai-cloud-engineering/actions/workflows/run-tests-shoppinglist-service.yml/badge.svg)](https://github.com/Onyxmoon/hsfl-master-ai-cloud-engineering/actions/workflows/run-tests-shoppinglist-service.yml)
[![Run tests (user-service)](https://github.com/Onyxmoon/hsfl-master-ai-cloud-engineering/actions/workflows/run-tests-user-service.yml/badge.svg)](https://github.com/Onyxmoon/hsfl-master-ai-cloud-engineering/actions/workflows/run-tests-user-service.yml)
Expand Down
5 changes: 3 additions & 2 deletions go.work
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
go 1.21
go 1.21.2

use (
lib
src/product-service
src/shoppinglist-service
src/user-service
src/web-service
src/shoppinglist-service
src/http-proxy-service
)
174 changes: 169 additions & 5 deletions go.work.sum

Large diffs are not rendered by default.

54 changes: 50 additions & 4 deletions lib/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package router

import (
"context"
"fmt"
"net/http"
"regexp"
)
Expand Down Expand Up @@ -53,16 +54,29 @@ func createRequestContext(r *http.Request, paramKeys []string, paramValues []str
}

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

params := make([]string, len(paramMatches))

if len(paramMatches) > 0 {
pattern = paramMatcher.ReplaceAllLiteralString(pattern, "([^/]+)")

pattern = paramMatcher.ReplaceAllStringFunc(pattern, func(substring string) string {
if substring == "*" {
return "(.*)"
} else {
return "([^/]+)"
}
})

wildcardNo := 0
for i, match := range paramMatches {
params[i] = match[1]
if match[1] != "" {
params[i] = match[1]
} else {
params[i] = fmt.Sprintf("wildcard%d", wildcardNo)
wildcardNo++
}

}
}

Expand All @@ -89,3 +103,35 @@ func (router *Router) PUT(pattern string, handler http.HandlerFunc) {
func (router *Router) DELETE(pattern string, handler http.HandlerFunc) {
router.addRoute(http.MethodDelete, pattern, handler)
}

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

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

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

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

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

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)
}
18 changes: 18 additions & 0 deletions src/http-proxy-service/config/proxyConfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
proxy:
listenAddress: localhost:8080
proxyRoutes:
- name: User Service
context: /users
target: http://localhost:3001
- name: Shoppinglist Service
context: /shoppinglists
target: http://localhost:3002
- name: Product Service
context: /products
target: http://localhost:3003
- name: Data Processing Service
context: /processing
target: http://localhost:3004
- name: Web Service
context: /
target: http://localhost:3000
26 changes: 26 additions & 0 deletions src/http-proxy-service/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module hsfl.de/group6/hsfl-master-ai-cloud-engineering/http-proxy-service

go 1.21.2

require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.17.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 44047a2

Please sign in to comment.