the ugliest code on earth...
[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 JSONdata 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 func JSONResponder(c *http.Conn, req *http.Request)
26 {
27         //stat := <- cominstatus;
28         
29         j, _, _ := json.StringToJson("{\"somestring\":\"somevalue\"}");
30         
31         io.WriteString(c, fmt.Sprintf("%s\n", json.JsonToString(j)));
32         
33         /*if ret {
34                 fmt.Printf("err was true\n");
35         } else {
36                 fmt.Printf("err was false\n");
37         }*/
38         
39         //fmt.Printf("errto %s\n", errto); 
40         
41 }
42
43
44 // hello world, the web server
45 func ServerResponder(c *http.Conn, req *http.Request) {
46         //comout <- 2;
47         //comout <- 3;
48         //comout <- 1;
49         fmt.Printf("lk: %s %s %s\n", req.Method, req.RawURL, req.URL); 
50         if req.RawURL == "/" {
51                 mainPage(c, req);
52         }
53         
54         if req.RawURL == "/confirm" {
55                 confirmPage(c, req);
56         }
57
58         if req.RawURL == "/begin" {
59                 beginPage(c, req);
60         }
61         
62         //k := <- comin;
63         //fmt.Printf("%d\n", k);
64 }
65
66 func beginPage(c *http.Conn, req *http.Request)
67 {
68
69         if req.Method != "GET" {
70                 http.Redirect(c, "/begin", 301);
71         } else {
72                 header(c);
73                 io.WriteString(c, "<h1>Starting hammer</h1>");
74                 //dbhost := req.FormValue("dbhost");
75                 //dbuser := req.FormValue("dbuser");
76                 //dbpass := req.FormValue("dbpass");
77                 //dbname := req.FormValue("dbname");
78                 //nthreads := req.FormValue("nthreads");
79                 //dbmult := req.FormValue("dbmult");
80                 comout <- "begin:setup";
81                 
82                 footer(c);
83         }
84         
85 }
86
87 func confirmPage(c *http.Conn, req *http.Request)
88 {
89         header(c);
90         io.WriteString(c, "<h1>Confirm</h1>");
91         dbhost := req.FormValue("dbhost");
92         dbuser := req.FormValue("dbuser");
93         dbpass := req.FormValue("dbpass");
94         dbname := req.FormValue("dbname");
95         nthreads := req.FormValue("nthreads");
96         dbmult := req.FormValue("dbmult");
97         var dbtype string;
98         
99         io.WriteString(c, "You are trying to perform a benchmark with the following values:<br>");
100         io.WriteString(c, "<table border=\"1\">");
101         io.WriteString(c, fmt.Sprintf("<tr><td>Host</td><td>%s</td></tr>", dbhost));
102         io.WriteString(c, fmt.Sprintf("<tr><td>User</td><td>%s</td></tr>", dbuser));
103         io.WriteString(c, fmt.Sprintf("<tr><td>Password</td><td>%s</td></tr>", dbpass));
104         io.WriteString(c, fmt.Sprintf("<tr><td>Database</td><td>%s</td></tr>", dbname));
105         io.WriteString(c, fmt.Sprintf("<tr><td>Data Multiplier</td><td>%s</td></tr>", dbmult));
106         io.WriteString(c, fmt.Sprintf("<tr><td>Number of Threads</td><td>%s</td></tr>", nthreads));
107         io.WriteString(c, "</table>");
108         io.WriteString(c, "<form method=\"post\" action=\"/begin\">");
109         io.WriteString(c, fmt.Sprintf("<input type=\"hidden\" name=\"dbhost\" value=\"%s\">",dbhost));
110         io.WriteString(c, fmt.Sprintf("<input type=\"hidden\" name=\"dbuser\" value=\"%s\">",dbuser));
111         io.WriteString(c, fmt.Sprintf("<input type=\"hidden\" name=\"dbpass\" value=\"%s\">",dbpass));
112         io.WriteString(c, fmt.Sprintf("<input type=\"hidden\" name=\"dbname\" value=\"%s\">",dbname));
113         io.WriteString(c, fmt.Sprintf("<input type=\"hidden\" name=\"nthreads\" value=\"%s\">",nthreads));
114         io.WriteString(c, fmt.Sprintf("<input type=\"hidden\" name=\"dbmult\" value=\"%s\">",dbmult));
115         io.WriteString(c, "<input type=\"submit\" name=\"Begin\" value=\"Begin\">");
116         io.WriteString(c, "</form>");
117         
118         dbtype = "mysql";
119         
120         footer(c);
121         comout <- fmt.Sprintf("configure:%s:%s:%s:%s:%s:%s:%s", dbtype, dbhost, dbuser, dbpass, dbname, dbmult, nthreads);
122
123 }
124
125 func mainPage(c *http.Conn, req *http.Request)
126 {
127         header(c);
128         io.WriteString(c, "<h1>goDBHammer</h1><br>");
129         io.WriteString(c, "Welcome to goDBHammer, the go based database benchmarking tool<br>");
130         io.WriteString(c, "<form method=\"post\" action=\"/confirm\">");
131         io.WriteString(c, "<table><tr><td>Database Type</td><td><input type=\"text\" name=\"dbtype\" value=\"ignored for now\"></td></tr>");
132         io.WriteString(c, "<tr><td>Database Host</td><td><input type=\"text\" name=\"dbhost\"></td></tr>");
133         io.WriteString(c, "<tr><td>Database User</td><td><input type=\"text\" name=\"dbuser\"></td></tr>");
134         io.WriteString(c, "<tr><td>Database Password</td><td><input type=\"text\" name=\"dbpass\"></td></tr>");
135         io.WriteString(c, "<tr><td>Database</td><td><input type=\"text\" name=\"dbname\"></td></tr>");  
136         io.WriteString(c, "<tr><td>Data Multiplier</td><td><input type=\"text\" name=\"dbmult\"></td></tr>");   
137         io.WriteString(c, "<tr><td>Number of Clients</td><td><input type=\"text\" name=\"nthreads\"></td></tr>");
138         io.WriteString(c, "<tr><td><input type=\"submit\" name=\"Setup\" value=\"Setup\"></td></tr>");
139         io.WriteString(c, "</table>");
140         io.WriteString(c, "</form>");
141         fmt.Printf("%s\n", req.FormValue("dbhost"));
142         footer(c);
143 }
144
145 func header(c *http.Conn)
146 {
147         io.WriteString(c, "<html><head></head><body>");
148 }
149
150 func footer(c *http.Conn)
151 {
152         io.WriteString(c, "</body></html>");
153 }
154
155 func main()
156 {
157         comin = make(chan string);
158         cominstatus = make(chan string);
159         comout = make(chan string);
160         
161         
162         bc := benchcontroller.CreateController();
163         go benchcontroller.MainLoop(bc, comout, comin, cominstatus);
164         
165         http.Handle("/", http.FileServer("./web", "/web"));
166         http.Handle("/json", http.HandlerFunc(JSONResponder));
167         err := http.ListenAndServe(":22222", nil);
168         if err != nil {
169                 panic("ListenAndServe: ", err.String())
170         }
171
172 }