' OWIN 정적 파일 호스팅 서버 사용 예제 ' ' 이 파일은 StaticFileServer를 사용하는 방법을 보여줍니다. ' 실제 사용 시에는 MdiMain.vb 또는 필요한 폼에서 사용하세요. Imports Eco2Ar.WebServer Imports System.IO Namespace Examples ''' ''' 웹 서버 사용 예제 ''' Public Class WebServerExample Private server As StaticFileServer ''' ''' 예제 1: 기본 사용법 ''' Public Sub Example1_BasicUsage() ' 1. 정적 파일을 서빙할 디렉토리 경로 설정 Dim wwwrootPath As String = Path.Combine(Application.StartupPath, "wwwroot") ' 2. 서버 인스턴스 생성 (포트 58123 사용) server = New StaticFileServer(wwwrootPath, 58123) ' 3. 서버 시작 server.Start() ' 4. 브라우저에서 열기 (선택사항) ' server.OpenInBrowser() MsgBox("웹 서버가 시작되었습니다." & vbCrLf & _ "URL: " & server.BaseUrl & vbCrLf & _ "루트 경로: " & server.RootPath, _ MsgBoxStyle.Information, "웹 서버") End Sub ''' ''' 예제 2: MdiMain에서 사용 (프로그램 시작 시 서버 시작) ''' Public Sub Example2_IntegrationWithMdiMain() ' MdiMain.vb의 MdiMain_Load 이벤트에 추가: ' ' Private webServer As StaticFileServer ' ' Private Sub MdiMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' ' ... 기존 코드 ... ' ' Try ' ' 웹 서버 초기화 및 시작 ' Dim wwwPath = Path.Combine(Application.StartupPath, "wwwroot") ' webServer = New StaticFileServer(wwwPath) ' webServer.Start() ' Catch ex As Exception ' ' 웹 서버 시작 실패해도 프로그램은 계속 실행 ' Debug.WriteLine("웹 서버 시작 실패: " & ex.Message) ' End Try ' End Sub ' ' Private Sub MdiMain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing ' ' ... 기존 코드 ... ' ' ' 웹 서버 정리 ' If webServer IsNot Nothing Then ' webServer.Stop() ' webServer.Dispose() ' End If ' End Sub End Sub ''' ''' 예제 3: 특정 파일 브라우저에서 열기 ''' Public Sub Example3_OpenSpecificFile() If server Is Nothing OrElse Not server.IsRunning Then MsgBox("서버가 실행 중이 아닙니다.", MsgBoxStyle.Exclamation, "오류") Return End If ' 리포트 HTML 파일 열기 server.OpenFileInBrowser("report.html") ' 또는 URL만 가져오기 Dim reportUrl As String = server.GetFileUrl("reports/2025/report.html") MsgBox("리포트 URL: " & reportUrl, MsgBoxStyle.Information, "URL") End Sub ''' ''' 예제 4: 동적으로 HTML 파일 생성 후 표시 ''' Public Sub Example4_GenerateAndDisplay() If server Is Nothing OrElse Not server.IsRunning Then MsgBox("서버가 실행 중이 아닙니다.", MsgBoxStyle.Exclamation, "오류") Return End If ' HTML 파일 생성 Dim htmlContent As String = "" & vbCrLf & _ "" & vbCrLf & _ "" & vbCrLf & _ " " & vbCrLf & _ " ECO2 리포트" & vbCrLf & _ " " & vbCrLf & _ "" & vbCrLf & _ "" & vbCrLf & _ "

건물 에너지 분석 리포트

" & vbCrLf & _ " " & vbCrLf & _ " " & vbCrLf & _ " " & vbCrLf & _ " " & vbCrLf & _ "
항목
1차 에너지 소요량123.45 kWh/m²·year
CO2 배출량45.67 kg/m²·year
" & vbCrLf & _ "" & vbCrLf & _ "" ' 파일 저장 Dim reportPath As String = Path.Combine(server.RootPath, "report.html") File.WriteAllText(reportPath, htmlContent, System.Text.Encoding.UTF8) ' 브라우저에서 열기 server.OpenFileInBrowser("report.html") End Sub ''' ''' 예제 5: 폼 버튼에서 웹 리포트 생성 및 표시 ''' Public Sub Example5_ButtonClick() ' 폼의 버튼 클릭 이벤트에 추가: ' ' Private Sub btnShowWebReport_Click(sender As Object, e As EventArgs) Handles btnShowWebReport.Click ' Try ' ' 서버가 실행 중이 아니면 시작 ' If webServer Is Nothing OrElse Not webServer.IsRunning Then ' Dim wwwPath = Path.Combine(Application.StartupPath, "wwwroot") ' webServer = New StaticFileServer(wwwPath) ' webServer.Start() ' End If ' ' ' HTML 리포트 생성 ' GenerateHtmlReport() ' ' ' 브라우저에서 열기 ' webServer.OpenFileInBrowser("report.html") ' Catch ex As Exception ' MsgBox("리포트 생성 실패: " & ex.Message, MsgBoxStyle.Critical, "오류") ' End Try ' End Sub ' ' Private Sub GenerateHtmlReport() ' ' 실제 데이터를 사용하여 HTML 생성 ' Dim html As String = BuildHtmlReport(DSET1, DSETR1, Result2) ' Dim path As String = Path.Combine(webServer.RootPath, "report.html") ' File.WriteAllText(path, html, System.Text.Encoding.UTF8) ' End Sub End Sub ''' ''' 서버 중지 ''' Public Sub StopServer() If server IsNot Nothing Then server.Stop() server.Dispose() server = Nothing End If End Sub End Class ''' ''' 간단한 HTML 빌더 헬퍼 클래스 ''' Public Class HtmlReportBuilder Private html As System.Text.StringBuilder Public Sub New(title As String) html = New System.Text.StringBuilder() html.AppendLine("") html.AppendLine("") html.AppendLine("") html.AppendLine(" ") html.AppendLine(" " & title & "") html.AppendLine(" ") html.AppendLine("") html.AppendLine("") html.AppendLine("

" & title & "

") End Sub Public Sub AddSection(sectionTitle As String) html.AppendLine("
") html.AppendLine("

" & sectionTitle & "

") End Sub Public Sub StartTable(ParamArray headers As String()) html.AppendLine(" ") html.Append(" ") For Each headerItem As String In headers html.Append("") Next html.AppendLine("") End Sub Public Sub AddRow(ParamArray cells As String()) html.Append(" ") For Each cellItem As String In cells html.Append("") Next html.AppendLine("") End Sub Public Sub EndTable() html.AppendLine("
" & headerItem & "
" & cellItem & "
") End Sub Public Sub EndSection() html.AppendLine("
") End Sub Public Function Build() As String html.AppendLine("") html.AppendLine("") Return html.ToString() End Function End Class End Namespace