The file src/main/controller/HelloController.kts
contains the implementation of the controller of this web application following the MVC pattern.
The handler function welcome()
serves the view src/main/resources/templates/welcome.html
.
The file error.html
is a custom error page which is under src/main/resources/templates/
for Spring MVC to detecting it automatically. The model variable error
is passed to the view error.html
to know what kind of error ocurred.
Gradle is a tool for automating building.
The Gradle build file build.gradle.kts
located in the main directory specifies Gradle's configuration for this project.
The build file consists of 4 main sections:
- Plugins
- Repositories
- Dependencies
- Kotlin
- Kotlin compiler options
Plugins are extensions that add features to Gradle. In this project we add plugins for Kotlin and SpringBoot.
In this section the repository for solving dependencies is declared.
In our case, mavenCentral()
specifies that the Maven Central public repository will be used to solve dependencies.
This sections contains the dependencies of the project which will be downloaded from the repository specified in the previous section.
The dependencies used in this project are:
- SpringBoot
- Jackson
- Kotlin Reflection
- Kotlin Standard Library JDK 8 extension
- Bootstrap WebJar
Kotlin is configured to run on the JVM.
When the Kotlin targets the JVM platform, options of the compile task are specified in the compileKotlin
variable.
In our case, we specify that the target version of the JVM is 11 with jvmTarget
and we configure the compiler to generate error by adding the -Xjsr305=strict
flag.
Testing is handled using JUnit, a powerful framework that allows you to check different aspects of your code.
Unit tests can be run with the following commands.
cd lab1-git-race
gradle test
All verification tasks, including unit tests, can be run with the following commands. Gradle offers a flag, -i, that can be used to show more information while running the checks.
cd lab1-git-race
gradle check
There's 3 tests, stored at src/test/kotlin
, that have been made for this Kotlin Webpage
The file src/test/kotlin/IntegrationTest.kt
contains two tests that checks the main behaivour of the HTML page itself:
-
testHome()
checks if making a request athttp://localhost:$port
(With$port
in this case being 0 for the shake of the test), yields both:-
A
OK
HTTP Status Code. -
A HTML body with
<title>hello
.If this happens, we can assume the webpage's HTML is the one intended.
-
-
testCss()
checks if the CSS of the webpage has basic functionality. For this, it requesthttp://localhost:$port/webjars/bootstrap/5.1.0/css/bootstrap.min.css
from the Web Server, and checks if it has a response with:- A
OK
HTML Status. - A body with
"body"
. - A file with a header equal to that of
"text/css"
.
- A
This ensures the webpage has a valid CSS file.
In order to deploy the app, you will need
git
andheroku
CLI installed in your machine.
Detailed instructions can be found here.
First of all, create an empty app:
$ heroku create
Creating app... done, ⬢ thawing-inlet-61413
https://thawing-inlet-61413.herokuapp.com/ | https://git.heroku.com/thawing-inlet-61413.git
Save the url, it will be needed.
Now, add a remote to your local repository with the heroku git:remote
CLI command. All you need is your Heroku app's name:
$ heroku git:remote -a thawing-inlet-61413
set git remote heroku to https://git.heroku.com/thawing-inlet-61413.git
Deploy the code!
git push heroku master
Do not be afraid of detaching the push
command, it won't cancel the build and the app will be deployed anyways.
It's very simple, just follow the following steps:
-
Run following commands. This if going to create the image that we need:
docker pull gradle:openj9 docker build -t lab1-git-race .
-
If all went correctly, a image has been created (Image ID and Size may be different):
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE lab1-git-race latest 6de6b5e29bda 1 minutes ago 709MB
-
Finally, the following command run the container and is going to link the port 8080 of the container with the port 8080 of the host. This can be changed for example
5000:8080
to link the port 8080 of the container with the port 5000 of the host.docker run -p 8080:8080 lab1-git-race