Skip to content

Instantly share code, notes, and snippets.

@elimisteve
Created November 9, 2018 23:54
Show Gist options
  • Save elimisteve/61f186f5367aac05dc33d4310c8b2932 to your computer and use it in GitHub Desktop.
Save elimisteve/61f186f5367aac05dc33d4310c8b2932 to your computer and use it in GitHub Desktop.
Trivial Go server
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