Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert backend to use Tapir #23

Merged
merged 13 commits into from
Sep 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*.gch
.cache
target
*.o

**/target/**
project/project
Expand Down
14 changes: 2 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,11 @@ jobs:
path: |
~/Library/Caches/sbt-vcpkg/vcpkg
~/.cache/sbt-vcpkg/vcpkg
~/.cache/sbt-vcpkg/vcpkg-install
key: ${{ runner.os }}-sbt-vcpkg

- name: Setup for Scala Native
run: |
sudo apt update
sudo apt install lsb-release wget software-properties-common
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
sudo add-apt-repository "deb http://apt.llvm.org/focal/ llvm-toolchain-focal-13 main"
sudo apt-get update
sudo apt-get install clang-13 lldb-13 libclang-13-dev llvm-13-dev lld-13
sudo curl --output /usr/share/keyrings/nginx-keyring.gpg https://unit.nginx.org/keys/nginx-keyring.gpg
sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/nginx-keyring.gpg] https://packages.nginx.org/unit/debian/ bullseye unit" >> /etc/apt/sources.list.d/unit.list'
sudo sh -c 'echo "deb-src [signed-by=/usr/share/keyrings/nginx-keyring.gpg] https://packages.nginx.org/unit/debian/ bullseye unit" >> /etc/apt/sources.list.d/unit.list'
sudo apt update
sudo apt install -y unit-dev
run: sudo ./scripts/setup-debian.sh

- name: Run tests
run: sbt app/nativeLink test
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
*.gch
.cache
target
*.o

**/target/**
project/project
project/metals.sbt
build/**

21 changes: 3 additions & 18 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
FROM eclipse-temurin:17-focal as builder

RUN apt update && apt install -y curl && \
curl -Lo /usr/local/bin/sbt https://raw.githubusercontent.com/sbt/sbt/1.8.x/sbt && \
chmod +x /usr/local/bin/sbt && \
curl -Lo llvm.sh https://apt.llvm.org/llvm.sh && \
chmod +x llvm.sh && \
apt install -y lsb-release wget software-properties-common gnupg && \
./llvm.sh 13 && \
apt update && \
apt install -y zip unzip tar make cmake autoconf pkg-config libclang-13-dev git && \
# install Unit, OpenSSL and Unit development headers
curl --output /usr/share/keyrings/nginx-keyring.gpg \
https://unit.nginx.org/keys/nginx-keyring.gpg && \
echo "deb [signed-by=/usr/share/keyrings/nginx-keyring.gpg] https://packages.nginx.org/unit/debian/ bullseye unit" >> /etc/apt/sources.list.d/unit.list && \
echo "deb-src [signed-by=/usr/share/keyrings/nginx-keyring.gpg] https://packages.nginx.org/unit/debian/ bullseye unit" >> /etc/apt/sources.list.d/unit.list && \
apt update && \
apt install -y unit-dev
COPY scripts /scripts
RUN /scripts/setup-debian.sh

ENV LLVM_BIN "/usr/lib/llvm-13/bin"
ENV CC "/usr/lib/llvm-13/bin/clang"

ENV SN_RELEASE "fast"
ENV CI "true"

COPY . /sources

RUN cd /sources && sbt clean app/test buildApp
RUN cd /sources && sbt clean buildApp

FROM nginx/unit:1.27.0-minimal as runtime_deps

Expand Down
117 changes: 0 additions & 117 deletions app/src/main/scala/api.helpers.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package twotm8

import trail.*
import snunit.*
import snunit.routes.*
import scala.util.Try
import scala.util.Failure
import scala.util.Success
Expand All @@ -23,125 +21,10 @@ trait ApiHelpers:
scribe.error(s"Failed request at <${req.method.name} ${req.path}>", exc)
req.serverError("Something broke yo")

inline def builder(routes: (Route[?], ArgsHandler[?])*): Handler =
routes.foldRight[Handler](_.notFound()) { case ((r, a), acc) =>
RouteHandler(
r.asInstanceOf[Route[Any]],
a.asInstanceOf[ArgsHandler[Any]],
acc
)
}

inline def api(methods: (Method, Handler)*): Handler =
val mp = methods.toMap

request =>
mp.get(request.method) match
case None => request.notFound()
case Some(h) =>
h.handleRequest(request)
end api

import upickle.default.{Reader, read}

inline def json[T: upickle.default.Reader](inline request: Request)(
inline f: T => Unit
) =
val payload = Try(upickle.default.read[T](request.contentRaw))

payload match
case Success(p) => f(p)
case Failure(ex) =>
scribe.error(
s"Error handling JSON request at <${request.method.name}> ${request.path}",
ex
)
request.badRequest("Invalid payload")
end json

extension (r: Request)
inline def sendJson[T: upickle.default.Writer](
status: StatusCode,
content: T,
headers: Map[String, String] = Map.empty
) =
r.send(
statusCode = status,
content = upickle.default.writeJs(content).render(),
headers = headers.updated("Content-type", "application/json").toSeq
)
inline def badRequest(
content: String,
headers: Map[String, String] = Map.empty
) =
r.send(
statusCode = StatusCode.BadRequest,
content = content,
headers = Seq.empty
)
inline def redirect(location: String) =
r.send(
StatusCode.TemporaryRedirect,
content = "",
headers = Seq("Location" -> location)
)

inline def noContent() =
r.send(StatusCode.NoContent, "", Seq.empty)

inline def unauthorized(inline msg: String = "Unathorized") =
r.send(StatusCode.Unauthorized, msg, Seq.empty)

inline def notFound(inline msg: String = "Not found") =
r.send(StatusCode.NotFound, msg, Seq.empty)

inline def serverError(inline msg: String = "Something broke yo") =
r.send(StatusCode.InternalServerError, msg, Seq.empty)
end extension

given Codec[UUID] with
def encode(t: UUID) = Some(t.toString)
def decode(v: Option[String]) =
v match
case None => None
case Some(str) =>
try Some(UUID.fromString(str))
catch case exc => None

case class Cookie(
name: String,
value: String,
params: Map[String, Option[String]]
):
def serialize =
name + "=" + value +
"; " +
params.toList
.sortBy(_._1)
.map((key, value) => key + value.map("=" + _).getOrElse(""))
.mkString("; ")
end Cookie

object Cookie:
def read(raw: String): Option[Cookie] =
try
val params =
raw
.split(";")
.map(pair =>
val split = pair.split("=")
if split.size == 1 then split(0).trim -> Option.empty
else split(0).trim -> Option(split(1))
)

params.headOption.map { case (name, valueO) =>
Cookie(name, valueO.getOrElse(""), params.tail.toMap)
}
catch
case ex =>
scribe.error("Failed to parse cookies", ex)
None
end Cookie
end ApiHelpers

object ApiHelpers extends ApiHelpers
Loading