-
Notifications
You must be signed in to change notification settings - Fork 0
/
new-api.go
185 lines (160 loc) · 4.81 KB
/
new-api.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"fmt"
"os"
"text/template"
"time"
"github.com/fatih/color"
)
var (
defaultAppPath = "/src/helloworld/app"
)
type tmplData struct {
APIProtocol string
APIEndpoints string
LambdaFunctionName string
APIProjectName string
Language string
}
type languageMapper struct {
AppFile string
DepsFile string
TmplAppVar string
TmplDepsVar string
AppPath string
DepsPath string
}
func initMap(projectName string) map[string]languageMapper {
appPath := projectName + defaultAppPath
var languages = map[string]languageMapper{
"node": {
AppFile: "index.js",
DepsFile: "package.json",
TmplAppVar: nodeFunction,
TmplDepsVar: packageJSON,
AppPath: appPath,
DepsPath: appPath,
},
"python": {
AppFile: "app.py",
DepsFile: "requirements.txt",
TmplAppVar: pythonFunction,
TmplDepsVar: requirementsFile,
AppPath: appPath,
DepsPath: appPath,
},
"ruby": {
AppFile: "app.rb",
DepsFile: "Gemfile",
TmplAppVar: rubyFunction,
TmplDepsVar: gemFile,
AppPath: appPath,
DepsPath: appPath,
},
"go": {
AppFile: "main.go",
DepsFile: "go.mod",
TmplAppVar: goFunction,
TmplDepsVar: goMod,
AppPath: appPath,
DepsPath: appPath,
},
}
return languages
}
func createDir(path string) error {
err := os.MkdirAll(path, os.ModePerm)
if err != nil {
return err
}
return nil
}
func contains(slice []string, contains string) bool {
for _, item := range slice {
if contains == item {
return true
}
}
return false
}
func createFileFromStruct(languageSpec languageMapper) error {
// not pretty, fix later
apa := &tmplData{}
err := createDir(languageSpec.AppPath)
if err != nil {
return err
}
err = apa.createFileFromTemplate(languageSpec.TmplAppVar, languageSpec.AppPath, languageSpec.AppFile)
if err != nil {
return err
}
err = apa.createFileFromTemplate(languageSpec.TmplDepsVar, languageSpec.DepsPath, languageSpec.DepsFile)
if err != nil {
return err
}
return nil
}
func (tmpl *tmplData) createFileFromTemplate(tmplVar string, path string, outName string) error {
t := template.Must(template.New("").Parse(tmplVar))
var file *os.File
var err error
file, err = os.Create(path + "/" + outName)
if err != nil {
return err
}
err = t.Execute(file, tmpl)
if err != nil {
return err
}
return nil
}
func (tmpl *tmplData) bootstrapAPI() error {
col := color.New(color.FgCyan).Add(color.Underline)
mg := color.New(color.FgGreen)
fmt.Printf("\n\U0001f3c1 ")
col.Printf("Bootstrapping API GW project: %s with Lambda\n\n", tmpl.APIProjectName)
time.Sleep(350 * time.Millisecond)
// create top dir
createDir(tmpl.APIProjectName)
// create readme
mg.Printf("\u2705 Writing out README\n")
time.Sleep(350 * time.Millisecond)
err := tmpl.createFileFromTemplate(reamdeFile, tmpl.APIProjectName, "README.md")
if err != nil {
panic(err)
}
// create apigw sam/cf
mg.Printf("\u2705 Writing out CF/SAM config\n")
time.Sleep(350 * time.Millisecond)
err = tmpl.createFileFromTemplate(apiGWConf, tmpl.APIProjectName, "apigw.yml")
if err != nil {
panic(err)
}
// create swagger file
mg.Printf("\u2705 Writing out Swagger config\n")
time.Sleep(350 * time.Millisecond)
err = tmpl.createFileFromTemplate(swagger, tmpl.APIProjectName, "swagger-api.yml")
if err != nil {
return err
}
languageMap := initMap(tmpl.APIProjectName)
mg.Printf("\u2705 Writing out %s lambda to: %s\n\n", tmpl.Language, tmpl.APIProjectName+defaultAppPath)
err = createFileFromStruct(languageMap[tmpl.Language])
if err != nil {
return err
}
green := color.New(color.FgGreen).SprintFunc()
consoleStdOut := "Success! Created API GW project at %s\nInside that directory," +
"you can run several commands:\n\n\t%s\n\t\t" +
"installs dependencies for all the Lambda functions\n\t%s\n\t\t" +
"run your api locally during development\n\t%s\n\t\t" +
"creates all needed AWS resources and deploys the code" +
"\n\n" +
"However I recommend taking a look at the README file first\n\n"
//fmt.Printf("Success! Created API GW project at %s\nInside that directory, you can run several commands:\n\n\t%s\n\t\tcreates a zip of your code and dependencies and uploads it to S3\n\t%s\n\t\tdeploys the specified CloudFormation/SAM template by creating and then executing a change set\n\nHowever I recommend taking a look at the README file first\n\n", green(tmpl.APIProjectName+"/"), green("sam package --template-file apigw.yml --output-template-file out.yaml --s3-bucket Your-S3-bucket"), green("aws cloudformation deploy --template-file ./out.yaml --stack-name my-api-stack --capabilities CAPABILITY_IAM"))
fmt.Printf(consoleStdOut, green(tmpl.APIProjectName+"/"),
green("sam build --template-file apigw.yml"),
green("sam local start-api"),
green("sam deploy --guided"))
return nil
}