What is the proper way to say, return a userlist p...
# kvision
s
What is the proper way to say, return a userlist page using ktor with Kvision? I've been following along the ktor example but am unsure of how to apply the route.
Copy code
fun Application.main() {
    install(Compression)
    routing {
        applyRoutes(PingServiceManager)
        get("/userlist") {
            call.respondHtml(HttpStatusCode.OK) {
                // send back my KVision UserPage class
            }
        }
    }
    kvisionInit()
}
I have the same file structure as the example (backendMain, commonMain, frontendMain). I'm simply unsure how to approach this. Should I create an
IUserListService
in
commonMain
with a
getUserListPage()
function, implement it in
backendMain
and then call it from somewhere in
frontendMain
? And add
applyRoutes(UserListServiceManager)
to the ktor configuration? Assume I have a
UserPage : SimplePanel()
class in the front end, also I see from the documentation that KVision Services' cannot return SimplePanel as a type... I'm not sure how I should be doing this...
r
Hi I would say your backend service should return just a
List<User>
(instead of
UserPage
). When communicating with the backend keep your model clean and simple. You shouldn't create routes manually on the ktor side. Instead just implement the service method and use
applyRoutes(...)
. After that you will be able to just call the service method from your frontend and get your list of users. And only after the list is retrieved you create/modify the UI to display it (you can do this with both imperative and reactive code - the way you like - I personally prefer the reactive approach with the global state - stateflow or redux).
You should think of the KVision fullstack app like this: 1. The backend is all about business logic. You create services and methods that work with some data. E.g. store and retrieve data from externals sources (db, other web services), process the data (generating reports), authorize users and so on. You also create methods needed to get all the data necessary for your user interface (including methods directly bound with KVision fullstack components). 2. The frontend is the place you design your UI with KVision components. There are no strict rules - you can choose the way that best fits your application. The only thing you need to remember is that service methods are suspending and need to be called from the coroutines context. They are in fact asynchronous and for bigger applications you need to implement some reasonable architecture for your app.
s
Thanks Robert for the answer, you explained things very well and cleared so much up for me, much appreciated!