Where to import Ktor Plugin "install" from - ktor

I just created a new Ktor project, but following the docs, I am unsure where to import Plugin "install" function.
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.netty.handler.codec.DefaultHeaders
fun main() {
embeddedServer(Netty, port = 8080, host = "0.0.0.0") {
install(DefaultHeaders)
install(CallLogging)
install(Koin) {
slf4jLogger()
modules(helloAppModule)
}
configurePlugins()
configureRouting()
}.start(wait = true)
}
My IDE has several suggestions, none of which seem to resolve correctly. I am using Ktor version 2.0.0.

The problem is that you have incorrect Default headers import.
Make sure you have it as dependency:
implementation("io.ktor:ktor-server-default-headers:$ktor_version")
Replace
import io.netty.handler.codec.DefaultHeaders
with
import io.ktor.server.plugins.defaultheaders.*
IDE should suggest you the following import for install:
import io.ktor.server.application.*

Related

Unresolved reference for Launch in CoroutineScope(IO).Launch

I've read the following, it doesn't answer my question.
Unresolved reference: launch
I'm trying to follow the following tutorial https://www.youtube.com/watch?v=z7lfPYLGE7k for creating a simple socket in Kotlin. Everything is trivial up to this point:
I have:
CoroutineScope(IO).Launch {
client(address, port)
}
However, this gives the error:
Unresolved reference: Launch
It is unclear to me what I should put in my dependencies in build.gradle, I've tried a slew:
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:x.x.x"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:x.x.x"
implementation("androidx.collection:collection-ktx:1.2.0")
implementation("androidx.fragment:fragment-ktx:1.5.4")
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.5.1"
implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.5.1")
implementation("androidx.navigation:navigation-runtime-ktx:2.5.3")
implementation("androidx.navigation:navigation-fragment-ktx:2.5.3")
implementation("androidx.navigation:navigation-ui-ktx:2.5.3")
implementation("androidx.palette:palette-ktx:1.0.0")
implementation("androidx.lifecycle:lifecycle-reactivestreams-ktx:2.5.1")
implementation("androidx.room:room-ktx:2.4.3")
implementation("androidx.sqlite:sqlite-ktx:2.2.0")
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1")
implementation("androidx.work:work-runtime-ktx:2.7.1")
implementation("com.google.android.play:core-ktx:1.8.1")
I also don't understand what I should import, again I've tried probably more than necessary:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.system.Os.socket
import java.net.Socket
import java.util.*
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import androidx.activity.viewModels
Failing this, I've also tried the following to see if I can call the client() function from these:
viewModelScope.launch {
}
viewLifecycleOwner.lifecycleScope.launch {
}
lifecycleScope.launch {
}
However, they have the same problem.
The method you're looking for is:
CoroutineScope(Dispatchers.IO).launch {
...
}
Without the capitalization in the method name.

IntelliJ missing reference to slf4j in new Ktor project

I started a new Ktor project through their IntelliJ plugin and everything is compiling and running fine with Gradle. However in IntelliJ everything that references the slf4j logger is having a Unresolved reference: ... error.
My import and setting log leve looks like this (sorry my reputation is not high enough to post images):
The project is generated with the following dependencies
// gradle.properties
ktor_version=1.4.0
logback_version=1.2.1
// build.gradle.kts
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version")
implementation("io.ktor:ktor-server-netty:$ktor_version")
implementation("ch.qos.logback:logback-classic:$logback_version")
implementation("io.ktor:ktor-server-core:$ktor_version")
implementation("io.ktor:ktor-auth:$ktor_version")
implementation("io.ktor:ktor-auth-jwt:$ktor_version")
implementation("io.ktor:ktor-jackson:$ktor_version")
testImplementation("io.ktor:ktor-server-tests:$ktor_version")
}
I have tried to add org.slf4j:slf4j-nop:1.7.30 to my dependencies without any luck.
So my questions is, has anyone seen this error before and know if there is a dependency or setting i could change? It is annoying to not have any code completion and errors scattered around the IDE even though it compiles and run fine.
The full application code generated with the Ktor plugin is below if needed:
package com.example
import io.ktor.application.*
import io.ktor.response.*
import io.ktor.request.*
import io.ktor.features.*
import org.slf4j.event.*
import io.ktor.routing.*
import io.ktor.http.*
import io.ktor.auth.*
import com.fasterxml.jackson.databind.*
import io.ktor.jackson.*
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
#Suppress("unused") // Referenced in application.conf
#kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {
install(CallLogging) {
level = Level.INFO
filter { call -> call.request.path().startsWith("/") }
}
install(Authentication) {
}
install(ContentNegotiation) {
jackson {
enable(SerializationFeature.INDENT_OUTPUT)
}
}
routing {
get("/") {
call.respondText("HELLO WORLD!", contentType = ContentType.Text.Plain)
}
get("/json/jackson") {
call.respond(mapOf("hello" to "world"))
}
}
}
This was a caching issue with Gradle and IntelliJ, probably caused from other project dependencies. To IntelliJ the slf4j-api library was empty. Removing the global Gradle cache with rm -rf ~/.gradle/caches and re-download the dependencies solved the issue for me.

Module not found: Error: Can't resolve 'react-native/Libraries/Image/resolveAssetSource'

I'm adding an audio module in my current react-native project. I have tried installing several modules (react-native-sound, react-native-track-player). Getting in both modules the same Error output, which is always pointing in the 'react-native/Libraries/Image/resolveAssetsource' as module not found.
ERROR in ./node_modules/react-native-track-player/lib/index.js
Module not found: Error: Can't resolve 'react-native/Libraries/Image/resolveAssetsource' in 'D:\workspaces\web\react\blink\node_modules\react-native-track-player\lib'
# ./node_modules/react-native-track-player/lib/index.js 1:401-459
# ./node_modules/react-native-track-player/index.js
# ./components/ui/BlinkAudio/BlinkAudio.web.js
# ./components/ui/BlinkAudio/index.web.js
# ./components/dialogs/ResourceDetails/ResourceDetails.js
# ./components/dialogs/ResourceDetails/index.js
# ./components/panels/catalog/CatalogPanel.js
# ./components/parts/Main/Main.js
# ./components/parts/Main/index.js
# ./index.web.js
This is the current imports in the index file of the audio module react-native-track-player:
import { Platform, AppRegistry, DeviceEventEmitter, NativeEventEmitter, NativeModules } from 'react-native';
import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetsource';
I have tried to fix this including 'resolveAssetsource' from the imports of 'react-native' as below:
import {
Platform,
AppRegistry,
DeviceEventEmitter,
NativeEventEmitter,
NativeModules,
resolveAssetsource
} from 'react-native';
But I am not pretty sure if it would be the best way and normally I don't like to touch package node-modules directly.
I also tried to exclude the audio module from webpack loaders with no result.
module: {
loaders: [
{
test: /\.js?$/,
exclude: function (modulePath) {
return (
/node_modules/.test(modulePath) &&
!/node_modules(\\|\/)react-native-track-player/.test(modulePath)
);
},
I wonder if someone could help me to find an answer and if is possible to deal with this react-native issue, as I'm thinking that these audio modules are calling wrongly the resolveAssetsource, or in the other hand, react-native doesn't provide this specific Library for third parties, I don't know.
This seems to be a syntax error issue. There is no file nor module named resolveAssetsource, the module and filename is resolveAssetSource with a capital S.
Try to remove react-native-track-player with yarn remove react-native-track-player and install it again (or delete the entire node_modules directory and run yarn install again) as the source code under react-native-track-player/lib/index.js has it properly without any errors:
import { Platform, AppRegistry, DeviceEventEmitter, NativeEventEmitter, NativeModules } from 'react-native';
import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetSource';
If you use it elsewhere, you'll have to import it like this:
import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetSource';
But that is not the prefered way to do it, as this method is a member of the RN Image class/component Image.resolveAssetSource(source), so, it's better to just import the Image component and use it like this:
import {
Platform,
AppRegistry,
DeviceEventEmitter,
NativeEventEmitter,
NativeModules,
Image // Add Image import
} from 'react-native';
// and use the function as of Image method
let audioAssetSource = Image.resolveAssetSource(audioSource);
Either way, you'll have the method working and resolve the asset source information.

Type 'SQLiteOriginal' is not assignable to type 'Provider'

I'm new to Ionic 4 and am trying to get SQLite working. I have added the cordova plugin and the ionic native sqlite but when I try and set it up in the app module I get the above error. Here is my app,module.ts
import { IonicModule, IonicRouteStrategy } from '#ionic/angular';
import { SplashScreen } from '#ionic-native/splash-screen/ngx';
import { StatusBar } from '#ionic-native/status-bar/ngx';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { IonicStorageModule } from '#ionic/storage';
import { SQLite } from '#ionic-native/sqlite';
#NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [BrowserModule, IonicModule.forRoot(), IonicStorageModule.forRoot(), AppRoutingModule],
providers: [
StatusBar,
SplashScreen,
SQLite,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
],
bootstrap: [AppComponent]
})
export class AppModule {}
Can anyone help?
You have to import from '#ionic-native/sqlite/ngx'
https://ionicframework.com/docs/native
It looks like this has to do with the recent release of Ionic 4. I ran into this issue with #ionic-native/file within my app. If you installed after the release without specifying the version you wanted, you probably got the latest or beta version.
I was able to fix it by rolling back to an earlier version by modifying my package.json to reflect the version required -- I had 5.0.0 installed and rolled back to 4.20.0. If you have VersionLens for VSCode it will show you the minimum and the latest versions.
Then, clear your node modules, and reinstall:
rm -rf node_modules/
npm install
There is another post open this, available here:
Type HTTPOriginal is not assignable to type Provider, ionic error after plugin installation
It happens because of the new update of ionic 4.
You have to add /ngx to your plugin's import. Like this:
import { PluginName} from '#ionic-native/pluginName/ngx';
Otherwise fallback to ionic v4.
More info here
Everywhere where native plugins are imported, you need to add /ngx/.
Moreover, this must be done throughout the project, otherwise the error will still appear.
Error example:
import {Market} from '#ionic-native/market';
True example:
import {Market} from '#ionic-native/market/ngx';
Import:
import { SQLitePorter } from '#ionic-native/sqlite-porter/ngx';
import { SQLite } from '#ionic-native/sqlite/ngx';
And add into provider:
providers: [
SQLite,
SQLitePorter
]
This usually happens if you import them from different path.
You have to import '#ionic-native/sqlite/ngx' at both app.module.ts and the page you want to use it.

Vue-FontAwesome: How to import all Free and Pro Icons? Duplicate declaration error

I own a FontAwesome Pro License and I use the Vue-FontAwesome Component.
When I try to import all icons from both the free and Pro repo I get an "Duplicate declaration error ..." and if I change the declaration name it can't be found anymore.
import { library } from '#fortawesome/fontawesome-svg-core'
import { fab } from '#fortawesome/free-brands-svg-icons'
import { fas } from '#fortawesome/pro-solid-svg-icons'
import { far } from '#fortawesome/pro-regular-svg-icons'
import { fal } from '#fortawesome/pro-light-svg-icons'
import { fas } from '#fortawesome/free-solid-svg-icons'
import { far } from '#fortawesome/free-regular-svg-icons'
library.add(fab)
library.add(fas)
library.add(far)
library.add(fal)
How do I import all icons from Free and Pro?
I tried to add Fontawesome pro icons to Vuetify(1.5.x). I found a solition. I share this solution with you.
First of all, in your project directory, create a file called .npmrc and add this to it:
#fortawesome:registry=https://npm.fontawesome.com/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX,
install fontawesome-pro package with npm or yarn
npm install --save-dev #fortawesome/fontawesome-pro
or
yarn add --dev #fortawesome/fontawesome-pro
Add this line in app.js or main.js
import '#fortawesome/fontawesome-pro/css/all.css'
Install pro icon packages
npm i --save #fortawesome/pro-solid-svg-icons
npm i --save #fortawesome/pro-regular-svg-icons
npm i --save #fortawesome/pro-light-svg-icons
You can use with pro icons with prefix (fas solid, far regular fal light. That's all you can use pro icons. I hope work with your project. Good Luck.
You're in effect importing multiple icon packs into to the same var for both far and fas, hence the error "Duplicate declaration".
As stated in comments, if you have FontAwesome Pro, that includes everything in FontAwesome Free. Import the pro-packages you need, and forget about the free edition.
That being said, importing the entire thing isn't ideal. If you're using a bundle manager with tree shaking (i.e webpack), it will save your application's weight impact tenfold. You rarely need all of the 5k icons.
Continuing down that road, that is; not importing the entire package: You can import different icon versions by importing and casting them. Like so:
import { library } from '#fortawesome/fontawesome-svg-core'
import { faCoffee as fasCoffee } from '#fortawesome/pro-solid-svg-icons'
import { faCoffee as farCoffee } from '#fortawesome/pro-regular-svg-icons'
import { faCoffee as falCoffee } from '#fortawesome/pro-light-svg-icons'
library.add(fasCoffee, farCoffee, falCoffee)
More on all of this in the official docs for FA-Pro.