wisski-cloud-distillery/internal/dis/component/web/web.go
Tom 17d64826df StackWithResources: Update ComposeYML behaviour
Previously, there was a function to manually read bytes for a
docker-compose.yml. But this proved to be akward at runtime.

Instead, this code automatically reads an existing docker-compose.yml,
and takes care of marshalling and unmarshalling.
2023-07-14 15:23:21 +02:00

65 lines
1.4 KiB
Go

package web
import (
"path/filepath"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
"gopkg.in/yaml.v3"
_ "embed"
)
// Web implements the ingress gateway for the distillery.
//
// It consists of an nginx docker container and an optional letsencrypt container.
type Web struct {
component.Base
}
var (
_ component.Installable = (*Web)(nil)
)
func (web *Web) Path() string {
return filepath.Join(web.Still.Config.Paths.Root, "core", "web")
}
func (*Web) Context(parent component.InstallationContext) component.InstallationContext {
return parent
}
//go:embed docker-compose-http.yml
var dockerComposeHTTP []byte
//go:embed docker-compose-https.yml
var dockerComposeHTTPS []byte
func (web *Web) Stack() component.StackWithResources {
var stack component.StackWithResources
stack.EnvContext = map[string]string{
"DOCKER_NETWORK_NAME": web.Config.Docker.Network(),
"CERT_EMAIL": web.Config.HTTP.CertbotEmail,
}
if web.Config.HTTP.HTTPSEnabled() {
stack.ComposerYML = readYaml(dockerComposeHTTPS)
stack.TouchFilesPerm = 0600
stack.TouchFiles = []string{"acme.json"}
} else {
stack.ComposerYML = readYaml(dockerComposeHTTP)
}
return component.MakeStack(web, stack)
}
func readYaml(bytes []byte) func(*yaml.Node) (*yaml.Node, error) {
return func(n *yaml.Node) (*yaml.Node, error) {
var node yaml.Node
err := yaml.Unmarshal(bytes, &node)
if err != nil {
return nil, err
}
return &node, nil
}
}