I need to make a very simple http server in a kotl...
# kotlin-native
c
I need to make a very simple http server in a kotlin native app (just for rpc with a webapp). I can't find any kotlin-native-ready http server libraries, so does anybody know of any, and if not, what would be the best choice for making a simple tcp listener in kotlin native (and I could slap together a simple http implementation that would be good enough for my purposes)?
s
Ktor is multi platform. I haven’t tried it yet on native though. https://ktor.io/docs/http-client-multiplatform.html#add-dependencies
That is a link to client, not server though..
b
Yes, ktor-client is mpp, but not ktor-server. Mpp server is planned after new kn memory model arrives
👍 1
There's this , see if it gives you any ideas
And a gist
c
Sadly I need something that will also work on windows. Was really hoping to avoid cinterop stuff.
s
I wanted to avoid cinterop too because it is so painful to setup! I ended up going with the route of creating an interface that is implemented by target platforms.
c
I can use cinterop as a last resort, but either way I'd need an option that even supports the windows platform
b
Posix should cover most of fs, no?
So cinterops on nativeMain should be sufficient to cover all platforms
The gist i linked to doesn't use any custom cinterops and is built only on posix. See if it works for your usecase
c
oh, I assumed posix excluded windows, but yeah, I'll test whether it can build for windows when I get the chance, thanks
b
Windows as always is a special snowflake when it comes to posix, but id does have a partial implementation of the standard, so some things still work.
d
@capitalthree another option is to build on
ktor-network
that offers raw TCP socket support with selection. See this Simple Echo Server. You just have to replace
<http://Dispatchers.IO|Dispatchers.IO>
by
coroutineContext
. So it will be single-threaded for now, until we have the new memory model and multithreaded coroutines. However, I guess coroutines on a single thread should be enough for a simple server. I tested it on linux and it works perfectly. (If you have a windows box, I'd be glad to know whether it works there too.) Based, on that it should not be too hard to implement a simple and naive HTTP implementation.
Btw, you also have its dependency
ktor-io
, if you want to do some funky binary IO.
And
ktor-network
also offers TLS...
c
Oh I'm seeing this way late, but thank you very much for the echo test example! I will work off of that.
👍 1