LearnGO/firstApp/main.go
2024-09-19 22:09:09 -04:00

38 lines
856 B
Go

package main
import (
"fmt"
"net/http"
"os"
"time"
)
// Serve the index.html file
func serveHTML(w http.ResponseWriter, r *http.Request) {
file, err := os.Open("index.html")
if err != nil {
http.Error(w, "File not found", http.StatusNotFound)
return
}
defer file.Close()
w.Header().Set("Content-Type", "text/html")
http.ServeContent(w, r, "index.html", time.Now(), file)
}
// Serve the current time as plain text
func serveTime(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, time.Now().Format(time.RFC1123))
}
func main() {
// Serve the static CSS file from the "static" directory
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
http.HandleFunc("/", serveHTML)
http.HandleFunc("/time", serveTime)
fmt.Println("Starting server on :8085")
http.ListenAndServe(":8085", nil)
}