Skip to content

Instantly share code, notes, and snippets.

@mathershifter
Created September 30, 2019 18:23
Show Gist options
  • Save mathershifter/f8e87627b0858b02cda2d4d416259820 to your computer and use it in GitHub Desktop.
Save mathershifter/f8e87627b0858b02cda2d4d416259820 to your computer and use it in GitHub Desktop.
Go DEMO HTTP server
// Copyright (c) 2019 Arista Networks, Inc.
// Use of this source code is governed by the Apache License 2.0
// that can be found in the COPYING file.
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)
func main() {
srv := &http.Server{
Addr: ":9000",
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
srv.SetKeepAlivesEnabled(false)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
//fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
switch r.Method {
case http.MethodGet:
// Serve the resource.
case http.MethodPost:
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
}
fmt.Fprint(w, string(body))
log.Println(string(body))
case http.MethodPut:
// Update an existing record.
case http.MethodDelete:
// Remove the record.
default:
// Give an error message.
}
})
log.Fatal(srv.ListenAndServe())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment