This example handles HTTP GET requests by responding with 'Hello, World!'

Overview

Hello world example

This example handles HTTP GET requests by responding with 'Hello, World!'.

// lib/functions.dart
import 'package:functions_framework/functions_framework.dart';
import 'package:shelf/shelf.dart';

@CloudFunction()
Response function(Request request) => Response.ok('Hello, World!');

Simulate a hosted environment on your own machine

You can run this function example on your own machine using Docker to simulate running in a hosted environment.

$ docker build -t hello .
...

$ docker run -it -p 8080:8080 --name app hello
Listening on :8080

From another terminal:

curl http://localhost:8080
Hello, World!

If you're curious about the size of the image you created, enter:

$ docker image ls hello
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
hello        latest    3f23c737877b   1 minute ago     11.6MB

Editing the function and testing locally

If you would like to rename the handler function (function) to something else (ex: handleGet), you need to ensure that the FUNCTION_TARGET environment variable is set to the new function name.

For example:

@CloudFunction()
Response handleGet(Request request) => Response.ok('Hello, World!');

Run the build_runner to regenerate bin/server.dart from lib/functions.dart

$ dart run build_runner build
[INFO] Generating build script completed, took 304ms
[INFO] Reading cached asset graph completed, took 46ms
[INFO] Checking for updates since last build completed, took 412ms
[INFO] Running build completed, took 2.2s
[INFO] Caching finalized dependency graph completed, took 28ms
[INFO] Succeeded after 2.3s with 1 outputs (1 actions)

Run tests (note that FUNCTION_TARGET must now be set for the test process):

$ FUNCTION_TARGET=handleGet dart test
00:02 +1: All tests passed!

Run it on your system:

$ FUNCTION_TARGET=handleGet dart run bin/server.dart
Listening on :8080

If you want to test this hosted on your machine, rebuild the image

$ docker build -t hello .
...

If you had a previous container running, make sure to remove it now. Assuming you named the container app (as demonstrated earlier):

docker rm -f app

Now launch another container, this time ensuring the environment variable is passed to Docker so that it will be set for the containerized function:

$ docker run -it -p 8080:8080 --name app -e 'FUNCTION_TARGET=handleGet' hello
App listening on :8080

Clean up

When finished, clean up by entering:

docker rm -f app        # remove the container
docker image rm hello   # remove the image

Makefile

If you're familiar with make and have it in your path, you can use the provided Makefile for convenience while developing and testing your source code locally until ready to test in a container or deploy it. The following targets are supported:

  • make build - this is the default target and will generate bin/server.dart
  • make clean - clears build_runner cache and removes the bin/server.dart
  • make test - runs clean and build targets, then runs tests
  • make run - runs the build target and then starts the Dart function server locally
You might also like...

A google browser clone which is made by using flutter and fetching the google search api for the search requests.

A google browser clone which is made by using flutter and fetching the google search api for the search requests.

google_clone A new Flutter project. Project Preview Getting Started This project is a starting point for a Flutter application. A few resources to get

May 31, 2022

🌈 Repository for a compass project, basically an App for displaying bank transfers, with API requests, Flag persistence, Infinite Scroll, Error Handling, Unit Tests, Extract Sharing working with SOLID, BLoC and Designer Patterns.

🌈 Repository for a compass project, basically an App for displaying bank transfers, with API requests, Flag persistence, Infinite Scroll, Error Handling, Unit Tests, Extract Sharing working with SOLID, BLoC and Designer Patterns.

πŸ’Έ Green Bank Aplicação desenvolvida em Flutter com intuito de trabalhar conexΓ£o com API, Gerenciamento de estado usando BLoC, Refatoração, Arquitetur

Oct 7, 2022

Listen to remote Flutter GTK application instances' command-line arguments and file open requests.

gtk_application This package allows the primary Flutter GTK application instance to listen to remote application instances' command-line arguments and

Dec 15, 2022

Lottie-package-example-Flutter - A simple example about lottie package in Flutter

Lottie-package-example-Flutter - A simple example about lottie package in Flutter

Lottie Package example - Flutter ScreenShot ⚠️ Essential Packages lottie: ^1.2.1

Dec 7, 2022

Smooth-Page-Indicator-Example-Flutter - A simple example about smooth page indicator in Flutter

Smooth-Page-Indicator-Example-Flutter - A simple example about smooth page indicator in Flutter

Smooth Page Indicator Example - Flutter Screenshots ⚠️ Essential Packages smooth

Dec 7, 2022

A set of real world timelines to showcase the use of timeline_tile package, built with Flutter.

A set of real world timelines to showcase the use of timeline_tile package, built with Flutter.

beatiful_timelines Beautiful timelines built with Flutter and timeline_tile. Current examples: Timeline Showcase Football Timeline Activity Timeline S

Dec 21, 2022

Breathe is a mental health blogging app where users can join communities of doctors and other users from around the world and both share their problems as well as lend a ear to and help others

Breathe is a mental health blogging app where users can join communities of doctors and other users from around the world and both share their problems as well as lend a ear to and help others

𝐏𝐫𝐨𝐣𝐞𝐜𝐭 𝐁𝐫𝐞𝐚𝐭𝐑𝐞 In a condensed, suffocating society you can feel closed off, when you can't process your emotions and are going through

May 16, 2022

An application to track bitcoin prices around the world.

![App Brewery Banner](https://github.com/londonappbrewery/Images/blob/master/AppBreweryBanner.png) # Bitcoin Ticker πŸ€‘ ## Our Goal The object

Jan 7, 2022

Sample flutter project to show exchange Rates of leading cryptos to different currencies in the world.

bitcointicker A bitcoin ticker project Getting Started This project is a starting point for a Flutter application. A few resources to get you started

Feb 15, 2022
Owner
TryIt (κΉ€λ™ν˜„)
세상을 λ³€ν™”μ‹œν‚€κ³  싢은 디지털 λ…Έλ§ˆλ”
TryIt (κΉ€λ™ν˜„)
App HTTP Client is a wrapper around the HTTP library Dio to make network requests and error handling simpler, more predictable, and less verbose.

App HTTP Client App HTTP Client is a wrapper around the HTTP library Dio to make network requests and error handling simpler, more predictable, and le

Joanna May 44 Nov 1, 2022
An easy-to-use flutter http network requests handler with more functionality than http but more simpler than dio.

network_requests An easy-to-use flutter http network requests handler with more functionality than http but more simpler than dio. Platform Supported

Coder_Manuel 3 Dec 15, 2022
A composable API for making HTTP requests in Dart.

A composable, Future-based library for making HTTP requests. This package contains a set of high-level functions and classes that make it easy to cons

Dart 888 Dec 26, 2022
A ready-made structure that holds HTTP requests.

A ready-made structure that holds HTTP requests. Usage import 'package:flutter/material.dart'; import 'package:uigitdev_request_holder/src/http_reques

Uigitdev 2 Oct 21, 2022
Flutter error catching & handling plugin. Handles and reports exceptions in your app!

Catcher Catcher is Flutter plugin which automatically catches error/exceptions and handle them. Catcher offers multiple way to handle errors. Catcher

Jakub 697 Jan 7, 2023
World Time App - World time application made using flutter

World Time App Flutter Application to view time in different parts of world. This was my first app built while learning flutter App Screenshots Loadin

Akash Rajpurohit 0 Jan 17, 2020
Flutter-world-time-practice - World Time Tutorial App For Flutter

world_time Result of Flutter Tutorial for Beginners Navigation: pushNamed, pop r

Seonghyeon Cho 1 Feb 7, 2022
Charlatan - A library for configuring and providing fake http responses to your dio HTTP client.

charlatan This package provides the ability to configure and return fake HTTP responses from your Dio HTTP Client. This makes it easy to test the beha

Betterment 14 Nov 28, 2022
Flutter Github Following Application, Using Flutter Provider and Flutter HTTP to get data from Github API.

Flutter Github Following Application Watch it on Youtube Previous Designs Checkout my Youtube channel Installation Please remember, after cloning this

Mohammad Rahmani 110 Dec 23, 2022
Generate secure passwords, check for exposed passwords, get visual feedback for password strength or get form validation with a minimum password strength required.

password_strength_checker Generate secure passwords, check for exposed passwords, get visual feedback for password strength or get form validation wit

Dario Varriale 6 Aug 8, 2023