Skip to content

Commit

Permalink
use /vars for all http routes
Browse files Browse the repository at this point in the history
  • Loading branch information
forrestjgq committed Dec 1, 2020
1 parent f979db4 commit b8fe030
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,43 @@ gomark monitor variable statistics through HTTP web server, so you need call
StartHTTPServer(port int)
```
to start HTTP service before using.
### Dock gomark to external HTTP server
Maybe you have your own HTTP server and you wish to dock gomark to it instead of create a new one. GoMark actually supports this deployment. You need:
#### Add routers
GoMark requires the following routers:
```go
r := mux.NewRouter()
r.HandleFunc("/vars/js/{script}", procJs) // Route: js
r.HandleFunc("/vars", procVar) // Route: vars
r.HandleFunc("/vars/{var}", procVar) // Route: vars
r.HandleFunc("/vars/debug", procDebug) // Route: debug
```
Here `{xxx}` indicate a path variable, like`/vars/test_adder` or `/vars/*adder*`.

Note that each router may require additional path parameters in form `?a=b` or `?a`, like `/vars/test_adder?dataonly`, here `dataonly` is a parameter, or `/vars/debug?p=1`, here `p` is a parameter.

Both path variables or parameters are stored in `gmi.Request` along with headers:
```go
type Request struct {
Router Route
Params map[string]string
headers map[string]string
}
```
You need fill them from HTTP request.

You may note that no URL is required here, that's because `Router` defines a string to represent above routes, see comments.

Like `gmi.Request`, `gmi.Response` defines all information you need to write into HTTP response, make sure you write them all.
```go
type Response struct {
Status int
headers map[string]string
Body []byte
}
```
#### Call GoMark on receiving HTTP requests
In your routing handler, you need convert HTTP request to `gmi.Request` and call `gomark.Request`, and write returned `gmi.Response` to HTTP response.

## Variable Create
A variable is an entity that maintains all information of statistics. There are several variables, and can be created by:
Expand Down
4 changes: 2 additions & 2 deletions internal/httpsrv/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ var htmlContent = `
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script language="javascript" type="text/javascript" src="/js/jquery_min"></script>
<script language="javascript" type="text/javascript" src="/js/flot_min"></script>
<script language="javascript" type="text/javascript" src="/vars/js/jquery_min"></script>
<script language="javascript" type="text/javascript" src="/vars/js/flot_min"></script>
<style type="text/css">
ol,ul { list-style:none; }
.tabs-menu {
Expand Down
14 changes: 7 additions & 7 deletions internal/httpsrv/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ func Start(port int) {
}

r := mux.NewRouter()
r.HandleFunc("/js/{script}", procJs)
r.HandleFunc("/vars/js/{script}", procJs)
r.HandleFunc("/vars", procVar)
r.HandleFunc("/vars/{var}", procVar)
r.HandleFunc("/debug", procDebug)
r.HandleFunc("/vars/debug", procDebug)
server.r = r

go func() {
Expand All @@ -56,7 +56,7 @@ func RequestHTTP(req *gmi.Request) *gmi.Response {
return serveVar(req)
default:
return &gmi.Response{
Status: 404,
Status: 404,
}
}
}
Expand All @@ -66,7 +66,7 @@ func procDebug(w http.ResponseWriter, r *http.Request) {
}
func serveDebug(req *gmi.Request) (rsp *gmi.Response) {
rsp = &gmi.Response{
Status: 200,
Status: 200,
}
p := req.GetParam("perf")
if p == "1" {
Expand All @@ -90,7 +90,7 @@ func procJs(w http.ResponseWriter, r *http.Request) {
func serveJs(req *gmi.Request) (rsp *gmi.Response) {
rsp = &gmi.Response{
Status: 200,
Body: nil,
Body: nil,
}

rsp.SetHeader("content-type", "application/javascript")
Expand Down Expand Up @@ -181,7 +181,7 @@ func procVar(w http.ResponseWriter, r *http.Request) {
}
func proc(route gmi.Route, w http.ResponseWriter, r *http.Request) {
req := &gmi.Request{
Params: make(map[string]string),
Params: make(map[string]string),
}
vars := mux.Vars(r)
for k, v := range vars {
Expand Down Expand Up @@ -225,7 +225,7 @@ func proc(route gmi.Route, w http.ResponseWriter, r *http.Request) {
}
func serveVar(req *gmi.Request) (rsp *gmi.Response) {
rsp = &gmi.Response{
Status: 200,
Status: 200,
}

buf := &bytes.Buffer{}
Expand Down

0 comments on commit b8fe030

Please sign in to comment.