Skip to content

Commit a143f9f

Browse files
author
nphau
committed
feat: add minimal embedded server with kotlin
0 parents  commit a143f9f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1012
-0
lines changed

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties

.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/compiler.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.project

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>ims.android.embeddedserver</name>
4+
<comment>Project ims.android.embeddedserver created by Buildship.</comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
16+
</natures>
17+
<filteredResources>
18+
<filter>
19+
<id>1634836034610</id>
20+
<name></name>
21+
<type>30</type>
22+
<matcher>
23+
<id>org.eclipse.core.resources.regexFilterMatcher</id>
24+
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
25+
</matcher>
26+
</filter>
27+
</filteredResources>
28+
</projectDescription>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
arguments=
2+
auto.sync=false
3+
build.scans.enabled=false
4+
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
5+
connection.project.dir=
6+
eclipse.preferences.version=1
7+
gradle.user.home=
8+
java.home=/Library/Java/JavaVirtualMachines/jdk-15.0.1.jdk/Contents/Home
9+
jvm.arguments=
10+
offline.mode=false
11+
override.workspace.settings=true
12+
show.console.view=true
13+
show.executions.view=true

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<h1 align="center"> IMStudio </h1>
2+
3+
<p align="center">
4+
<a>
5+
<img src="https://img.shields.io/badge/kotlin-v1.5.31-blue.svg">
6+
<img src="https://img.shields.io/badge/gradle-7.0.2-blueviolet.svg">
7+
<img src="https://img.shields.io/badge/API-21%2B-blue.svg?style=flat">
8+
<img src="https://img.shields.io/badge/License-Apache%202.0-success.svg">
9+
<img src="https://circleci.com/gh/twilio-labs/plugin-rtc.svg?style=svg">
10+
</a>
11+
<br/>
12+
<br/>
13+
<a href="https://imstudio.medium.com/" target="_blank">
14+
<img src="logo.png" width="128" margin="8">
15+
</a>
16+
</p>
17+
18+
[![-----------------------------------------------------](https://raw.githubusercontent.com/andreasbm/readme/master/assets/lines/colored.png)](#table-of-contents)
19+
20+
## 🚀Authors
21+
22+
- https://github.com/nphau
23+
- https://medium.com/@imstudio

app/.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-15/"/>
4+
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
5+
<classpathentry kind="output" path="bin/default"/>
6+
</classpath>

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/.project

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>app</name>
4+
<comment>Project app created by Buildship.</comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
22+
</natures>
23+
<filteredResources>
24+
<filter>
25+
<id>1634836034605</id>
26+
<name></name>
27+
<type>30</type>
28+
<matcher>
29+
<id>org.eclipse.core.resources.regexFilterMatcher</id>
30+
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
31+
</matcher>
32+
</filter>
33+
</filteredResources>
34+
</projectDescription>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
connection.project.dir=..
2+
eclipse.preferences.version=1

app/build.gradle

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
plugins {
2+
id 'com.android.application'
3+
id 'kotlin-android'
4+
}
5+
6+
android {
7+
compileSdk 31
8+
9+
defaultConfig {
10+
applicationId "com.imstudio.app.embeddedserver"
11+
minSdk 21
12+
targetSdk 31
13+
versionCode 1
14+
versionName "1.0"
15+
16+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17+
}
18+
19+
buildTypes {
20+
release {
21+
minifyEnabled false
22+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23+
}
24+
}
25+
compileOptions {
26+
sourceCompatibility JavaVersion.VERSION_1_8
27+
targetCompatibility JavaVersion.VERSION_1_8
28+
}
29+
kotlinOptions {
30+
jvmTarget = '1.8'
31+
}
32+
packagingOptions {
33+
exclude 'META-INF/*'
34+
// Due to https://github.com/Kotlin/kotlinx.coroutines/issues/2023
35+
exclude 'META-INF/licenses/*'
36+
exclude '**/attach_hotspot_windows.dll'
37+
}
38+
}
39+
40+
dependencies {
41+
42+
implementation 'androidx.core:core-ktx:1.6.0'
43+
implementation 'androidx.appcompat:appcompat:1.3.1'
44+
implementation 'com.google.android.material:material:1.4.0'
45+
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
46+
testImplementation 'junit:junit:4.+'
47+
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
48+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
49+
50+
// Coroutines
51+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2"
52+
53+
// Embedded Server
54+
implementation 'io.ktor:ktor-server-core:1.6.2'
55+
implementation 'io.ktor:ktor-server-netty:1.6.2'
56+
implementation 'io.ktor:ktor-websockets:1.6.2'
57+
}

app/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.imstudio.app.embeddedserver
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.imstudio.app.embeddedserver", appContext.packageName)
23+
}
24+
}

app/src/main/AndroidManifest.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.imstudio.app.embeddedserver">
4+
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
7+
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
8+
9+
10+
<application
11+
android:allowBackup="true"
12+
android:icon="@mipmap/ic_launcher"
13+
android:label="@string/app_name"
14+
android:roundIcon="@mipmap/ic_launcher"
15+
android:supportsRtl="true"
16+
android:theme="@style/Theme.MyApplication">
17+
<activity
18+
android:name=".MainActivity"
19+
android:exported="true">
20+
<intent-filter>
21+
<action android:name="android.intent.action.MAIN" />
22+
23+
<category android:name="android.intent.category.LAUNCHER" />
24+
</intent-filter>
25+
</activity>
26+
</application>
27+
28+
</manifest>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.imstudio.app.embeddedserver
2+
3+
import android.os.Build
4+
import androidx.appcompat.app.AppCompatActivity
5+
import android.os.Bundle
6+
import android.widget.TextView
7+
import io.ktor.application.*
8+
import io.ktor.features.*
9+
import io.ktor.http.*
10+
import io.ktor.response.*
11+
import io.ktor.routing.*
12+
import io.ktor.server.engine.*
13+
import io.ktor.server.netty.*
14+
import io.ktor.websocket.*
15+
import kotlinx.coroutines.CoroutineScope
16+
import kotlinx.coroutines.Dispatchers
17+
import kotlinx.coroutines.launch
18+
19+
class MainActivity : AppCompatActivity() {
20+
21+
companion object {
22+
private const val PORT = 5001
23+
}
24+
25+
private val server by lazy {
26+
embeddedServer(Netty, PORT, watchPaths = emptyList()) {
27+
install(WebSockets)
28+
install(CallLogging)
29+
routing {
30+
get("/") {
31+
call.respondText(
32+
text = "Hello!! You are here in ${Build.MODEL}",
33+
contentType = ContentType.Text.Plain
34+
)
35+
}
36+
}
37+
}
38+
}
39+
40+
override fun onCreate(savedInstanceState: Bundle?) {
41+
super.onCreate(savedInstanceState)
42+
setContentView(R.layout.activity_main)
43+
44+
// Start server
45+
// Show IP Address
46+
findViewById<TextView>(R.id.text_status).text =
47+
String.format("%s:%d running ... ", NetworkUtils.getLocalIpAddress(), PORT)
48+
49+
CoroutineScope(Dispatchers.IO).launch {
50+
server.start(wait = true)
51+
}
52+
}
53+
54+
override fun onDestroy() {
55+
server.stop(1_000, 2_000)
56+
super.onDestroy()
57+
}
58+
59+
}

0 commit comments

Comments
 (0)