36 lines
		
	
	
		
			854 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			36 lines
		
	
	
		
			854 B
		
	
	
	
		
			Go
		
	
	
	
	
	
package main
 | 
						|
 | 
						|
import (
 | 
						|
	"fmt"
 | 
						|
	"log"
 | 
						|
	"net/http"
 | 
						|
	"os"
 | 
						|
	"time"
 | 
						|
)
 | 
						|
 | 
						|
func main() {
 | 
						|
	// 获取环境变量或使用默认值
 | 
						|
	port := os.Getenv("PORT")
 | 
						|
	if port == "" {
 | 
						|
		port = "8080"
 | 
						|
	}
 | 
						|
 | 
						|
	// 创建一个简单的HTTP服务器
 | 
						|
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
 | 
						|
		hostname, _ := os.Hostname()
 | 
						|
		fmt.Fprintf(w, "Hello from DevContainer!\n")
 | 
						|
		fmt.Fprintf(w, "Server time: %s\n", time.Now().Format(time.RFC1123))
 | 
						|
		fmt.Fprintf(w, "Hostname: %s\n", hostname)
 | 
						|
		fmt.Fprintf(w, "Remote Address: %s\n", r.RemoteAddr)
 | 
						|
		fmt.Fprintf(w, "User-Agent: %s\n", r.UserAgent())
 | 
						|
	})
 | 
						|
 | 
						|
	http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
 | 
						|
		w.WriteHeader(http.StatusOK)
 | 
						|
		fmt.Fprintf(w, "Healthy")
 | 
						|
	})
 | 
						|
 | 
						|
	// 启动服务器
 | 
						|
	log.Printf("Starting server on port %s", port)
 | 
						|
	log.Fatal(http.ListenAndServe(":"+port, nil))
 | 
						|
} |