Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.