@@ -10,7 +10,6 @@ import io.ktor.http.*
1010import io.ktor.serialization.jackson.*
1111import io.ktor.server.application.*
1212import io.ktor.server.plugins.contentnegotiation.*
13- import io.ktor.server.plugins.cors.routing.*
1413import io.ktor.server.request.*
1514import io.ktor.server.response.*
1615import io.ktor.server.routing.*
@@ -36,62 +35,45 @@ fun Application.dashify() {
3635 jackson {}
3736 }
3837
39- routing {
40- get( " / " ) {
41- if ( ! checkIsEnabled()) return @get call.respond(
38+ suspend fun checkError ( call : ApplicationCall ): Boolean {
39+ if ( ! checkIsEnabled() ) {
40+ call.respond(
4241 HttpStatusCode .fromValue(418 ),
4342 hashMapOf(" status" to " I'm a tea pot :3" , " detail" to " server disabled plugin." )
4443 )
45- val authHeader = call.request.headers[" Authorization" ]
46- authHeader ? : return @get call.respond(
44+ return true
45+ }
46+ val authHeader = call.request.headers[" Authorization" ]
47+ if (authHeader == null ) {
48+ call.respond(
4749 HttpStatusCode .Unauthorized ,
4850 hashMapOf(" status" to " Unauthorized" , " detail" to " Authorization header not found." )
4951 )
50- if (authHeader != " Bearer ${ConfigHandler [" key" ].toString()} " ) {
51- return @get call.respond(
52- HttpStatusCode .Unauthorized ,
53- hashMapOf(" status" to " Unauthorized" , " detail" to " Invalid key." )
54- )
55- }
52+ return true
53+ }
54+ if (authHeader != " Bearer ${ConfigHandler [" key" ].toString()} " ) {
55+ call.respond(
56+ HttpStatusCode .Unauthorized ,
57+ hashMapOf(" status" to " Unauthorized" , " detail" to " Invalid key." )
58+ )
59+ return true
60+ }
5661
62+ return false
63+ }
64+
65+ routing {
66+ get(" /" ) {
67+ if (checkError(call)) { return @get }
5768 call.respond(HttpStatusCode .OK , hashMapOf(" status" to " ok" ))
5869 }
5970
6071 get(" /worlds" ) {
61- if (! checkIsEnabled()) return @get call.respond(
62- HttpStatusCode .fromValue(418 ),
63- hashMapOf(" status" to " I'm a tea pot :3" , " detail" to " server disabled plugin." )
64- )
65- val authHeader = call.request.headers[" Authorization" ]
66- authHeader ? : return @get call.respond(
67- HttpStatusCode .Unauthorized ,
68- hashMapOf(" status" to " Unauthorized" , " detail" to " Authorization header not found." )
69- )
70- if (authHeader != " Bearer ${ConfigHandler [" key" ].toString()} " ) {
71- return @get call.respond(
72- HttpStatusCode .Unauthorized ,
73- hashMapOf(" status" to " Unauthorized" , " detail" to " Invalid key." )
74- )
75- }
76-
72+ if (checkError(call)) { return @get }
7773 call.respond(HttpStatusCode .OK , WorldManager .getWorldsList())
7874 }
7975 get(" /worlds/{uuid}" ) {
80- if (! checkIsEnabled()) return @get call.respond(
81- HttpStatusCode .fromValue(418 ),
82- hashMapOf(" status" to " I'm a tea pot :3" , " detail" to " server disabled plugin." )
83- )
84- val authHeader = call.request.headers[" Authorization" ]
85- authHeader ? : return @get call.respond(
86- HttpStatusCode .Unauthorized ,
87- hashMapOf(" status" to " Unauthorized" , " detail" to " Authorization header not found." )
88- )
89- if (authHeader != " Bearer ${ConfigHandler [" key" ].toString()} " ) {
90- return @get call.respond(
91- HttpStatusCode .Unauthorized ,
92- hashMapOf(" status" to " Unauthorized" , " detail" to " Invalid key." )
93- )
94- }
76+ if (checkError(call)) { return @get }
9577
9678 val result = WorldManager .getWorldInfo(call.parameters[" uuid" ]!! )
9779 if (result[" error" ] == null ) {
@@ -102,40 +84,11 @@ fun Application.dashify() {
10284 }
10385
10486 get(" /players" ) {
105- if (! checkIsEnabled()) return @get call.respond(
106- HttpStatusCode .fromValue(418 ),
107- hashMapOf(" status" to " I'm a tea pot :3" , " detail" to " server disabled plugin." )
108- )
109- val authHeader = call.request.headers[" Authorization" ]
110- authHeader ? : return @get call.respond(
111- HttpStatusCode .Unauthorized ,
112- hashMapOf(" status" to " Unauthorized" , " detail" to " Authorization header not found." )
113- )
114- if (authHeader != " Bearer ${ConfigHandler [" key" ].toString()} " ) {
115- return @get call.respond(
116- HttpStatusCode .Unauthorized ,
117- hashMapOf(" status" to " Unauthorized" , " detail" to " Invalid key." )
118- )
119- }
120-
87+ if (checkError(call)) { return @get }
12188 call.respond(HttpStatusCode .OK , PlayerManager .getPlayerList())
12289 }
12390 get(" /players/{uuid}" ) {
124- if (! checkIsEnabled()) return @get call.respond(
125- HttpStatusCode .fromValue(418 ),
126- hashMapOf(" status" to " I'm a tea pot :3" , " detail" to " server disabled plugin." )
127- )
128- val authHeader = call.request.headers[" Authorization" ]
129- authHeader ? : return @get call.respond(
130- HttpStatusCode .Unauthorized ,
131- hashMapOf(" status" to " Unauthorized" , " detail" to " Authorization header not found." )
132- )
133- if (authHeader != " Bearer ${ConfigHandler [" key" ].toString()} " ) {
134- return @get call.respond(
135- HttpStatusCode .Unauthorized ,
136- hashMapOf(" status" to " Unauthorized" , " detail" to " Invalid key." )
137- )
138- }
91+ if (checkError(call)) { return @get }
13992
14093 val result = PlayerManager .getPlayerInfo(call.parameters[" uuid" ]!! )
14194 if (result[" error" ] == null ) {
@@ -145,21 +98,7 @@ fun Application.dashify() {
14598 }
14699 }
147100 post(" /players/{uuid}/kick" ) {
148- if (! checkIsEnabled()) return @post call.respond(
149- HttpStatusCode .fromValue(418 ),
150- hashMapOf(" status" to " I'm a tea pot :3" , " detail" to " server disabled plugin." )
151- )
152- val authHeader = call.request.headers[" Authorization" ]
153- authHeader ? : return @post call.respond(
154- HttpStatusCode .Unauthorized ,
155- hashMapOf(" status" to " Unauthorized" , " detail" to " Authorization header not found." )
156- )
157- if (authHeader != " Bearer ${ConfigHandler [" key" ].toString()} " ) {
158- return @post call.respond(
159- HttpStatusCode .Unauthorized ,
160- hashMapOf(" status" to " Unauthorized" , " detail" to " Invalid key." )
161- )
162- }
101+ if (checkError(call)) { return @post }
163102
164103 val result = PlayerManager .managePlayer(" kick" , call.parameters[" uuid" ]!! , call.receiveText())
165104 if (result[" error" ] == null ) {
@@ -169,21 +108,7 @@ fun Application.dashify() {
169108 }
170109 }
171110 post(" /players/{uuid}/ban" ) {
172- if (! checkIsEnabled()) return @post call.respond(
173- HttpStatusCode .fromValue(418 ),
174- hashMapOf(" status" to " I'm a tea pot :3" , " detail" to " server disabled plugin." )
175- )
176- val authHeader = call.request.headers[" Authorization" ]
177- authHeader ? : return @post call.respond(
178- HttpStatusCode .Unauthorized ,
179- hashMapOf(" status" to " Unauthorized" , " detail" to " Authorization header not found." )
180- )
181- if (authHeader != " Bearer ${ConfigHandler [" key" ].toString()} " ) {
182- return @post call.respond(
183- HttpStatusCode .Unauthorized ,
184- hashMapOf(" status" to " Unauthorized" , " detail" to " Invalid key." )
185- )
186- }
111+ if (checkError(call)) { return @post }
187112
188113 val result = PlayerManager .managePlayer(" ban" , call.parameters[" uuid" ]!! , call.receiveText())
189114 if (result[" error" ] == null ) {
@@ -193,21 +118,7 @@ fun Application.dashify() {
193118 }
194119 }
195120 get(" /stats" ) {
196- if (! checkIsEnabled()) return @get call.respond(
197- HttpStatusCode .fromValue(418 ),
198- hashMapOf(" status" to " I'm a tea pot :3" , " detail" to " server disabled plugin." )
199- )
200- val authHeader = call.request.headers[" Authorization" ]
201- authHeader ? : return @get call.respond(
202- HttpStatusCode .Unauthorized ,
203- hashMapOf(" status" to " Unauthorized" , " detail" to " Authorization header not found." )
204- )
205- if (authHeader != " Bearer ${ConfigHandler [" key" ].toString()} " ) {
206- return @get call.respond(
207- HttpStatusCode .Unauthorized ,
208- hashMapOf(" status" to " Unauthorized" , " detail" to " Invalid key." )
209- )
210- }
121+ if (checkError(call)) { return @get }
211122
212123 val stats = SystemManager .getSysInfo()
213124 stats[" jvm" ] = RuntimeManager .getMemory()
0 commit comments