2023-11-09 10:53:39 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
r := mux.NewRouter()
|
|
|
|
r.HandleFunc("/", HomeHandler)
|
|
|
|
r.HandleFunc("/now", TimeHandler)
|
|
|
|
http.Handle("/", r)
|
|
|
|
|
|
|
|
srv := &http.Server{
|
|
|
|
Handler: r,
|
|
|
|
Addr: "0.0.0.0:80",
|
|
|
|
// Good practice: enforce timeouts for servers you create!
|
|
|
|
WriteTimeout: 15 * time.Second,
|
|
|
|
ReadTimeout: 15 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Fatal(srv.ListenAndServe())
|
|
|
|
}
|
|
|
|
|
|
|
|
func HomeHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprint(w, "Hello world")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TimeHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
now := time.Now()
|
2023-11-09 11:03:41 +08:00
|
|
|
res := now.Format(time.RFC3339Nano)
|
2023-11-09 10:53:39 +08:00
|
|
|
fmt.Fprint(w, res)
|
|
|
|
}
|