Skip to content

Commit

Permalink
add contextPath
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay authored and Jay committed Jul 7, 2024
1 parent bc7fc9e commit d6e51b7
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 10 deletions.
10 changes: 6 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ type Config struct {
}

type HttpServerConfig struct {
Port int
Mode string
Port int
Mode string
ContextPath string
}

type StorageConfig struct {
Expand Down Expand Up @@ -51,6 +52,7 @@ func GetConfig() *Config {
func Print(config *Config) {
log.Printf("Server port: %d\n", config.HttpServer.Port)
log.Printf("Server mode: %s\n", config.HttpServer.Mode)
log.Printf("Server context path: %s\n", config.HttpServer.ContextPath)
log.Printf("redisHost: %s\n", config.Storage.RedisHost)
p := ""
if len(config.Storage.RedisPass) > 0 {
Expand All @@ -76,8 +78,9 @@ func LoadConfig(path string) (*Config, error) {
if err := viper.ReadInConfig(); err != nil {
log.Fatalf("Error reading config file: %v", err)
}

//to override config
viper.SetEnvPrefix("app")
_ = viper.BindEnv("httpServer.contextPath", "SERVER_CONTEXT_PATH")
_ = viper.BindEnv("storage.redisHost", "REDIS_HOST")
_ = viper.BindEnv("storage.redisPass", "REDIS_PASS")
_ = viper.BindEnv("kafka.brokers", "BROKERS")
Expand All @@ -86,6 +89,5 @@ func LoadConfig(path string) (*Config, error) {
if err := viper.Unmarshal(&config); err != nil {
return nil, err
}

return &config, nil
}
1 change: 1 addition & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
httpServer:
port: 8088
mode: debug # or release
contextPath: /

storage:
redisHost: localhost:6379
Expand Down
1 change: 1 addition & 0 deletions docker-multi-worker/config_multi_worker.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
httpServer:
port: 8088
mode: debug # or release
contextPath: /api/

storage:
redisHost: localhost:6379
Expand Down
20 changes: 20 additions & 0 deletions docker-multi-worker/health-checks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

# Check if backend servers are healthy and update NGINX configuration

BACKENDS=("docker-multi-worker-worker-ts-1" "docker-multi-worker-worker-ts-2" "docker-multi-worker-worker-ts-3")
CONFIG_FILE="/etc/nginx/conf.d/upstream.conf"
TMP_FILE="/tmp/upstream.conf.tmp"

echo "upstream api_backend {" > $TMP_FILE
for BACKEND in "${BACKENDS[@]}"; do
if curl -s --head http://$BACKEND | grep "200 OK" > /dev/null; then
echo " server $BACKEND;" >> $TMP_FILE
else
echo " # server $BACKEND (down);" >> $TMP_FILE
fi
done
echo "}" >> $TMP_FILE

mv $TMP_FILE $CONFIG_FILE
nginx -s reload
11 changes: 8 additions & 3 deletions docker-multi-worker/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ events { }

http {
upstream worker-ts {
least_conn;
server docker-multi-worker-worker-ts-1:8088;
server docker-multi-worker-worker-ts-2:8088;
server docker-multi-worker-worker-ts-3:8088;

# Advanced health check using the third-party module is not available,
# but you can use simple methods such as max_fails and fail_timeout.
server docker-multi-worker-worker-ts-1:8088 max_fails=3 fail_timeout=30s;
server docker-multi-worker-worker-ts-2:8088 max_fails=3 fail_timeout=30s;
server docker-multi-worker-worker-ts-3:8088 max_fails=3 fail_timeout=30s;
}

upstream kafka-ui-ts {
Expand All @@ -15,15 +20,15 @@ http {
server {
listen 8088;

location / {
location /api/ {
proxy_pass http://worker-ts;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

location /kafka-ui-ts {
location /kafka-ui-ts/ {
proxy_pass http://kafka-ui-ts;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
Expand Down
1 change: 1 addition & 0 deletions e2e/kafka-test.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
httpServer:
port: 8089
mode: test
contextPath: /

storage:
redisHost: ${REDIS_HOST}
Expand Down
7 changes: 4 additions & 3 deletions httpserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ func (s *server) stopServer() {
}

func (s *server) setupGinRouter() *gin.Engine {

gin.SetMode(gin.ReleaseMode)
r := gin.Default()
r.GET("/ping", s.PingHandler)
r.GET("/task", s.GetAllTasksHandler)
r.POST("/task", s.SetNewTaskHandler)
r.GET(fmt.Sprintf("%sping", s.config.HttpServer.ContextPath), s.PingHandler)
r.GET(fmt.Sprintf("%stask", s.config.HttpServer.ContextPath), s.GetAllTasksHandler)
r.POST(fmt.Sprintf("%stask", s.config.HttpServer.ContextPath), s.SetNewTaskHandler)
return r
}

Expand Down

0 comments on commit d6e51b7

Please sign in to comment.