Technologies β’ Documentations β’ Project diagram β’ Getting Started β’ Collaborators β’ Contribute
Basic implementation of a Back-End Web framework in Kotlin and Gradle
- Kotlin
- Gradle
π Basic-http Web Framework documentation
π Kotlin documentation
π Gradle documentation
git clone project-url
cd project-namerepositories {
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.host=localhost
server.port=3000
server.jwtkey=SECRET_KEYfun main() {
BasicHttpConfig.startServer()
}data class User(val id: Int, val name: String)@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")
)
)
}
}@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")
}
}database.url=jdbc:mysql://localhost:3306/database_name
database.username=root
database.password=rootinterface 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()
}// 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)|
Filipe AndrΓ© |
git clone project-urlgit checkout -b feature-name- Follow commit patterns
- Open a Pull Request explaining the problem solved or feature made, if exists, append screenshot of visual modifications and wait for the review!