Skip to content

Basic implementation of a Back-End Web framework in Kotlin and Gradle

License

Notifications You must be signed in to change notification settings

Flipedds/basic-http

Repository files navigation

BASIC-HTTP 🌐

Technologies β€’ Documentations β€’ Project diagram β€’ Getting Started β€’ Collaborators β€’ Contribute

Basic implementation of a Back-End Web framework in Kotlin and Gradle

πŸ’» Technologies

  • Kotlin
  • Gradle

πŸ’» Documentations

πŸ“š Basic-http Web Framework documentation
πŸ“š Kotlin documentation
πŸ“š Gradle documentation

Project diagram

πŸš€ Getting started

Prerequisites

Cloning

git clone project-url
cd project-name

Starting

Using with Gradle

repositories {
    mavenCentral()
    maven {
            url = uri("https://maven.pkg.github.com/flipedds/basic-http")
            credentials {
                username = "flipedds"
                password = "a_token_for_read_packages_on_github_contact_me_for_it"
            }
        }
}

dependencies {
    implementation 'com.flipedds:basic-http:2.1.1'
}

Server config

Add server.properties on main properties folder => src/main/resources/server.properties

server.host=localhost
server.port=3000
server.jwtkey=SECRET_KEY

Framework Initializer

fun main() {
    BasicHttpConfig.startServer()
}

Entity Definition

data class User(val id: Int, val name: String)

Controller Definition

@Controller
class UserController(private val userService: IUserService) : BaseController,
    IJwtCreator by JwtCreator("SECRET_KEY"){
    @Mapping(path = "/users/auth", method = RequestMethod.POST)
    fun authUser(): Json<String>{
        return Json(
                message = "User authenticated !!",
                statusCode = StatusCode.Ok,
                data = createJwt("user")
        )
    }
    
    @UseAuthentication
    @Mapping(path = "/users/get/{id}", method = RequestMethod.GET)
    fun getUserById(@PathParam id: Int?): Json<User> {
        return Json(
                message = "User found !!",
                statusCode = StatusCode.Ok,
                data = id?.let { userService.getUserById(it) }
        )
    }
    
    @UseAuthentication
    @Mapping(path = "/users", method = RequestMethod.GET)
    fun getUser(@QueryParam("id") id: Int?): Json<User> {
        return Json(
            message = "User found !!",
            statusCode = StatusCode.Ok,
            data = id?.let { userService.getUserById(it) }
        )
    }

    @Mapping(path = "/users/create", method = RequestMethod.POST)
    fun postUser(@Body user: User): Json<User> {
        println(user.id)
        println(user.name)
        return Json(
            message = "User created",
            statusCode = StatusCode.Created,
            data = user
        )
    }

    @Mapping(path = "/users/", method = RequestMethod.GET)
    fun getUserList(): Json<List<User>> {
        return Json(
            message = "Users found !!",
            statusCode = StatusCode.Ok,
            data = listOf(
                User(1, "teste"), User(2, "teste"), User(3, "teste")
            )
        )
    }
}

Controller Injectables Definition

@Injectable
interface IUserService {
    fun getUserById(id: Int) : User
}

class UserService(private val userRepository: IUserRepository): IUserService {
    override fun getUserById(id: Int) : User {
        return userRepository.getUserById(id)
    }
}

@Injectable
interface IUserRepository {
    fun getUserById(id: Int) : User
}

class UserRepository: IUserRepository {
    override fun getUserById(id: Int) : User {
        return User(id = id, name = "teste")
    }
}

Orm Config

Add server.properties on main properties folder => src/main/resources/server.properties

database.url=jdbc:mysql://localhost:3306/database_name
database.username=root
database.password=root

Orm Methods

interface IBasicOrm<T : Any> {
    fun insert(entity: T)
    fun findAll(): MutableList<T>
    fun findOne(id: Any): T?
    fun updateOne(entity: T)
    fun deleteOne(entity: T)
    fun closeConnection()
}

Orm Use Definition

// entity class of orm, required empty constructor
@Table(name = "tb_test")
class TbTest {
    constructor(user: TbUser?, name: String, id: Int) {
        this.user = user
        this.name = name
        this.id = id
    }
    
    constructor()
        
    @Id(GeneratedBy.APPLICATION)
    @Column(name = "id")
    var id: Int = 0

    @Column(name = "name")
    var name: String = ""

    @JoinColumn(name = "user_id", type = Relation.ManyToOne)
    var user: TbUser? = null

    override fun toString(): String {
        return "TbTest(id=$id, name='$name', user=$user)"
    }
}

// injectable config
@Injectable
interface ITbTestOrm: IBasicOrm<TbTest>

// injectable implementation
class TbTestOrm: BasicOrm<TbTest>(TbTest::class), ITbTestOrm

// orm injectable use
@Injectable
interface ITbTestService {
    fun getTbTestById(id: Int) : TbTest?
}

class TbTestService(private val tbTestOrm: ITbTestOrm): ITbTestService {
    override fun getTbTestById(id: Int) : TbTest? {
        return tbTestOrm.findOne(id)
    }
}

// orm normal use, if you don't want to use injectable
val tbTest = TbTest()
tbTest.id = 10
tbTest.name = "test"
tbTest.user = TbUser(1,"test", "", 20)

val tbTestOrm = TbTestOrm()
tbTestOrm.insert(tbTest)

🀝 Collaborators

Filipe AndrΓ© Profile Picture
Filipe AndrΓ©

πŸ“« Contribute

  1. git clone project-url
  2. git checkout -b feature-name
  3. Follow commit patterns
  4. Open a Pull Request explaining the problem solved or feature made, if exists, append screenshot of visual modifications and wait for the review!

Documentations that might help

πŸ“ How to create a Pull Request

πŸ’Ύ Commit pattern

About

Basic implementation of a Back-End Web framework in Kotlin and Gradle

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages