-
-
Save aquaflamingo/c4bdb99840a0a758416920534d5cff91 to your computer and use it in GitHub Desktop.
GET and POST golang API
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
/* | |
* 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