Hi <@UFT11FKSP>, Thank you for the awesome framewo...
# kvision
k
Hi @Robert Jaros, Thank you for the awesome framework. I have a question: I have developed a kvision full-stack application that runs as a fat jar. Now I need to expose a few REST endpoint as back end API from the same running application to reuse the auth and other service layers from the backendMain. Is there a recommended way in kvision to proxy the url from frontend like <domain>/#/api/v1/... to serve a json response?
r
Hi!
Why do you want to proxy from frontend? Can't you just call /api/v1/... directly from the backend?
The frontend is just a browser application. If it has direct access to the api endpoint, so does the browser itself. There is no sense in proxying.
k
As I said, I am running the fat jar. say my application is running at 8080. How to access backend directly without frontend interface?
r
<domain>/api/v1/...
or
<domain>:8080/api/v1/...
When you are calling
/#
you are opening
/index.html
with your frontend app. But you can easily call all other endpoints or resources served by your backend server. Just use the correct path.
k
I tried that, and I got a 404.
To reproduce -> Run backendMain (:8080) and frontendMain (:3000) in two terminals, and the API works fine via localhost:8080/api... -> Run java -jar packaked-fat.jar , Application works fine from :8080 but the api endpoints are not found (404)
r
what backend do you use?
k
Sprint boot
Spring*
r
I would say it's strange. I'm using spring boot as well and I'm using it exactly this way.
How do you build you app (fatjar)?
k
./gradlew jar
r
do you use the configuration from the template-fullstack-spring-boot project?
I mean the gradle configuration
k
r
should be ok
And how do you define your api endpoints?
k
It is a Spring RestController inside the backendMain source..
Copy code
@RestController
class ApiController {

    @GetMapping("/api/v1/test", produces = [MediaType.APPLICATION_JSON_VALUE])
    fun helloKotlin(): String {
        return "hello from API"
    }
}
r
I'll make some tests, because I use different way (
coRouter
).
k
👍
r
It works for me as well with
@RestController
Are you sure you are not making any typo? 🙂 If you api works fine when running from
bootRun
command it should definitely work as well when running from fatjar.
Could you check you have the same gradle configuration for
bootJar
and
bootRun
tasks?
But still if your /kv/* endpoints for kvision services work when running from fatjar, there is no reason for /api/* endpoints not to work. They are defined in the same project, aren't they?
Perhaps your app is open source or you can share a working example with this problem? I could check it out. I don't have an idea what else could be wrong.
k
I quickly cloned your example again and added my controller, and it worked. So it must be something wrong with my config/gradle setup. I was under the assumption that the framework is intercepting all calls, that is why wanted to check with you.. I will check from my side. Thank you very much for your time :)
👍 1
r
extra["kotlin.version"] = "1.4.10"
this should be removed
👍 1
it was needed with some older version
it should be removed in realworld example as well
everything else looks ok, but perhaps the appengine plugin is doing something to the classpath.
k
I see
While going through the build config. I have another question. in this block...
proxy = mapOf( "/kv/*" to "http://localhost:8080", "/kvws/*" to mapOf("target" to "ws://localhost:8080", "ws" to true) ),
should we pass the domain and port dynamically instead of localhost:8080?
r
there is no other way, it just generates static webpack configuration file
if you need to develop with a different domain or port you can change it, but as far as I remember webpack allows only localhost as a domain by default (it can be changed with additional configuration)
k
ok
r
why do you use two different appengine plugins in one project?
I've applied the current version with:
Copy code
id("com.google.cloud.tools.appengine") version "2.4.1"
and additional block in `settings.gradle.kts`:
Copy code
requested.id.id == "com.google.cloud.tools.appengine" -> useModule("com.google.cloud.tools:appengine-gradle-plugin:${requested.version}")
without bulildscript and without apply(plugin = ...)
still the api endpoint is working fine with appengine plugin applied
k
ah yes, it must be the appengine plugin version conflict then, by upgrading to 2.4.1, the API worked fine..
👍 1
Another question: Is there any recommended way to store a "flash" attribute in the flow? I am thinking of assigning a value in the state and clearing it once read. Just wondering if any other way in kvision.
r
Why do you need this? As far as I understand "flash" is usually used to keep some simple, short lived data between page reloads (e.g. error messages). But KVision apps are SPAs and are not reloaded at all. You can just keep any state you want inside KVision app.
k
Actually, I was thinking of a use case, when the user activation is done, I can attach a message to the home page and throw it away on visiting any other page or home page again.. I will set in the state and remove it then.. Cheers