Skip to content

Commit

Permalink
Copying over akka-http sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Devon Stewart authored and blast-hardcheese committed Dec 14, 2023
0 parents commit 58b40eb
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: CI

on:
push:
branches:
- master
pull_request:
branches:
- master
workflow_dispatch: {}

jobs:
core:
runs-on: ubuntu-20.04
strategy:
matrix:
java: [ '15' ]
scala: [
{ version: '2.12.12', bincompat: '2.12' }
]
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: actions/setup-java@v1
with:
java-version: ${{ matrix.java }}
- name: print Java version
run: java -version
- uses: actions/cache@v1
with:
path: ~/.cache/coursier
key: ${{ runner.os }}-scala-${{ matrix.scala.version }}-${{ hashFiles('**/*.sbt') }}
restore-keys: |
${{ runner.os }}-scala-${{ matrix.scala.version }}-
- name: Run tests
run: sbt ++${{ matrix.scala.version }} clean test:compile
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
target/
.bsp/
.bloop/
metals.sbt
.metals/
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2023 Guardrail LLC.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
27 changes: 27 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Compile / guardrailTasks := List(
ScalaServer(file("petstore.yaml"), pkg="foo"),
)

val akkaVersion = "2.6.20"
val akkaHttpVersion = "10.2.10"
val catsVersion = "2.6.1"
val circeVersion = "0.14.1"
val scalatestVersion = "3.2.9"
val jaxbApiVersion = "2.3.1"

scalacOptions ++= Seq("-Ypartial-unification", "-deprecation")

libraryDependencies += "org.slf4j" % "slf4j-simple" % "1.7.30"

libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-actor" % akkaVersion,
"com.typesafe.akka" %% "akka-stream" % akkaVersion,
"com.typesafe.akka" %% "akka-http" % akkaHttpVersion,
"com.typesafe.akka" %% "akka-http-testkit" % akkaHttpVersion,
"io.circe" %% "circe-core" % circeVersion,
"io.circe" %% "circe-generic" % circeVersion,
"io.circe" %% "circe-parser" % circeVersion,
"javax.xml.bind" % "jaxb-api" % jaxbApiVersion,
"org.scalatest" %% "scalatest" % scalatestVersion % Test,
"org.typelevel" %% "cats-core" % catsVersion,
)
104 changes: 104 additions & 0 deletions petstore.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
openapi: 3.0.0
servers:
- url: http://petstore.swagger.io/v1
info:
version: ""
title: ""
paths:
/pet/{name}:
put:
operationId: updatePet
x-scala-package: pet
parameters:
- name: name
in: path
schema:
type: string
required: true
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/Pet"
responses:
"200":
description: ""
content:
"multipart/form-data":
schema:
$ref: "#/components/schemas/Pet"
/pets:
get:
operationId: getPets
x-scala-package: pet
requestBody:
required: true
content:
application/x-www-form-urlencoded:
schema:
properties:
name:
type: array
items:
type: string
status:
type: string
required:
- name
responses:
"200":
description: ""
content:
"application/json":
schema:
type: array
items:
$ref: "#/components/schemas/Pet"
post:
operationId: createPet
x-scala-package: pet
requestBody:
required: true
content:
application/x-www-form-urlencoded:
schema:
properties:
name:
type: string
status:
type: string
required:
- name
# multipart/form-data:
# schema:
# properties:
# name:
# type: string
# status:
# type: string
# file:
# type: string
# format: binary
# required:
# - name
# application/json:
# schema:
# $ref: "#/components/schemas/Pet"
responses:
"200":
description: ""
content:
"*/*":
schema:
$ref: "#/components/schemas/Pet"
components:
schemas:
Pet:
type: object
required:
- name
properties:
name:
type: string
status:
type: string
1 change: 1 addition & 0 deletions project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.9.7
1 change: 1 addition & 0 deletions project/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
libraryDependencies += "io.swagger.parser.v3" % "swagger-parser-v2-converter" % "2.0.24"
1 change: 1 addition & 0 deletions project/guardrail.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("dev.guardrail" % "sbt-guardrail" % "0.75.2")
1 change: 1 addition & 0 deletions project/revolver.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("io.spray" % "sbt-revolver" % "0.9.1")
40 changes: 40 additions & 0 deletions src/main/scala/App.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent._
import scala.concurrent.duration.Duration

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.ContentType

import foo.pet._
import foo.definitions.Pet
import java.io.File

object App extends App {

implicit def actorSystem = ActorSystem()

val routes = PetResource.routes(new PetHandler {
// application/x-www-form-urlencoded
def createPet(respond: PetResource.CreatePetResponse.type)(name: String, status: Option[String]): scala.concurrent.Future[PetResource.CreatePetResponse] = {
Future.successful(respond.OK(Pet(name=name, status=status)))
}
// multipart/form-data
def createPet(respond: PetResource.CreatePetResponse.type)(name: String, status: Option[String], file: Option[(java.io.File, Option[String], ContentType)]): scala.concurrent.Future[PetResource.CreatePetResponse] = {
Future.successful(respond.OK(Pet(name=name, status=status)))
}
def createPetMapFileField(fieldName: String, fileName: Option[String], contentType: ContentType): File = ???

// application/json
def createPet(respond: PetResource.CreatePetResponse.type)(body: Pet): Future[PetResource.CreatePetResponse] = {
Future.successful(respond.OK(body))
}

override def updatePet(respond: PetResource.UpdatePetResponse.type)(name: String, body: Option[Pet]): Future[PetResource.UpdatePetResponse] = ???

def getPets(respond: PetResource.GetPetsResponse.type)(name: Vector[String], status: Option[String]) = ???
})

Await.result(Http().newServerAt("127.0.0.1", 8080).bindFlow(routes), Duration.Inf)
println("Running at http://localhost:8080 !")
}

0 comments on commit 58b40eb

Please sign in to comment.