Skip to content

Instantly share code, notes, and snippets.

@byronmansfield
Created May 23, 2015 02:11
Show Gist options
  • Save byronmansfield/6aaa98429c6b115ddd77 to your computer and use it in GitHub Desktop.
Save byronmansfield/6aaa98429c6b115ddd77 to your computer and use it in GitHub Desktop.
Go logger server
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
)
type LogMsg struct {
Test string
}
func handleRequest(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
fmt.Fprintf(w, "Hi from %s", r.URL.Path[1:])
decoder := json.NewDecoder(r.Body)
var l LogMsg
err := decoder.Decode(&l)
f, err := os.OpenFile("perfman.log", os.O_RDWR|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
defer f.Close()
if _, err = f.WriteString(l.Test); err != nil {
panic(err)
}
}
func main() {
http.HandleFunc("/logger", handleRequest)
http.ListenAndServe(":3333", nil)
}
@byronmansfield
Copy link
Author

So far its a start. Very simple proof of concept. But it is running in production. Want to upgrade it to be concurrent and track more information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment