55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
/*
|
||
main.go - Web服务器主程序
|
||
这是一个简单的HTTP Web服务器,演示了Go语言的网络编程应用
|
||
*/
|
||
|
||
package main
|
||
|
||
import (
|
||
"fmt"
|
||
"log"
|
||
"os"
|
||
"os/signal"
|
||
"syscall"
|
||
|
||
"webserver/server"
|
||
)
|
||
|
||
func main() {
|
||
// 创建服务器实例
|
||
srv := server.NewServer(":8080")
|
||
|
||
// 设置路由
|
||
srv.SetupRoutes()
|
||
|
||
// 启动信息
|
||
fmt.Println("🚀 Go Web服务器启动中...")
|
||
fmt.Println("📍 地址: http://localhost:8080")
|
||
fmt.Println("📊 健康检查: http://localhost:8080/health")
|
||
fmt.Println("📚 API文档: http://localhost:8080/api")
|
||
fmt.Println("按 Ctrl+C 停止服务器")
|
||
fmt.Println()
|
||
|
||
// 启动服务器(在goroutine中)
|
||
go func() {
|
||
if err := srv.Start(); err != nil {
|
||
log.Printf("❌ 服务器启动失败: %v", err)
|
||
os.Exit(1)
|
||
}
|
||
}()
|
||
|
||
// 等待中断信号
|
||
quit := make(chan os.Signal, 1)
|
||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||
<-quit
|
||
|
||
fmt.Println("\n🛑 正在关闭服务器...")
|
||
|
||
// 优雅关闭服务器
|
||
if err := srv.Shutdown(); err != nil {
|
||
log.Printf("❌ 服务器关闭失败: %v", err)
|
||
} else {
|
||
fmt.Println("✅ 服务器已安全关闭")
|
||
}
|
||
}
|