feat: add http server
This commit is contained in:
37
main.go
Normal file
37
main.go
Normal file
@@ -0,0 +1,37 @@
|
||||
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)
|
||||
}
|
Reference in New Issue
Block a user