Skip to content

Instantly share code, notes, and snippets.

@fwfurtado
Last active September 16, 2022 01:57
Show Gist options
  • Save fwfurtado/28e5b57285f6db349e0eff15f0a97623 to your computer and use it in GitHub Desktop.
Save fwfurtado/28e5b57285f6db349e0eff15f0a97623 to your computer and use it in GitHub Desktop.
postgres docker compose
micronaut:
application:
name: demo
datasources:
default:
url: jdbc:postgresql://localhost:5432/demo
username: postgres
password: postgres
dialect: POSTGRES
driverClassName: org.postgresql.Driver
jpa.default.properties.hibernate.hbm2ddl.auto: update
netty:
default:
allocator:
max-order: 3
{
"info": {
"_postman_id": "39f2afad-8af6-40d3-bdeb-f50e1d50f577",
"name": "Demo",
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
},
"item": [
{
"name": "List People",
"request": {
"method": "GET",
"header": [],
"url": "http://localhost:8080/people"
},
"response": []
},
{
"name": "Find Person",
"request": {
"method": "GET",
"header": [],
"url": "http://localhost:8080/people/1"
},
"response": []
},
{
"name": "Save Person",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"name\": \"Eiji\"\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": "http://localhost:8080/people"
},
"response": []
}
]
}
version: "3.7"
services:
database:
image: postgres
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: demo
volumes:
- database:/var/lib/postgresql/data
ports:
- "5432:5432"
volumes:
database:
package com.example
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Post
import java.lang.IllegalArgumentException
@Controller("/people")
class PersonController(
private val repository: PersonRepository
) {
@Get
fun list(): List<PersonEntity> {
return repository.findAll()
}
@Get("{id}")
fun get(id: Long): PersonEntity {
return repository.findById(id) ?: throw NotFoundException()
}
@Post
fun create(person: PersonEntity): PersonEntity {
return repository.save(person)
}
}
class NotFoundException : IllegalArgumentException() {
}
package com.example
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.Id
@Entity
class PersonEntity {
@Id
@GeneratedValue
var id: Long = 0L
var name: String = ""
}
package com.example
import io.micronaut.data.annotation.Repository
import io.micronaut.data.repository.GenericRepository
@Repository
interface PersonRepository : GenericRepository<PersonEntity, Long> {
fun findAll(): List<PersonEntity>
fun findById(id: Long): PersonEntity?
fun save(person: PersonEntity): PersonEntity
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment