Add Docker support with auto database initialization

This commit is contained in:
2026-03-04 21:46:29 -07:00
parent aeb8002b2b
commit 5b4797fdcc
4 changed files with 93 additions and 4 deletions

View File

@@ -0,0 +1,40 @@
package com.petshop.backend.config;
import com.petshop.backend.entity.User;
import com.petshop.backend.repository.UserRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
@Component
public class DataInitializer implements CommandLineRunner {
private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;
@Override
public void run(String... args) {
if (userRepository.findByUsername("admin").isEmpty()) {
User admin = new User();
admin.setUsername("admin");
admin.setPassword(passwordEncoder.encode("admin123"));
admin.setFullName("Admin User");
admin.setEmail("admin@petshop.com");
admin.setRole(User.Role.ADMIN);
admin.setActive(true);
userRepository.save(admin);
}
if (userRepository.findByUsername("staff").isEmpty()) {
User staff = new User();
staff.setUsername("staff");
staff.setPassword(passwordEncoder.encode("staff123"));
staff.setFullName("Staff User");
staff.setEmail("staff@petshop.com");
staff.setRole(User.Role.STAFF);
staff.setActive(true);
userRepository.save(staff);
}
}
}

View File

@@ -3,9 +3,9 @@ spring:
name: petshop-backend
datasource:
url: ${DB_URL:jdbc:mysql://localhost:3306/petshop?createDatabaseIfNotExist=true}
username: ${DB_USERNAME:root}
password: ${DB_PASSWORD:password}
url: ${SPRING_DATASOURCE_URL:jdbc:mysql://localhost:3306/Petstoredb?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC}
username: ${SPRING_DATASOURCE_USERNAME:petshop}
password: ${SPRING_DATASOURCE_PASSWORD:petshop}
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
@@ -29,7 +29,7 @@ springdoc:
path: /swagger-ui
jwt:
secret: ${JWT_SECRET:your-256-bit-secret-key-change-this-in-production-min-32-chars}
secret: ${JWT_SECRET:change_me_please_make_this_at_least_32_characters_long_for_security}
expiration: ${JWT_EXPIRATION:86400000}
logging: