at master 4.0 kB view raw
1<# 2 This file is part of NKK. 3 4 NKK is free software: you can redistribute it and/or modify it under the 5 terms of the GNU Affero General Public License as published by the Free 6 Software Foundation, either version 3 of the License, or (at your option) 7 any later version. 8 9 NKK is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 11 FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for 12 more details. 13 14 You should have received a copy of the GNU Affero General Public License 15 along with NKK. If not, see <http://www.gnu.org/licenses/>. 16#> 17 18Import-Module Mizumiya 19Import-Module PSParseHTML -Function Optimize-HTML 20 21if (-not (Get-Command tailwindcss -ErrorAction SilentlyContinue)) { 22 _warn "The tailwindcss binary is missing. Source styles will not be synced with the public style.css." 23 _warn "This is only needed during development, don't worry about it in production" 24} else { 25 Add-PodeFileWatcher -Path $PSScriptRoot -Exclude "$PSScriptRoot/public" -ScriptBlock { 26 tailwindcss --input $PSScriptRoot/tailwind/style.tw.css --output $PSScriptRoot/public/style.css 27 } 28} 29 30# Use-PodeScript -Path functions.ps1 # https://github.com/Badgerati/Pode/issues/1582 31Use-PodeScript -Path $PSScriptRoot/functions.ps1 32 33New-PodeLoggingMethod -Custom -ScriptBlock { 34 param ($Item) 35 36 $Date = $Item.UtcDate | Get-Date -Format s 37 $Query = $Item.Request.Query -eq '-' ? '' : '?' + $Item.Request.Query 38 39 $PrevCol = $PSStyle.Reset + $PSStyle.Foreground.BrightBlack 40 switch ($Item.Response.StatusCode) { 41 {$_ -ge 400} { $Col = $PSStyle.Foreground.BrightRed } 42 {$_ -ge 500} { $Col = $PSStyle.Foreground.White + $PSStyle.Background.Red } 43 default { $Col = '' } 44 } 45 46 $RequestLine = "$($Item.Host) $($Item.Request.Method) $($PSStyle.Foreground.White)$($Item.Request.Resource)$Query$PrevCol $($Item.Request.Protocol) by ""$($Item.Request.Agent)""" 47 $ResponseLine = "$Col$($Item.Response.StatusCode) $($Item.Response.StatusDescription)$PrevCol $(_format_size $Item.Response.Size)" 48 49 _request "$Date | $RequestLine >>> $ResponseLine" 50} | Enable-PodeRequestLogging -Raw 51 52New-PodeLoggingMethod -Custom -ScriptBlock { 53 param ($Item) 54 55 _warn "$($Item.Date | Get-Date -Format s) | $($Item.Level) ($($Item.Server):$($Item.ThreadId)) | $($Item.Category): $($Item.Message)" 56 $Item.StackTrace -split "`n" | % { _warn $_ } 57} | Enable-PodeErrorLogging -Raw -Levels Error, Warning, Informational 58 59Add-PodeEndpoint -Address * -Port 8081 -Protocol HTTP 60 61Set-PodeViewEngine -Type Mizumiya -Extension ps1 -ScriptBlock { 62 param ($Path, $Data) 63 . ./functions.ps1 64 65 ([String] (. $Path $Data)) | Optimize-HTML 66} 67 68Add-PodeRoute -Method GET -Path /random-splash -ScriptBlock { 69 Write-PodeViewResponse -Path random_splash 70} 71 72Add-PodeRoute -Method GET -Path /sayings -ScriptBlock { 73 Write-PodeViewResponse -Path sayings -Data @{ SayingsRoot = (Join-Path $PSScriptRoot 'sayings') } 74} 75 76Add-PodeRoute -Method GET -Path /sayings/img/* -ScriptBlock { 77 # terminate with / to avoid something like .../NKK/... <-> .../NKKsomewhere/... 78 $Root = $PSScriptRoot + '/' 79 $Path = $WebEvent.Path 80 $JoinedPath = Join-Path $Root $Path 81 82 if (-not (_check_joinedpath_is_based $Root $JoinedPath)) { 83 _warn "/sayings/img/*: query for $JoinedPath ($Path) refused!" 84 Set-PodeResponseStatus -Code 404 85 return 86 } 87 88 Write-PodeFileResponse -Path $JoinedPath 89} 90 91Add-PodeRoute -Method GET -Path /sayings/* -ScriptBlock { 92 # terminate with / to avoid something like .../NKK/... <-> .../NKKsomewhere/... 93 $Root = $PSScriptRoot + '/' 94 $Path = $WebEvent.Path + '.md' 95 $JoinedPath = Join-Path $Root $Path 96 97 if (-not (_check_joinedpath_is_based $Root $JoinedPath)) { 98 _warn "/sayings/*: query for $JoinedPath ($Path) refused!" 99 Set-PodeResponseStatus -Code 404 100 return 101 } 102 103 Write-PodeViewResponse -Path sayings_post -Data @{ Path=$JoinedPath } 104} 105 106Add-PodeRoute -Method GET -Path / -ScriptBlock { 107 Write-PodeViewResponse -Path index 108}