Created
November 9, 2018 23:54
-
-
Save elimisteve/61f186f5367aac05dc33d4310c8b2932 to your computer and use it in GitHub Desktop.
Trivial Go server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"net/http" | |
"time" | |
"github.com/gorilla/mux" | |
) | |
func main() { | |
r := mux.NewRouter() | |
r.HandleFunc("/", GetIndex) | |
r.HandleFunc("/now", GetNow) | |
panic(getServer(":5000", r).ListenAndServe()) | |
} | |
func getServer(httpAddr string, *mux.Router) string { | |
return &http.Server{ | |
Addr: httpAddr, | |
ReadTimeout: 1000 * time.Second, | |
WriteTimeout: 1000 * time.Second, | |
IdleTimeout: 120 * time.Second, | |
Handler: r, | |
} | |
} | |
func GetIndex(w http.ResponseWriter, req *http.Request) { | |
w.Write([]byte("Hello, world! Visit /now for the current time.")) | |
} | |
func GetNow(w http.ResponseWriter, req *http.Request) { | |
nowb, _ := json.Marshal(map[string]time.Time{"now": time.Now()}) | |
w.Write(nowb) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment