Skip to content

Commit 1d9190f

Browse files
committed
feat: add static resources
1 parent 9d4b088 commit 1d9190f

File tree

11 files changed

+143
-2
lines changed

11 files changed

+143
-2
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ embeddedServer(Netty, PORT, watchPaths = emptyList()) {
2929
}
3030
}
3131
```
32+
3233
## 🚀 How to use
3334

3435
Cloning the repository into a local directory and checkout the desired branch:
@@ -38,8 +39,20 @@ git clone [email protected]:nphausg/android.embeddedserver.git
3839
cd android.embeddedserver
3940
git checkout master
4041
```
41-
## 🍲 Screenshots
4242

43+
## 🍲 Static resource
44+
45+
Config | Demo |
46+
--- | --- |
47+
<img src="docs/static_config.png"> | <img src="docs/static_demo.png"> |
48+
49+
```kotlin
50+
staticResources("/static", ""){
51+
default("index.html")
52+
}
53+
```
54+
55+
## 🍲 Screenshots
4356

4457
<h4 align="center">
4558

@@ -52,6 +65,7 @@ Device | Connect |
5265
<img src="docs/demo.gif"> | <img src="docs/edge_get.gif"> |
5366

5467
## ✨ Contributing
68+
5569
Please feel free to contact me or make a pull request.
5670

5771
## 👀 Author

app/src/main/java/com/nphausg/app/embeddedserver/EmbeddedServer.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,38 @@ import com.nphausg.app.embeddedserver.data.BaseResponse
1111
import com.nphausg.app.embeddedserver.data.Database
1212
import com.nphausg.app.embeddedserver.data.models.Cart
1313
import com.nphausg.app.embeddedserver.utils.NetworkUtils
14+
import io.ktor.http.ContentDisposition
15+
import io.ktor.http.ContentDisposition.Companion.File
1416
import io.ktor.http.ContentType
17+
import io.ktor.http.HttpHeaders
1518
import io.ktor.http.HttpStatusCode
19+
import io.ktor.http.HttpStatusCode.Companion.PartialContent
1620
import io.ktor.serialization.kotlinx.json.json
1721
import io.ktor.server.application.ApplicationCall
1822
import io.ktor.server.application.call
1923
import io.ktor.server.application.install
2024
import io.ktor.server.engine.embeddedServer
25+
import io.ktor.server.http.content.defaultResource
26+
import io.ktor.server.http.content.resource
27+
import io.ktor.server.http.content.resources
28+
import io.ktor.server.http.content.static
29+
import io.ktor.server.http.content.staticFiles
30+
import io.ktor.server.http.content.staticResources
2131
import kotlinx.serialization.json.Json
2232
import kotlinx.serialization.encodeToString
2333
import io.ktor.server.netty.Netty
2434
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
2535
import io.ktor.server.plugins.cors.routing.CORS
36+
import io.ktor.server.response.header
2637
import io.ktor.server.response.respond
38+
import io.ktor.server.response.respondFile
2739
import io.ktor.server.response.respondText
2840
import io.ktor.server.routing.get
2941
import io.ktor.server.routing.routing
3042
import kotlinx.coroutines.CoroutineScope
3143
import kotlinx.coroutines.Dispatchers
3244
import kotlinx.coroutines.launch
45+
import java.io.File
3346

3447
object EmbeddedServer {
3548

@@ -50,6 +63,10 @@ object EmbeddedServer {
5063
})
5164
}
5265
routing {
66+
// staticResources
67+
staticResources("/static", ""){
68+
default("index.html")
69+
}
5370
get("/") {
5471
okText(call, "Hello!! You are here in ${Build.MODEL}")
5572
}
@@ -67,6 +84,18 @@ object EmbeddedServer {
6784
call.respond(HttpStatusCode.NotFound)
6885
}
6986
}
87+
get("/download") {
88+
val file = File("files/ktor_logo.png")
89+
call.response.header(
90+
HttpHeaders.ContentDisposition,
91+
ContentDisposition.Attachment.withParameter(
92+
ContentDisposition.Parameters.FileName,
93+
"ktor_logo.png"
94+
).toString()
95+
)
96+
call.response.status(HttpStatusCode.OK)
97+
call.respondFile(file)
98+
}
7099
}
71100
}
72101
}

app/src/main/java/com/nphausg/app/embeddedserver/activities/MainActivity.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private fun Logo() {
107107

108108
Card(
109109
modifier = Modifier
110-
.size(96.dp)
110+
.size(128.dp)
111111
.offset {
112112
IntOffset(
113113
offsetX.value.toInt(),
@@ -230,6 +230,17 @@ private fun MainScreen() {
230230
style = MaterialTheme.typography.titleMedium,
231231
)
232232
}
233+
234+
Row {
235+
Icon(imageVector = ImsIcons.PlayArrow, contentDescription = null)
236+
Text(
237+
color = Color.Black,
238+
textAlign = TextAlign.Start,
239+
text = String.format("STATIC: %s/static", EmbeddedServer.host),
240+
style = MaterialTheme.typography.titleMedium,
241+
)
242+
}
243+
233244
}
234245

235246
Row(
@@ -250,6 +261,7 @@ private fun MainScreen() {
250261
enabled = hasStarted,
251262
modifier = Modifier.weight(1f),
252263
onClick = {
264+
ticks = 0
253265
hasStarted = false
254266
EmbeddedServer.stop()
255267
},

app/src/main/resources/docs/demo.gif

4.08 MB
Loading
68.4 KB
Loading
589 KB
Loading
151 KB
Loading

app/src/main/resources/favicon.ico

5.12 KB
Binary file not shown.

app/src/main/resources/index.html

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<!-- Required meta tags -->
6+
<meta charset="utf-8">
7+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
8+
9+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
10+
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
11+
12+
<link rel="icon" href="./favicon.ico" type="image/x-icon">
13+
<title>Android Embedded Server</title>
14+
</head>
15+
16+
<body style="padding: 96px 0px 96px 0px;">
17+
18+
<h1 align="center"> Android Embedded Server </h1>
19+
20+
<div align="center">
21+
<img src="https://img.shields.io/badge/kotlin-v1.9.23-blue.svg">
22+
<img src="https://img.shields.io/badge/gradle-8.3.2-blueviolet.svg">
23+
<img src="https://img.shields.io/badge/API-21%2B-blue.svg?style=flat">
24+
<img src="https://img.shields.io/badge/License-Apache%202.0-success.svg">
25+
<img src="https://circleci.com/gh/twilio-labs/plugin-rtc.svg?style=svg">
26+
<a href="https://github.com/nphau/android.embeddedserver/actions/workflows/app-build.yml"><img
27+
alt="Build Status"
28+
src="https://github.com/nphau/android.embeddedserver/actions/workflows/app-build.yml/badge.svg" /></a>
29+
</div>
30+
31+
<p>
32+
<img src="https://raw.githubusercontent.com/andreasbm/readme/master/assets/lines/colored.png" style="width: 100%;"
33+
alt="-----------------------------------------------------">
34+
</p>
35+
36+
<div style="padding: 0px 16px 0px 16px;">
37+
38+
<h2 id="-overview">👉 Overview</h2>
39+
<p>A minimal way to create HTTP server in android with Kotlin. Create asynchronous client and server
40+
applications.
41+
Anything from microservices to multiplatform HTTP client apps in a simple way. Open Source, free, and fun!
42+
</p>
43+
<pre><code class="lang-kotlin"><span class="hljs-function"><span class="hljs-title">embeddedServer</span><span class="hljs-params">(Netty, PORT, watchPaths = emptyList()</span></span>) {
44+
install(WebSockets)
45+
install(CallLogging)
46+
routing {
47+
get(<span class="hljs-string">"/"</span>) {
48+
call.respondText(
49+
text = <span class="hljs-string">"Hello!! You are here in ${Build.MODEL}"</span>,
50+
contentType = ContentType<span class="hljs-selector-class">.Text</span><span class="hljs-selector-class">.Plain</span>
51+
)
52+
}
53+
}
54+
}
55+
</code></pre>
56+
<h2 id="-how-to-use">🚀 How to use</h2>
57+
<p>Cloning the repository into a local directory and checkout the desired branch:</p>
58+
<pre><code>git clone git@github<span class="hljs-selector-class">.com</span>:nphausg/android<span class="hljs-selector-class">.embeddedserver</span><span class="hljs-selector-class">.git</span>
59+
cd android<span class="hljs-selector-class">.embeddedserver</span>
60+
git checkout master
61+
</code></pre>
62+
<h2 id="-contributing">✨ Contributing</h2>
63+
<p>Please feel free to contact me or make a pull request.</p>
64+
<h2 id="-author">👀 Author</h2>
65+
<p>
66+
<a href="https://nphausg.medium.com" target="_blank">
67+
<img src="https://avatars2.githubusercontent.com/u/13111806?s=400&u=f09b6160dbbe2b7eeae0aeb0ab4efac0caad57d7&v=4"
68+
width="96" height="96">
69+
</a>
70+
<a href="https://github.com/hieuwu" target="_blank">
71+
<img src="https://avatars.githubusercontent.com/u/43868345?v=4" width="96" height="96">
72+
</a>
73+
</p>
74+
</div>
75+
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
76+
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
77+
crossorigin="anonymous"></script>
78+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"
79+
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
80+
crossorigin="anonymous"></script>
81+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"
82+
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
83+
crossorigin="anonymous"></script>
84+
</body>
85+
86+
</html>

docs/static_config.png

48.7 KB
Loading

docs/static_demo.png

251 KB
Loading

0 commit comments

Comments
 (0)