Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a basic web application tutorial using smithy #101

Merged
merged 26 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ bin/

# Ignore Gradle build output directory
build

5 changes: 5 additions & 0 deletions smithy-templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@
".gitignore",
".gitattributes"
]
},
"full-stack-application": {
"documentation": "Tutorial project that demonstrates how to use the Smithy to create a full-stack application.",
"path": "tutorials/full-stack-application",
"include": [".gitignore",".gitattributes"]
}
}
}
8 changes: 8 additions & 0 deletions tutorials/full-stack-application/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Ignore node_modules, next cache
node_modules
.next
**/dist

# Ignore generated code
client/sdk
server/ssdk
276 changes: 276 additions & 0 deletions tutorials/full-stack-application/.patches/start.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
diff --git a/app/app/index.ts b/app/app/index.ts
index 4fde2d6..0805132 100644
--- a/app/app/index.ts
+++ b/app/app/index.ts
@@ -17,8 +17,6 @@ export function getImage(type: CoffeeType | string): string {
return "/latte.png"
case CoffeeType.ESPRESSO:
return "/espresso.png"
- case CoffeeType.COLD_BREW:
- return "/cold-brew.png"
default:
return "/pour-over.png"
}
diff --git a/server/src/CoffeeShop.ts b/server/src/CoffeeShop.ts
index a8419ef..9011890 100644
--- a/server/src/CoffeeShop.ts
+++ b/server/src/CoffeeShop.ts
@@ -14,76 +14,20 @@ export class CoffeeShop implements CoffeeShopService<CoffeeShopContext> {

async CreateOrder(input: CreateOrderServerInput, context: CoffeeShopContext): Promise<CreateOrderServerOutput> {
console.log("received an order request...")
- const order = {
- orderId: randomUUID(),
- coffeeType: input.coffeeType,
- status: OrderStatus.IN_PROGRESS
- }
-
- context.orders.set(order.orderId, order)
- context.queue.push(order)
-
- console.log(`created order: ${JSON.stringify(order)}`)
- return {
- id: order.orderId,
- coffeeType: order.coffeeType,
- status: order.status
- }
+ // TODO: Implement me!
+ return;
}

async GetMenu(input: GetMenuServerInput, context: CoffeeShopContext): Promise<GetMenuServerOutput> {
console.log("getting menu...")
- return {
- items: [
- {
- type: CoffeeType.DRIP,
- description: "A clean-bodied, rounder, and more simplistic flavour profile.\n" +
- "Often praised for mellow and less intense notes.\n" +
- "Far less concentrated than espresso."
- },
- {
- type: CoffeeType.POUR_OVER,
- description: "Similar to drip coffee, but with a process that brings out more subtle nuances in flavor.\n" +
- "More concentrated than drip, but less than espresso."
- },
- {
- type: CoffeeType.LATTE,
- description: "A creamier, milk-based drink made with espresso.\n" +
- "A subtle coffee taste, with smooth texture.\n" +
- "High milk-to-coffee ratio."
- },
- {
- type: CoffeeType.ESPRESSO,
- description: "A highly concentrated form of coffee, brewed under high pressure.\n" +
- "Syrupy, thick liquid in a small serving size.\n" +
- "Full bodied and intensely aromatic."
- },
- {
- type: CoffeeType.COLD_BREW,
- description: "A high-extraction and chilled form of coffee that has been cold-pressed..\n" +
- "Different flavor profile than other hot methods of brewing.\n" +
- "Smooth and slightly more caffeinated as a result of its concentration."
- }
- ]
- }
+ // TODO: Implement me!
+ return;
}

async GetOrder(input: GetOrderServerInput, context: CoffeeShopContext): Promise<GetOrderServerOutput> {
console.log(`getting an order (${input.id})...`)
- if (context.orders.has(input.id)) {
- const order = context.orders.get(input.id)
- return {
- id: order.orderId,
- coffeeType: order.coffeeType,
- status: order.status
- }
- } else {
- console.log(`order (${input.id}) does not exist.`)
- throw new OrderNotFound({
- message: `order ${input.id} not found.`,
- orderId: input.id
- })
- }
+ // TODO: Implement me!
+ return;
}

// Handle orders as they come in (FIFO), marking them completed based on some random
diff --git a/smithy/model/coffee.smithy b/smithy/model/coffee.smithy
index 3dd6c49..25cc77f 100644
--- a/smithy/model/coffee.smithy
+++ b/smithy/model/coffee.smithy
@@ -1,26 +1,3 @@
$version: "2.0"

namespace com.example
-
-/// An enum describing the types of coffees available
-enum CoffeeType {
- DRIP
- POUR_OVER
- LATTE
- ESPRESSO
- COLD_BREW
-}
-
-/// A structure which defines a coffee item which can be ordered
-structure CoffeeItem {
- @required
- type: CoffeeType
-
- @required
- description: String
-}
-
-/// A list of coffee items
-list CoffeeItems {
- member: CoffeeItem
-}
diff --git a/smithy/model/main.smithy b/smithy/model/main.smithy
index 2e2b0f4..25cc77f 100644
--- a/smithy/model/main.smithy
+++ b/smithy/model/main.smithy
@@ -1,32 +1,3 @@
$version: "2.0"

namespace com.example
-
-use aws.protocols#restJson1
-use smithy.framework#ValidationException
-
-/// Allows users to retrieve a menu, create a coffee order, and
-/// and to view the status of their orders
-@title("Coffee Shop Service")
-@restJson1
-service CoffeeShop {
- version: "2024-08-23"
- operations: [
- GetMenu
- ]
- resources: [
- Order
- ]
- errors: [
- ValidationException
- ]
-}
-
-/// Retrieve the menu
-@http(method: "GET", uri: "/menu")
-@readonly
-operation GetMenu {
- output := {
- items: CoffeeItems
- }
-}
diff --git a/smithy/model/order.smithy b/smithy/model/order.smithy
index be1b19a..25cc77f 100644
--- a/smithy/model/order.smithy
+++ b/smithy/model/order.smithy
@@ -1,78 +1,3 @@
$version: "2.0"

namespace com.example
-
-/// An Order resource, which has an id and descibes an order by the type of coffee
-/// and the order's status
-resource Order {
- identifiers: { id: Uuid }
- properties: { coffeeType: CoffeeType, status: OrderStatus }
- read: GetOrder
- create: CreateOrder
-}
-
-/// Create an order
-@idempotent
-@http(method: "PUT", uri: "/order")
-operation CreateOrder {
- input := for Order {
- @required
- $coffeeType
- }
-
- output := for Order {
- @required
- $id
-
- @required
- $coffeeType
-
- @required
- $status
- }
-}
-
-/// Retrieve an order
-@readonly
-@http(method: "GET", uri: "/order/{id}")
-operation GetOrder {
- input := for Order {
- @httpLabel
- @required
- $id
- }
-
- output := for Order {
- @required
- $id
-
- @required
- $coffeeType
-
- @required
- $status
- }
-
- errors: [
- OrderNotFound
- ]
-}
-
-/// An error indicating an order could not be found
-@httpError(404)
-@error("client")
-structure OrderNotFound {
- message: String
- orderId: Uuid
-}
-
-/// An identifier to describe a unique order
-@length(min: 1, max: 128)
-@pattern("^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$")
-string Uuid
-
-/// An enum describing the status of an order
-enum OrderStatus {
- IN_PROGRESS
- COMPLETED
-}
diff --git a/smithy/smithy-build.json b/smithy/smithy-build.json
index 4b1269d..d757d9a 100644
--- a/smithy/smithy-build.json
+++ b/smithy/smithy-build.json
@@ -3,19 +3,7 @@
"sources": ["model/"],
"maven": {
"dependencies": [
- "software.amazon.smithy:smithy-aws-traits:1.50.0",
- "software.amazon.smithy:smithy-validation-model:1.50.0",
- "software.amazon.smithy.typescript:smithy-aws-typescript-codegen:0.22.0"
+ "software.amazon.smithy:smithy-aws-traits:1.50.0"
]
- },
- "plugins": {
- "typescript-client-codegen": {
- "package": "@com.example/coffee-shop-client",
- "packageVersion": "0.0.1"
- },
- "typescript-ssdk-codegen": {
- "package" : "@com.example/coffee-shop-server",
- "packageVersion": "0.0.1"
- }
}
}
71 changes: 71 additions & 0 deletions tutorials/full-stack-application/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
.DEFAULT_GOAL:=help

help: ## Display this help
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n\nTargets:\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-13s\033[0m %s\n", $$1, $$2 }' $(MAKEFILE_LIST)

init: ## Initialize the project and set it to the tutorial state
@echo Initializing...
git init; git add -A; git commit -m 'Initial commit'; git apply .patches/start.patch
@echo Initialized.

reset: clean ## Reset the project to its completed state
@echo Resetting...
git reset --hard HEAD
@echo Done.

clean: ## Clean all build artifacts of this project
@echo Cleaning build Directories...
rm -rf build/ */build/ */dist */node_modules client/sdk server/ssdk app/.next
@echo Cleaning complete.

build-smithy: ## Build the smithy model and code-generate the client and server
@echo Building smithy models...
cd smithy; smithy format model/; smithy build
@echo Finished building models.

build-ssdk: build-smithy ## Set up and build the generated server-sdk (`ssdk`)
@echo Building server-sdk...
cd server; ln -fs ../smithy/build/smithy/source/typescript-ssdk-codegen ssdk
cd server/ssdk; yarn && yarn build
@echo Finished building server-sdk.

build-server: build-ssdk ## Build the server implementation
@echo Building server...
cd server; yarn && yarn build; yarn install
@echo Finished building server.

build-client: build-smithy ## Set up and build the generated client
@echo Building client...
cd client; ln -fs ../smithy/build/smithy/source/typescript-client-codegen sdk
cd client/sdk; yarn && yarn build
@echo Finished building client.

build-app: build-client ## Build the web application
@echo Building app...
cd app; yarn && yarn build
@echo Finished building app.

build: build-server build-client build-app ## Build the entire project

dev-server: build-ssdk ## Run the server in development-mode
cd server; yarn && yarn dev

dev-app: build-client ## Run the web application in development-mode
cd app; yarn && yarn dev

dev: # Run the server and web application in development-mode in the same session
$(MAKE) -j 2 dev-server dev-app

run-server: build-server ## Run the server
cd server; yarn start

run-app: build-app ## Run the web application
cd app; yarn start

run: ## Run the server and web application in the same session
$(MAKE) -j 2 run-server run-app

repl-client: build-client ## Start a REPL with in the client installed
cd client; yarn && yarn start

test-all: build
Loading
Loading