-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick-start.html
604 lines (566 loc) · 29.6 KB
/
quick-start.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="description" content="manual">
<meta name="generator" content="Paradox, paradox-material-theme=0.6.0, mkdocs-material=3.0.3">
<meta name="lang:clipboard.copy" content="Copy to clipboard">
<meta name="lang:clipboard.copied" content="Copied to clipboard">
<meta name="lang:search.language" content="">
<meta name="lang:search.pipeline.stopwords" content="true">
<meta name="lang:search.pipeline.trimmer" content="true">
<meta name="lang:search.result.none" content="No matching documents">
<meta name="lang:search.result.one" content="1 matching document">
<meta name="lang:search.result.other" content="# matching documents">
<meta name="lang:search.tokenizer" content="[\s\-]+">
<meta name="description" content="manual">
<link rel="shortcut icon" href="assets/images/favicon.png">
<title>Quick start · endpoints4s</title>
<link rel="stylesheet" href="assets/stylesheets/application.451f80e5.css">
<link rel="stylesheet" href="lib/material__tabs/dist/mdc.tabs.min.css">
<link rel="stylesheet" href="lib/prettify/prettify.css">
<script src="assets/javascripts/modernizr.1aa3b519.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,400i,700|Roboto+Mono">
<style>
body,input{font-family:"Roboto","Helvetica Neue",Helvetica,Arial,sans-serif}
code,kbd,pre{font-family:"Roboto Mono","Courier New",Courier,monospace}
</style>
<link rel="stylesheet" href="assets/fonts/font-awesome.css">
<link rel="stylesheet" href="assets/fonts/material-icons.css">
<link rel="stylesheet" href="assets/stylesheets/paradox-material-theme.css">
<link rel="stylesheet" href="snippets.css">
</head>
<body
>
<input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
<input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
<label class="md-overlay" data-md-component="overlay" for="__drawer"></label>
<header class="md-header" data-md-component="header">
<nav class="md-header-nav md-grid">
<div class="md-flex">
<div class="md-flex__cell md-flex__cell--shrink">
<a href="index.html" title="endpoints4s" class="md-header-nav__button md-logo">
<i class="md-icon">local_library</i>
</a>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<label class="md-icon md-icon--menu md-header-nav__button" for="__drawer"></label>
</div>
<div class="md-flex__cell md-flex__cell--stretch">
<div class="md-flex__ellipsis md-header-nav__title" data-md-component="title">
<span class="md-header-nav__topic">
endpoints4s
</span>
<span class="md-header-nav__topic">
Quick start
</span>
</div>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<label class="md-icon md-icon--search md-header-nav__button" for="__search"></label>
<div class="md-search" data-md-component="search" role="dialog">
<label class="md-search__overlay" for="__search"></label>
<div class="md-search__inner" role="search">
<form class="md-search__form" name="search">
<input type="text" class="md-search__input" name="query" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="query" data-md-state="active">
<label class="md-icon md-search__icon" for="__search"></label>
<button type="reset" class="md-icon md-search__icon" data-md-component="reset" tabindex="-1"></button>
</form>
<div class="md-search__output">
<div class="md-search__scrollwrap" data-md-scrollfix>
<div class="md-search-result" data-md-component="result">
<div class="md-search-result__meta">
Type to start searching
</div>
<ol class="md-search-result__list"></ol>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<div class="md-header-nav__source">
<a href="https://github.com/endpoints4s/endpoints4s"
title="Go to repository"
class="md-source"
data-md-source="github">
<div class="md-source__icon">
<i class="fa fa-github"></i>
</div>
<div class="md-source__repository">
endpoints4s/endpoints4s
</div>
</a>
</div>
</div>
</div>
</nav>
</header>
<div class="md-container">
<main class="md-main">
<div class="md-main__inner md-grid" data-md-component="container">
<div class="md-sidebar md-sidebar--primary" data-md-component="navigation">
<div class="md-sidebar__scrollwrap">
<div class="md-sidebar__inner">
<nav class="md-nav md-nav--primary" data-md-level="0" style="visibility: hidden">
<label class="md-nav__title md-nav__title--site" for="drawer">
<a href="index.html" title="endpoints4s" class="md-nav__button md-logo">
<span class="md-nav__button md-logo">
<i class="md-icon">local_library</i>
</a>
<a href="index.html" title="endpoints4s">
endpoints4s
</a>
</label>
<div class="md-nav__source">
<a href="https://github.com/endpoints4s/endpoints4s"
title="Go to repository"
class="md-source"
data-md-source="github">
<div class="md-source__icon">
<i class="fa fa-github"></i>
</div>
<div class="md-source__repository">
endpoints4s/endpoints4s
</div>
</a>
</div>
<ul>
<li><a href="use-cases.html" class="page">Use Cases</a></li>
<li><a href="quick-start.html" class="active page">Quick start</a></li>
<li><a href="design.html" class="page">Design in a nutshell</a></li>
<li><a href="algebras-and-interpreters.html" class="page">Algebras and interpreters</a>
<ul>
<li><a href="algebras/endpoints.html" class="page"><code>Endpoints</code></a></li>
<li><a href="algebras/json-entities.html" class="page">JSON Entities</a></li>
<li><a href="algebras/json-schemas.html" class="page">JSON Schemas</a></li>
<li><a href="algebras/chunked-entities.html" class="page">Chunked Entities</a></li>
<li><a href="algebras/middlewares.html" class="page">Middlewares</a></li>
<li><a href="algebras/assets.html" class="page">Assets</a></li>
<li><a href="algebras/mux-endpoints.html" class="page">Multiplexed Endpoints</a></li>
<li><a href="interpreters/pekko-http.html" class="page">Pekko HTTP</a></li>
<li><a href="interpreters/http4s.html" class="page">http4s</a></li>
<li><a href="interpreters/scalajs-web-fetch.html" class="page">Scala.js web client (Fetch)</a></li>
<li><a href="interpreters/sttp.html" class="page">sttp</a></li>
<li><a href="interpreters/openapi.html" class="page">OpenAPI</a></li>
<li><a href="interpreters/circe.html" class="page">Circe</a></li>
<li><a href="interpreters/play-json.html" class="page">Play JSON</a></li>
</ul></li>
<li><a href="guides.html" class="page">Guides</a>
<ul>
<li><a href="guides/tupler.html" class="page"><code>Tupler</code></a></li>
<li><a href="guides/custom-authentication.html" class="page">Application-specific authentication</a></li>
</ul></li>
<li><a href="community.html" class="page">Community</a></li>
<li><a href="comparison.html" class="page">Comparison with similar tools</a></li>
<li><a href="talks.html" class="page">Talks and Articles</a></li>
<li><a href="release-and-compatibility-notes.html" class="page">Release and Compatibility Notes</a></li>
</ul>
<nav class="md-nav md-nav--secondary">
<label class="md-nav__title" for="__toc">Table of contents</label>
<ul>
<li><a href="quick-start.html#quick-start" class="header">Quick start</a>
<ul>
<li><a href="quick-start.html#project-layout" class="header">Project layout</a></li>
<li><a href="quick-start.html#dependencies" class="header">Dependencies</a></li>
<li><a href="quick-start.html#description-of-the-http-endpoints" class="header">Description of the HTTP endpoints</a></li>
<li><a href="quick-start.html#client-implementation" class="header">Client implementation</a></li>
<li><a href="quick-start.html#server-implementation" class="header">Server implementation</a></li>
<li><a href="quick-start.html#documentation-generation" class="header">Documentation generation</a></li>
<li><a href="quick-start.html#running-the-application" class="header">Running the application</a></li>
<li><a href="quick-start.html#next-step" class="header">Next Step</a></li>
</ul></li>
</ul>
</nav>
</nav>
<ul style="display: none">
<li class="md-nav__item md-version" id="project.version">
<label class="md-nav__link" for="__version">
<i class="md-icon" title="Version">label_outline</i> 1.12.1
</label>
</li>
</ul>
</div>
</div>
</div>
<div class="md-sidebar md-sidebar--secondary" data-md-component="toc">
<div class="md-sidebar__scrollwrap">
<div class="md-sidebar__inner">
<nav class="md-nav md-nav--secondary">
<label class="md-nav__title" for="__toc">Table of contents</label>
<ul>
<li><a href="quick-start.html#quick-start" class="header">Quick start</a>
<ul>
<li><a href="quick-start.html#project-layout" class="header">Project layout</a></li>
<li><a href="quick-start.html#dependencies" class="header">Dependencies</a></li>
<li><a href="quick-start.html#description-of-the-http-endpoints" class="header">Description of the HTTP endpoints</a></li>
<li><a href="quick-start.html#client-implementation" class="header">Client implementation</a></li>
<li><a href="quick-start.html#server-implementation" class="header">Server implementation</a></li>
<li><a href="quick-start.html#documentation-generation" class="header">Documentation generation</a></li>
<li><a href="quick-start.html#running-the-application" class="header">Running the application</a></li>
<li><a href="quick-start.html#next-step" class="header">Next Step</a></li>
</ul></li>
</ul>
</nav>
</div>
</div>
</div>
<div class="md-content">
<article class="md-content__inner md-typeset">
<div class="md-content__searchable">
<h1><a href="#quick-start" name="quick-start" class="anchor"><span class="anchor-link"></span></a>Quick start</h1>
<p>The central idea of endpoints4s is that you first define an abstract description of your HTTP endpoints and then the library provides:</p>
<ul>
<li>a server implementation decoding requests and building responses,</li>
<li>a client implementation building requests and decoding responses,</li>
<li>a machine readable documentation (OpenAPI document).</li>
</ul>
<h2><a href="#project-layout" name="project-layout" class="anchor"><span class="anchor-link"></span></a>Project layout</h2>
<p>The typical setup consists in a multi-project build, with a <code>client</code> project and a <code>server</code> project both depending on a <code>shared</code> project.</p>
<p>The <code>shared</code> project contains the <em>description</em> of the communication protocol. The <code>server</code> project <em>implements</em> this communication protocol. The <code>client</code> project <em>uses</em> the protocol to communicate with the <code>server</code>.</p>
<p><img src="project-layout.svg" /></p>
<h2><a href="#dependencies" name="dependencies" class="anchor"><span class="anchor-link"></span></a>Dependencies</h2>
<p>The <code>shared</code> project has to depend on so-called <em>algebras</em>, which provide the vocabulary to describe the communication endpoints, and the <code>client</code> and <code>server</code> projects have to depend on <em>interpreters</em>, which give a concrete meaning to the endpoint descriptions. See the <a href="algebras-and-interpreters.html">algebras and interpreters</a> page for an exhaustive list.</p>
<p>In this example you will use the following dependencies:</p>
<pre class="prettyprint"><code class="language-scala">val shared =
crossProject(JSPlatform, JVMPlatform).crossType(CrossType.Pure).settings(
libraryDependencies ++= Seq(
"org.endpoints4s" %%% "algebra" % "1.12.1",
// optional, see explanation below
"org.endpoints4s" %%% "json-schema-generic" % "1.12.1"
)
)
val sharedJS = shared.js
val sharedJVM = shared.jvm
val client =
project.enablePlugins(ScalaJSPlugin).settings(
libraryDependencies += "org.endpoints4s" %%% "fetch-client" % "3.2.2"
).dependsOn(sharedJS)
val server =
project.settings(
libraryDependencies ++= Seq(
"org.endpoints4s" %% "pekko-http-server" % "1.0.2",
"org.apache.pekko" %% "pekko-stream" % "1.0.3",
"org.scala-stm" %% "scala-stm" % "0.8"
)
).dependsOn(sharedJVM)
</code></pre>
<p>The <code>shared</code> project uses the <a href="algebras/json-schemas.html#generic-derivation-of-json-schemas-based-on-shapeless-"><code>json-schema-generic</code> module</a> in addition to the required algebra interface <a href="algebras/endpoints.html"><code>algebra</code></a>, to define the communication endpoints and to automatically derive the JSON schemas of the entities from their Scala type definitions.</p>
<p>The <code>client</code> project uses a <a href="interpreters/scalajs-web-fetch.html">Scala.js web (Fetch)</a> client interpreter.</p>
<p>Finally, the <code>server</code> project uses a server interpreter backed by <a href="interpreters/pekko-http.html">Pekko HTTP</a>. It also uses the scala-stm library for implementing the business logic.</p>
<h2><a href="#description-of-the-http-endpoints" name="description-of-the-http-endpoints" class="anchor"><span class="anchor-link"></span></a>Description of the HTTP endpoints</h2>
<p>In the <code>shared</code> project, define a <code>CounterEndpoints</code> trait describing two endpoints, one for getting a counter value and one for incrementing it:</p>
<pre class="prettyprint"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><a class="snippet-button go-to-source" href="https://github.com/endpoints4s/endpoints4s/tree/master/documentation/examples/quickstart/endpoints/src/main/scala/quickstart/CounterEndpoints.scala#L5-L51" target="_blank" title="Go to snippet source">source</a><code class="language-scala">import endpoints4s.{algebra, generic}
/** Defines the HTTP endpoints description of a web service implementing a counter.
* This web service has two endpoints: one for getting the current value of the counter,
* and one for incrementing it.
*/
trait CounterEndpoints
extends algebra.Endpoints
with algebra.JsonEntitiesFromSchemas
with generic.JsonSchemas {
/** Get the counter current value.
* Uses the HTTP verb “GET” and URL path “/current-value”.
* The response entity is a JSON document representing the counter value.
*/
val currentValue: Endpoint[Unit, Counter] =
endpoint(get(path / "current-value"), ok(jsonResponse[Counter]))
/** Increments the counter value.
* Uses the HTTP verb “POST” and URL path “/increment”.
* The request entity is a JSON document representing the increment to apply to the counter.
* The response entity is empty.
*/
val increment: Endpoint[Increment, Unit] =
endpoint(
post(path / "increment", jsonRequest[Increment]),
ok(emptyResponse)
)
// Generically derive the JSON schema of our `Counter`
// and `Increment` case classes defined thereafter
implicit lazy val counterSchema: JsonSchema[Counter] = genericJsonSchema
implicit lazy val incrementSchema: JsonSchema[Increment] = genericJsonSchema
}
case class Counter(value: Int)
case class Increment(step: Int)</code></pre>
<p>The <code>currentValue</code> and <code>increment</code> members define the endpoints for getting the counter current value or incrementing it, as their names suggest. The <code>counterSchema</code> and <code>incrementSchema</code> members define a JSON schema that will be used to serialize and deserialize the request and response entities.</p>
<h2><a href="#client-implementation" name="client-implementation" class="anchor"><span class="anchor-link"></span></a>Client implementation</h2>
<p>A client implementation of the endpoints can be obtained by mixing so-called “interpreters” to the <code>CounterEndpoints</code> trait defined above. In this example, you want to get a JavaScript (Scala.js) client that uses <code>Fetch</code> under the hood. Defines the following <code>CounterClient</code> object in the <code>client</code> project:</p>
<pre class="prettyprint"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><a class="snippet-button go-to-source" href="https://github.com/endpoints4s/endpoints4s/tree/master/documentation/examples/quickstart/client/src/main/scala/quickstart/CounterClient.scala#L4-L16" target="_blank" title="Go to snippet source">source</a><code class="language-scala">import endpoints4s.fetch
import endpoints4s.fetch.EndpointsSettings
/** Defines an HTTP client for the endpoints described in the `CounterEndpoints` trait.
* The derived HTTP client uses XMLHttpRequest to perform requests and returns
* results in a `js.Thenable`.
*/
object CounterClient
extends CounterEndpoints
with fetch.thenable.Endpoints
with fetch.JsonEntitiesFromSchemas {
lazy val settings: EndpointsSettings = EndpointsSettings()
}</code></pre>
<p>And then, the <code>CounterClient</code> object can be used as follows:</p>
<pre class="prettyprint"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><a class="snippet-button go-to-source" href="https://github.com/endpoints4s/endpoints4s/tree/master/documentation/examples/quickstart/client/src/main/scala/quickstart/Usage.scala#L6-L11" target="_blank" title="Go to snippet source">source</a><code class="language-scala">import scala.scalajs.js
/** Performs an XMLHttpRequest on the `currentValue` endpoint, and then
* deserializes the JSON response as a `Counter`.
*/
val eventuallyCounter: js.Thenable[Counter] = CounterClient.currentValue(()).thenable</code></pre>
<p>And also:</p>
<pre class="prettyprint"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><a class="snippet-button go-to-source" href="https://github.com/endpoints4s/endpoints4s/tree/master/documentation/examples/quickstart/client/src/main/scala/quickstart/Usage.scala#L15-L18" target="_blank" title="Go to snippet source">source</a><code class="language-scala">/** Serializes the `Increment` value into JSON and performs an XMLHttpRequest
* on the `increment` endpoint.
*/
val eventuallyDone: js.Thenable[Unit] = CounterClient.increment(Increment(42)).thenable</code></pre>
<p>As you can see, invoking an endpoint consists of calling a function on the <code>CounterClient</code> object. endpoints4s then builds an HTTP request (according to the endpoint description), sends it to the server, and eventually decodes the HTTP response (according to the endpoint description).</p>
<h2><a href="#server-implementation" name="server-implementation" class="anchor"><span class="anchor-link"></span></a>Server implementation</h2>
<p>Similarly, a server implementation of the endpoints can be obtained by mixing the appropriate interpreters to the <code>CounterEndpoints</code> trait. In this example, you want to get a JVM server that uses Pekko HTTP under the hood. Create the following <code>CounterServer</code> class in the <code>server</code> project:</p>
<pre class="prettyprint"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><a class="snippet-button go-to-source" href="https://github.com/endpoints4s/endpoints4s/tree/master/documentation/examples/quickstart/server/src/main/scala/quickstart/CounterServer.scala#L4-L34" target="_blank" title="Go to snippet source">source</a><code class="language-scala">import org.apache.pekko.http.scaladsl.server.Directives._
import org.apache.pekko.http.scaladsl.server.Route
import endpoints4s.pekkohttp.server
import scala.concurrent.stm.Ref
/** Defines a Play router (and reverse router) for the endpoints described
* in the `CounterEndpoints` trait.
*/
object CounterServer
extends CounterEndpoints
with server.Endpoints
with server.JsonEntitiesFromSchemas {
/** Simple implementation of an in-memory counter */
val counter = Ref(0)
// Implements the `currentValue` endpoint
val currentValueRoute =
currentValue.implementedBy(_ => Counter(counter.single.get))
// Implements the `increment` endpoint
val incrementRoute =
increment.implementedBy(inc => counter.single += inc.step)
val routes: Route =
currentValueRoute ~ incrementRoute
}</code></pre>
<p>The <code>routes</code> value produced by endpoints4s is a <code>Route</code> value directly usable by Pekko HTTP. The last section shows how to setup an Pekko HTTP server that uses these routes.</p>
<p>The routes implementations provided by endpoints4s decode the incoming HTTP requests, call the corresponding logic (here, incrementing the counter or getting its current value), and build the HTTP responses.</p>
<h2><a href="#documentation-generation" name="documentation-generation" class="anchor"><span class="anchor-link"></span></a>Documentation generation</h2>
<p>You can also generate documentation for the endpoints, again by mixing the appropriate interpreters. Create the following <code>CounterDocumentation</code> object in the <code>server</code> project:</p>
<pre class="prettyprint"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><a class="snippet-button go-to-source" href="https://github.com/endpoints4s/endpoints4s/tree/master/documentation/examples/quickstart/server/src/main/scala/quickstart/CounterDocumentation.scala#L4-L19" target="_blank" title="Go to snippet source">source</a><code class="language-scala">import endpoints4s.openapi
import endpoints4s.openapi.model.{Info, OpenApi}
/** Generates OpenAPI documentation for the endpoints described in the `CounterEndpoints` trait.
*/
object CounterDocumentation
extends CounterEndpoints
with openapi.Endpoints
with openapi.JsonEntitiesFromSchemas {
val api: OpenApi =
openApi(
Info(title = "API to manipulate a counter", version = "1.0.0")
)(currentValue, increment)
}</code></pre>
<p>This code defines a <code>CounterDocumentation</code> object with an <code>api</code> member containing an OpenAPI object documenting the <code>currentValue</code> and <code>increment</code> endpoints.</p>
<h2><a href="#running-the-application" name="running-the-application" class="anchor"><span class="anchor-link"></span></a>Running the application</h2>
<p>Finally, to run your application you need to build a proper Pekko HTTP server serving your routes. Define the following <code>Main</code> object:</p>
<pre class="prettyprint"><button class="snippet-button copy-snippet" title="Copy snippet to clipboard">copy</button><a class="snippet-button go-to-source" href="https://github.com/endpoints4s/endpoints4s/tree/master/documentation/examples/quickstart/server/src/main/scala/quickstart/Main.scala#L4-L27" target="_blank" title="Go to snippet source">source</a><code class="language-scala">import org.apache.pekko.actor.ActorSystem
import org.apache.pekko.http.scaladsl.Http
import org.apache.pekko.http.scaladsl.server.Directives._
object Main extends App {
implicit val system: ActorSystem = ActorSystem("server-system")
val routes = CounterServer.routes ~ DocumentationServer.routes
Http().newServerAt("0.0.0.0", 8000).bindFlow(routes)
}
// Additional route for serving the OpenAPI documentation
import endpoints4s.openapi.model.OpenApi
import endpoints4s.pekkohttp.server
object DocumentationServer
extends server.Endpoints
with server.JsonEntitiesFromEncodersAndDecoders {
val routes =
endpoint(get(path / "documentation.json"), ok(jsonResponse[OpenApi]))
.implementedBy(_ => CounterDocumentation.api)
}</code></pre>
<p>You can then browse the <a href="http://localhost:8000/current-value">http://localhost:8000/current-value</a> URL to query the counter value, or the <a href="http://localhost:8000/current-value">http://localhost:8000/documentation.json</a> URL to get the generated OpenAPI documentation, which should look like the following:</p>
<pre class="prettyprint"><code class="language-javascript">{
"openapi": "3.0.0",
"info": {
"title": "API to manipulate a counter",
"version": "1.0.0"
},
"components": {
"schemas": {
"quickstart.Counter": {
"type": "object",
"properties": {
"value": {
"format": "int32",
"type": "integer"
}
},
"required": ["value"]
},
"quickstart.Increment": {
"type": "object",
"properties": {
"step": {
"format": "int32",
"type": "integer"
}
},
"required": ["step"]
},
"endpoints.Errors": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"paths": {
"/increment": {
"post": {
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/quickstart.Increment"
}
}
}
},
"responses": {
"400": {
"description": "Client error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/endpoints.Errors"
}
}
}
},
"500": {
"description": "Server error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/endpoints.Errors"
}
}
}
},
"200": {
"description": ""
}
}
}
},
"/current-value": {
"get": {
"responses": {
"400": {
"description": "Client error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/endpoints.Errors"
}
}
}
},
"500": {
"description": "Server error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/endpoints.Errors"
}
}
}
},
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/quickstart.Counter"
}
}
}
}
}
}
}
}
}
</code></pre>
<h2><a href="#next-step" name="next-step" class="anchor"><span class="anchor-link"></span></a>Next Step</h2>
<p>Learn about the <a href="design.html">design principles</a> of the <em>endpoints</em> library.</p>
</div>
<div>
<a href="https://github.com/endpoints4s/endpoints4s/tree/master/documentation/manual/src/main/paradox/quick-start.md" title="Edit this page" class="md-source-file md-edit">
Edit this page
</a>
</div>
<div class="print-only">
<span class="md-source-file md-version">
1.12.1
</span>
</div>
</article>
</div>
</div>
</main>
<footer class="md-footer">
<div class="md-footer-nav">
<nav class="md-footer-nav__inner md-grid">
<a href="use-cases.html" title="Use Cases" class="md-flex md-footer-nav__link md-footer-nav__link--prev" rel="prev">
<div class="md-flex__cell md-flex__cell--shrink">
<i class="md-icon md-icon--arrow-back md-footer-nav__button"></i>
</div>
<div class="md-flex__cell md-flex__cell--stretch md-footer-nav__title">
<span class="md-flex__ellipsis">
<span class="md-footer-nav__direction">
Previous
</span>
Use Cases
</span>
</div>
</a>
<a href="design.html" title="Design in a nutshell" class="md-flex md-footer-nav__link md-footer-nav__link--next" rel="next">
<div class="md-flex__cell md-flex__cell--stretch md-footer-nav__title">
<span class="md-flex__ellipsis">
<span class="md-footer-nav__direction">
Next
</span>
Design in a nutshell
</span>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<i class="md-icon md-icon--arrow-forward md-footer-nav__button"></i>
</div>
</a>
</nav>
</div>
<div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid">
<div class="md-footer-copyright">
Powered by
<a href="https://github.com/lightbend/paradox">Paradox</a>
and
<a href="https://jonas.github.io/paradox-material-theme/">Paradox Material Theme</a>
</div>
<div class="md-footer-social">
<a href="https://github.com/endpoints4s/endpoints4s" class="md-footer-social__link fa fa-github"></a>
</div>
</div>
</div>
</footer>
</div>
<script src="assets/javascripts/application.583bbe55.js"></script>
<script src="assets/javascripts/paradox-material-theme.js"></script>
<script>app.initialize({version:"0.17",url:{base:"."}})</script>
<script type="text/javascript" src="lib/prettify/prettify.js"></script>
<script type="text/javascript" src="lib/prettify/lang-scala.js"></script>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function(event) {
window.prettyPrint && prettyPrint();
});
</script>
</body>
</html>