-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
158 lines (132 loc) · 3.87 KB
/
handler.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// Copyright (C) 2020 - 2024 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/fogfish/scud
//
package scud
import (
"fmt"
"path/filepath"
"strings"
"github.com/aws/aws-cdk-go/awscdk/v2"
"github.com/aws/aws-cdk-go/awscdk/v2/awslambda"
"github.com/aws/aws-cdk-go/awscdk/v2/awslogs"
"github.com/aws/constructs-go/constructs/v10"
"github.com/aws/jsii-runtime-go"
)
// FunctionGoProps is properties of the function
type FunctionGoProps struct {
*awslambda.FunctionProps
// Canonical name of Golang module that containing the function
// SourceCodeModule: "github.com/fogfish/scud",
SourceCodeModule string
// Path to lambda function relative to the module
// SourceCodeLambda: "test/lambda/go"
SourceCodeLambda string
// The version of software asset passed as linker flag
// -ldflags '-X main.version=...'
SourceCodeVersion string
// Variables and its values passed as linker flags
// -ldflags '-X key1=val1 -X key2=val2 ...'
GoVar map[string]string
// Go environment, default includes
// GOOS=linux
// GOARCH=arm64
// CGO_ENABLED=0
GoEnv map[string]string
}
func (*FunctionGoProps) Type(awslambda.Function) {}
func (props *FunctionGoProps) UniqueID() string {
return funcName(props.SourceCodeModule, props.SourceCodeLambda)
}
func (props *FunctionGoProps) Setenv(key, val string) {
if props.FunctionProps == nil {
props.FunctionProps = &awslambda.FunctionProps{}
}
if props.FunctionProps.Environment == nil {
props.FunctionProps.Environment = &map[string]*string{}
}
(*props.FunctionProps.Environment)[key] = jsii.String(val)
}
// NewFunctionGo creates Golang Lambda Function from "inline" code
func NewFunctionGo(scope constructs.Construct, id *string, spec *FunctionGoProps) awslambda.Function {
var props awslambda.FunctionProps
if spec.FunctionProps != nil {
props = *spec.FunctionProps
}
if props.Timeout == nil {
props.Timeout = awscdk.Duration_Minutes(jsii.Number(1))
}
if props.LogRetention == "" {
props.LogRetention = awslogs.RetentionDays_FIVE_DAYS
}
if props.FunctionName == nil {
props.FunctionName = jsii.String(fmt.Sprintf("%s-%s",
*awscdk.Aws_STACK_NAME(), funcName(spec.SourceCodeModule, spec.SourceCodeLambda)))
}
// arm64 is default deployment
props.Architecture = awslambda.Architecture_ARM_64()
if spec.GoEnv != nil {
switch spec.GoEnv["GOARCH"] {
case "amd64":
props.Architecture = awslambda.Architecture_X86_64()
case "arm64":
props.Architecture = awslambda.Architecture_ARM_64()
}
}
gocc := NewGoCompiler(
spec.SourceCodeModule,
spec.SourceCodeLambda,
spec.SourceCodeVersion,
spec.GoVar,
spec.GoEnv,
)
props.Code = AssetCodeGo(gocc)
props.Handler = jsii.String(goBinary)
props.Runtime = awslambda.Runtime_PROVIDED_AL2()
return awslambda.NewFunction(scope, id, &props)
}
func funcName(scModule, scLambda string) string {
return shorten(filepath.Join(scModule, scLambda))
}
func shorten(path string) string {
seq := strings.Split(path, string(filepath.Separator))
if len(seq) == 1 {
return path
}
for i := 0; i < len(seq)-1; i++ {
seq[i] = shortenSegment(seq[i])
}
return strings.Join(seq, "")
}
// shortenSegment heuristically shortens a path segment
func shortenSegment(segment string) string {
length := len(segment)
switch {
case length <= 3:
return segment // Keep short names unchanged
case length <= 6:
return removeVowels(segment) // Remove vowels from medium-length names
default:
short := removeVowels(segment)
if len(short) > 4 {
return short[:4]
}
return short
}
}
func removeVowels(segment string) string {
vowels := "aeiouAEIOU"
keep := []rune{}
for i, r := range segment {
if i == 0 || !strings.ContainsRune(vowels, r) {
keep = append(keep, r)
}
}
if len(keep) < 2 { // Ensure at least 2 characters remain
return segment
}
return string(keep)
}