Skip to content

Commit 1143026

Browse files
authoredJul 13, 2023
Implement admin web (#5)
* Implement admin web * Add readme and rename packages to spra * Minor changes * Missing change
1 parent c8b3ec9 commit 1143026

File tree

90 files changed

+344
-167
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+344
-167
lines changed
 

‎README.md

+27
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,29 @@
11
# scala-postgres-react-admin
22
SPRA exposes your Postgres database through a nice React-Admin UI
3+
4+
## How to run
5+
- Add an AbstractModule that provides a DataExplorerSettings to your PlayFramework application, for example:
6+
```scala
7+
class DataExplorerModule extends AbstractModule {
8+
9+
@Provides()
10+
def dataExplorerSettings: DataExplorerSettings = DataExplorerSettings(settings)
11+
12+
val settings = List(
13+
TableSettings(
14+
tableName = "users",
15+
primaryKeyField = "user_id",
16+
hiddenColumns = List("password", "email"),
17+
nonEditableColumns = List("user_id", "email", "created_at", "verified_on", "name"),
18+
canBeDeleted = false,
19+
filterableColumns = List("name", "last_name")
20+
)
21+
)
22+
}
23+
```
24+
- Add the `AppRouter` routes to your `routes` file:
25+
```scala
26+
-> / net.wiringbits.spra.admin.AppRouter
27+
```
28+
- Run the PlayFramework application
29+
- Run `sbt spra-dev` to start the SPRA web

‎build.sbt

+78-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import java.nio.file.Files
2+
import java.nio.file.StandardCopyOption.REPLACE_EXISTING
3+
14
ThisBuild / versionScheme := Some("early-semver")
25
// For all Sonatype accounts created on or after February 2021
36
ThisBuild / sonatypeCredentialHost := "s01.oss.sonatype.org"
@@ -26,6 +29,8 @@ val sttp = "3.5.0"
2629

2730
val consoleDisabledOptions = Seq("-Xfatal-warnings", "-Ywarn-unused", "-Ywarn-unused-import")
2831

32+
lazy val build = TaskKey[File]("build")
33+
2934
// Used only by the server
3035
lazy val baseServerSettings: Project => Project = {
3136
_.settings(
@@ -194,15 +199,84 @@ lazy val bundlerSettings: Project => Project =
194199
Compile / fullOptJS / webpackDevServerExtraArgs += "--mode=production"
195200
)
196201

202+
lazy val spraWebBuildInfoSettings: Project => Project = _.enablePlugins(BuildInfoPlugin)
203+
.settings(
204+
buildInfoKeys := Seq[BuildInfoKey](name, version, scalaVersion, sbtVersion),
205+
buildInfoKeys ++= {
206+
val apiUrl = sys.env.get("API_URL")
207+
val values = Seq(
208+
"apiUrl" -> apiUrl
209+
)
210+
// Logging these values is useful to make sure that the necessary settings
211+
// are being overriden when packaging the app.
212+
sLog.value.info(s"BuildInfo settings:\n${values.mkString("\n")}")
213+
values.map(t => BuildInfoKey(t._1, t._2))
214+
},
215+
buildInfoPackage := "net.wiringbits",
216+
buildInfoUsePackageAsPath := true
217+
)
218+
219+
lazy val browserProject: Project => Project =
220+
_.settings(
221+
build := {
222+
val artifacts = (Compile / fullOptJS / webpack).value
223+
val artifactFolder = (Compile / fullOptJS / crossTarget).value
224+
val jsFolder = baseDirectory.value / "src" / "main" / "js"
225+
val distFolder = baseDirectory.value / "build"
226+
227+
distFolder.mkdirs()
228+
artifacts.foreach { artifact =>
229+
val target = artifact.data.relativeTo(artifactFolder) match {
230+
case None => distFolder / artifact.data.name
231+
case Some(relFile) => distFolder / relFile.toString
232+
}
233+
234+
Files.copy(artifact.data.toPath, target.toPath, REPLACE_EXISTING)
235+
}
236+
237+
// copy public resources
238+
Files
239+
.walk(jsFolder.toPath)
240+
.filter(x => !Files.isDirectory(x))
241+
.forEach(source => {
242+
source.toFile.relativeTo(jsFolder).foreach { relativeSource =>
243+
val dest = distFolder / relativeSource.toString
244+
dest.getParentFile.mkdirs()
245+
Files.copy(source, dest.toPath, REPLACE_EXISTING)
246+
}
247+
})
248+
249+
// link the proper js bundle
250+
val indexFrom = baseDirectory.value / "src/main/js/index.html"
251+
val indexTo = distFolder / "index.html"
252+
253+
val indexPatchedContent = {
254+
import collection.JavaConverters._
255+
Files
256+
.readAllLines(indexFrom.toPath, IO.utf8)
257+
.asScala
258+
.map(_.replaceAllLiterally("-fastopt", "-opt"))
259+
.mkString("\n")
260+
}
261+
262+
Files.write(indexTo.toPath, indexPatchedContent.getBytes(IO.utf8))
263+
distFolder
264+
}
265+
)
266+
197267
lazy val spraWeb = (project in file("spra-web"))
198-
.dependsOn(spraApi.js)
199-
.configure(bundlerSettings, baseLibSettings)
268+
.dependsOn(spraApi.js, spraPlayServer)
269+
.configure(bundlerSettings, baseLibSettings, browserProject, spraWebBuildInfoSettings)
200270
.configure(_.enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin))
201271
.settings(
202272
scalaVersion := "2.13.8",
203273
crossScalaVersions := Seq("2.13.8", "3.1.2"),
204274
name := "spra-web",
205275
Test / fork := false, // sjs needs this to run tests
276+
scalaJSUseMainModuleInitializer := true,
277+
scalaJSLinkerConfig := scalaJSLinkerConfig.value.withSourceMap(false),
278+
webpackDevServerPort := 8081,
279+
webpackBundlingMode := BundlingMode.LibraryOnly(),
206280
libraryDependencies ++= Seq(
207281
"org.scala-js" %%% "scala-js-macrotask-executor" % "1.0.0",
208282
"me.shadaj" %%% "slinky-core" % "0.7.3",
@@ -238,3 +312,5 @@ lazy val root = (project in file("."))
238312
publishLocal := {},
239313
publish / skip := true
240314
)
315+
316+
addCommandAlias("spra-admin", ";spraWeb/fastOptJS::startWebpackDevServer;~spraWeb/fastOptJS")

0 commit comments

Comments
 (0)
Please sign in to comment.