Skip to content

Commit bd52914

Browse files
authored
feat: move to yml based config (#131)
* feat: move to yml based config * feat: replaced env vars with system config * chore: rename serviceconfig to systemconfig * chore: remopve http support from haproxy image * chore: haproxy no need to expose 5555 anymore * chore: update config variable names * chore: configuration example added * chore: configuration example added * chore: tidy dependecies
1 parent 1e4c25a commit bd52914

32 files changed

+384
-152
lines changed

config.example.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
version: 1.0
2+
mode: standalone # standalone or cluster
3+
service:
4+
auto_tls: true # true or false
5+
bind_address: 0.0.0.0
6+
bind_port: 3333 # choose any other ports except 80 and 443
7+
network_name: swiftwave_network # docker swarm overflow network name
8+
data_dir: ~/swiftwave/data
9+
docker_unix_socket_path: /var/run/docker.sock
10+
restricted_ports: # ports that can't be exposed and bound to haproxy
11+
- 2377 # docker swarm port
12+
- 7946 # docker swarm port
13+
- 4789 # docker swarm port
14+
- 3333 # swiftwave port
15+
haproxy:
16+
service_name: haproxy
17+
image: ghcr.io/swiftwave-org/haproxy:2.9
18+
unix_socket_path: ~/swiftwave/dataplaneapi.sock
19+
user: admin
20+
password: admin
21+
postgresql:
22+
host: localhost
23+
port: 5432
24+
user: postgres
25+
password: postgres
26+
database: swiftwave
27+
time_zone: Asia/Kolkata
28+
lets_encrypt:
29+
staging_environment: false # true or false
30+
31+
account_private_key_path: ~/swiftwave/account.key
32+
pubsub:
33+
mode: local # local or remote
34+
buffer_length: 1000
35+
redis: # all the info should be filled if mode is remote
36+
host: localhost
37+
port: 6379
38+
password: ""
39+
database_id: 0
40+
task_queue:
41+
mode: local # local or remote
42+
max_outstanding_messages_per_queue: 1000
43+
amqp: # all the info should be filled if mode is remote
44+
protocol: amqp # amqp or amqps
45+
host: localhost
46+
user: guest
47+
password: guest
48+
vhost:
49+
client_name: swiftwave

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ require (
5252
github.com/containerd/containerd v1.7.2 // indirect
5353
github.com/emirpasic/gods v1.18.1 // indirect
5454
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
55-
github.com/joho/godotenv v1.5.1
5655
github.com/kevinburke/ssh_config v1.2.0 // indirect
5756
github.com/klauspost/compress v1.16.7 // indirect
5857
github.com/moby/patternmatcher v0.5.0 // indirect

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
9999
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
100100
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
101101
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
102-
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
103-
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
104102
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
105103
github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
106104
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=

haproxy_manager/constructor.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,9 @@ package haproxymanager
22

33
// Init HaProxy Manager with a unix socket
44
func (s *Manager) InitUnixSocket(unixSocketPath string) {
5-
s.isUnix = true
65
s.unixSocketPath = unixSocketPath
76
}
87

9-
// Init HaProxy Manager with tcp socket info (host, port)
10-
func (s *Manager) InitTcpSocket(host string, port int) {
11-
s.isUnix = false
12-
s.Host = host
13-
s.Port = port
14-
}
15-
168
// Update auth credentials for HaProxy Manager
179
func (s *Manager) Auth(username string, password string) {
1810
s.username = username

haproxy_manager/request.go

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@ package haproxymanager
22

33
import (
44
"bytes"
5+
"context"
56
"errors"
67
"io"
78
"mime/multipart"
9+
"net"
810
"net/http"
911
"path/filepath"
10-
"strconv"
1112
"strings"
1213
)
1314

1415
// Generate Base URI for HAProxy Server
1516
func (s Manager) URI() string {
16-
if s.isUnix {
17-
return "unix://" + s.unixSocketPath
18-
}
19-
return "http://" + s.Host + ":" + strconv.Itoa(s.Port) + "/v2"
17+
return "http://unix/v2"
2018
}
2119

2220
// Wrapper to send request to HAProxy Server
@@ -30,7 +28,13 @@ func (s Manager) getRequest(route string, queryParams QueryParameters) (*http.Re
3028
return nil, err
3129
}
3230
req.SetBasicAuth(s.username, s.password)
33-
client := &http.Client{}
31+
client := &http.Client{
32+
Transport: &http.Transport{
33+
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
34+
return net.Dial("unix", s.unixSocketPath)
35+
},
36+
},
37+
}
3438
return client.Do(req)
3539
}
3640

@@ -45,7 +49,13 @@ func (s Manager) deleteRequest(route string, queryParams QueryParameters) (*http
4549
return nil, err
4650
}
4751
req.SetBasicAuth(s.username, s.password)
48-
client := &http.Client{}
52+
client := &http.Client{
53+
Transport: &http.Transport{
54+
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
55+
return net.Dial("unix", s.unixSocketPath)
56+
},
57+
},
58+
}
4959
return client.Do(req)
5060
}
5161

@@ -61,7 +71,13 @@ func (s Manager) postRequest(route string, queryParams QueryParameters, body io.
6171
}
6272
req.SetBasicAuth(s.username, s.password)
6373
req.Header.Add("Content-Type", "application/json")
64-
client := &http.Client{}
74+
client := &http.Client{
75+
Transport: &http.Transport{
76+
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
77+
return net.Dial("unix", s.unixSocketPath)
78+
},
79+
},
80+
}
6581
return client.Do(req)
6682
}
6783

@@ -77,7 +93,13 @@ func (s Manager) putRequest(route string, queryParams QueryParameters, body io.R
7793
}
7894
req.SetBasicAuth(s.username, s.password)
7995
req.Header.Add("Content-Type", "application/json")
80-
client := &http.Client{}
96+
client := &http.Client{
97+
Transport: &http.Transport{
98+
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
99+
return net.Dial("unix", s.unixSocketPath)
100+
},
101+
},
102+
}
81103
return client.Do(req)
82104
}
83105

@@ -113,7 +135,13 @@ func (s Manager) uploadSSL(route string, domain string, file io.Reader) (*http.R
113135
}
114136
req.SetBasicAuth(s.username, s.password)
115137
req.Header.Add("Content-Type", writer.FormDataContentType())
116-
client := &http.Client{}
138+
client := &http.Client{
139+
Transport: &http.Transport{
140+
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
141+
return net.Dial("unix", s.unixSocketPath)
142+
},
143+
},
144+
}
117145
return client.Do(req)
118146
}
119147

@@ -138,6 +166,12 @@ func (s Manager) replaceSSL(route string, domain string, file io.Reader) (*http.
138166
}
139167
req.SetBasicAuth(s.username, s.password)
140168
req.Header.Add("Content-Type", "text/plain")
141-
client := &http.Client{}
169+
client := &http.Client{
170+
Transport: &http.Transport{
171+
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
172+
return net.Dial("unix", s.unixSocketPath)
173+
},
174+
},
175+
}
142176
return client.Do(req)
143177
}

haproxy_manager/types.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package haproxymanager
22

33
type Manager struct {
4-
Host string
5-
Port int
6-
isUnix bool // If true, Host is a path to a unix socket
74
unixSocketPath string
85
username string
96
password string

main.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
11
package main
22

33
import (
4-
"github.com/joho/godotenv"
54
"github.com/labstack/echo/v4"
65
"github.com/labstack/echo/v4/middleware"
76
swiftwave "github.com/swiftwave-org/swiftwave/swiftwave_service"
87
"github.com/swiftwave-org/swiftwave/swiftwave_service/core"
98
"github.com/swiftwave-org/swiftwave/swiftwave_service/cronjob"
109
"github.com/swiftwave-org/swiftwave/swiftwave_service/worker"
10+
"github.com/swiftwave-org/swiftwave/system_config"
1111
"log"
12+
"os"
1213
)
1314

1415
func main() {
15-
err := godotenv.Load()
16+
// Load config path from environment variable
17+
systemConfigPath := os.Getenv("SWIFTWAVE_CONFIG_PATH")
18+
if systemConfigPath == "" {
19+
systemConfigPath = "~/swiftwave/config.yaml"
20+
log.Println("SWIFTWAVE_CONFIG_PATH environment variable not set, using default path > ", systemConfigPath)
21+
}
22+
// Load the config
23+
config, err := system_config.ReadFromFile(systemConfigPath)
1624
if err != nil {
17-
log.Println("WARN: error loading .env file. Ignoring")
25+
panic(err)
1826
}
27+
1928
// Load the manager
20-
config := core.ServiceConfig{}
2129
manager := core.ServiceManager{}
22-
config.Load()
23-
manager.Load()
30+
manager.Load(*config)
2431

2532
// Create the worker manager
26-
workerManager := worker.NewManager(&config, &manager)
33+
workerManager := worker.NewManager(config, &manager)
2734
err = workerManager.StartConsumers(true)
2835
if err != nil {
2936
panic(err)
3037
}
3138

3239
// Create the cronjob manager
33-
cronjobManager := cronjob.NewManager(&config, &manager)
40+
cronjobManager := cronjob.NewManager(config, &manager)
3441
cronjobManager.Start(true)
3542

3643
// create a channel to block the main thread
@@ -43,7 +50,7 @@ func main() {
4350
echoServer.Use(middleware.CORS())
4451

4552
// Start the swift wave server
46-
go swiftwave.StartServer(&config, &manager, echoServer, workerManager, true)
53+
go swiftwave.StartServer(config, &manager, echoServer, workerManager, true)
4754
// Wait for consumers
4855
go workerManager.WaitForConsumers()
4956
// Wait for cronjobs

swiftwave_service/core/create_db_client.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ package core
33
import (
44
"gorm.io/driver/postgres"
55
"gorm.io/gorm"
6-
"os"
76
)
87

9-
func createDbClient() (*gorm.DB, error) {
8+
func createDbClient(dsn string) (*gorm.DB, error) {
109
// Initiating database client
11-
dbDialect := postgres.Open(os.Getenv("POSTGRESQL_DSN"))
10+
dbDialect := postgres.Open(dsn)
1211
client, err := gorm.Open(dbDialect, &gorm.Config{})
1312
if err != nil {
1413
return nil, err

swiftwave_service/core/load_service_config.go

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)