Skip to content
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,10 @@ This template is distributed without any warranty. See <http://creativecommons.o
[g8]: http://www.foundweekends.org/giter8/
[sbt]: http://www.scala-sbt.org/
[sbt_download]: http://www.scala-sbt.org/download.html

## Formatting generated projects

The generated sbt project uses the Apache Pekko Scalafmt configuration and Java formatter.
Run `sbt applyCodeStyle` to format source and build files, then
`sbt checkCodeStyle` to check formatting. Run these tasks on JDK 17 or later. Java import sorting is disabled.
These tasks do not format generated managed sources. Maven and Gradle build configurations are unchanged.
85 changes: 85 additions & 0 deletions src/main/g8/.scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
version = 3.11.5
runner.dialect = scala213source3
project.git = false
style = defaultWithAlign
docstrings.style = Asterisk
docstrings.wrap = false
indentOperator.preset = spray
maxColumn = 120
lineEndings = preserve
rewrite.rules = [RedundantParens, AvoidInfix]
indentOperator.exemptScope = all
align.preset = some
align.tokens."+" = [
{
code = "~>"
owners = [
{ regex = "Term.ApplyInfix" }
]
}
]
literals.hexDigits = upper
literals.hexPrefix = lower
binPack.unsafeCallSite = always
binPack.unsafeDefnSite = always
binPack.indentCallSiteSingleArg = false
binPack.indentCallSiteOnce = true
binPack.importSelectors = unfold
newlines.avoidForSimpleOverflow = [slc]
newlines.source = keep
newlines.beforeMultiline = keep
align.openParenDefnSite = false
align.openParenCallSite = false
align.allowOverflow = true
optIn.breakChainOnFirstMethodDot = false
optIn.configStyleArguments = false
danglingParentheses.preset = false
spaces.inImportCurlyBraces = true
rewrite.neverInfix.excludeFilters = [
and
min
max
until
to
by
eq
ne
"should.*"
"contain.*"
"must.*"
in
ignore
be
taggedAs
thrownBy
synchronized
have
when
size
only
noneOf
oneElementOf
noElementsOf
atLeastOneElementOf
atMostOneElementOf
allElementsOf
inOrderElementsOf
theSameElementsAs
theSameElementsInOrderAs
]
rewriteTokens = {
"⇒": "=>"
"→": "->"
"←": "<-"
}
project.layout = StandardConvention

rewrite.scala3.convertToNewSyntax = true
runner {
dialectOverride {
allowSignificantIndentation = false
allowAsForImportRename = false
allowStarWildcardImport = false
allowPostfixStarVarargSplices = false
}
}
29 changes: 18 additions & 11 deletions src/main/g8/build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
val pekkoHttpVersion = "$pekko_http_version$"
val pekkoVersion = "$pekko_version$"
val pekkoVersion = "$pekko_version$"

lazy val root = (project in file(".")).
settings(
Expand All @@ -10,18 +10,25 @@ lazy val root = (project in file(".")).
)),
name := "TestProject",
libraryDependencies ++= Seq(
"org.apache.pekko" %% "pekko-http" % pekkoHttpVersion,
"org.apache.pekko" %% "pekko-actor-typed" % pekkoVersion,
"org.apache.pekko" %% "pekko-stream" % pekkoVersion,
"org.apache.pekko" %% "pekko-http-jackson" % pekkoHttpVersion,
"ch.qos.logback" % "logback-classic" % "1.6.6",
"org.apache.pekko" %% "pekko-http" % pekkoHttpVersion,
"org.apache.pekko" %% "pekko-actor-typed" % pekkoVersion,
"org.apache.pekko" %% "pekko-stream" % pekkoVersion,
"org.apache.pekko" %% "pekko-http-jackson" % pekkoHttpVersion,
"ch.qos.logback" % "logback-classic" % "1.6.6",

"org.apache.pekko" %% "pekko-testkit" % pekkoVersion % Test,
"org.apache.pekko" %% "pekko-http-testkit" % pekkoHttpVersion % Test,
"org.apache.pekko" %% "pekko-actor-testkit-typed" % pekkoVersion % Test,
"junit" % "junit" % "4.13.2" % Test,
"com.github.sbt" % "junit-interface" % "0.13.3" % Test
"org.apache.pekko" %% "pekko-testkit" % pekkoVersion % Test,
"org.apache.pekko" %% "pekko-http-testkit" % pekkoHttpVersion % Test,
"org.apache.pekko" %% "pekko-actor-testkit-typed" % pekkoVersion % Test,
"junit" % "junit" % "4.13.2" % Test,
"com.github.sbt" % "junit-interface" % "0.13.3" % Test
),

testOptions += Tests.Argument(TestFrameworks.JUnit, "-v")
)

// Match Apache Pekko formatting; preserve Java import order.
ThisBuild / javafmtFormatterCompatibleJavaVersion := 17
ThisBuild / javafmtSortImports := false

addCommandAlias("applyCodeStyle", "scalafmtAll; scalafmtSbt; javafmtAll")
addCommandAlias("checkCodeStyle", "scalafmtCheckAll; scalafmtSbtCheck; javafmtCheckAll")
3 changes: 3 additions & 0 deletions src/main/g8/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
addSbtPlugin("io.spray" % "sbt-revolver" % "0.10.0")

addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.6.2")
addSbtPlugin("com.github.sbt" % "sbt-java-formatter" % "0.14.0")
75 changes: 35 additions & 40 deletions src/main/g8/src/main/java/$package$/QuickstartApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,55 @@
import org.apache.pekko.NotUsed;
import org.apache.pekko.actor.typed.ActorRef;
import org.apache.pekko.actor.typed.Behavior;
import org.apache.pekko.http.javadsl.ConnectHttp;
import org.apache.pekko.http.javadsl.Http;
import org.apache.pekko.http.javadsl.ServerBinding;
import org.apache.pekko.http.javadsl.model.HttpRequest;
import org.apache.pekko.http.javadsl.model.HttpResponse;
import org.apache.pekko.http.javadsl.server.Route;
import org.apache.pekko.stream.Materializer;
import org.apache.pekko.stream.javadsl.Flow;
import org.apache.pekko.actor.typed.javadsl.Adapter;
import org.apache.pekko.actor.typed.javadsl.Behaviors;
import org.apache.pekko.actor.typed.ActorSystem;

import java.net.InetSocketAddress;
import java.util.concurrent.CompletionStage;

//#main-class
// #main-class
public class QuickstartApp {
// #start-http-server
static void startHttpServer(Route route, ActorSystem<?> system) {
CompletionStage<ServerBinding> futureBinding =
Http.get(system).newServerAt("localhost", 8080).bind(route);

futureBinding.whenComplete((binding, exception) -> {
if (binding != null) {
InetSocketAddress address = binding.localAddress();
system.log().info("Server online at http://{}:{}/",
address.getHostString(),
address.getPort());
} else {
system.log().error("Failed to bind HTTP endpoint, terminating system", exception);
system.terminate();
}
// #start-http-server
static void startHttpServer(Route route, ActorSystem<?> system) {
CompletionStage<ServerBinding> futureBinding =
Http.get(system).newServerAt("localhost", 8080).bind(route);

futureBinding.whenComplete(
(binding, exception) -> {
if (binding != null) {
InetSocketAddress address = binding.localAddress();
system
.log()
.info("Server online at http://{}:{}/", address.getHostString(), address.getPort());
} else {
system.log().error("Failed to bind HTTP endpoint, terminating system", exception);
system.terminate();
}
});
}
// #start-http-server
}

public static void main(String[] args) throws Exception {
//#server-bootstrapping
Behavior<NotUsed> rootBehavior = Behaviors.setup(context -> {
ActorRef<UserRegistry.Command> userRegistryActor =
context.spawn(UserRegistry.create(), "UserRegistry");
// #start-http-server

UserRoutes userRoutes = new UserRoutes(context.getSystem(), userRegistryActor);
startHttpServer(userRoutes.userRoutes(), context.getSystem());
public static void main(String[] args) throws Exception {
// #server-bootstrapping
Behavior<NotUsed> rootBehavior =
Behaviors.setup(
context -> {
ActorRef<UserRegistry.Command> userRegistryActor =
context.spawn(UserRegistry.create(), "UserRegistry");

return Behaviors.empty();
});
UserRoutes userRoutes = new UserRoutes(context.getSystem(), userRegistryActor);
startHttpServer(userRoutes.userRoutes(), context.getSystem());

// boot up server using the route as defined below
ActorSystem.create(rootBehavior, "HelloPekkoHttpServer");
//#server-bootstrapping
}
return Behaviors.empty();
});

// boot up server using the route as defined below
ActorSystem.create(rootBehavior, "HelloPekkoHttpServer");
// #server-bootstrapping
}
}
//#main-class


// #main-class
42 changes: 21 additions & 21 deletions src/main/g8/src/main/java/$package$/UserRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,36 @@
import org.apache.pekko.actor.typed.javadsl.ActorContext;
import org.apache.pekko.actor.typed.javadsl.Behaviors;
import org.apache.pekko.actor.typed.javadsl.Receive;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.*;

//#user-registry-actor
public class UserRegistry extends AbstractBehavior<UserRegistry.Command> {
// #user-registry-actor
public class UserRegistry extends AbstractBehavior<UserRegistry.Command> {

// actor protocol
sealed interface Command {}

public final static record GetUsers(ActorRef<Users> replyTo) implements Command {}
public static final record GetUsers(ActorRef<Users> replyTo) implements Command {}

public final static record CreateUser(User user, ActorRef<ActionPerformed> replyTo) implements Command {}
public static final record CreateUser(User user, ActorRef<ActionPerformed> replyTo)
implements Command {}

public final static record GetUserResponse(Optional<User> maybeUser) {}
public static final record GetUserResponse(Optional<User> maybeUser) {}

public final static record GetUser(String name, ActorRef<GetUserResponse> replyTo) implements Command {}
public static final record GetUser(String name, ActorRef<GetUserResponse> replyTo)
implements Command {}

public static final record DeleteUser(String name, ActorRef<ActionPerformed> replyTo)
implements Command {}

public final static record DeleteUser(String name, ActorRef<ActionPerformed> replyTo) implements Command {}
public static final record ActionPerformed(String description) implements Command {}

// #user-case-classes
public static final record User(String name, int age, String countryOfResidence) {}

public final static record ActionPerformed(String description) implements Command {}
public static final record Users(List<User> users) {}

//#user-case-classes
public final static record User(String name, int age, String countryOfResidence) {}

public final static record Users(List<User> users) {}
//#user-case-classes
// #user-case-classes

private final List<User> users = new ArrayList<>();

Expand Down Expand Up @@ -66,14 +66,15 @@ private Behavior<Command> onGetUsers(GetUsers command) {

private Behavior<Command> onCreateUser(CreateUser command) {
users.add(command.user());
command.replyTo().tell(new ActionPerformed(String.format("User %s created.", command.user().name())));
command
.replyTo()
.tell(new ActionPerformed(String.format("User %s created.", command.user().name())));
return this;
}

private Behavior<Command> onGetUser(GetUser command) {
Optional<User> maybeUser = users.stream()
.filter(user -> user.name().equals(command.name()))
.findFirst();
Optional<User> maybeUser =
users.stream().filter(user -> user.name().equals(command.name())).findFirst();
command.replyTo().tell(new GetUserResponse(maybeUser));
return this;
}
Expand All @@ -83,6 +84,5 @@ private Behavior<Command> onDeleteUser(DeleteUser command) {
command.replyTo().tell(new ActionPerformed(String.format("User %s deleted.", command.name)));
return this;
}

}
//#user-registry-actor
// #user-registry-actor
Loading