-
Notifications
You must be signed in to change notification settings - Fork 372
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
42 changed files
with
3,560 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# Quarkus REST API Exercise | ||
|
||
## Goal | ||
|
||
The goal of this exercise is to learn how to use GitHub Copilot, using an exercise that consist of building a REST API using [Quarkus](https://quarkus.io/). | ||
|
||
## Exercises | ||
|
||
We have created a Quarkus project with some files already created, you can find the project in the folder **exercisefiles/quarkus**. | ||
|
||
Let's start copiloting!!! | ||
|
||
### 1. Create the code to handle a simple GET request | ||
|
||
Move to the 'DemoResource.java' file and start writing the code to handle a simple GET request. In this first exercise, we have provided a comment that describes the code you need to generate. Just press enter and wait a couple of seconds, Copilot will generate the code for you. If you are not happy with the code generated, you can press enter again and Copilot will generate a new code | ||
|
||
There is already a unit test implemented for this exercise, you can run it using the command `mvn test` before and after to validate that the code generated by Copilot is correct. | ||
|
||
Then, create a new unit test for the case when no key is provided in the request. | ||
|
||
After every exercise, feel free to package and run your application to test it. | ||
|
||
Package: `mvn package` | ||
|
||
Run: `mvn quarkus:dev` | ||
|
||
Test: `curl -v http://localhost:8080/hello?key=world` | ||
|
||
### 2. Dates comparison | ||
|
||
New operation under /diffdates that calculates the difference between two dates. The operation should receive two dates as parameter in format dd-MM-yyyy and return the difference in days. | ||
|
||
Additionally, create a unit test that validates the operation. | ||
|
||
From now on, you will have to create the unit tests for every new operation. Wasn't it easy with Copilot? | ||
|
||
### 3. Validate the format of a spanish phone | ||
|
||
Validate the format of a spanish phone number (+34 prefix, then 9 digits, starting with 6, 7 or 9). The operation should receive a phone number as parameter and return true if the format is correct, false otherwise. | ||
|
||
### 4. Validate the format of a spanish DNI | ||
|
||
Validate the format of a spanish DNI (8 digits and 1 letter). The operation should receive a DNI as parameter and return true if the format is correct, false otherwise. | ||
|
||
### 5. From color name to hexadecimal code | ||
|
||
Based on existing colors.json file under resources, given the name of the color as path parameter, return the hexadecimal code. If the color is not found, return 404 | ||
|
||
Hint: Use TDD. Start by creating the unit test and then implement the code. | ||
|
||
### 6. Jokes creator | ||
|
||
Create a new operation that call the API https://api.chucknorris.io/jokes/random and return the joke. | ||
|
||
### 7. URL parsing | ||
|
||
Given a url as query parameter, parse it and return the protocol, host, port, path and query parameters. The response should be in Json format. | ||
|
||
### 8. List files and folders | ||
|
||
List files and folders under a given path. The path should be a query parameter. The response should be in Json format. | ||
|
||
### 9. Word counting | ||
|
||
Given the path of a file and count the number of occurrence of a provided word. The path and the word should be query parameters. The response should be in Json format. | ||
|
||
### 10. Zipping | ||
|
||
Create a zip file with the content of a given folder. The path of the folder should be a query parameter. | ||
|
||
### 11. Containerize the application | ||
|
||
Use the Dockerfile provided to create a docker image of the application. In this case, the full content is provided, but in order build, run and test the docker image, you will use Copilot as well to generate the commands. | ||
|
||
I have created a DOCKER.md file where we will document the steps to build the application (native), build the container image, yun the container and test the container. | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
* | ||
!target/*-runner | ||
!target/*-runner.jar | ||
!target/lib/* | ||
!target/quarkus-app/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#Maven | ||
target/ | ||
pom.xml.tag | ||
pom.xml.releaseBackup | ||
pom.xml.versionsBackup | ||
release.properties | ||
.flattened-pom.xml | ||
|
||
# Eclipse | ||
.project | ||
.classpath | ||
.settings/ | ||
bin/ | ||
|
||
# IntelliJ | ||
.idea | ||
*.ipr | ||
*.iml | ||
*.iws | ||
|
||
# NetBeans | ||
nb-configuration.xml | ||
|
||
# Visual Studio Code | ||
.vscode | ||
.factorypath | ||
|
||
# OSX | ||
.DS_Store | ||
|
||
# Vim | ||
*.swp | ||
*.swo | ||
|
||
# patch | ||
*.orig | ||
*.rej | ||
|
||
# Local environment | ||
.env | ||
|
||
# Plugin directory | ||
/.quarkus/cli/plugins/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
maven-wrapper.jar |
98 changes: 98 additions & 0 deletions
98
completesolution/quarkus/copilot-demo/.mvn/wrapper/MavenWrapperDownloader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.Authenticator; | ||
import java.net.PasswordAuthentication; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardCopyOption; | ||
|
||
public final class MavenWrapperDownloader | ||
{ | ||
private static final String WRAPPER_VERSION = "3.2.0"; | ||
|
||
private static final boolean VERBOSE = Boolean.parseBoolean( System.getenv( "MVNW_VERBOSE" ) ); | ||
|
||
public static void main( String[] args ) | ||
{ | ||
log( "Apache Maven Wrapper Downloader " + WRAPPER_VERSION ); | ||
|
||
if ( args.length != 2 ) | ||
{ | ||
System.err.println( " - ERROR wrapperUrl or wrapperJarPath parameter missing" ); | ||
System.exit( 1 ); | ||
} | ||
|
||
try | ||
{ | ||
log( " - Downloader started" ); | ||
final URL wrapperUrl = new URL( args[0] ); | ||
final String jarPath = args[1].replace( "..", "" ); // Sanitize path | ||
final Path wrapperJarPath = Paths.get( jarPath ).toAbsolutePath().normalize(); | ||
downloadFileFromURL( wrapperUrl, wrapperJarPath ); | ||
log( "Done" ); | ||
} | ||
catch ( IOException e ) | ||
{ | ||
System.err.println( "- Error downloading: " + e.getMessage() ); | ||
if ( VERBOSE ) | ||
{ | ||
e.printStackTrace(); | ||
} | ||
System.exit( 1 ); | ||
} | ||
} | ||
|
||
private static void downloadFileFromURL( URL wrapperUrl, Path wrapperJarPath ) | ||
throws IOException | ||
{ | ||
log( " - Downloading to: " + wrapperJarPath ); | ||
if ( System.getenv( "MVNW_USERNAME" ) != null && System.getenv( "MVNW_PASSWORD" ) != null ) | ||
{ | ||
final String username = System.getenv( "MVNW_USERNAME" ); | ||
final char[] password = System.getenv( "MVNW_PASSWORD" ).toCharArray(); | ||
Authenticator.setDefault( new Authenticator() | ||
{ | ||
@Override | ||
protected PasswordAuthentication getPasswordAuthentication() | ||
{ | ||
return new PasswordAuthentication( username, password ); | ||
} | ||
} ); | ||
} | ||
try ( InputStream inStream = wrapperUrl.openStream() ) | ||
{ | ||
Files.copy( inStream, wrapperJarPath, StandardCopyOption.REPLACE_EXISTING ); | ||
} | ||
log( " - Downloader complete" ); | ||
} | ||
|
||
private static void log( String msg ) | ||
{ | ||
if ( VERBOSE ) | ||
{ | ||
System.out.println( msg ); | ||
} | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
completesolution/quarkus/copilot-demo/.mvn/wrapper/maven-wrapper.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.8/apache-maven-3.8.8-bin.zip | ||
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Build a Quarkus native image | ||
|
||
## Build the native executable | ||
|
||
```bash | ||
mvn clean package -Pnative | ||
``` | ||
|
||
## Build the docker image using the Dockerfile.native-micro | ||
|
||
```bash | ||
docker build -f Dockerfile.native-micro -t quarkus/quarkus-native-micro . | ||
``` | ||
|
||
## Run the docker image | ||
|
||
```bash | ||
docker run -i --rm -p 8080:8080 quarkus/quarkus-native-micro | ||
``` | ||
|
||
## Test the application | ||
|
||
```bash | ||
curl -v http://localhost:8080/hello | ||
``` | ||
|
16 changes: 16 additions & 0 deletions
16
completesolution/quarkus/copilot-demo/Dockerfile.native-micro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#### | ||
# This Dockerfile is used in order to build a container that runs the Quarkus application in native (no JVM) mode. | ||
# It uses a micro base image, tuned for Quarkus native executables. | ||
# | ||
### | ||
FROM quay.io/quarkus/quarkus-micro-image:2.0 | ||
WORKDIR /work/ | ||
RUN chown 1001 /work \ | ||
&& chmod "g+rwX" /work \ | ||
&& chown 1001:root /work | ||
COPY --chown=1001:root target/*-runner /work/application | ||
|
||
EXPOSE 8080 | ||
USER 1001 | ||
|
||
CMD ["./application", "-Dquarkus.http.host=0.0.0.0"] |
Oops, something went wrong.