Skip to content

Commit

Permalink
webserver update
Browse files Browse the repository at this point in the history
  • Loading branch information
steve02081504 committed Jul 8, 2024
1 parent 4e20537 commit 009e632
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 23 deletions.
14 changes: 4 additions & 10 deletions src/WebServer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,20 @@
var playing = false
userInput.addEventListener("focus", event => {
if (playing) MIDIjs.resume()
else MIDIjs.play('./bgm', true)
else MIDIjs.play('./bgm.mid', true)
playing = true;
})
userInput.addEventListener("blur", event => MIDIjs.pause())
const a = document.getElementById("downloadLink")
function compileCode() {
if (!userInput.value) {
alert("No code to compile")
return
}
fetch("./compile", {
if (!userInput.value) return alert("No code to compile")
fetch("./api/compile", {
method: "POST",
body: userInput.value
})
.then(response => response.blob())
.then(blob => {
if (blob.type == "text/plain") {
blob.text().then(alert)
return
}
if (blob.type == "text/plain") return blob.text().then(alert)
if (blob.type == "application/octet-stream") {
a.href = window.URL.createObjectURL(blob)
a.click()
Expand Down
20 changes: 12 additions & 8 deletions src/WebServer/main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ The maximum number of requests per minute per IP
The maximum size of the cached file
.PARAMETER MaxScriptFileSize
The maximum size of the script file
.PARAMETER CacheDir
The directory to store the cached files
.PARAMETER Localize
The language code to be used for server-side logging
.PARAMETER help
Expand All @@ -36,6 +38,7 @@ param (
$ReqLimitPerMin = 5,
$MaxCachedFileSize = 32mb,
$MaxScriptFileSize = 2mb,
$CacheDir = "$PSScriptRoot/outputs",
#_if PSScript
[ArgumentCompleter({
Param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)
Expand Down Expand Up @@ -116,7 +119,7 @@ function HandleWebCompileRequest($userInput, $context) {
foreach ($byte in $userInputHash) {
$userInputHashStr += $byte.ToString('x2')
}
$compiledExePath = "$PSScriptRoot/outputs/$userInputHashStr.bin"
$compiledExePath = "$CacheDir/$userInputHashStr.bin"
#if match cached file
if (Test-Path -Path $compiledExePath -ErrorAction Ignore) {
$context.Response.ContentType = "application/octet-stream"
Expand All @@ -126,12 +129,12 @@ function HandleWebCompileRequest($userInput, $context) {
$runspace = [powershell]::Create()
$runspace.RunspacePool = $runspacePool
$AsyncResult = $runspace.AddScript({
param ($userInput, $Response, $ScriptRoot, $compiledExePath, $clientIP)
param ($userInput, $Response, $ScriptRoot, $CacheDir, $compiledExePath, $clientIP)

# 加载ps12exe用于处理编译请求
Import-Module $ScriptRoot/../../ps12exe.psm1 -ErrorAction Stop

New-Item -Path $ScriptRoot/outputs -ItemType Directory -Force | Out-Null
New-Item -Path $CacheDir -ItemType Directory -Force | Out-Null

# 编译代码
try {
Expand All @@ -156,7 +159,8 @@ function HandleWebCompileRequest($userInput, $context) {
$Response.Close()
}).
AddArgument($userInput).AddArgument($context.Response).
AddArgument($PSScriptRoot).AddArgument($compiledExePath).AddArgument($clientIP).
AddArgument($PSScriptRoot).AddArgument($CacheDir).
AddArgument($compiledExePath).AddArgument($clientIP).
BeginInvoke()
$AsyncResultArray.Add(@{
AsyncHandle = $AsyncResult
Expand All @@ -174,15 +178,15 @@ function HandleRequest($context) {
}
$RequestUrl = $RequestUrl.Substring($HostSubUrl.Length)
switch ($RequestUrl) {
'/compile' {
'/api/compile' {
$Reader = New-Object System.IO.StreamReader($context.Request.InputStream)
$userInput = $Reader.ReadToEnd()
$Reader.Close()
$Reader.Dispose()
HandleWebCompileRequest $userInput $context
return
}
'/bgm' {
'/bgm.mid' {
# midi file
$context.Response.ContentType = "audio/midi"
$buffer = [System.IO.File]::ReadAllBytes("$PSScriptRoot/../bin/Unravel.mid")
Expand All @@ -208,7 +212,7 @@ function HandleRequest($context) {
$context.Response.Close()
}
function AutoCacheClear {
$Cache = Get-ChildItem -Path $PSScriptRoot/outputs -ErrorAction Ignore
$Cache = Get-ChildItem -Path $CacheDir -ErrorAction Ignore
if ($MaxCachedFileSize -lt ($Cache | Measure-Object -Property Length -Sum).Sum) {
$Cache | Sort-Object -Property LastAccessTime -Descending |
Select-Object -First $([math]::Floor($Cache.Count / 2)) |
Expand Down Expand Up @@ -261,7 +265,7 @@ finally {
# Restore Console Window Title
$Host.UI.RawUI.WindowTitle = $BackUpTitle
# 清空缓存
Remove-Item $PSScriptRoot/outputs/* -Recurse -Force -ErrorAction Ignore
Remove-Item $CacheDir/* -Recurse -Force -ErrorAction Ignore
}
#_else
#_require ps12exe
Expand Down
3 changes: 2 additions & 1 deletion src/locale/en-UK.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
WebServerHelpData = @{
title = "Usage:"
Usage = "Start-ps12exeWebServer [[-HostUrl] '<url>'] [-MaxCompileThreads '<uint>'] [-MaxCompileTime '<uint>']
[-ReqLimitPerMin '<uint>'] [-MaxCachedFileSize '<uint>'] [-MaxScriptFileSize '<uint>']
[-ReqLimitPerMin '<uint>'] [-MaxCachedFileSize '<uint>'] [-MaxScriptFileSize '<uint>'] [-CacheDir '<path>']
[-Localize '<language code>'] [-help]"
PrarmsData = [ordered]@{
HostUrl = "The HTTP server address to register."
Expand All @@ -30,6 +30,7 @@
ReqLimitPerMin = "The maximum number of requests per minute per IP."
MaxCachedFileSize = "The maximum size of the cached file."
MaxScriptFileSize = "The maximum size of the script file."
CacheDir = "The directory to store the cached files."
Localize = "The language code to be used for server-side logging."
help = "Display this help information."
}
Expand Down
3 changes: 2 additions & 1 deletion src/locale/es-ES.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
WebServerHelpData = @{
title = "Uso:"
Usage = "Start-ps12exeWebServer [[-HostUrl] '<url>'] [-MaxCompileThreads '<uint>'] [-MaxCompileTime '<uint>']
[-ReqLimitPerMin '<uint>'] [-MaxCachedFileSize '<uint>'] [-MaxScriptFileSize '<uint>']
[-ReqLimitPerMin '<uint>'] [-MaxCachedFileSize '<uint>'] [-MaxScriptFileSize '<uint>'] [-CacheDir '<path>']
[-Localize '<código de idioma>'] [-help]"
PrarmsData = [ordered]@{
HostUrl = "La dirección del servidor HTTP que se registrará."
Expand All @@ -30,6 +30,7 @@
ReqLimitPerMin = "El número de solicitudes por minuto para cada IP."
MaxCachedFileSize = "El tamaño maximo de archivo caché."
MaxScriptFileSize = "El tamaño maximo de archivo de código."
CacheDir = "El directorio donde se almacenan los archivos caché."
Localize = "El código de idioma para el registro en el lado del servidor."
help = "Mostrar esta información de ayuda."
}
Expand Down
3 changes: 2 additions & 1 deletion src/locale/hi-IN.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
WebServerHelpData = @{
title = "उपयोग:"
Usage = "Start-ps12exeWebServer [[-HostUrl] '<url>'] [-MaxCompileThreads '<uint>'] [-MaxCompileTime '<uint>']
[-ReqLimitPerMin '<uint>'] [-MaxCachedFileSize '<uint>'] [-MaxScriptFileSize '<uint>']
[-ReqLimitPerMin '<uint>'] [-MaxCachedFileSize '<uint>'] [-MaxScriptFileSize '<uint>'] [-CacheDir '<पथ>']
[-Localize '<भाषा कोड>'] [-help]"
PrarmsData = [ordered]@{
HostUrl = "रजिस्टर करने के लिए HTTP सर्वर पता।"
Expand All @@ -30,6 +30,7 @@
ReqLimitPerMin = "फ़ाइल प्रति मिनट की अनुरोध सीमा।"
MaxCachedFileSize = "अधिकतम कैश फ़ाइल का आकार।"
MaxScriptFileSize = "अधिकतम स्क्रिप्ट फ़ाइल का आकार।"
CacheDir = "अधिकतम कैश फ़ाइल का डाइरेक्टरी पथ।"
Localize = "सर्वर साइड रिकॉर्ड करने के लिए उपयोग किए जाने वाले भाषा कोड।"
help = "इस मदद सूचना को दिखाएँ।"
}
Expand Down
3 changes: 2 additions & 1 deletion src/locale/ja-JP.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
WebServerHelpData = @{
title = "使用方法:"
Usage = "Start-ps12exeWebServer [[-HostUrl] '<url>'] [-MaxCompileThreads '<uint>'] [-MaxCompileTime '<uint>']
[-ReqLimitPerMin '<uint>'] [-MaxCachedFileSize '<uint>'] [-MaxScriptFileSize '<uint>']
[-ReqLimitPerMin '<uint>'] [-MaxCachedFileSize '<uint>'] [-MaxScriptFileSize '<uint>'] [-CacheDir '<パス>']
[-Localize '<言語コード>'] [-help]"
PrarmsData = [ordered]@{
HostUrl = "登録するHTTPサーバーのアドレス。"
Expand All @@ -30,6 +30,7 @@
ReqLimitPerMin = "IPアドレスごとの1分間のリクエスト制限。"
MaxCachedFileSize = "最大キャッシュファイルサイズ。"
MaxScriptFileSize = "最大スクリプトファイルサイズ。"
CacheDir = "キャッシュディレクトリ。"
Localize = "サーバー側の記録に使用する言語コード。"
help = "このヘルプ情報を表示します。"
}
Expand Down
3 changes: 2 additions & 1 deletion src/locale/zh-CN.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
WebServerHelpData = @{
title = "用法:"
Usage = "Start-ps12exeWebServer [[-HostUrl] '<url>'] [-MaxCompileThreads '<uint>'] [-MaxCompileTime '<uint>']
[-ReqLimitPerMin '<uint>'] [-MaxCachedFileSize '<uint>'] [-MaxScriptFileSize '<uint>']
[-ReqLimitPerMin '<uint>'] [-MaxCachedFileSize '<uint>'] [-MaxScriptFileSize '<uint>'] [-CacheDir '<路径>']
[-Localize '<语言代码>'] [-help]"
PrarmsData = [ordered]@{
HostUrl = "要注册的 HTTP 服务器地址。"
Expand All @@ -30,6 +30,7 @@
ReqLimitPerMin = "每个IP每分钟的请求限制。"
MaxCachedFileSize = "最大缓存文件大小。"
MaxScriptFileSize = "最大脚本文件大小。"
CacheDir = "缓存目录。"
Localize = "服务器端记录要使用的语言代码。"
help = "显示此帮助信息。"
}
Expand Down

0 comments on commit 009e632

Please sign in to comment.