Skip to content

Commit

Permalink
Backend: Adds feature when backend serves frontend (#3)
Browse files Browse the repository at this point in the history
To keep the system simple and not to have nodejs running in the same image or a separate image for frontend. Backend will serve static files and redirect game ids to the index.html
  • Loading branch information
zakhio committed Apr 26, 2023
1 parent 80e8317 commit 75ade7e
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 14 deletions.
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ RUN npm run build
# Step 2: Backend Builder
FROM gradle:7.6-jdk17 as backend-builder
COPY backend /backend
COPY --from=frontend-builder /frontend/dist /backend/src/main/resources/public
COPY --from=frontend-builder /frontend/dist /backend/src/main/resources/static
WORKDIR /backend
RUN gradle build -x check

# Step 3: Final
FROM eclipse-temurin:17-jre
COPY --from=backend-builder /backend/build/libs/tri-game-backend.war /app/tri-game.war
CMD ["java","-jar","/app/tri-game.war"]
COPY --from=backend-builder /backend/build/libs/tri-game-backend.jar /app/tri-game.jar
CMD ["java","-jar","/app/tri-game.jar"]
3 changes: 3 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ out/

### VS Code ###
.vscode/

### Frontend build ###
backend/src/main/resources/static/
2 changes: 0 additions & 2 deletions backend/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ plugins {
id("org.springframework.boot") version "3.0.5"
id("io.spring.dependency-management") version "1.1.0"
id("org.springdoc.openapi-gradle-plugin") version "1.6.0"

war
}

group = "io.zakh"
Expand Down
13 changes: 4 additions & 9 deletions backend/src/main/kotlin/io/zakh/tri/WebConfig.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package io.zakh.tri

import org.springframework.context.annotation.Configuration
import org.springframework.web.servlet.config.annotation.CorsRegistry
import org.springframework.web.servlet.config.annotation.EnableWebMvc
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer

@Configuration
@EnableWebMvc
class WebConfig : WebMvcConfigurer {
override fun addCorsMappings(registry: CorsRegistry) {

registry.addMapping("/**")
.allowedOrigins("http://localhost:5173", "http://localhost:4000")
.allowCredentials(true)
.allowedMethods("*")
override fun addViewControllers(registry: ViewControllerRegistry) {
registry.addViewController("/{gameID:\\w*}")
.setViewName("index.html")
}
}
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview",
"watch": "vite build --watch --emptyOutDir --outDir ../backend/src/main/resources/static",
"generate-rest-client": "swagger-typescript-api -p ../backend/build/openapi.json -o ./src/api -n rest.ts"
},
"dependencies": {
Expand Down

0 comments on commit 75ade7e

Please sign in to comment.