de43ec682de0a5e1b336e740dba8129cb71808d8
[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         "fmt";
12 )
13
14 var comin chan string;
15 var comout chan string;
16
17 // hello world, the web server
18 func ServerResponder(c *http.Conn, req *http.Request) {
19         //comout <- 2;
20         //comout <- 3;
21         //comout <- 1;
22         fmt.Printf("lk: %s %s %s\n", req.Method, req.RawURL, req.URL); 
23         if req.RawURL == "/" {
24                 mainPage(c, req);
25         }
26         
27         if req.RawURL == "/confirm" {
28                 confirmPage(c, req);
29         }
30
31         if req.RawURL == "/begin" {
32                 beginPage(c, req);
33         }
34         
35         //k := <- comin;
36         //fmt.Printf("%d\n", k);
37 }
38
39 func beginPage(c *http.Conn, req *http.Request)
40 {
41
42         if req.Method != "GET" {
43                 http.Redirect(c, "/begin", 301);
44         } else {
45                 header(c);
46                 io.WriteString(c, "<h1>Starting hammer</h1>");
47                 //dbhost := req.FormValue("dbhost");
48                 //dbuser := req.FormValue("dbuser");
49                 //dbpass := req.FormValue("dbpass");
50                 //dbname := req.FormValue("dbname");
51                 //nthreads := req.FormValue("nthreads");
52                 //dbmult := req.FormValue("dbmult");
53                 
54                 footer(c);
55         }
56         
57 }
58
59 func confirmPage(c *http.Conn, req *http.Request)
60 {
61         header(c);
62         io.WriteString(c, "<h1>Confirm</h1>");
63         dbhost := req.FormValue("dbhost");
64         dbuser := req.FormValue("dbuser");
65         dbpass := req.FormValue("dbpass");
66         dbname := req.FormValue("dbname");
67         nthreads := req.FormValue("nthreads");
68         dbmult := req.FormValue("dbmult");
69         var dbtype string;
70         
71         io.WriteString(c, "You are trying to perform a benchmark with the following values:<br>");
72         io.WriteString(c, "<table border=\"1\">");
73         io.WriteString(c, fmt.Sprintf("<tr><td>Host</td><td>%s</td></tr>", dbhost));
74         io.WriteString(c, fmt.Sprintf("<tr><td>User</td><td>%s</td></tr>", dbuser));
75         io.WriteString(c, fmt.Sprintf("<tr><td>Password</td><td>%s</td></tr>", dbpass));
76         io.WriteString(c, fmt.Sprintf("<tr><td>Database</td><td>%s</td></tr>", dbname));
77         io.WriteString(c, fmt.Sprintf("<tr><td>Data Multiplier</td><td>%s</td></tr>", dbmult));
78         io.WriteString(c, fmt.Sprintf("<tr><td>Number of Threads</td><td>%s</td></tr>", nthreads));
79         io.WriteString(c, "</table>");
80         io.WriteString(c, "<form method=\"post\" action=\"/begin\">");
81         io.WriteString(c, fmt.Sprintf("<input type=\"hidden\" name=\"dbhost\" value=\"%s\">",dbhost));
82         io.WriteString(c, fmt.Sprintf("<input type=\"hidden\" name=\"dbuser\" value=\"%s\">",dbuser));
83         io.WriteString(c, fmt.Sprintf("<input type=\"hidden\" name=\"dbpass\" value=\"%s\">",dbpass));
84         io.WriteString(c, fmt.Sprintf("<input type=\"hidden\" name=\"dbname\" value=\"%s\">",dbname));
85         io.WriteString(c, fmt.Sprintf("<input type=\"hidden\" name=\"nthreads\" value=\"%s\">",nthreads));
86         io.WriteString(c, fmt.Sprintf("<input type=\"hidden\" name=\"dbmult\" value=\"%s\">",dbmult));
87         io.WriteString(c, "<input type=\"submit\" name=\"Begin\" value=\"Begin\">");
88         io.WriteString(c, "</form>");
89         
90         dbtype = "mysql";
91         
92         footer(c);
93         comout <- fmt.Sprintf("configure:%s:%s:%s:%s:%s:%s:%s", dbtype, dbhost, dbuser, dbpass, dbname, dbmult, nthreads);
94
95 }
96
97 func mainPage(c *http.Conn, req *http.Request)
98 {
99         header(c);
100         io.WriteString(c, "<h1>goDBHammer</h1><br>");
101         io.WriteString(c, "Welcome to goDBHammer, the go based database benchmarking tool<br>");
102         io.WriteString(c, "<form method=\"post\" action=\"/confirm\">");
103         io.WriteString(c, "<table><tr><td>Database Type</td><td><input type=\"text\" name=\"dbtype\" value=\"ignored for now\"></td></tr>");
104         io.WriteString(c, "<tr><td>Database Host</td><td><input type=\"text\" name=\"dbhost\"></td></tr>");
105         io.WriteString(c, "<tr><td>Database User</td><td><input type=\"text\" name=\"dbuser\"></td></tr>");
106         io.WriteString(c, "<tr><td>Database Password</td><td><input type=\"text\" name=\"dbpass\"></td></tr>");
107         io.WriteString(c, "<tr><td>Database</td><td><input type=\"text\" name=\"dbname\"></td></tr>");  
108         io.WriteString(c, "<tr><td>Data Multiplier</td><td><input type=\"text\" name=\"dbmult\"></td></tr>");   
109         io.WriteString(c, "<tr><td>Number of Clients</td><td><input type=\"text\" name=\"nthreads\"></td></tr>");
110         io.WriteString(c, "<tr><td><input type=\"submit\" name=\"Setup\" value=\"Setup\"></td></tr>");
111         io.WriteString(c, "</table>");
112         io.WriteString(c, "</form>");
113         fmt.Printf("%s\n", req.FormValue("dbhost"));
114         footer(c);
115 }
116
117 func header(c *http.Conn)
118 {
119         io.WriteString(c, "<html><head></head><body>");
120 }
121
122 func footer(c *http.Conn)
123 {
124         io.WriteString(c, "</body></html>");
125 }
126
127 func main()
128 {
129         comin = make(chan string);
130         comout = make(chan string);
131         
132         bc := benchcontroller.CreateController();
133         go benchcontroller.MainLoop(bc, comout, comin);
134         
135         http.Handle("/", http.HandlerFunc(ServerResponder));
136         err := http.ListenAndServe(":22222", nil);
137         if err != nil {
138                 panic("ListenAndServe: ", err.String())
139         }
140
141 }