Skip to content

Commit 152355e

Browse files
committed
fix api gateway
1 parent 06338c6 commit 152355e

File tree

9 files changed

+109
-13
lines changed

9 files changed

+109
-13
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ vendor/
2525

2626
# Windows
2727
Thumbs.db
28+
29+
golang-simple-gateway

Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
FROM golang:1.21-alpine3.18
2+
3+
4+
# The latest alpine images don't have some tools like (`git` and `bash`).
5+
# Adding git, bash and openssh to the image
6+
RUN apk update && apk upgrade && \
7+
apk add --no-cache bash git openssh
8+
9+
# Set the Current Working Directory inside the container
10+
WORKDIR /app
11+
12+
13+
COPY go.mod go.sum ./
14+
15+
# Download all dependancies. Dependencies will be cached if the go.mod and go.sum files are not changed
16+
RUN go mod download
17+
18+
COPY . .
19+
20+
# Build the Go app
21+
RUN go build -o golang-simple-gateway .
22+
23+
# Expose port 8080 to the outside world
24+
EXPOSE 8080
25+
26+
# Run the executable
27+
CMD ["./golang-simple-gateway"]

config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
configs:
2-
- endpoint: http://localhost:8011/
2+
- endpoint: http://host.docker.internal:8011
33
namespace: httpbin
44
header_auth:
55
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
66
X-Api-Key: testAPI KEY
7-
- endpoint: http://ip-api.com/
7+
- endpoint: http://ip-api.com/json
88
namespace: ip-api
99
header_auth:

docker-compose.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
version: '3.8'
2+
services:
3+
app:
4+
container_name: simple-gateway-app
5+
build:
6+
context: .
7+
dockerfile: Dockerfile
8+
ports:
9+
- "8080:8080"
10+
restart: unless-stopped
11+
extra_hosts:
12+
- "host.docker.internal:host-gateway"
13+
depends_on:
14+
- redis
15+
networks:
16+
- goapi-gateway-network
17+
18+
redis:
19+
container_name: simple-gateway-redis
20+
image: redis:7.0.5-alpine
21+
networks:
22+
- goapi-gateway-network
23+
ports:
24+
- "6300:6379"
25+
26+
httpbin:
27+
container_name: simple-gateway-httpbin
28+
image: stevet/httpbin-alpine
29+
ports:
30+
- "8011:8000"
31+
networks:
32+
- goapi-gateway-network
33+
34+
networks:
35+
goapi-gateway-network:
36+
driver: bridge

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module goapi-id/gateway
1+
module github.com/daniwebdev/go-simple-gateway
22

33
go 1.18
44

main

-10 MB
Binary file not shown.

main.go

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ import (
99
"os"
1010
"strings"
1111

12+
"github.com/daniwebdev/go-simple-gateway/middleware"
1213
"github.com/gofiber/fiber/v2"
13-
"github.com/gofiber/fiber/v2/middleware/logger"
1414
"github.com/gofiber/fiber/v2/middleware/recover"
1515
"gopkg.in/yaml.v2"
1616
)
1717

18+
19+
1820
type Config struct {
1921
Endpoint string `yaml:"endpoint"`
2022
Namespace string `yaml:"namespace"`
@@ -26,19 +28,29 @@ type GatewayConfig struct {
2628
}
2729

2830
func main() {
31+
configFile := "config.yml"
32+
33+
if len(os.Args) > 1 {
34+
configFile = os.Args[1]
35+
}
36+
2937
// Load configuration from YAML file
30-
config, err := loadConfig("config.yml")
38+
config, err := loadConfig(configFile)
3139
if err != nil {
3240
log.Fatalf("Failed to load config: %v", err)
3341
}
3442

3543
app := fiber.New()
3644

3745
// Middleware
38-
app.Use(logger.New())
46+
// app.Use(logger.New())
47+
app.Use(middleware.LoggerMiddleware())
3948
app.Use(recover.New())
4049

4150
// Routes
51+
app.Get("/", func(c *fiber.Ctx) error {
52+
return c.SendString("Ok")
53+
})
4254
app.All("/v1/*", func(c *fiber.Ctx) error {
4355
path := c.Params("*")
4456

@@ -78,18 +90,25 @@ func main() {
7890
}
7991

8092
// Set body parameter from the request (if applicable)
81-
if c.Method() == fiber.MethodPost ||
93+
if c.Method() == fiber.MethodPost ||
8294
c.Method() == fiber.MethodPut ||
8395
c.Method() == fiber.MethodPatch ||
8496
c.Method() == fiber.MethodDelete {
85-
bodyBytes := c.Body()
86-
req.Body = ioutil.NopCloser(bytes.NewReader(bodyBytes))
87-
req.ContentLength = int64(len(bodyBytes))
88-
}
97+
bodyBytes := c.Body()
98+
req.Body = ioutil.NopCloser(bytes.NewReader(bodyBytes))
99+
req.ContentLength = int64(len(bodyBytes))
100+
}
89101

90-
// Set headers from the configuration
102+
// Pass all headers from the incoming request to the new request
103+
c.Request().Header.VisitAll(func(key, value []byte) {
104+
req.Header.Set(string(key), string(value))
105+
})
106+
107+
// Set headers from the configuration, allowing client headers to overwrite
91108
for key, value := range config.Headers {
92-
req.Header.Set(key, value)
109+
if req.Header.Get(key) == "" {
110+
req.Header.Set(key, value)
111+
}
93112
}
94113

95114
// Perform the HTTP request

middleware/logger.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package middleware
2+
3+
import (
4+
"github.com/gofiber/fiber/v2"
5+
"github.com/gofiber/fiber/v2/middleware/logger"
6+
)
7+
8+
func LoggerMiddleware() fiber.Handler {
9+
return logger.New()
10+
}

utils/redis.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package utils
2+

0 commit comments

Comments
 (0)