-
Notifications
You must be signed in to change notification settings - Fork 0
/
sampleapp.go
247 lines (228 loc) · 6.6 KB
/
sampleapp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright 2022 Gleb Otochkin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package sampleapp
import (
"database/sql"
"fmt"
"html/template"
"log"
"net/http"
"os"
"time"
_ "github.com/jackc/pgx/v4/stdlib"
)
var (
dbversion string
db *sql.DB
)
func renderTmpl(w http.ResponseWriter, r *http.Request, db *sql.DB) {
//
var (
data EmpData
err error
)
if os.Getenv("DBVERSION") == "" || dbversion == "POSTGRESQL" {
data, err = getEmployeesPG(db)
if err != nil {
log.Printf("func getEmployeesPG: failed to get data: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
} else if os.Getenv("DBVERSION") == "ORACLE" || dbversion == "ORACLE" {
data, err = getEmployeesOra(db)
if err != nil {
log.Printf("func getEmployeesOra: failed to get data: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
} else {
fmt.Println("Can't find proper database version!")
}
indexTmpl := template.Must(template.New("index").Parse(indexFile))
err = indexTmpl.Execute(w, data)
if err != nil {
log.Printf("func renderTmpl: failed to render template: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
}
func postEmployee(w http.ResponseWriter, r *http.Request, db *sql.DB) {
if os.Getenv("DBVERSION") == "" || dbversion == "POSTGRESQL" {
PostEmployeePG(w, r, db)
} else if os.Getenv("DBVERSION") == "ORACLE" || dbversion == "ORACLE" {
PostEmployeeOra(w, r, db)
}
}
func dbConnect() *sql.DB {
//
var (
db *sql.DB
err error
)
if os.Getenv("DBVERSION") == "" || os.Getenv("DBVERSION") == "POSTGRESQL" {
db, err = connectPostgres()
dbversion = "POSTGRESQL"
if err != nil {
log.Fatalf("connectPostgres: Unable to connect to the database %s", err)
}
// if err := initDBPG(db); err != nil {
// log.Fatalf("initDBPG unable to create table: %s", err)
// }
} else if os.Getenv("DBVERSION") == "ORACLE" {
// Connect using Oracle driver
db, err = connectOracle()
dbversion = "ORACLE"
if err != nil {
log.Fatalf("connectOracle: Unable to connect to the database %s", err)
}
} else {
log.Fatal("This database driver is not supported")
}
return db
}
func execStmt(tdll string) error {
//
_, err := db.Exec(tdll)
return err
}
type Employee struct {
Employee_id int64
First_name string
Last_name string
Hire_date time.Time
Manager_id int64
}
type EmpData struct {
Employees []Employee
GetResponseTime string
}
func RunApp(w http.ResponseWriter, r *http.Request) {
//
switch r.Method {
case http.MethodGet:
renderTmpl(w, r, dbConnect())
case http.MethodPost:
//
postEmployee(w, r, dbConnect())
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
func configurePool(db *sql.DB) {
// Maximum number of connections in idle connection pool.
db.SetMaxIdleConns(3)
// Maximum number of open connections to the database.
db.SetMaxOpenConns(10)
// Maximum time (in seconds) that a connection can remain open.
db.SetConnMaxLifetime(1800 * time.Second)
}
var indexFile = `
<html lang="en">
<head>
<head>
<title>Sample DB app</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/5.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/5.2.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="page-title">
<h1>Sample App</h1>
<p class="lead">Sample DB app with Go</p>
<hr />
</div>
<h3>Last 10 added employees</h3>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Employee_id</th>
<th>First_name</th>
<th>Last_name</th>
<th>Hire_date</th>
<th>Manager_id</th>
</tr>
</thead>
<tbody>
{{ range .Employees }}
<tr>
<td>{{ .Employee_id }}</td>
<td>{{ .First_name }}</td>
<td>{{ .Last_name }}</td>
<td>{{ .Hire_date }}</td>
<td>{{ .Manager_id }}</td>
</tr>
{{ end}}
</tbody>
</table>
<div class="panel panel-default">
<div class="panel-heading">Response time</div>
<div class="panel-body">{{ .GetResponseTime}}</div>
</div>
</div>
<h3>Add employee</h3>
<form class="form-horizontal" method="post">
<div class="form-group">
<label class="control-label col-sm-1" for="fname">First Name:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="fname" placeholder="Enter First Name">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-1" for="lname">Last Name:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="lname" placeholder="Enter Last Name">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-1" for="hdate">Hire Date:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="hdate" placeholder="mm-dd-yyyy">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-1" for="mgrid">Manager ID:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="mgrid" placeholder="Enter Manager ID">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="button" class="btn btn-default" id="submitEmployee">Submit</button>
</div>
</div>
</form>
</div>
<script>
function postemployee(fname,lname,hdate,mgrid) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (this.readyState == 4) {
window.location.reload();
}
};
xhr.open("POST", "/", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send("fname=" + fname + "&lname=" + lname + "&hdate=" + hdate + "&mgrid=" + mgrid);
}
document.getElementById("submitEmployee").addEventListener("click", function () {
postemployee(fname.value,lname.value,hdate.value,mgrid.value);
});
</script>
</body>
</html>
`