Files
golang/golang-learning/10-projects/03-web-server/main.go
2025-08-24 13:01:09 +08:00

55 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
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("✅ 服务器已安全关闭")
}
}