Skip to content

Commit

Permalink
feature: add configurable cors origin through environment variable
Browse files Browse the repository at this point in the history
- add BASE_URL environment variable in order to set the allowed origin

Also:
- fix copyFiles property phase
- fix app description in Swagger UI
  • Loading branch information
MDeLuise committed Sep 7, 2023
1 parent 5ba0117 commit 18bd711
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 12 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ There are 2 configuration file available:
CACHE_PORT=6379
TRAFLE_KEY= # put you key here, otherwise the "search" feature will include only user generated species
UPLOAD_DIR= # path to the directory used to store uploaded images, if on docker deployment leave as it is and change the volume binding if needed
BASE_URL=http://localhost:3000 # CORS allowed origin
```
Change the properties values according to your system.

Expand All @@ -149,7 +150,7 @@ Feel free to contribute and help improve the repo.
You can submit any of this in the [issues](https:/MDeLuise/plant-it/issues/new/choose) section of the repository. Chose the right template and then fill the required info.

### Bug fix
If you fix a bug, please follow the [contribution-guideline](https:/MDeLuise/plant-it#how-to-contribute) in order to merge the fix in the repository.
If you fix a bug, please follow the [contribution guideline](https:/MDeLuise/plant-it#how-to-contribute) in order to merge the fix in the repository.

### Feature development
Let's discuss first possible solutions for the development before start working on that, please open a [feature request issue](https:/MDeLuise/plant-it/issues/new?assignees=&labels=&projects=&template=fr.yml).
Expand All @@ -169,6 +170,6 @@ To fix a bug or create a feature, follow these steps:
#### Local environment
If you want to make some changes in the project, you can use the following commands:
* in order to run the frontend: `cd frontend`, then `npm run dev`.
* in order to run the backend: `cd backend`, then `./mvnw spring-boot:run -Dspring-boot.run.profiles=dev`. This enables the `dev` profile, which uses an embedded h2 database instead of one external mysql instance, and creates a user with username `user` and password `user` with a predefined plant's collection.
* in order to run the backend: `cd backend`, then `./mvnw spring-boot:run -Dspring-boot.run.profiles=dev -DcopyFiles`. This enables the `dev` profile, which uses an embedded h2 database instead of one external mysql instance, and creates a user with username `user` and password `user` with a predefined plant's collection. Also a dummy user uploaded image is copied under `/tmp/plant-it/` directory.

Consider that this environment speed up the developing process, but the app should be tested (at least for new big features) even without the `dev` backend profile and with a local docker deployment.
4 changes: 2 additions & 2 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<artifactId>plant-it</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>Plant it</name>
<description>Gardening companion system</description>
<description>Gardening companion app</description>
<properties>
<java.version>20</java.version>
<start-class>com.github.mdeluise.plantit.Application</start-class>
Expand Down Expand Up @@ -158,7 +158,7 @@
<version>1.8</version>
<executions>
<execution>
<phase>test</phase>
<phase>initialize</phase>
<goals>
<goal>run</goal>
</goals>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
@OpenAPIDefinition(
info = @Info(
title = "Plant-it REST API", version = "1.0",
description = "<h1>Introduction</h1>" + "<p>Plant-it is a self-hosted, open " + "source, ...</p>",
license = @License(name = "Apache 2.0", url = "https://www.apache.org/licenses/LICENSE-2.0"),
description = "<h1>Introduction</h1>" + "<p>Plant-it is a self-hosted, open " +
"source, gardening companion app.</p>",
license = @License(name = "GPL 3.0", url = "https://www.gnu.org/licenses/gpl-3.0.en.html"),
contact = @Contact(name = "GitHub page", url = "https:/MDeLuise/plant-it")
), security = {@SecurityRequirement(name = "bearerAuth")}, servers = {
@Server(description = "Production", url = "/api"),
@Server(description = "Localhost", url = "/api"),
@Server(
description = "Custom",
url = "{protocol}://{host}:{port}/{basePath}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.mdeluise.plantit.security.apikey.ApiKeyFilter;
import com.github.mdeluise.plantit.security.jwt.JwtTokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
Expand Down Expand Up @@ -103,12 +104,12 @@ public SecurityFilterChain configure(HttpSecurity http) throws Exception {

@Bean
@Profile("dev")
public WebMvcConfigurer corsConfigurer() {
public WebMvcConfigurer corsConfigurer(@Value("${server.cors.allowed-origins}") String allowedOrigins) {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:3000")
.allowedOrigins(allowedOrigins)
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTION");
}
};
Expand Down
1 change: 1 addition & 0 deletions backend/src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ jwt.cookie.name = plant-it
#
server.port = 8085
server.servlet.context-path = /api
server.cors.allowed-origins = ${FRONTEND_URL:http://localhost:3000}


#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ jwt.cookie.name = plant-it
#
server.port = 8090
server.address = 0.0.0.0
server.cors.allowed-origins = http://localhost:3000


#
Expand Down
1 change: 1 addition & 0 deletions backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ jwt.cookie.name = plant-it
#
server.port = 8080
server.servlet.context-path = /api
server.cors.allowed-origins = ${FRONTEND_URL:http://localhost:3000}


#
Expand Down
10 changes: 8 additions & 2 deletions deployment/backend.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ MYSQL_HOST=db
MYSQL_PORT=3306
MYSQL_USERNAME=root
MYSQL_PSW=root
JWT_SECRET=putTheSecretHere
JWT_EXP=1
MYSQL_ROOT_PASSWORD=root
MYSQL_DATABASE=bootdb

JWT_SECRET=putTheSecretHere
JWT_EXP=1

USERS_LIMIT=2 # including the admin account, so <= 0 if undefined, >= 2 if defined

CACHE_TTL=86400
CACHE_HOST=cache
CACHE_PORT=6379

FRONTEND_URL=http://localhost:3000

5 changes: 4 additions & 1 deletion deployment/frontend.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
API_URL=http://localhost:8080/api
BROWSER=none

PAGE_SIZE=25

BROWSER=none

0 comments on commit 18bd711

Please sign in to comment.