What do servers do in swagger-routes-express? - node.js

I'm learning how to use swagger/openapi, and starting to use swagger-routes-express package to simplify the association of routes to OpenAPI definition. One of the things I'm confused about is the servers config. For example, here's an excerpt from the swagger-routes-express example for OpenAPI Version 3 example:
paths:
/ping:
get:
tags:
- root
summary: Get Server Information
description: Returns information about the server
operationId: ping
responses:
'200':
description: success
content:
application/json:
schema:
$ref: '#/components/schemas/ServerInfo'
servers:
- url: /api/v1
Here we've got a get path of /ping. It seems like this gets added onto the end of the server's url:
/api/v1/ping
I understand that multiple servers can be listed under the servers section, and you might want to do that for development, testing, and production. Based on my testing, it seems like the route path that's created is based on the first server entry. Is that correct? How will this work when multiple servers are defined?

Related

Change Swagger UI `server` dynamically at run time based on the url it is running on

I am using a swagger interface to test my API endpoints. I Have this running on two different servers
A production server https://example.co.app
A staging server https://example-staging.co.app
I managed to make a dropdown list to select the server you want to use like so in a via options in a config.js file:
servers: [
{
url: 'https://example.co.app',
description: 'prod server'
},
{
url: 'https://example-staging.co.app',
description: 'staging server'
}
]
This works but will always use prod server by default since it is the first one in the list.
I would like to make the default dynamic at run time based on what server It is actually running in.
Is this possible with swagger configurations?

GAE dispatch.yaml not properly routing to services

I'm having some trouble getting my dispatch.yaml to work, but I also haven't found any answers that address my problem. Most seem to be issues of putting the default service first in the dispatch, but I don't list mine at all.
dispatch:
- url: "*/timestamps/*"
service: timestamps
- url: "*/reqheaders/*"
service: reqheaders
I have my custom domain setting pointing to sub.example.com and for some reason, every route just points to the default service, so I get an error saying Cannot GET /timestamps or Cannot GET /timestamps/subpath

Add and Update DNS Records in Route 53 to Cloudfront with Serverless Framework

I'm currently working on expanding this repo. It contains source code to deploy a sapper/svelte webapp with the Serverless Framework to AWS. I'm thinking about how to expand the serverless.yml to include Route53 DNS routing to Cloudfront, but I can't seem to wrap my head around it. The serverless-domain-manager plugin seems to be designed for Route53 to API Gateway routing, but I'm deploying my app only with Cloudfront, an S3 bucket and an Lambda#Edge handler server-side-rendering the application.
I've done something similar here.
I've trimmed the serverless.yml for the relevant parts:
WebAppCloudFrontDistribution:
Type: AWS::CloudFront::Distribution
DependsOn:
- WebAppS3Bucket
Properties:
DistributionConfig:
### content trimmed
Aliases:
- 'www.example.com'
### content trimmed
ViewerCertificate:
AcmCertificateArn: 'CertificateArn' # in the linked repo I'm using a plugin to auto generate the certificate
SslSupportMethod: sni-only
WebsiteDNSName:
Type: AWS::Route53::RecordSetGroup
Properties:
HostedZoneId: 'HostedZoneId' # Taken from the AWS Console
RecordSets:
- Name: 'www.example.com'
Type: A
AliasTarget:
HostedZoneId: Z2FDTNDATAQYW2
DNSName: !GetAtt [WebAppCloudFrontDistribution, DomainName]
EvaluateTargetHealth: false

Structure of a serverless application

I am new to serverless application. I followed the aws tutorial to build a simple nodejs serverless app with codestar and lambda.
However, imagine this node app does multiple things. In consequence, it has mutiple functions inside index.js, one for functionnality A, one for functionnality B, etc (for example).
Do I have to attach multiple lambda expressions, one for each functionality, to this codestar project?
Question: Do I have to attach multiple lambda expressions, one for each functionality, to this codestar project?
Answer: Yes
AWS CodeStar Project Details:
AWS Code star project contains below file structure(reference link):
README.md - this file
buildspec.yml - this file is used by AWS CodeBuild to package your service for deployment to AWS Lambda
app.js - this file contains the sample Node.js code for the web service
index.js - this file contains the AWS Lambda handler code
template.yml - this file contains the Serverless Application Model (SAM) used by AWS Cloudformation to deploy your service to AWS Lambda and Amazon API Gateway.
Assume you have the template.yml file like below:
AWSTemplateFormatVersion: 2010-09-09
Transform:
- AWS::Serverless-2016-10-31
- AWS::CodeStar
Resources:
HelloWorld:
Type: AWS::Serverless::Function
Properties:
Handler: index.first_handler
Runtime: nodejs4.3
Role:
Fn::ImportValue:
!Join ['-', [!Ref 'ProjectId', !Ref 'AWS::Region', 'LambdaTrustRole']]
Events:
GetEvent:
Type: Api
Properties:
Path: /first
Method: get
HelloWorld2:
Type: AWS::Serverless::Function
Properties:
Handler: index.second_handler
Runtime: nodejs4.3
Role:
Fn::ImportValue:
!Join ['-', [!Ref 'ProjectId', !Ref 'AWS::Region', 'LambdaTrustRole']]
Events:
GetEvent:
Type: Api
Properties:
Path: /second
Method: get
Notice that, in above tamplate.yml file specified "HelloWorld" and "HelloWorld2" configurations.
HelloWorld configuration contains "Handler" value as "index.first_handler" meaning that "index" is the filename of index.js and first_handler is the method in index.js file.
Likewise, HelloWorld2 configuration contains "Handler" value as "index.second_handler" meaning that "index" is the filename of index.js and second_handler is the method in index.js file.
Conclusion:
You can specify any number of lambda functions in your index.js (whatever.js) file. Only you need to specify the proper Handler to identify the app your lambda function.
Hope this is the answer to your question. Feel free to ask doubts, if you have!
you don't need multiple handler functions (index.js) and you cannot have multiple handler functions. If the different functionality is doing the logically separated job then you can add multiple JS files and write functions there but you should refer that to your handler function (index.js). Alternatively, you can write functionality in index.js itself but better idea and clean code is to separate logically different functionality to another file and refer it

Events for custom resources?

I've created a new entity with ResourcesBundle but on the profiler I can't see the Events.
Must I create manualy?
Creating an EventListener for sylius.book.pre_create doesn't do nothing.
Adding Info
Hi anothertime lchrusciel.
This is my configuration for my resource:
dinamic_sylius_post:
resource: |
alias: dinamic.post
path: blog/post
type: sylius.resource
dinamic_sylius_admin_post:
resource: |
alias: dinamic.post
section: admin
templates: SyliusAdminBundle:Crud
except: ['show', 'delete']
redirect: index
grid: dinamic_sylius_blog_post
type: sylius.resource
prefix: admin/
And on my bundle config I have this:
sylius_resource:
resources:
dinamic.post:
classes:
model: Dinamic\Bundle\SyliusBlogBundle\Entity\Post
form:
default: Dinamic\Bundle\SyliusBlogBundle\Form\PostType
What I'm doing wrong then?
If it is your custom resource you should look for app.book.pre_create event.
As you can see here event name depends on application name which is sylius for predefined Sylius resources, but if you defined your own, it usually app.
If you have followed Sylius docs about using ResourceBundle with your own resources you have found following config:
sylius_resource:
resources:
app.book:
classes:
model: AppBundle\Entity\Book
So important part of this config is an alias of resource app.book. Dot split alias to application name(app) and resource name(book).
Same rules apply to crud generation config:
app_book:
resource: |
alias: app.book
type: sylius.resource_api
Using app as a application name is Sylius recommendation, but you can arbitrary choose any other.
Edit
In your example this is an important part:
sylius_resource:
resources:
dinamic.post:
classes:
According to it, dinamic is an application name, and post is a resource name. So the following events should be triggered:
dinamic.post.pre_create
dinamic.post.post_create
dinamic.post.pre_update
dinamic.post.post_update
dinamic.post.pre_delete
dinamic.post.post_delete

Resources