git clone https://github.com/androiddevnotes/ktor-demo-app
Note: some installation instructions are for mac, for windows/linux please install accordingly.
On Terminal
createdb ktor_demo_app_db
psql ktor_demo_app_db
CREATE USER useralpha WITH PASSWORD 'passwordalpha';
GRANT ALL PRIVILEGES ON DATABASE ktor_demo_app_db TO useralpha;
\q
On Terminal
brew install --cask pgadmin4
Open PgAdmin
In the "Create - Server" dialog that appears, fill in the following information:
General tab:
- Name: Give your server a name (e.g., "Ktor Demo App")
Connection tab:
- Host name/address: localhost (if your PostgreSQL server is on the same machine)
- Port: 5432 (default PostgreSQL port)
- Maintenance database: postgres (default database)
- Username: useralpha (the user you created for your application)
- Password: passwordalpha (the password you set for useralpha)
Create a .env
file in the project root with the following content:
DATABASE_URL=jdbc:postgresql://localhost:5432/ktor_demo_app_db
DATABASE_USER=useralpha
DATABASE_PASSWORD=passwordalpha
JWT_SECRET=your_jwt_secret
JWT_ISSUER=ktor-demo-app
JWT_AUDIENCE=ktor-demo-app-users
UPLOAD_DIR=/Users/ask/PROJECTS/KMP/ktor/ktor-demo-app/uploads
Replace the values with your actual configuration.
On Terminal
./gradlew -t build
On Terminal
./gradlew run
On PgAdmin
Three dictionary entries are added automatically from dictionary_entries.json
We are using Flyway to manage the database migrations.
The migrations are in the db/migration directory.
The naming convention is V<version>__<description>.sql
.
The migrations are applied automatically when the project is run.
When you add a new column to a table, you need to add a new migration.
Create a new file in the db/migration directory with the new migration.
The content of the file should be the SQL statement to add the new column.
The filename should be the next version number.
Example:
V1__Create_initial_tables.sql
ALTER TABLE quotes ADD COLUMN tags TEXT;
To generate the OpenAPI documentation, we use IntelliJ IDEA Ultimate Ktor plugin with the "Generate OpenAPI" action.
Use the OpenAPI documentation to generate the Postman collection and environment.
Import the documentation.yaml file in Postman to generate the collection
Create new environment in Postman
environment variables:
baseUrl: http://localhost:8080
apiKey: your_api_key
bearerToken: your_bearer_token
To generate bearerToken,
register a new user:
Then login with new user: Copy the bearerToken and add it to the environment variables.
Then get API Key: Copy the API Key and add it to the environment variables.
If you get error:
{
"status": 401,
"message": "Authentication required",
"code": "UNAUTHORIZED"
}
Ensure you have added API key or Bearer Token to the request whichever is required.
- Heroku CLI installed on your machine
- A Heroku account
- Git installed on your machine
Ensure that your Procfile
is correctly configured and in the root directory of your project:
web: ./build/install/com.example.ktor-sample/bin/com.example.ktor-sample
Replace com.example.ktor-sample
with your actual project name if different.
heroku login
heroku create ktor-demo-app
Go to heroku dashboard and add addons:
Choose essential plan (cheapest one)
On terminal:
heroku config:set JWT_SECRET=your_jwt_secret
heroku config:set JWT_ISSUER=your_jwt_issuer
heroku config:set JWT_AUDIENCE=your_jwt_audience
heroku config:set UPLOAD_DIR=/app/uploads
git add .
git commit -m "Prepare for Heroku deployment"
git push heroku main
To deploy again to live ... after making changes. Add, commit, push to heroku:
Your API is now live!
Go to your heroku url for example:
https://ktor-demo-app-56ada9af7cac.herokuapp.com/api/v1/quotes
The quotes routes currently are not protected by authentication. The dictionary routes are protected by API Key authentication.
To automatically deploy to heroku from git, go to heroku dashboard, click on the app, go to "Deploy" tab, click on "Connect to GitHub", and connect to your repository.
heroku logs --tail --app ktor-demo-app
This is a Ktor-based API application for managing quotes and a dictionary. The application provides CRUD operations for quotes and dictionary entries, user authentication, and API key management.
These are few. You can use Postman to test the other routes.
Here are some of the main API routes:
- User Registration: POST /api/v1/register
- User Login: POST /api/v1/login
- Generate API Key: POST /api/v1/api-key
- Get Quotes: GET /api/v1/quotes
- Create Quote: POST /api/v1/quotes
- Get Dictionary Entries: GET /api/v1/dictionary
- Create Dictionary Entry: POST /api/v1/dictionary
- Search Quotes: GET /api/v1/quotes/search
- Search Dictionary: GET /api/v1/dictionary/search
- Backend Framework: Ktor
- Database: PostgreSQL
- ORM: Exposed
- Authentication: JWT and API Keys
- Database Migration: Flyway
- Serialization: Kotlinx Serialization
- Logging: SLF4J with Logback
- Make changes to the code
- Build the project: ./gradlew -t build
- Run the application: ./gradlew run
- For database schema changes, create a new migration in src/main/resources/db/migration/ (as discussed above with flyway)
- API documentation is generated using the IntelliJ IDEA Ultimate Ktor plugin with the "Generate OpenAPI" action.
- The OpenAPI documentation can be found at src/main/resources/openapi/documentation.yaml.
- Swagger UI is available at /swagger when the application is running.
If you encounter any issues during setup or running the application, please check the following:
- Ensure all environment variables are correctly set in the .env file
- Check the application logs for any error messages
- Verify that the database is running and accessible
- Make sure you have the correct permissions for the upload directory
- Ktor Documentation: https://ktor.io/docs/
- Exposed Documentation: https://github.com/JetBrains/Exposed
- Flyway Documentation: https://flywaydb.org/documentation/
- PostgreSQL Documentation: https://www.postgresql.org/docs/
Done