/
static_site.go
113 lines (96 loc) · 3.07 KB
/
static_site.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package manifest
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/copilot-cli/internal/pkg/manifest/manifestinfo"
"github.com/aws/copilot-cli/internal/pkg/template"
"github.com/imdario/mergo"
)
const (
staticSiteManifestPath = "workloads/services/static-site/manifest.yml"
)
// StaticSite holds the configuration to configure and upload static assets to the static site service.
type StaticSite struct {
Workload `yaml:",inline"`
StaticSiteConfig `yaml:",inline"`
// Use *StaticSiteConfig because of https://github.com/imdario/mergo/issues/146
Environments map[string]*StaticSiteConfig `yaml:",flow"` // Fields to override per environment.
parser template.Parser
}
// StaticSiteConfig holds the configuration for a static site service.
type StaticSiteConfig struct {
HTTP StaticSiteHTTP `yaml:"http"`
FileUploads []FileUpload `yaml:"files"`
}
// StaticSiteHTTP defines the http configuration for the static site.
type StaticSiteHTTP struct {
Alias string `yaml:"alias"`
Certificate string `yaml:"certificate"`
}
// FileUpload represents the options for file uploading.
type FileUpload struct {
Source string `yaml:"source"`
Destination string `yaml:"destination"`
Recursive bool `yaml:"recursive"`
Exclude StringSliceOrString `yaml:"exclude"`
Reinclude StringSliceOrString `yaml:"reinclude"`
}
// StaticSiteProps represents the configuration needed to create a static site service.
type StaticSiteProps struct {
Name string
StaticSiteConfig
}
// NewStaticSite creates a new static site service with props.
func NewStaticSite(props StaticSiteProps) *StaticSite {
svc := newDefaultStaticSite()
// Apply overrides.
svc.Name = stringP(props.Name)
svc.FileUploads = props.StaticSiteConfig.FileUploads
svc.parser = template.New()
return svc
}
func newDefaultStaticSite() *StaticSite {
return &StaticSite{
Workload: Workload{
Type: aws.String(manifestinfo.StaticSiteType),
},
}
}
func (s StaticSite) applyEnv(envName string) (workloadManifest, error) {
overrideConfig, ok := s.Environments[envName]
if !ok {
return &s, nil
}
if overrideConfig == nil {
return &s, nil
}
// Apply overrides to the original service s.
for _, t := range defaultTransformers {
err := mergo.Merge(&s, StaticSite{
StaticSiteConfig: *overrideConfig,
}, mergo.WithOverride, mergo.WithTransformers(t))
if err != nil {
return nil, err
}
}
s.Environments = nil
return &s, nil
}
// MarshalBinary serializes the manifest object into a binary YAML document.
// Implements the encoding.BinaryMarshaler interface.
func (s *StaticSite) MarshalBinary() ([]byte, error) {
content, err := s.parser.Parse(staticSiteManifestPath, *s)
if err != nil {
return nil, err
}
return content.Bytes(), nil
}
// To implement workloadManifest.
func (s *StaticSite) subnets() *SubnetListOrArgs {
return nil
}
// To implement workloadManifest.
func (s *StaticSite) requiredEnvironmentFeatures() []string {
return nil
}