forked from davecheney/gcvis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request davecheney#19 from kouno/go15-support
Go 1.5 support + Refactor
- Loading branch information
Showing
9 changed files
with
355 additions
and
106 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net" | ||
"net/http" | ||
"sync" | ||
) | ||
|
||
type HttpServer struct { | ||
graph *Graph | ||
listener net.Listener | ||
iface string | ||
port string | ||
|
||
listenerMtx sync.Mutex | ||
} | ||
|
||
func NewHttpServer(iface string, port string, graph *Graph) *HttpServer { | ||
h := &HttpServer{ | ||
graph: graph, | ||
iface: iface, | ||
port: port, | ||
} | ||
|
||
return h | ||
} | ||
|
||
func (h *HttpServer) Start() { | ||
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { | ||
h.graph.Write(w) | ||
}) | ||
http.Serve(h.Listener(), nil) | ||
} | ||
|
||
func (h *HttpServer) Close() { | ||
h.Listener().Close() | ||
} | ||
|
||
func (h *HttpServer) Url() string { | ||
return fmt.Sprintf("http://%s/", h.Listener().Addr()) | ||
} | ||
|
||
func (h *HttpServer) Listener() net.Listener { | ||
h.listenerMtx.Lock() | ||
defer h.listenerMtx.Unlock() | ||
|
||
if h.listener != nil { | ||
return h.listener | ||
} | ||
|
||
ifaceAndPort := fmt.Sprintf("%v:%v", h.iface, h.port) | ||
listener, err := net.Listen("tcp4", ifaceAndPort) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
h.listener = listener | ||
return h.listener | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestHttpServerListener(t *testing.T) { | ||
graph := NewGraph("fake title", GCVIS_TMPL) | ||
server := NewHttpServer("127.0.0.1", "0", &graph) | ||
|
||
url := server.Url() | ||
|
||
if !strings.Contains(url, "http://127.0.0.1") { | ||
t.Fatalf("Server URL didn't contain localhost address: %v", url) | ||
} | ||
} | ||
|
||
func TestHttpServerResponse(t *testing.T) { | ||
graph := NewGraph("fake title", GCVIS_TMPL) | ||
graph.AddGCTraceGraphPoint(&gctrace{}) | ||
server := NewHttpServer("127.0.0.1", "0", &graph) | ||
|
||
go server.Start() | ||
defer server.Close() | ||
|
||
response, err := http.Get(server.Url()) | ||
if err != nil { | ||
t.Errorf("HTTP request returned an error: %v", err) | ||
} | ||
defer response.Body.Close() | ||
|
||
body, err := ioutil.ReadAll(response.Body) | ||
if err != nil { | ||
t.Errorf("Error while reading response body: %v", err) | ||
} | ||
|
||
w := &bytes.Buffer{} | ||
|
||
if err = graph.Write(w); err != nil { | ||
t.Errorf("Error while writing template: %v", err) | ||
} | ||
|
||
expectedBody, err := ioutil.ReadAll(w) | ||
if err != nil { | ||
t.Errorf("Error while reading buffer: %v", err) | ||
} | ||
|
||
if !bytes.Equal(expectedBody, body) { | ||
t.Fatalf( | ||
"Expected response body to equal parsed template.\nExpected: %v\nGot: %v", | ||
string(expectedBody), | ||
string(body), | ||
) | ||
} | ||
} |
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
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
Oops, something went wrong.