mIRC Scripting ile basit bir rest api modeli oluşturdum. Dileyen alıp geliştirebilir.
Kod: Kodu kopyalamak için üzerine çift tıklayın!
;### Defines config ###
alias -l _host return 127.0.0.1
alias -l _port return 8001
alias -l sw sockwrite -n $1-
alias -l test_user return SuLh
alias -l test_pass return 123456
alias config {
if (!$isid) { echo -a Usage: $+($,config().host) | return }
if ($prop == $null) {
return $+($_host,:,$_port)
}
if ($prop == host) return $_host
if ($prop == port) return $_port
}
;### Validatator ###
alias validate {
var %check = $true, %i = 1
set -u0 %validate _host,_port,config, post_var, $get_var,response_json
tokenize 44 %validate
while ($0 >= %i) { if (!$isalias($eval($+($,%i), 2))) %check = $false | inc %i }
if ($isid) return %check
else echo -a %check
}
;### Helpers ###
alias -l post_var {
var %json_data = $1-, %json_key = $token(%json_data, 1, 58), %json_val = $remove($token(%json_data,2,58),$chr(32))
if ($prop == key) return %json_key
else return %json_val
}
alias -l _middleware {
}
;### Route Methods ###
alias _routes return get_index,post_login,post_logout
alias validate_route return $iif($regex($_routes, $+(\b,$replace($1,*,\*),\b)), $true, $false)
alias handle_route {
if ($prop == get) {
var %route = $1, %full_route = $2, %sw = $3, %eval = $+(get_, %route)
if ($token(%full_route,0,63) > 1) {
var %params = $token(%full_route, 2, 63)
}
else {
var %params = $null
}
_headers_html %sw
$eval(%eval %sw %params, 2)
}
elseif ($prop == post) {
var %route = $1, %sw = $2, %eval = %route
%eval %sw %post_value
unset %post_value
sockclose %sw
}
}
;### Methods ###
alias post_login {
var %user, %pass, %sw = $1
tokenize 124 $2-
var %i = 1
while ($0 >= %i) {
var %data $eval($+($,%i), 2)
if ($post_var(%data).key == user) {
%user = $post_var(%data).val
}
elseif ($post_var(%data).key == pass) {
%pass = $post_var(%data).val
}
inc %i
}
if (%user == $test_user && %pass = $test_pass) {
$response_json(Giris basarili!, %sw).success
}
else {
$response_json(Giris basarisiz!, %sw).bad
}
}
;### Get Pages ###
alias -l get_index {
sw $1 index page with $2
}
;json
alias -l response_json {
if ($prop == bad) {
var %json $json_create(false, $1)
_headers $2 $len(%json)
sw $2 %json
}
elseif ($prop == success) {
var %json $json_create(true, $1)
_headers $2 $len(%json)
sw $2 %json
}
}
alias -l json_create {
var %json = $chr(123) "status": $1 $+ $chr(44)
%json = %json $+ "message": $+(", $2,") $chr(125)
return %json
}
alias _start {
if (!$validate) return $response_json(Missing some functions).bad
socklisten api $config().port
}
alias _stop if ($sock(api)) sockclose api
on *:socklisten:api: sockaccept $+(api_res_,$r(1,999999))
on *:sockread:api_res_*: {
var %s | sockread -f %s
if ($regex(%s, /^GET\s/ig) || $regex(%s, /^POST\s/ig)) {
tokenize 32 %s | %method = $1 | %route = $+($lower(%method),_,$right($token($2, 1, 63),-1))
if (!$validate_route(%route)) {
$response_json($_bad_request_route, $sockname).bad
sockclose $sockname | return
}
elseif (%method == GET) {
$handle_route($right($token($2, 1, 63),-1), $2, $sockname).get
sockclose $sockname | return
}
}
if ($chr(34) isin %s) {
var %key = $remove($token(%s, 1, 58), "), %val = $remove($remove($token(%s, 2, 58), "), $chr(44))
echo -a %key --- %val
set %post_value %post_value $+ %key $+ : $+ %val $+ |
}
if ($regex(%s, /(".+"):\s(".+")$/i)) {
$handle_route(%route, $sockname).post
}
}
alias _bad_request_route return Bad Request: Route not found
alias _headers_html {
sw $1 HTTP/1.1 200
sw $1 Connection: Keep-Alive
sw $1 Content-Type: text/html; charset=UTF-8
sw $1 Vary: Accept-Encoding
sw $1 Date: $fulldate
sw $1 $crlf
}
alias _headers {
sw $1 HTTP/1.1 200
sw $1 Connection: Keep-Alive
sw $1 Content-Type: application/json; charset=UTF-8
sw $1 Content-Length: $2
sw $1 Date: $fulldate
sw $1 Cache-Control: no-cache
sw $1 Expires: $fulldate
sw $1 Access-Control-Allow-Origin: *
sw $1 Server: SuLh Api Server
sw $1 $crlf
}