94 lines
3.7 KiB
VB.net
94 lines
3.7 KiB
VB.net
' OWIN Startup 클래스
|
|
Imports Owin
|
|
Imports Microsoft.Owin.StaticFiles
|
|
Imports Microsoft.Owin.FileSystems
|
|
|
|
Namespace WebServer
|
|
|
|
''' <summary>
|
|
''' OWIN 시작 구성 클래스
|
|
''' </summary>
|
|
Public Class Startup
|
|
|
|
' 정적 필드로 RootPath 저장
|
|
Public Shared RootPath As String = String.Empty
|
|
|
|
''' <summary>
|
|
''' OWIN 미들웨어 구성
|
|
''' </summary>
|
|
''' <param name="app">App builder</param>
|
|
Public Sub Configuration(app As IAppBuilder)
|
|
' 루트 경로 확인
|
|
If String.IsNullOrEmpty(RootPath) Then
|
|
RootPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot")
|
|
End If
|
|
|
|
' 정적 파일 서빙을 위한 파일 시스템 설정
|
|
Dim fileSystem = New PhysicalFileSystem(RootPath)
|
|
|
|
' 정적 파일 옵션 설정
|
|
Dim options As New FileServerOptions() With {
|
|
.RequestPath = New Microsoft.Owin.PathString(""),
|
|
.FileSystem = fileSystem,
|
|
.EnableDirectoryBrowsing = False
|
|
}
|
|
|
|
' MIME 타입 매핑 추가 (필요한 경우)
|
|
options.StaticFileOptions.ContentTypeProvider = New CustomContentTypeProvider()
|
|
|
|
' 정적 파일 미들웨어 사용
|
|
app.UseFileServer(options)
|
|
|
|
' 기본 핸들러 (루트 경로 접근 시)
|
|
app.Run(Function(context)
|
|
' 루트 경로 접근 시 index.html로 리다이렉트
|
|
If context.Request.Path.Value = "/" OrElse context.Request.Path.Value = "" Then
|
|
Dim indexPath = System.IO.Path.Combine(RootPath, "index.html")
|
|
If System.IO.File.Exists(indexPath) Then
|
|
context.Response.Redirect("/index.html")
|
|
Else
|
|
context.Response.StatusCode = 200
|
|
context.Response.ContentType = "text/html"
|
|
Dim tcs = New System.Threading.Tasks.TaskCompletionSource(Of Integer)()
|
|
context.Response.WriteAsync("<html><body><h1>ECO2 Web Server</h1><p>서버가 정상적으로 실행 중입니다.</p><p>루트 경로: " & RootPath & "</p></body></html>")
|
|
tcs.SetResult(0)
|
|
Return tcs.Task
|
|
End If
|
|
End If
|
|
Dim tcs2 = New System.Threading.Tasks.TaskCompletionSource(Of Integer)()
|
|
tcs2.SetResult(0)
|
|
Return tcs2.Task
|
|
End Function)
|
|
End Sub
|
|
|
|
End Class
|
|
|
|
''' <summary>
|
|
''' 커스텀 Content Type Provider
|
|
''' </summary>
|
|
Public Class CustomContentTypeProvider
|
|
Inherits Microsoft.Owin.StaticFiles.ContentTypes.FileExtensionContentTypeProvider
|
|
|
|
Public Sub New()
|
|
MyBase.New()
|
|
|
|
' 추가 MIME 타입 매핑 (필요한 경우)
|
|
' 기본적으로 일반적인 타입들은 이미 포함되어 있음
|
|
If Not Me.Mappings.ContainsKey(".json") Then
|
|
Me.Mappings.Add(".json", "application/json")
|
|
End If
|
|
If Not Me.Mappings.ContainsKey(".svg") Then
|
|
Me.Mappings.Add(".svg", "image/svg+xml")
|
|
End If
|
|
If Not Me.Mappings.ContainsKey(".woff") Then
|
|
Me.Mappings.Add(".woff", "font/woff")
|
|
End If
|
|
If Not Me.Mappings.ContainsKey(".woff2") Then
|
|
Me.Mappings.Add(".woff2", "font/woff2")
|
|
End If
|
|
End Sub
|
|
|
|
End Class
|
|
|
|
End Namespace
|