package main

import (
	"fmt"
	"net/http"
	"os"
)

func main() {
	// 定义静态文件服务的根目录
	dir := "/www/wwwroot"

	// 创建文件服务器
	fileServer := http.FileServer(http.Dir(dir))

	// 定义处理函数
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		// 记录请求
		fmt.Printf("[%s] %s %s\n", r.Method, r.URL.Path, r.RemoteAddr)

		// 处理静态文件
		fileServer.ServeHTTP(w, r)
	})

	// http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
	// 	w.Write([]byte(`hello world:` + r.RequestURI))
	// })

	// 启动服务器
	port := "8088"
	fmt.Printf("Server starting on http://localhost:%s\n", port)
	fmt.Printf("Serving files from: %s\n", dir)
	fmt.Printf("Press Ctrl+C to stop server\n")

	err := http.ListenAndServe(":"+port, nil)
	if err != nil {
		fmt.Printf("Error starting server: %v\n", err)
		os.Exit(1)
	}
}
