Created
September 30, 2019 18:23
-
-
Save mathershifter/f8e87627b0858b02cda2d4d416259820 to your computer and use it in GitHub Desktop.
Go DEMO HTTP 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
// 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