mirror of
https://github.com/tiennm99/try-deploy-spring-boot-on-coolify.git
synced 2026-08-03 06:23:44 +00:00
[Add] base
This commit is contained in:
+19
-18
@@ -1,38 +1,39 @@
|
||||
plugins {
|
||||
java
|
||||
id("org.springframework.boot") version "3.3.3"
|
||||
id("io.spring.dependency-management") version "1.1.6"
|
||||
java
|
||||
id("org.springframework.boot") version "3.3.3"
|
||||
id("io.spring.dependency-management") version "1.1.6"
|
||||
}
|
||||
|
||||
group = "com.miti99"
|
||||
version = "0.0.1-SNAPSHOT"
|
||||
|
||||
java {
|
||||
toolchain {
|
||||
languageVersion = JavaLanguageVersion.of(21)
|
||||
}
|
||||
toolchain {
|
||||
languageVersion = JavaLanguageVersion.of(21)
|
||||
}
|
||||
}
|
||||
|
||||
configurations {
|
||||
compileOnly {
|
||||
extendsFrom(configurations.annotationProcessor.get())
|
||||
}
|
||||
compileOnly {
|
||||
extendsFrom(configurations.annotationProcessor.get())
|
||||
}
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
|
||||
implementation("org.springframework.boot:spring-boot-starter-data-redis")
|
||||
implementation("org.springframework.boot:spring-boot-starter-web")
|
||||
compileOnly("org.projectlombok:lombok")
|
||||
annotationProcessor("org.projectlombok:lombok")
|
||||
testImplementation("org.springframework.boot:spring-boot-starter-test")
|
||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||
annotationProcessor("org.projectlombok:lombok")
|
||||
|
||||
implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
|
||||
implementation("org.springframework.boot:spring-boot-starter-data-redis")
|
||||
implementation("org.springframework.boot:spring-boot-starter-web")
|
||||
|
||||
testImplementation("org.springframework.boot:spring-boot-starter-test")
|
||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||
}
|
||||
|
||||
tasks.withType<Test> {
|
||||
useJUnitPlatform()
|
||||
useJUnitPlatform()
|
||||
}
|
||||
|
||||
+3
-3
@@ -6,8 +6,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
@SpringBootApplication
|
||||
public class TestDeploySpringBootOnCoolifyApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TestDeploySpringBootOnCoolifyApplication.class, args);
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TestDeploySpringBootOnCoolifyApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.miti99.testdeployspringbootoncoolify.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
@Configuration
|
||||
public class RedisConfig {
|
||||
|
||||
@Bean
|
||||
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory connectionFactory) {
|
||||
RedisTemplate<String, Object> template = new RedisTemplate<>();
|
||||
template.setConnectionFactory(connectionFactory);
|
||||
template.setKeySerializer(new StringRedisSerializer());
|
||||
template.setValueSerializer(new StringRedisSerializer());
|
||||
return template;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.miti99.testdeployspringbootoncoolify.controller;
|
||||
|
||||
import com.miti99.testdeployspringbootoncoolify.entity.User;
|
||||
import com.miti99.testdeployspringbootoncoolify.service.UserService;
|
||||
import java.util.Optional;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/users")
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<User> createUser(@RequestBody User user) {
|
||||
return ResponseEntity.ok(userService.createUser(user));
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<User> getUser(@PathVariable String id) {
|
||||
Optional<User> user = userService.getUser(id);
|
||||
return user.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public ResponseEntity<User> updateUser(@PathVariable String id, @RequestBody User user) {
|
||||
User updatedUser = userService.updateUser(id, user);
|
||||
if (updatedUser != null) {
|
||||
return ResponseEntity.ok(updatedUser);
|
||||
}
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseEntity<Void> deleteUser(@PathVariable String id) {
|
||||
userService.deleteUser(id);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.miti99.testdeployspringbootoncoolify.entity;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
|
||||
@Document(collection = "users")
|
||||
@Data
|
||||
public class User {
|
||||
@Id
|
||||
private String id;
|
||||
private String name;
|
||||
private String email;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.miti99.testdeployspringbootoncoolify.entity;
|
||||
|
||||
import java.io.Serializable;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class UserCache implements Serializable {
|
||||
private String id;
|
||||
private String name;
|
||||
private String email;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.miti99.testdeployspringbootoncoolify.repository;
|
||||
|
||||
import com.miti99.testdeployspringbootoncoolify.entity.User;
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
|
||||
public interface UserRepository extends MongoRepository<User, String> {
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.miti99.testdeployspringbootoncoolify.service;
|
||||
|
||||
import com.miti99.testdeployspringbootoncoolify.entity.User;
|
||||
import com.miti99.testdeployspringbootoncoolify.repository.UserRepository;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
private final String USER_CACHE_KEY = "USER_";
|
||||
|
||||
public User createUser(User user) {
|
||||
User savedUser = userRepository.save(user);
|
||||
redisTemplate.opsForValue().set(USER_CACHE_KEY + savedUser.getId(), savedUser.toString(), 10, TimeUnit.MINUTES);
|
||||
return savedUser;
|
||||
}
|
||||
|
||||
public Optional<User> getUser(String id) {
|
||||
User user = (User) redisTemplate.opsForValue().get(USER_CACHE_KEY + id);
|
||||
if (user == null) {
|
||||
Optional<User> userFromDb = userRepository.findById(id);
|
||||
userFromDb.ifPresent(value -> redisTemplate.opsForValue().set(USER_CACHE_KEY + id, value, 10, TimeUnit.MINUTES));
|
||||
return userFromDb;
|
||||
}
|
||||
return Optional.of(user);
|
||||
}
|
||||
|
||||
public User updateUser(String id, User updatedUser) {
|
||||
if (userRepository.existsById(id)) {
|
||||
updatedUser.setId(id);
|
||||
User savedUser = userRepository.save(updatedUser);
|
||||
redisTemplate.opsForValue().set(USER_CACHE_KEY + savedUser.getId(), savedUser, 10, TimeUnit.MINUTES);
|
||||
return savedUser;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void deleteUser(String id) {
|
||||
userRepository.deleteById(id);
|
||||
redisTemplate.delete(USER_CACHE_KEY + id);
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
spring.application.name=test-deploy-spring-boot-on-coolify
|
||||
@@ -0,0 +1,7 @@
|
||||
spring:
|
||||
data:
|
||||
mongodb:
|
||||
uri: ${MONGODB_URI:mongodb://localhost:27017/mydatabase}
|
||||
redis:
|
||||
host: ${REDIS_HOST:localhost}
|
||||
port: ${REDIS_PORT:6379}
|
||||
+3
-3
@@ -6,8 +6,8 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||
@SpringBootTest
|
||||
class TestDeploySpringBootOnCoolifyApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user