Share
Embed
Kotlin and Spring boot a pleasant experience and some rough edges
In the last months, we have been working on Karumi with Spring boot building web services with Kotlin. The experience has been quite pleasant. I will explain how we have used Kotlin to write quality backend code, how to write tests and how to get the most out of both technologies.
Besides, we will explain some problems we have had in the development of the applications and how we have solved them.
Karumi
March 29, 2019
Transcript
52 slides
-
Kotlin and Spring boot a pleasant experience and some rough
edges Jorge Juan Barroso Carmona Developer and GDE [email protected] @flipper83 https://greach.contestia.es -
Testing for android & iOS. Trainings Architecture, Patterns and principles
for Android & iOS. Mastering Git. Advanced mobile development. Testing for android & iOS. Architecture, Patterns and principles for Android & iOS. Companies For Everybody -
Adam Tornhill “Computer languages differ not so much in what
they make possible, but in what they make easy.” Larry Wall -
Adam Tornhill ProjectController GetProject Projects Project Repository ProjectsApi Project DataSources
Project Entity GetProject -
@RestController class ProjectController( private val obtainProjects: ObtainProjects ) { @GetMapping("/projects")
fun getProjectsEndpoint( authentication: Authentication ) = obtainProjects( authentication.getCurrentUser() ).fold( ifRight{ ResponseEntity.ok(it.mapToApi()) }, ifLeft = { it.toErrorApi() } ) } -
@RestController class ProjectController( private val obtainProjects: ObtainProjects ) { @GetMapping("/projects")
fun getProjectsEndpoint( authentication: Authentication ) = obtainProjects( authentication.getCurrentUser() ).fold( ifRight{ ResponseEntity.ok(it.mapToApi()) }, ifLeft = { it.toErrorApi() } ) } -
@RestController class ProjectController( private val obtainProjects: ObtainProjects ) { @GetMapping("/projects")
fun getProjectsEndpoint( authentication: Authentication ) = obtainProjects( authentication.getCurrentUser() ).fold( ifRight{ ResponseEntity.ok(it.mapToApi()) }, ifLeft = { it.toErrorApi() } ) } -
@Component class ObtainProjects( val projectRepository: ProjectRepository ) { operator fun
invoke( currentUser: User ): Either<ProjectError, Projects> = projectRepository.getAll(currentUser) } -
@Component //@Service class ObtainProjects( val projectRepository: ProjectRepository ) { operator
fun invoke( currentUser: User ): Either<ProjectError, Projects> = projectRepository.getAll(currentUser) } -
@Component class ObtainProjects( val projectRepository: ProjectRepository ) { operator fun
invoke( currentUser: User ): Either<ProjectError, Projects> = projectRepository.getAll(currentUser) } -
@Repository class ProjectRepository( private val projectDataSource: ProjectDataSource ) { fun
getAll( currentUser: User ): Either<ProjectError, Projects> = TryLogger { Projects( projects = projectDataSource .findByOwner( userEntity = currentUser.mapToEntity() ) .mapToDomain() ) }.toEither { ProjectError.DbStorageError } } -
@Repository class ProjectRepository( private val projectDataSource: ProjectDataSource ) { fun
getAll( currentUser: User ): Either<ProjectError, Projects> = TryLogger { Projects( projects = projectDataSource .findByOwner( userEntity = currentUser.mapToEntity() ) .mapToDomain() ) }.toEither { ProjectError.DbStorageError } } -
/**/ Projects( projects = projectDataSource .findByOwner( userEntity = currentUser.mapToEntity() )
.mapToDomain() ) /**/ fun User.mapToEntity(): UserEntity = UserEntity( _id = this.id, name = this.name, email = this.email, username = this.username ) -
@Repository class ProjectRepository( private val projectDataSource: ProjectDataSource ) { fun
getAll( currentUser: User ): Either<ProjectError, Projects> = TryLogger { Projects( projects = projectDataSource .findByOwner( userEntity = currentUser.mapToEntity() ) .mapToDomain() ) }.toEither { ProjectError.DbStorageError } } -
@Repository class ProjectRepository( private val projectDataSource: ProjectDataSource ) { fun
getAll( currentUser: User ): Either<ProjectError, Projects> = TryLogger { Projects( projects = projectDataSource .findByOwner( userEntity = currentUser.mapToEntity() ) .mapToDomain() ) }.toEither { ProjectError.DbStorageError } } -
@Repository class ProjectRepository( private val projectDataSource: ProjectDataSource ) { fun
getAll( currentUser: User ): Either<ProjectError, Projects> = TryLogger { Projects( projects = projectDataSource .findByOwner( userEntity = currentUser.mapToEntity() ) .mapToDomain() ) }.toEither { ProjectError.DbStorageError } } -
sealed class DomainError( val message: String ) sealed class ProjectError(
message: String ) : DomainError(message, field) { object DbStorageError : ProjectError( message = "Error accessing to the db” ) class NotFound( field: String ) : ProjectError(message = "$field not found”) } -
@Component class ObtainProjects( val projectRepository: ProjectRepository ) { operator fun
invoke( currentUser: User ): Either<ProjectError, Projects> = projectRepository.getAll(currentUser) } -
@RestController class ProjectController( private val obtainProjects: ObtainProjects ) { @GetMapping("/projects")
fun getProjectsEndpoint( authentication: Authentication ) = obtainProjects( authentication.getCurrentUser() ).fold( ifRight = { ResponseEntity.ok(it.mapToApi()) }, ifLeft = { it.toErrorApi() } ) } -
fun ProjectError.toErrorApi(): ResponseEntity<String> = when (this) { ProjectError.DbStorageError -> ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE)
is ProjectError.NotFound -> ResponseEntity.status(HttpStatus.NOT_FOUND }.body(createErrorMessage(this)) fun createErrorMessage( projectError: ProjectError ): String = projectError.toJson() -
fun ProjectError.toErrorApi(): ResponseEntity<String> = when (this) { ProjectError.DbStorageError -> ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE)
is ProjectError.NotFound -> ResponseEntity.status(HttpStatus.NOT_FOUND }.body(createErrorMessage(this)) fun createErrorMessage( projectError: ProjectError ): String = projectError.toJson() -
@RestController class ProjectController( private val obtainProjects: ObtainProjects ) { @GetMapping(“/projects”)
// @Secured({"ROLE_ADMIN", "ROLE_USER"}) // @PreAuthorize(“hasRole(‘ADMIN')") fun getProjectsEndpoint( authentication: Authentication ) = obtainProjects( authentication.getCurrentUser() ).fold( ifRight{ ResponseEntity.ok(it.mapToApi()) }, ifLeft = { it.toErrorApi() } ) } -
class JwtTokenFilter( private val jwtTokenProvider: JwtTokenProvider) : OncePerRequestFilter() { @Throws(ServletException::class,
IOException::class) override fun doFilterInternal( httpServletRequest: HttpServletRequest, httpServletResponse: HttpServletResponse, filterChain: FilterChain ) { val token = jwtTokenProvider.resolveToken(httpServletRequest) if (token == null) { filterChain.doFilter(httpServletRequest, httpServletResponse) return } if (jwtTokenProvider.validateToken(token)) { val auth = jwtTokenProvider.getAuthentication(token) SecurityContextHolder.getContext().authentication = auth } filterChain.doFilter(httpServletRequest, httpServletResponse) } } -
if (jwtTokenProvider.validateToken(token)) { val auth = jwtTokenProvider.getAuthentication(token) SecurityContextHolder.getContext().authentication = auth
} @GetMapping(“/projects”) fun getProjectsEndpoint( authentication: Authentication ) -
@Component class UploadVideo( val projectRepository: ProjectRepository, val storageService: StorageService, val
videoRepository: VideoRepository ) { operator fun invoke(newVideo: NewVideo): Either<ProjectError, Video> = binding { if (!validateVideo(newVideo)) ProjectError.VideoFormatError.left().bind() val project = projectRepository[newPhoto.projectId] .bind() .toEither { ProjectError.NotFound("project") } .bind() val videoStorageInfo = storageService .storePhoto( generateName(project.id), video ).bind() photoRepository.add(VideoToStore( filename = videoStorageInfo.filename, videoUrl = videoStorageInfo.url, owner = newVideo.owner, projectId = projectId )).bind() } -
@Component class UploadVideo( val projectRepository: ProjectRepository, val storageService: StorageService, val
videoRepository: VideoRepository ) { operator fun invoke(newVideo: NewVideo): Either<ProjectError, Video> = binding { if (!validateVideo(newVideo)) ProjectError.VideoFormatError.left().bind() val project = projectRepository[newPhoto.projectId] .bind() .toEither { ProjectError.NotFound("project") } .bind() val videoStorageInfo = storageService .storePhoto( generateName(project.id), video ).bind() photoRepository.add(VideoToStore( filename = videoStorageInfo.filename, videoUrl = videoStorageInfo.url, owner = newVideo.owner, projectId = projectId )).bind() } -
@Component class UploadVideo( val projectRepository: ProjectRepository, val storageService: StorageService, val
videoRepository: VideoRepository ) { operator fun invoke(newVideo: NewVideo): Either<ProjectError, Video> = binding { if (!validateVideo(newVideo)) ProjectError.VideoFormatError.left().bind() val project = projectRepository[newPhoto.projectId] .bind() .toEither { ProjectError.NotFound("project") } .bind() val videoStorageInfo = storageService .storePhoto( generateName(project.id), video ).bind() photoRepository.add(VideoToStore( filename = videoStorageInfo.filename, videoUrl = videoStorageInfo.url, owner = newVideo.owner, projectId = projectId )).bind() } -
@Component class UploadVideo( val projectRepository: ProjectRepository, val storageService: StorageService, val
videoRepository: VideoRepository ) { operator fun invoke(newVideo: NewVideo): Either<ProjectError, Video> = binding { if (!validateVideo(newVideo)) ProjectError.VideoFormatError.left().bind() val project = projectRepository[newPhoto.projectId] .bind() .toEither { ProjectError.NotFound("project") } .bind() val videoStorageInfo = storageService .storePhoto( generateName(project.id), video ).bind() photoRepository.add(VideoToStore( filename = videoStorageInfo.filename, videoUrl = videoStorageInfo.url, owner = newVideo.owner, projectId = projectId )).bind() } -
@Component class UploadVideo( val projectRepository: ProjectRepository, val storageService: StorageService, val
videoRepository: VideoRepository ) { operator fun invoke(newVideo: NewVideo): Either<ProjectError, Video> = binding { if (!validateVideo(newVideo)) ProjectError.VideoFormatError.left().bind() val project = projectRepository[newPhoto.projectId] .bind() .toEither { ProjectError.NotFound("project") } .bind() val videoStorageInfo = storageService .storePhoto( generateName(project.id), video ).bind() photoRepository.add(VideoToStore( filename = videoStorageInfo.filename, videoUrl = videoStorageInfo.url, owner = newVideo.owner, projectId = projectId )).bind() } -
@Component class UploadVideo( val projectRepository: ProjectRepository, val storageService: StorageService, val
videoRepository: VideoRepository ) { operator fun invoke(newVideo: NewVideo): Either<ProjectError, Video> = binding { if (!validateVideo(newVideo)) ProjectError.VideoFormatError.left().bind() val project = projectRepository[newPhoto.projectId] .bind() .toEither { ProjectError.NotFound("project") } .bind() val videoStorageInfo = storageService .storePhoto( generateName(project.id), video ).bind() photoRepository.add(VideoToStore( filename = videoStorageInfo.filename, videoUrl = videoStorageInfo.url, owner = newVideo.owner, projectId = projectId )).bind() }