Skip to content

Commit

Permalink
wip: init a whole api gw project
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerwelin committed Feb 10, 2020
1 parent bda5eec commit 94071cd
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 24 deletions.
44 changes: 39 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,44 @@ func contains(slice []string, contains string) bool {
return false
}

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

if path != "" {
file, err = os.Create(path + "/" + outName)
if err != nil {
return err
}
} else {
file, err = os.Create(outName)
if err != nil {
return err
}
}
err = t.Execute(file, tmpl)
if err != nil {
return err
}
return nil
}

func main() {

apiTmpl := TmplData{
apiTmpl := &TmplData{
ApiProtocol: "rest",
ApiEndpoints: "regional",
LambdaFunctionName: "helloworld",
ApiProjectName: "Hello-World-API",
}

t := template.Must(template.New("apigw").Parse(apiGWConf))
file, err := os.Create("apigw.yml")
if err != err {
err := apiTmpl.createFileFromTemplate(apiGWConf, "", "apigw.yml")
if err != nil {
panic(err)
}
err = t.Execute(file, apiTmpl)

err = apiTmpl.createFileFromTemplate(swagger, "", "swagger-api.yml")
if err != nil {
panic(err)
}
Expand All @@ -55,4 +78,15 @@ func main() {
if err != nil {
panic(err)
}

err = apiTmpl.createFileFromTemplate(nodeFunction, "src/helloworld/app", "index.js")
if err != nil {
panic(err)
}

err = apiTmpl.createFileFromTemplate(packageJson, "src/helloworld/app", "package.json")
if err != nil {
panic(err)
}

}
48 changes: 48 additions & 0 deletions tmpl-nodejs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

var nodeFunction = `
'use strict'
const winston = require('winston')
const logger = winston.createLogger({
level: 'info',
format: winston.format.combine(
winston.format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
),
});
exports.handler = function(event, context) {
logger.log({
level: 'info',
message: event,
});
let msg = {
msg: 'hello world'
}
context.succeed(JSON.stringify(msg));
};
`

var packageJson = `
{
"name": "hello",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"winston": "^3.2.1"
},
"devDependencies": {
"lambda-local": "^1.6.3"
}
}
`
27 changes: 8 additions & 19 deletions tmpl-swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
const swagger = `
---
openapi: "3.0.1"
{{ if and (eq .apiEndpoints "private") }}
{{ if and (eq .ApiEndpoints "private") }}
x-amazon-apigateway-policy:
Version: '2012-10-17'
Statement:
Expand All @@ -20,7 +20,7 @@ x-amazon-apigateway-policy:
{{ end }}
info:
title: {{ .apiProjectName }}
title: {{ .ApiProjectName }}
description: your awesome description here
version: "v1.0"
Expand All @@ -31,7 +31,7 @@ servers:
description: Production environment URL
paths:
/v1/{{ .lambdaFunctionName }}/{userId}:
/v1/{{ .LambdaFunctionName }}/{userId}:
get:
summary: hello world endpoint
description: outputs hello world
Expand All @@ -47,9 +47,9 @@ paths:
content:
application/json:
schema:
type: array
type: object
items:
$ref: "#/components/schemas/ArticleObj"
$ref: "#/components/schemas/HelloWorldObj"
500:
description: "Internal Server Error"
content: {}
Expand All @@ -61,19 +61,8 @@ paths:
type: aws_proxy
components:
schemas:
ArticleObj:
HelloWorldObj:
properties:
rowValues:
type: "array"
items:
type: "object"
properties:
modelId:
type: "number"
Article_Id:
type: "string"
MU_UOM_Cd:
type: "string"
rank:
type: "number"
msg:
type: "string"
`

0 comments on commit 94071cd

Please sign in to comment.