Skip to content

Commit 47c213d

Browse files
authored
feat: autocert integrated (#132)
1 parent bd52914 commit 47c213d

File tree

6 files changed

+52
-20
lines changed

6 files changed

+52
-20
lines changed

config.example.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ version: 1.0
22
mode: standalone # standalone or cluster
33
service:
44
auto_tls: true # true or false
5+
tls_cache_dir: /var/www/.cache
6+
whitelisted_domains: # domains that can be used to access the service and generate certificates
7+
- *
58
bind_address: 0.0.0.0
69
bind_port: 3333 # choose any other ports except 80 and 443
710
network_name: swiftwave_network # docker swarm overflow network name

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ require (
8585
go.uber.org/atomic v1.11.0 // indirect
8686
go.uber.org/multierr v1.11.0 // indirect
8787
go.uber.org/zap v1.24.0 // indirect
88-
golang.org/x/crypto v0.14.0 // indirect
88+
golang.org/x/crypto v0.14.0
8989
golang.org/x/mod v0.12.0 // indirect
9090
golang.org/x/net v0.17.0 // indirect
9191
golang.org/x/sys v0.13.0 // indirect

main.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package main
22

33
import (
4-
"github.com/labstack/echo/v4"
5-
"github.com/labstack/echo/v4/middleware"
64
swiftwave "github.com/swiftwave-org/swiftwave/swiftwave_service"
75
"github.com/swiftwave-org/swiftwave/swiftwave_service/core"
86
"github.com/swiftwave-org/swiftwave/swiftwave_service/cronjob"
@@ -43,14 +41,8 @@ func main() {
4341
// create a channel to block the main thread
4442
var waitForever chan struct{}
4543

46-
// Create Echo Server
47-
echoServer := echo.New()
48-
echoServer.Pre(middleware.RemoveTrailingSlash())
49-
echoServer.Use(middleware.Recover())
50-
echoServer.Use(middleware.CORS())
51-
5244
// Start the swift wave server
53-
go swiftwave.StartServer(config, &manager, echoServer, workerManager, true)
45+
go swiftwave.StartServer(config, &manager, workerManager, true)
5446
// Wait for consumers
5547
go workerManager.WaitForConsumers()
5648
// Wait for cronjobs

swiftwave_service/start_server.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,30 @@ package swiftwave
33
import (
44
"fmt"
55
"github.com/labstack/echo/v4"
6+
"github.com/labstack/echo/v4/middleware"
67
"github.com/swiftwave-org/swiftwave/swiftwave_service/core"
78
"github.com/swiftwave-org/swiftwave/swiftwave_service/graphql"
89
"github.com/swiftwave-org/swiftwave/swiftwave_service/rest"
910
"github.com/swiftwave-org/swiftwave/swiftwave_service/worker"
1011
"github.com/swiftwave-org/swiftwave/system_config"
12+
"golang.org/x/crypto/acme/autocert"
1113
)
1214

13-
func StartServer(config *system_config.Config, manager *core.ServiceManager, echoServer *echo.Echo, workerManager *worker.Manager, migrateDatabase bool) {
15+
func StartServer(config *system_config.Config, manager *core.ServiceManager, workerManager *worker.Manager, migrateDatabase bool) {
16+
// Create Echo Server
17+
echoServer := echo.New()
18+
echoServer.Pre(middleware.RemoveTrailingSlash())
19+
echoServer.Use(middleware.Recover())
20+
echoServer.Use(middleware.Logger())
21+
echoServer.Use(middleware.CORS())
22+
// enable host whitelist if not all domains are allowed
23+
if !config.ServiceConfig.IsAllDomainsAllowed() {
24+
echoServer.AutoTLSManager.HostPolicy = autocert.HostWhitelist(config.ServiceConfig.WhiteListedDomains...)
25+
}
26+
// Configure Auto TLS
27+
if config.ServiceConfig.AutoTLS {
28+
echoServer.AutoTLSManager.HostPolicy = autocert.HostWhitelist(config.ServiceConfig.NetworkName)
29+
}
1430
// Create Rest Server
1531
restServer := rest.Server{
1632
EchoServer: echoServer,
@@ -35,5 +51,9 @@ func StartServer(config *system_config.Config, manager *core.ServiceManager, ech
3551
}
3652
// Start the server
3753
address := fmt.Sprintf("%s:%d", config.ServiceConfig.BindAddress, config.ServiceConfig.BindPort)
38-
echoServer.Logger.Fatal(echoServer.Start(address))
54+
if config.ServiceConfig.AutoTLS {
55+
echoServer.Logger.Fatal(echoServer.StartAutoTLS(address))
56+
} else {
57+
echoServer.Logger.Fatal(echoServer.Start(address))
58+
}
3959
}

system_config/types.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ type Config struct {
1212
}
1313

1414
type ServiceConfig struct {
15-
AutoTLS bool `yaml:"auto_tls"`
16-
BindAddress string `yaml:"bind_address"`
17-
BindPort int `yaml:"bind_port"`
18-
NetworkName string `yaml:"network_name"`
19-
DataDir string `yaml:"data_dir"`
20-
DockerUnixSocketPath string `yaml:"docker_unix_socket_path"`
21-
RestrictedPorts []int `yaml:"restricted_ports"`
15+
AutoTLS bool `yaml:"auto_tls"`
16+
TLSCacheDir string `yaml:"tls_cache_dir"`
17+
WhiteListedDomains []string `yaml:"whitelisted_domains"`
18+
BindAddress string `yaml:"bind_address"`
19+
BindPort int `yaml:"bind_port"`
20+
NetworkName string `yaml:"network_name"`
21+
DataDir string `yaml:"data_dir"`
22+
DockerUnixSocketPath string `yaml:"docker_unix_socket_path"`
23+
RestrictedPorts []int `yaml:"restricted_ports"`
2224
}
2325

2426
type PostgresqlConfig struct {

system_config/utils.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package system_config
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
"strings"
6+
)
47

58
func (p PostgresqlConfig) DSN() string {
69
return fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s TimeZone=%s sslmode=disable", p.Host, p.Port, p.User, p.Password, p.Database, p.TimeZone)
@@ -9,3 +12,15 @@ func (p PostgresqlConfig) DSN() string {
912
func (a AMQPConfig) URI() string {
1013
return fmt.Sprintf("%s://%s:%s@%s:%d", a.Protocol, a.User, a.Password, a.Host)
1114
}
15+
16+
func (c ServiceConfig) IsAllDomainsAllowed() bool {
17+
if len(c.WhiteListedDomains) == 0 {
18+
return true
19+
}
20+
for _, domain := range c.WhiteListedDomains {
21+
if strings.Trim(domain, " ") == "*" {
22+
return true
23+
}
24+
}
25+
return false
26+
}

0 commit comments

Comments
 (0)