3216db3ebea12c499691c926a0d897b1591461f4
[goDBhammer.git] / src / webconnector.go
1 // im a web connector
2
3 package main
4
5
6 import (
7         "http";
8         "io";
9         //"./dbibridge";
10         "./benchcontroller";
11         //"dbconnector";
12         "fmt";
13         "json";
14 )
15
16 type JSONStatData struct {
17         currentJob int; // 1 = nothing, 2 = have database login, 3 = createing data, 4 = running test, 5 = finished
18         currentStatus int; // percent complete
19 }
20
21 var comin chan string;
22 var cominstatus chan string;
23 var comout chan string;
24
25 var stat int;
26
27 func JSONResponder(c *http.Conn, req *http.Request)
28 {
29         //stat := <- cominstatus;
30         
31         if(req.Method == "GET") {
32         
33                 j, _, _ := json.StringToJson(fmt.Sprintf("{\"status\":%d, \"complete\":90}", stat));
34                 
35                 io.WriteString(c, fmt.Sprintf("%s\n", json.JsonToString(j)));
36                 fmt.Printf("i got a get\n");
37         }
38         if(req.Method == "POST") {
39                 lp := make([]byte, 1048576);
40                 req.Body.Read(lp);
41                 fmt.Printf("i got a post...%s\n", (string)(lp));
42                 //http.Redirect(c, "/", 301);
43                 stat = 2;
44         }
45         
46 }
47
48
49 func header(c *http.Conn)
50 {
51         io.WriteString(c, "<html><head></head><body>");
52 }
53
54 func footer(c *http.Conn)
55 {
56         io.WriteString(c, "</body></html>");
57 }
58
59 func main()
60 {
61         comin = make(chan string);
62         cominstatus = make(chan string);
63         comout = make(chan string);
64         
65         stat = 1;
66         
67         bc := benchcontroller.CreateController();
68         go benchcontroller.MainLoop(bc, comout, comin, cominstatus);
69         
70         http.Handle("/", http.FileServer("./web/", "/"));
71         http.Handle("/json", http.HandlerFunc(JSONResponder));
72         err := http.ListenAndServe(":22222", nil);
73         if err != nil {
74                 panic("ListenAndServe: ", err.String())
75         }
76
77 }