Skip to content

Instantly share code, notes, and snippets.

@rajkumarpb
Forked from alyssaq/main.go
Created September 8, 2017 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajkumarpb/ee43353ad06b2dc9ffa0f621eb414652 to your computer and use it in GitHub Desktop.
Save rajkumarpb/ee43353ad06b2dc9ffa0f621eb414652 to your computer and use it in GitHub Desktop.
GET and POST golang API
/*
* Sample API with GET and POST endpoint.
* POST data is converted to string and saved in internal memory.
* GET endpoint returns all strings in an array.
*/
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)
var (
// flagPort is the open port the application listens on
flagPort = flag.String("port", "9000", "Port to listen on")
)
var results []string
// GetHandler handles the index route
func GetHandler(w http.ResponseWriter, r *http.Request) {
jsonBody, err := json.Marshal(results)
if err != nil {
http.Error(w, "Error converting results to json",
http.StatusInternalServerError)
}
w.Write(jsonBody)
}
// PostHandler converts post request body to string
func PostHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "Error reading request body",
http.StatusInternalServerError)
}
results = append(results, string(body))
fmt.Fprint(w, "POST done")
} else {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
}
}
func init() {
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
flag.Parse()
}
func main() {
results = append(results, time.Now().Format(time.RFC3339))
mux := http.NewServeMux()
mux.HandleFunc("/", GetHandler)
mux.HandleFunc("/post", PostHandler)
log.Printf("listening on port %s", *flagPort)
log.Fatal(http.ListenAndServe(":"+*flagPort, mux))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment