/
Main.scala
135 lines (108 loc) · 4.39 KB
/
Main.scala
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright © 2015-2019 the contributors (see Contributors.md).
*
* This file is part of Knora.
*
* Knora is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Knora is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with Knora. If not, see <http://www.gnu.org/licenses/>.
*/
package org.knora.salsah
import java.io.{File, PrintWriter}
import java.nio.file.{Path, Paths}
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.Http.ServerBinding
import akka.http.scaladsl.model._
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.server.directives.ContentTypeResolver.Default
import akka.stream.Materializer
import scala.concurrent.Future
import scala.io.Source
object Main extends App {
implicit val system = ActorSystem("salsah-system")
implicit val materializer = Materializer.matFromSystem(system)
implicit val ec = system.dispatcher
/**
* The application's configuration.
*/
val settings: SettingsImpl = Settings(system)
val log = akka.event.Logging(system, this.getClass)
log.info(s"Deployed: ${settings.deployed}")
val handler = if (settings.deployed) {
// deployed state
val workdir = settings.workingDirectory
log.info(s"Working Directory: $workdir")
val publicDir = workdir + "/public"
log.info(s"Public Directory: $publicDir")
val webapiUrl = settings.webapiUrl
log.info("webapiUrl: {}", webapiUrl)
val sipiUrl = settings.sipiUrl
log.info("sipiUrl: {}", sipiUrl)
//create /tmp directory if it does not exist
val tmpDir = new File("/tmp")
if (!tmpDir.exists()) {
tmpDir.mkdir()
}
/* rewriting webapi and sipi url in public/js/00_init_javascript.js */
val originalFile = new File(s"$publicDir/js/00_init_javascript.js") // Original File
val tempFile = new File("/tmp/00_init_javascript.js") // Temporary File
val printWriter = new PrintWriter(tempFile)
Source.fromFile(originalFile)("UTF-8")
.getLines
.map { line =>
if (line.contains("http://0.0.0.0:3333")) {
s"var API_URL = '$webapiUrl';"
} else if (line.contains("http://0.0.0.0:1024")) {
s"var SIPI_URL = '$sipiUrl';"
} else {
line.toString
}
}
.foreach(x => printWriter.println(x))
printWriter.close()
tempFile.renameTo(originalFile)
serveFromPublicDir(publicDir)
} else {
// undeployed state (default when run from sbt)
val wherami = System.getProperty("user.dir")
log.info(s"user.dir: $wherami")
val publicDir = wherami + "/src/public"
log.info(s"serving files from: $publicDir")
serveFromPublicDir(publicDir)
}
val (host, port) = (settings.hostName, settings.httpPort)
log.info(s"Salsah online at http://$host:$port/index.html")
val bindingFuture: Future[ServerBinding] = Http().bindAndHandle(handler, host, port)
bindingFuture onFailure {
case ex: Exception =>
log.error(ex, s"Failed to bind to $host:$port")
}
private def serveFromPublicDir(publicDir: String): Route = get {
entity(as[HttpRequest]) { requestData =>
val fullPath: Path = requestData.uri.path.toString match {
case "/" => Paths.get(publicDir + "/index.html")
case "" => Paths.get(publicDir + "/index.html")
case _ => Paths.get(publicDir + requestData.uri.path.toString)
}
getFromFile(fullPath.toString)
}
}
private def getExtensions(fileName: String): String = {
val index = fileName.lastIndexOf('.')
if (index != 0) {
fileName.drop(index + 1)
} else
""
}
}