245 lines
9.8 KiB
VB.net
245 lines
9.8 KiB
VB.net
' OWIN 정적 파일 호스팅 서버 사용 예제
|
|
'
|
|
' 이 파일은 StaticFileServer를 사용하는 방법을 보여줍니다.
|
|
' 실제 사용 시에는 MdiMain.vb 또는 필요한 폼에서 사용하세요.
|
|
|
|
Imports Eco2Ar.WebServer
|
|
Imports System.IO
|
|
|
|
Namespace Examples
|
|
|
|
''' <summary>
|
|
''' 웹 서버 사용 예제
|
|
''' </summary>
|
|
Public Class WebServerExample
|
|
|
|
Private server As StaticFileServer
|
|
|
|
''' <summary>
|
|
''' 예제 1: 기본 사용법
|
|
''' </summary>
|
|
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
|
|
|
|
''' <summary>
|
|
''' 예제 2: MdiMain에서 사용 (프로그램 시작 시 서버 시작)
|
|
''' </summary>
|
|
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
|
|
|
|
''' <summary>
|
|
''' 예제 3: 특정 파일 브라우저에서 열기
|
|
''' </summary>
|
|
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
|
|
|
|
''' <summary>
|
|
''' 예제 4: 동적으로 HTML 파일 생성 후 표시
|
|
''' </summary>
|
|
Public Sub Example4_GenerateAndDisplay()
|
|
If server Is Nothing OrElse Not server.IsRunning Then
|
|
MsgBox("서버가 실행 중이 아닙니다.", MsgBoxStyle.Exclamation, "오류")
|
|
Return
|
|
End If
|
|
|
|
' HTML 파일 생성
|
|
Dim htmlContent As String = "<!DOCTYPE html>" & vbCrLf & _
|
|
"<html>" & vbCrLf & _
|
|
"<head>" & vbCrLf & _
|
|
" <meta charset=""utf-8"">" & vbCrLf & _
|
|
" <title>ECO2 리포트</title>" & vbCrLf & _
|
|
" <style>" & vbCrLf & _
|
|
" body { font-family: 'Malgun Gothic', sans-serif; margin: 20px; }" & vbCrLf & _
|
|
" h1 { color: #2c3e50; }" & vbCrLf & _
|
|
" table { border-collapse: collapse; width: 100%; }" & vbCrLf & _
|
|
" th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }" & vbCrLf & _
|
|
" th { background-color: #4CAF50; color: white; }" & vbCrLf & _
|
|
" </style>" & vbCrLf & _
|
|
"</head>" & vbCrLf & _
|
|
"<body>" & vbCrLf & _
|
|
" <h1>건물 에너지 분석 리포트</h1>" & vbCrLf & _
|
|
" <table>" & vbCrLf & _
|
|
" <tr><th>항목</th><th>값</th></tr>" & vbCrLf & _
|
|
" <tr><td>1차 에너지 소요량</td><td>123.45 kWh/m²·year</td></tr>" & vbCrLf & _
|
|
" <tr><td>CO2 배출량</td><td>45.67 kg/m²·year</td></tr>" & vbCrLf & _
|
|
" </table>" & vbCrLf & _
|
|
"</body>" & vbCrLf & _
|
|
"</html>"
|
|
|
|
' 파일 저장
|
|
Dim reportPath As String = Path.Combine(server.RootPath, "report.html")
|
|
File.WriteAllText(reportPath, htmlContent, System.Text.Encoding.UTF8)
|
|
|
|
' 브라우저에서 열기
|
|
server.OpenFileInBrowser("report.html")
|
|
End Sub
|
|
|
|
''' <summary>
|
|
''' 예제 5: 폼 버튼에서 웹 리포트 생성 및 표시
|
|
''' </summary>
|
|
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
|
|
|
|
''' <summary>
|
|
''' 서버 중지
|
|
''' </summary>
|
|
Public Sub StopServer()
|
|
If server IsNot Nothing Then
|
|
server.Stop()
|
|
server.Dispose()
|
|
server = Nothing
|
|
End If
|
|
End Sub
|
|
|
|
End Class
|
|
|
|
''' <summary>
|
|
''' 간단한 HTML 빌더 헬퍼 클래스
|
|
''' </summary>
|
|
Public Class HtmlReportBuilder
|
|
|
|
Private html As System.Text.StringBuilder
|
|
|
|
Public Sub New(title As String)
|
|
html = New System.Text.StringBuilder()
|
|
html.AppendLine("<!DOCTYPE html>")
|
|
html.AppendLine("<html>")
|
|
html.AppendLine("<head>")
|
|
html.AppendLine(" <meta charset=""utf-8"">")
|
|
html.AppendLine(" <title>" & title & "</title>")
|
|
html.AppendLine(" <style>")
|
|
html.AppendLine(" body { font-family: 'Malgun Gothic', sans-serif; margin: 20px; }")
|
|
html.AppendLine(" h1 { color: #2c3e50; }")
|
|
html.AppendLine(" h2 { color: #34495e; border-bottom: 2px solid #3498db; padding-bottom: 5px; }")
|
|
html.AppendLine(" table { border-collapse: collapse; width: 100%; margin: 20px 0; }")
|
|
html.AppendLine(" th, td { border: 1px solid #ddd; padding: 12px; text-align: left; }")
|
|
html.AppendLine(" th { background-color: #4CAF50; color: white; }")
|
|
html.AppendLine(" tr:nth-child(even) { background-color: #f2f2f2; }")
|
|
html.AppendLine(" .section { margin: 30px 0; }")
|
|
html.AppendLine(" </style>")
|
|
html.AppendLine("</head>")
|
|
html.AppendLine("<body>")
|
|
html.AppendLine(" <h1>" & title & "</h1>")
|
|
End Sub
|
|
|
|
Public Sub AddSection(sectionTitle As String)
|
|
html.AppendLine(" <div class=""section"">")
|
|
html.AppendLine(" <h2>" & sectionTitle & "</h2>")
|
|
End Sub
|
|
|
|
Public Sub StartTable(ParamArray headers As String())
|
|
html.AppendLine(" <table>")
|
|
html.Append(" <tr>")
|
|
For Each headerItem As String In headers
|
|
html.Append("<th>" & headerItem & "</th>")
|
|
Next
|
|
html.AppendLine("</tr>")
|
|
End Sub
|
|
|
|
Public Sub AddRow(ParamArray cells As String())
|
|
html.Append(" <tr>")
|
|
For Each cellItem As String In cells
|
|
html.Append("<td>" & cellItem & "</td>")
|
|
Next
|
|
html.AppendLine("</tr>")
|
|
End Sub
|
|
|
|
Public Sub EndTable()
|
|
html.AppendLine(" </table>")
|
|
End Sub
|
|
|
|
Public Sub EndSection()
|
|
html.AppendLine(" </div>")
|
|
End Sub
|
|
|
|
Public Function Build() As String
|
|
html.AppendLine("</body>")
|
|
html.AppendLine("</html>")
|
|
Return html.ToString()
|
|
End Function
|
|
|
|
End Class
|
|
|
|
End Namespace
|