Sveletor is a starter project for SvelteKit multi-page applications that are served via a Ktor Backend.
Sveletor is in V 0.0.1, as in it barely works without some duct tape, but it is worth it to be able to use an actually good UI framework w/Ktor.
The application is already configured, just clone this repository and the gradle scripts should be able to run and configure the necessary packages itself.
If you'd like to clone a blank version of this project, with just the home page and the widgets, clone this branch:
build.gradle.kts relies on npmBuild.cmd which is literally just:
cd .\src\web\
call npm i
call npm run build
So when you rerun the server, the frontend is also recompiled.
src/jvmMain is source code for the Ktor server, where com.sveletor.application.Server.kt is the entrypoint for the application.
src/web contains the svelte app. When the application is built, the static adapter writes the contents of the Svelte App's build output to /src/jvmMain/resources/web.
Use the prebuilt sveltePage
application extension function to serve a static svelte page. Supplying the endpoint will tell also automatically find the file in the server's resource folder.
Because Svelte's Static Router is set up for file based routing, you should adopt the same file pattern for your endpoints.
For Example:
If your svelte application's routes directory looks like this:
routes
|_ +page.svelte
|_ foo
|_ +page.svelte
|_ bar
|_ +page.svelte
You'd serve those three pages like so:
sveltePage("/")
sveltePage("/foo")
sveltePage("/foo/bar")
And the function would automatically associate the endpoints to the routes:
"/" becomes index.html
"/foo" becomes foo.html
"/foo/bar" becomes foo/bar.html
This is family simple in Ktor, for any page you'd like to lock behind a login, simply replace a call to sveltePage
with a call to authenticatedSveltePage
This will preform a validation check on the SveletorSession
object associated w/ the application call, which, if invalid, will redirect a user to a login screen.
In the case that you'd like to preform further validation before a page is served to a client, there is a preResponse
parameter in the sveltePage
function which allows you to write a function to preform additional validation and then potentially redirect if some conditions aren't met
onMount
lifecycle hook./session
route is reserved for this.