38 lines
657 B
Go
38 lines
657 B
Go
|
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()
|
||
|
res := now.GoString()
|
||
|
fmt.Fprint(w, res)
|
||
|
}
|