diff --git a/README.md b/README.md index d53c7c8..6337cc1 100644 --- a/README.md +++ b/README.md @@ -6,13 +6,13 @@ A simple Giter8 template to generate a http4s server that read/write json via [s The command below will generate a project that works with the latest version of Smithy4s. If you decide to go with another version, you might need to adjust the generated project to make it work. -## sbt - `sbt new disneystreaming/smithy4s.g8` -## mill +# SBT / Mill + +You will be prompted for the build tool you want to generate a build for. By default, SBT is selected. For mill, reply "no" to "sbt", +and "yes" to "mill". -`sbt new disneystreaming/smithy4s.g8 --directory mill`. ## how-to run? diff --git a/mill/.gitignore b/mill/.gitignore deleted file mode 100644 index 505e152..0000000 --- a/mill/.gitignore +++ /dev/null @@ -1,53 +0,0 @@ -*.class -*.log - -# these are moved into the doc project by the build -modules/docs/src/main/tut/changelog.md -modules/docs/src/main/tut/license.md - -# sbt specific -dist/* -target/ -lib_managed/ -src_managed/ -project/boot/ -project/plugins/project/ -project/hydra.sbt - -# Scala-IDE specific -.scala_dependencies -.cache -.classpath -.project -.worksheet/ -bin/ -.settings/ - -# OS X -.DS_Store - -# Ctags -.tags - -# ENSIME -.ensime -.ensime_cache/ - -# IntelliJ -.idea/ - -# Mill -out/ - -# Bloop/Metals -.bloop/ -.metals/ -metals.sbt -.bsp/ - -# VS-Code -.vscode/ - -# smithy lsp -build/smithy/classpath.json - diff --git a/mill/.scalafmt.conf b/mill/.scalafmt.conf deleted file mode 100644 index 042cf1e..0000000 --- a/mill/.scalafmt.conf +++ /dev/null @@ -1,2 +0,0 @@ -version = "3.7.0" -runner.dialect = scala213 \ No newline at end of file diff --git a/mill/default.properties b/mill/default.properties deleted file mode 100644 index 517ca7e..0000000 --- a/mill/default.properties +++ /dev/null @@ -1,2 +0,0 @@ -name = MyApi -smithy4s_version = maven(com.disneystreaming.smithy4s, smithy4s-core_2.13, stable) diff --git a/mill/service/src/com/example/Main.scala b/mill/service/src/com/example/Main.scala deleted file mode 100644 index 749507a..0000000 --- a/mill/service/src/com/example/Main.scala +++ /dev/null @@ -1,44 +0,0 @@ -package com.example - -import com.example.hello._ -import cats.effect._ -import cats.implicits._ -import org.http4s.implicits._ -import org.http4s.ember.server._ -import org.http4s._ -import com.comcast.ip4s._ -import smithy4s.http4s.SimpleRestJsonBuilder - -object HelloWorldImpl extends HelloWorldService[IO] { - def hello(name: String, town: Option[String]): IO[Greeting] = IO.pure { - town match { - case None => Greeting(s"Hello " + name + "!") - case Some(t) => Greeting(s"Hello " + name + " from " + t + "!") - } - } -} - -object Routes { - private val example: Resource[IO, HttpRoutes[IO]] = - SimpleRestJsonBuilder.routes(HelloWorldImpl).resource - - private val docs: HttpRoutes[IO] = - smithy4s.http4s.swagger.docs[IO](HelloWorldService) - - val all: Resource[IO, HttpRoutes[IO]] = example.map(_ <+> docs) -} - -object Main extends IOApp.Simple { - val run = Routes.all.flatMap { routes => - val thePort = port"9000" - val theHost = host"localhost" - EmberServerBuilder - .default[IO] - .withPort(thePort) - .withHost(theHost) - .withHttpApp(routes.orNotFound) - .build <* - Resource.eval(IO.println(s"Server started on: \$theHost:\$thePort")) - }.useForever - -} diff --git a/mill/smithy-build.json b/mill/smithy-build.json deleted file mode 100644 index 9274d70..0000000 --- a/mill/smithy-build.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "maven": { - "dependencies": [ - "com.disneystreaming.smithy4s:smithy4s-protocol:$smithy4s_version$", - "com.disneystreaming.alloy:alloy-core:0.1.21" - ] - } -} diff --git a/mill/.mill-version b/src/main/g8/$if(mill.truthy)$.$endif$/.mill-version similarity index 100% rename from mill/.mill-version rename to src/main/g8/$if(mill.truthy)$.$endif$/.mill-version diff --git a/mill/build.sc b/src/main/g8/$if(mill.truthy)$.$endif$/build.sc similarity index 72% rename from mill/build.sc rename to src/main/g8/$if(mill.truthy)$.$endif$/build.sc index eed7194..c0b43f1 100644 --- a/mill/build.sc +++ b/src/main/g8/$if(mill.truthy)$.$endif$/build.sc @@ -3,8 +3,11 @@ import \$ivy.`com.disneystreaming.smithy4s::smithy4s-mill-codegen-plugin::$smith import smithy4s.codegen.mill._ import mill._, mill.scalalib._ -object service extends ScalaModule with Smithy4sModule { - +object service extends SbtModule with Smithy4sModule { + + def millSourcePath = os.pwd + def smithy4sInputDirs = T.sources(millSourcePath / "src" / "main" / "smithy") + def scalaVersion = "2.13.9" override def ivyDeps = Agg( ivy"com.disneystreaming.smithy4s::smithy4s-core:\${smithy4sVersion()}", diff --git a/src/main/g8/$if(mill.truthy)$.$endif$/mill b/src/main/g8/$if(mill.truthy)$.$endif$/mill new file mode 100755 index 0000000..980a1b6 --- /dev/null +++ b/src/main/g8/$if(mill.truthy)$.$endif$/mill @@ -0,0 +1,49 @@ +#!/usr/bin/env sh + +# This is a wrapper script, that automatically download mill from GitHub release pages +# You can give the required mill version with MILL_VERSION env variable +# If no version is given, it falls back to the value of DEFAULT_MILL_VERSION +DEFAULT_MILL_VERSION=0.10.5 + +set -e + +if [ -z "\$MILL_VERSION" ] ; then + if [ -f ".mill-version" ] ; then + MILL_VERSION="\$(head -n 1 .mill-version 2> /dev/null)" + elif [ -f "mill" ] && [ "\$0" != "mill" ] ; then + MILL_VERSION=\$(grep -F "DEFAULT_MILL_VERSION=" "mill" | head -n 1 | cut -d= -f2) + else + MILL_VERSION=\$DEFAULT_MILL_VERSION + fi +fi + +if [ "x\${XDG_CACHE_HOME}" != "x" ] ; then + MILL_DOWNLOAD_PATH="\${XDG_CACHE_HOME}/mill/download" +else + MILL_DOWNLOAD_PATH="\${HOME}/.cache/mill/download" +fi +MILL_EXEC_PATH="\${MILL_DOWNLOAD_PATH}/\${MILL_VERSION}" + +version_remainder="\$MILL_VERSION" +MILL_MAJOR_VERSION="\${version_remainder%%.*}"; version_remainder="\${version_remainder#*.}" +MILL_MINOR_VERSION="\${version_remainder%%.*}"; version_remainder="\${version_remainder#*.}" + +if [ ! -s "\$MILL_EXEC_PATH" ] ; then + mkdir -p "\$MILL_DOWNLOAD_PATH" + if [ "\$MILL_MAJOR_VERSION" -gt 0 ] || [ "\$MILL_MINOR_VERSION" -ge 5 ] ; then + ASSEMBLY="-assembly" + fi + DOWNLOAD_FILE=\$MILL_EXEC_PATH-tmp-download + MILL_VERSION_TAG=\$(echo \$MILL_VERSION | sed -E 's/([^-]+)(-M[0-9]+)?(-.*)?/\1\2/') + MILL_DOWNLOAD_URL="https://github.com/lihaoyi/mill/releases/download/\${MILL_VERSION_TAG}/\$MILL_VERSION\${ASSEMBLY}" + curl --fail -L -o "\$DOWNLOAD_FILE" "\$MILL_DOWNLOAD_URL" + chmod +x "\$DOWNLOAD_FILE" + mv "\$DOWNLOAD_FILE" "\$MILL_EXEC_PATH" + unset DOWNLOAD_FILE + unset MILL_DOWNLOAD_URL +fi + +unset MILL_DOWNLOAD_PATH +unset MILL_VERSION + +exec \$MILL_EXEC_PATH "\$@" diff --git a/src/main/g8/build.sbt b/src/main/g8/$if(sbt.truthy)$.$endif$/build.sbt similarity index 86% rename from src/main/g8/build.sbt rename to src/main/g8/$if(sbt.truthy)$.$endif$/build.sbt index 4392054..8ca0a2b 100644 --- a/src/main/g8/build.sbt +++ b/src/main/g8/$if(sbt.truthy)$.$endif$/build.sbt @@ -11,5 +11,7 @@ lazy val root = (project in file(".")) "com.disneystreaming.smithy4s" %% "smithy4s-http4s" % smithy4sVersion.value, "com.disneystreaming.smithy4s" %% "smithy4s-http4s-swagger" % smithy4sVersion.value, "org.http4s" %% "http4s-ember-server" % "0.23.16" - ) + ), + Compile / run / fork := true, + Compile / run / connectInput := true ) diff --git a/src/main/g8/project/build.properties b/src/main/g8/$if(sbt.truthy)$.$endif$/project/build.properties similarity index 100% rename from src/main/g8/project/build.properties rename to src/main/g8/$if(sbt.truthy)$.$endif$/project/build.properties diff --git a/src/main/g8/project/plugins.sbt b/src/main/g8/$if(sbt.truthy)$.$endif$/project/plugins.sbt similarity index 100% rename from src/main/g8/project/plugins.sbt rename to src/main/g8/$if(sbt.truthy)$.$endif$/project/plugins.sbt diff --git a/src/main/g8/default.properties b/src/main/g8/default.properties index 517ca7e..1d80e73 100644 --- a/src/main/g8/default.properties +++ b/src/main/g8/default.properties @@ -1,2 +1,4 @@ -name = MyApi +name = smithy4s-quickstart +sbt = yes +mill = no smithy4s_version = maven(com.disneystreaming.smithy4s, smithy4s-core_2.13, stable) diff --git a/src/main/g8/src/main/scala/com/example/Main.scala b/src/main/g8/src/main/scala/com/example/Main.scala index 749507a..601c4c0 100644 --- a/src/main/g8/src/main/scala/com/example/Main.scala +++ b/src/main/g8/src/main/scala/com/example/Main.scala @@ -1,8 +1,9 @@ package com.example -import com.example.hello._ +import hello._ +import cats.syntax.all._ import cats.effect._ -import cats.implicits._ +import cats.effect.syntax.all._ import org.http4s.implicits._ import org.http4s.ember.server._ import org.http4s._ @@ -29,16 +30,24 @@ object Routes { } object Main extends IOApp.Simple { - val run = Routes.all.flatMap { routes => - val thePort = port"9000" - val theHost = host"localhost" - EmberServerBuilder - .default[IO] - .withPort(thePort) - .withHost(theHost) - .withHttpApp(routes.orNotFound) - .build <* - Resource.eval(IO.println(s"Server started on: \$theHost:\$thePort")) - }.useForever + val run = Routes.all + .flatMap { routes => + val thePort = port"9000" + val theHost = host"localhost" + val message = + s"Server started on: \$theHost:\$thePort, press enter to stop" + + EmberServerBuilder + .default[IO] + .withPort(thePort) + .withHost(theHost) + .withHttpApp(routes.orNotFound) + .build + .productL(IO.println(message).toResource) + } + .useForever + .race(IO.readLine) + .void + .guarantee(IO.println("Goodbye!")) } diff --git a/src/main/g8/src/main/smithy/MyApi.smithy b/src/main/g8/src/main/smithy/MyApi.smithy deleted file mode 100644 index bd7acbd..0000000 --- a/src/main/g8/src/main/smithy/MyApi.smithy +++ /dev/null @@ -1,31 +0,0 @@ -\$version: "2" - -namespace com.example.hello - -use alloy#simpleRestJson - -@simpleRestJson -service HelloWorldService { - version: "1.0.0", - operations: [Hello] -} - -@http(method: "POST", uri: "/{name}", code: 200) -operation Hello { - input: Person, - output: Greeting -} - -structure Person { - @httpLabel - @required - name: String, - - @httpQuery("town") - town: String -} - -structure Greeting { - @required - message: String -} diff --git a/mill/service/smithy/MyApi.smithy b/src/main/g8/src/main/smithy/hello.smithy similarity index 81% rename from mill/service/smithy/MyApi.smithy rename to src/main/g8/src/main/smithy/hello.smithy index bd7acbd..6872be5 100644 --- a/mill/service/smithy/MyApi.smithy +++ b/src/main/g8/src/main/smithy/hello.smithy @@ -1,6 +1,6 @@ \$version: "2" -namespace com.example.hello +namespace hello use alloy#simpleRestJson @@ -10,7 +10,7 @@ service HelloWorldService { operations: [Hello] } -@http(method: "POST", uri: "/{name}", code: 200) +@http(method: "GET", uri: "/hello/{name}", code: 200) operation Hello { input: Person, output: Greeting