40 lines
925 B
Go
40 lines
925 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
// Serve the index.html file
|
|
func serveHTML(w http.ResponseWriter, r *http.Request) {
|
|
// Open the file
|
|
file, err := os.Open("index.html")
|
|
if err != nil {
|
|
http.Error(w, "File not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
defer file.Close()
|
|
|
|
// Set the content type as HTML and send the file contents
|
|
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 HTML file on the root URL
|
|
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
|
|
|
|
// Serve the current time on the /time endpoint
|
|
http.HandleFunc("/time", serveTime)
|
|
|
|
// Start the server on port 8085
|
|
http.ListenAndServe(":8085", nil)
|
|
}
|