Created
May 23, 2015 02:11
-
-
Save byronmansfield/6aaa98429c6b115ddd77 to your computer and use it in GitHub Desktop.
Go logger server
This file contains 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
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.