Skip to content

Commit 7188cc0

Browse files
committed
initial commit
1 parent 0458391 commit 7188cc0

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

WebApp1/main.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"log"
6+
"net/http"
7+
"text/template"
8+
9+
_ "github.com/mattn/go-sqlite3"
10+
)
11+
12+
var port = ":8080"
13+
14+
type Page struct {
15+
Name string
16+
DBStatus bool
17+
}
18+
19+
type SearchResult struct {
20+
Title string
21+
Author string
22+
Year string
23+
ID string
24+
}
25+
26+
func main() {
27+
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Llongfile)
28+
29+
templates := template.Must(template.ParseFiles("templates/index.html"))
30+
31+
//db, _ := sql.Open("sqlite3", "dev.db")
32+
33+
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
34+
err := templates.ExecuteTemplate(w, "index.html", nil)
35+
if err != nil {
36+
http.Error(w, err.Error(), http.StatusInternalServerError)
37+
}
38+
})
39+
40+
http.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) {
41+
results := []SearchResult{
42+
SearchResult{"Nagarahvu", "T R Subbarao", "1972", "51JeF5abJuXT1zoNjlJe"},
43+
SearchResult{"And then ther were none", "Agatha Christie", "1939", "eeSqQGbnJxMgQSk9oCDL"},
44+
SearchResult{"IT", "Stephen King", "1986", "ZOlu3Nvwxz6vlTjDwLtE"},
45+
SearchResult{"Eradu Kanasu", "Vani", "1974", "9YVvLgV8dN60DMg0HTGn"},
46+
}
47+
encoder := json.NewEncoder(w)
48+
err := encoder.Encode(results)
49+
if err != nil {
50+
http.Error(w, err.Error(), http.StatusInternalServerError)
51+
}
52+
})
53+
54+
log.Println(http.ListenAndServe(port, nil))
55+
}

WebApp1/templates/index.html

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<html>
2+
3+
<head>
4+
<title>
5+
Go Web Development
6+
</title>
7+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
8+
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous">
9+
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
10+
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
11+
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
12+
</head>
13+
14+
<body>
15+
<nav class="navbar navbar-dark bg-dark">
16+
<a class="navbar-brand" href="#">
17+
<img src="http://i.stack.imgur.com/2mWEC.png" width="30" height="30" class="d-inline-block align-top"
18+
alt="">
19+
Go Web Development
20+
</a>
21+
</nav>
22+
<ul class="list-group">
23+
<li class="list-group-item">
24+
<ul class="list-group">
25+
<li class="list-group-item">
26+
<form id="search-form" class="form-inline" onsubmit="return false">
27+
<label>
28+
<span class="badge badge-dark mr-sm-4">
29+
Search
30+
</span>
31+
</label>
32+
<input class="form-control mr-sm-4" type="search" placeholder="Search" aria-label="Search">
33+
<button class="btn btn-success" title="Search" type="submit" onclick="return submitSearch()"><i class="fas fa-search"></i></button>
34+
</form>
35+
</li>
36+
<table class="table table-hover table-bordered table-dark">
37+
<thead id="table-head">
38+
<tr>
39+
<th scope="col">Title</th>
40+
<th scope="col">Author</th>
41+
<th scope="col">Year</th>
42+
<th scope="col">Date</th>
43+
</tr>
44+
</thead>
45+
<tbody id="search-results">
46+
47+
</tbody>
48+
</table>
49+
</ul>
50+
</li>
51+
</ul>
52+
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
53+
<script type="text/javascript">
54+
function submitSearch() {
55+
$.ajax({
56+
url: "/search",
57+
method: "POST",
58+
data: $("#search-form").serialize(),
59+
success: function (rawData) {
60+
var parsed = JSON.parse(rawData);
61+
if (!parsed) return;
62+
63+
var searchResults = $("#search-results");
64+
searchResults.empty();
65+
66+
parsed.forEach(function (result) {
67+
var row = $("<tr><td>" + result.Title + "</td><td>" + result.Author + "</td><td>" + result.Year + "</td><td>" + result.ID + "</td></tr>");
68+
searchResults.append(row);
69+
});
70+
}
71+
});
72+
return false;
73+
}
74+
</script>
75+
</body>
76+
77+
</html>

0 commit comments

Comments
 (0)