forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.scala
167 lines (141 loc) · 4.28 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package controllers
import akka.pattern.ask
import play.api.data._, Forms._
import play.api.libs.json._
import play.api.mvc._
import lila.app._
import lila.api.Context
import lila.common.HTTPRequest
import lila.hub.actorApi.captcha.ValidCaptcha
import makeTimeout.large
import views._
object Main extends LilaController {
private lazy val blindForm = Form(tuple(
"enable" -> nonEmptyText,
"redirect" -> nonEmptyText
))
def toggleBlindMode = OpenBody { implicit ctx =>
implicit val req = ctx.body
fuccess {
blindForm.bindFromRequest.fold(
err => BadRequest, {
case (enable, redirect) =>
Redirect(redirect) withCookies lila.common.LilaCookie.cookie(
Env.api.Accessibility.blindCookieName,
if (enable == "0") "" else Env.api.Accessibility.hash,
maxAge = Env.api.Accessibility.blindCookieMaxAge.some,
httpOnly = true.some
)
}
)
}
}
def websocket(apiVersion: Int) = SocketOption { implicit ctx =>
getSocketUid("sri") ?? { uid =>
Env.site.socketHandler.human(uid, ctx.userId, apiVersion, get("flag")) map some
}
}
def apiWebsocket = WebSocket.tryAccept { req =>
Env.site.socketHandler.api(lila.api.Mobile.Api.currentVersion) map Right.apply
}
def captchaCheck(id: String) = Open { implicit ctx =>
Env.hub.captcher ? ValidCaptcha(id, ~get("solution")) map {
case valid: Boolean => Ok(if (valid) 1 else 0)
}
}
def webmasters = Open { implicit ctx =>
pageHit
fuccess {
html.site.help.webmasters()
}
}
def lag = Open { implicit ctx =>
pageHit
fuccess {
html.site.lag()
}
}
def mobile = Open { implicit ctx =>
pageHit
OptionOk(Prismic getBookmark "mobile-apk") {
case (doc, resolver) => html.mobile.home(doc, resolver)
}
}
def mobileRegister(platform: String, deviceId: String) = Auth { implicit ctx => me =>
Env.push.registerDevice(me, platform, deviceId)
}
def mobileUnregister = Auth { implicit ctx => me =>
Env.push.unregisterDevices(me)
}
def jslog(id: String) = Open { ctx =>
Env.round.selfReport(
userId = ctx.userId,
ip = HTTPRequest lastRemoteAddress ctx.req,
fullId = id,
name = get("n", ctx.req) | "?"
)
NoContent.fuccess
}
/**
* Event monitoring endpoint
*/
def jsmon(event: String) = Action {
if (event == "socket_gap") lila.mon.jsmon.socketGap()
else lila.mon.jsmon.unknown()
NoContent
}
private lazy val glyphsResult: Result = {
import chess.format.pgn.Glyph
import lila.tree.Node.glyphWriter
Ok(Json.obj(
"move" -> Glyph.MoveAssessment.display,
"position" -> Glyph.PositionAssessment.display,
"observation" -> Glyph.Observation.display
)) as JSON
}
val glyphs = Action(glyphsResult)
def image(id: String, hash: String, name: String) = Action.async { req =>
Env.db.image.fetch(id) map {
case None => NotFound
case Some(image) =>
lila.log("image").info(s"Serving ${image.path} to ${HTTPRequest printClient req}")
Ok(image.data).withHeaders(
CONTENT_TYPE -> image.contentType.getOrElse("image/jpeg"),
CONTENT_DISPOSITION -> image.name,
CONTENT_LENGTH -> image.size.toString
)
}
}
val robots = Action { req =>
Ok {
if (Env.api.Net.Crawlable && req.domain == Env.api.Net.Domain) """User-agent: *
Allow: /
Disallow: /game/export
Disallow: /games/export
"""
else "User-agent: *\nDisallow: /"
}
}
val freeJs = Open { implicit ctx =>
Ok(html.site.freeJs(ctx)).fuccess
}
def renderNotFound(req: RequestHeader): Fu[Result] =
reqToCtx(req) map renderNotFound
def renderNotFound(ctx: Context): Result = {
lila.mon.http.response.code404()
NotFound(html.base.notFound()(ctx))
}
def fpmenu = Open { implicit ctx =>
Ok(html.base.fpmenu()).fuccess
}
def getFishnet = Open { implicit ctx =>
Ok(html.site.getFishnet()).fuccess
}
def costs = Open { implicit ctx =>
Redirect("https://docs.google.com/spreadsheets/d/1CGgu-7aNxlZkjLl9l-OlL00fch06xp0Q7eCVDDakYEE/preview").fuccess
}
def contact = Open { implicit ctx =>
Ok(html.site.contact()).fuccess
}
def versionedAsset(version: String, file: String) = Assets.at(path = "/public", file)
}