-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.scala
More file actions
63 lines (50 loc) · 2.24 KB
/
Copy pathdev.scala
File metadata and controls
63 lines (50 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//> using scala 3.3
//> using dep com.lihaoyi::os-lib:0.11.6
import com.sun.net.httpserver.*
import java.net.InetSocketAddress
import java.nio.file.{Files, Paths}
@main def serve(args: String*): Unit =
val port = args.headOption.flatMap(_.toIntOption).getOrElse(8000)
// First run markdown processor to generate Opinions.scala
println("Processing markdown files...")
val buildResult = os.proc("scala-cli", "run", "build.scala")
.call(cwd = os.pwd, check = false)
if buildResult.exitCode != 0 then
println(s"Markdown processing failed:\n${buildResult.err.text()}")
sys.exit(1)
// Then build the Scala.js
println("Building Scala.js...")
val result = os.proc("scala-cli", "--power", "package", "--js", "src/Main.scala", "src/generated/Opinions.scala", "-o", "public/main.js", "-f")
.call(cwd = os.pwd, check = false)
if result.exitCode != 0 then
println(s"Build failed:\n${result.err.text()}")
sys.exit(1)
println(s"Build complete. Starting server at http://localhost:$port")
val server = HttpServer.create(InetSocketAddress(port), 0)
val publicDir = Paths.get("public").toAbsolutePath
server.createContext("/", exchange => {
val path = exchange.getRequestURI.getPath match
case "/" => "/index.html"
case p => p
val file = publicDir.resolve(path.stripPrefix("/"))
if Files.exists(file) && Files.isRegularFile(file) then
val content = Files.readAllBytes(file)
val contentType = path match
case p if p.endsWith(".html") => "text/html"
case p if p.endsWith(".js") => "application/javascript"
case p if p.endsWith(".css") => "text/css"
case p if p.endsWith(".svg") => "image/svg+xml"
case p if p.endsWith(".txt") => "text/plain"
case _ => "application/octet-stream"
exchange.getResponseHeaders.set("Content-Type", contentType)
exchange.sendResponseHeaders(200, content.length)
exchange.getResponseBody.write(content)
exchange.getResponseBody.close()
else
exchange.sendResponseHeaders(404, 0)
exchange.getResponseBody.close()
})
server.setExecutor(null)
server.start()
println(s"Serving public/ at http://localhost:$port (Ctrl+C to stop)")
Thread.currentThread().join()