Skip to content

Commit 5a44d96

Browse files
authored
Add a Java Spring Boot Example (#1261)
1 parent 14ab456 commit 5a44d96

File tree

20 files changed

+671
-0
lines changed

20 files changed

+671
-0
lines changed

examples/development/python/pip/devbox.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
"lockfile_version": "1",
33
"packages": {
44
"python310": {
5+
"plugin_version": "0.0.1",
56
"resolved": "github:NixOS/nixpkgs/f80ac848e3d6f0c12c52758c0f25c10c97ca3b62#python310"
67
},
78
89
"last_modified": "2023-05-01T16:53:22Z",
10+
"plugin_version": "0.0.1",
911
"resolved": "github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#python310Packages.pip",
1012
"version": "23.0.1"
1113
}

examples/stacks/laravel/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Laravel
2+
3+
Laravel is a powerful web application framework built with PHP. It's a great choice for building web applications and APIs.
4+
5+
This example shows how to build a simple Laravel application backed by MariaDB and Redis. It uses Devbox Plugins for all 3 Nix packages to simplify configuration
6+
7+
[![Open In Devbox.sh](https://jetpack.io/img/devbox/open-in-devbox.svg)](https://devbox.sh/github.com/jetpack-io/devbox/?folder=examples/stacks/laravel)
8+
9+
## How to Run
10+
11+
1. Install [Devbox](https://www.jetpack.io/devbox/docs/installing_devbox/)
12+
13+
1. Create a new Laravel App by running `devbox create --template laravel`. This will create a new Laravel project in your current directory.
14+
15+
1. Start your MariaDB and Redis services by running `devbox services up`.
16+
1. This step will also create an empty MariaDB Data Directory and initialize your database with the default settings
17+
2. This will also start the php-fpm service for serving your PHP project over fcgi. Learn more about [PHP-FPM](https://www.php.net/manual/en/install.fpm.php)
18+
19+
1. Create the laravel database by running `devbox run db:create`, and then run Laravel's initial migrations using `devbox run db:migrate`
20+
21+
1. You can now start the artisan server by running `devbox run serve:dev`. This will start the server on port 8000, which you can access at `localhost:8000`
22+
23+
1. If you're using Laravel on Devbox Cloud, you can test the app by appending `/port/8000` to your Devbox Cloud URL
24+
25+
1. For more details on building and developing your Laravel project, visit the [Laravel Docs](https://laravel.com/docs/10.x)
26+
27+
28+
## How to Recreate this Example
29+
30+
### Creating the Laravel Project
31+
32+
1. Create a new project with `devbox init`
33+
34+
2. Add the packages using the command below. Installing the packages with `devbox add` will ensure that the plugins are activated:
35+
36+
```bash
37+
devbox add mariadb@latest, [email protected], nodejs@18, redis@latest, php81Packages.composer@latest
38+
```
39+
40+
3. Run `devbox shell` to start your shell. This will also initialize your database by running `initdb` in the init hook.
41+
42+
4. Create your laravel project by running:
43+
44+
```bash
45+
composer create-project laravel/laravel tmp
46+
47+
mv tmp/* tmp/.* .
48+
```
49+
50+
### Setting up MariaDB
51+
52+
To use MariaDB, you need to create the default Laravel database. You can do this by running the following commands in your `devbox shell`:
53+
54+
```bash
55+
# Start the MariaDB service
56+
devbox services up mariadb -b
57+
58+
# Create the database
59+
mysql -u root -e "CREATE DATABASE laravel;"
60+
61+
# Once you're done, stop the MariaDB service
62+
devbox services stop mariadb
63+
```

examples/stacks/spring/.gitignore

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
HELP.md
2+
.gradle
3+
build/
4+
!gradle/wrapper/gradle-wrapper.jar
5+
!**/src/main/**/build/
6+
!**/src/test/**/build/
7+
8+
### STS ###
9+
.apt_generated
10+
.classpath
11+
.factorypath
12+
.project
13+
.settings
14+
.springBeans
15+
.sts4-cache
16+
bin/
17+
!**/src/main/**/bin/
18+
!**/src/test/**/bin/
19+
20+
### IntelliJ IDEA ###
21+
.idea
22+
*.iws
23+
*.iml
24+
*.ipr
25+
out/
26+
!**/src/main/**/out/
27+
!**/src/test/**/out/
28+
29+
### NetBeans ###
30+
/nbproject/private/
31+
/nbbuild/
32+
/dist/
33+
/nbdist/
34+
/.nb-gradle/
35+
36+
### VS Code ###
37+
.vscode/

examples/stacks/spring/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Spring Boot Example
2+
3+
This example combines Java, Spring Boot, and MySQL to expose a simple REST API. This example is based on the official [Spring Boot Documentation](https://spring.io/guides/gs/accessing-data-mysql/).
4+
5+
## How to Run
6+
7+
1. Install [Devbox](https://www.jetpack.io/devbox/docs/installing_devbox/)
8+
9+
1. Prepare the database by running `devbox run setup_db`. This will create the user and database that Spring expects in `stacks/spring/src/main/resources/application.properties`
10+
1. You can now start the Spring Boot service by running `devbox run bootRun`. This will start your MySQL service and run the application
11+
1. You can test the service using `GET localhost:8080/demo/all` or `POST localhost:8080/demo/add`. See the Spring Documentation for more details.

examples/stacks/spring/build.gradle

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
plugins {
2+
id 'java'
3+
id 'org.springframework.boot' version '3.1.1'
4+
id 'io.spring.dependency-management' version '1.1.0'
5+
}
6+
7+
group = 'com.devbox.example.spring'
8+
version = '0.0.1-SNAPSHOT'
9+
10+
java {
11+
sourceCompatibility = '17'
12+
}
13+
14+
repositories {
15+
mavenCentral()
16+
}
17+
18+
dependencies {
19+
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
20+
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
21+
implementation 'org.springframework.boot:spring-boot-starter-web'
22+
runtimeOnly 'com.mysql:mysql-connector-j'
23+
testImplementation 'org.springframework.boot:spring-boot-starter-test'
24+
}
25+
26+
tasks.named('test') {
27+
useJUnitPlatform()
28+
}

examples/stacks/spring/devbox.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"packages": [
3+
"jdk@17",
4+
"gradle@latest",
5+
"mysql80@latest"
6+
],
7+
"shell": {
8+
"init_hook": [
9+
"echo 'Welcome to devbox!'\n",
10+
"echo 'Setup MySQL by running `devbox run setup_db`",
11+
"echo 'Run the example using `devbox run bootRun"
12+
],
13+
"scripts": {
14+
"bootRun": [
15+
"devbox services up -b",
16+
"./gradlew bootRun",
17+
"devbox services stop"
18+
],
19+
"build": [
20+
"./gradlew build"
21+
],
22+
"setup_db": [
23+
"devbox services up mysql -b",
24+
"mysql -u root < setup_db.sql",
25+
"devbox services stop"
26+
],
27+
"test": [
28+
"./gradlew test"
29+
]
30+
}
31+
}
32+
}

examples/stacks/spring/devbox.lock

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"lockfile_version": "1",
3+
"packages": {
4+
"gradle@latest": {
5+
"last_modified": "2023-06-29T16:20:38Z",
6+
"plugin_version": "0.0.1",
7+
"resolved": "github:NixOS/nixpkgs/3c614fbc76fc152f3e1bc4b2263da6d90adf80fb#gradle",
8+
"version": "8.0.1"
9+
},
10+
"jdk@17": {
11+
"last_modified": "2023-06-29T16:20:38Z",
12+
"resolved": "github:NixOS/nixpkgs/3c614fbc76fc152f3e1bc4b2263da6d90adf80fb#jdk17",
13+
"version": "17.0.7+7"
14+
},
15+
"mysql80@latest": {
16+
"last_modified": "2023-06-29T16:20:38Z",
17+
"plugin_version": "0.0.1",
18+
"resolved": "github:NixOS/nixpkgs/3c614fbc76fc152f3e1bc4b2263da6d90adf80fb#mysql80",
19+
"version": "8.0.33"
20+
}
21+
}
22+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.gradle.java.home=/nix/store/csfdcflywb9gj20b1mvsp1ixy6f398bg-zulu17.34.19-ca-jdk-17.0.3
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
4+
networkTimeout=10000
5+
zipStoreBase=GRADLE_USER_HOME
6+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)