Skip to content

Commit

Permalink
completed http server
Browse files Browse the repository at this point in the history
  • Loading branch information
grj1046 committed Apr 1, 2021
1 parent b7d242c commit a5e8ffc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
33 changes: 18 additions & 15 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</head>
<body>
<div class="container">
<h2 class="font-weight-light">LookCos榜单聚合<small class="font-weight-light">-实时更新</small></h2>
<h2 class="font-weight-light">LookCos榜单聚合 <small class="font-weight-light">- 实时更新</small></h2>
<br />
<div class="row">
<div class="col-sm-3">
Expand Down Expand Up @@ -99,23 +99,26 @@ <h6>知乎热榜</h6>
<script src="https://cdn.staticfile.org/popper.js/1.12.5/umd/popper.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/js/bootstrap.min.js"></script>
<script type="text/javascript">
function html_xr(data_item) {
var page = "/hotspot?jsoncallback=?&item=" + data_item;
//var siteArray = ["baidusj", "baidurd", "weibo", "tieba", "vsite", "zhihu"];
$(function () {
var page = "hotspot";
$.getJSON(page, function (data_all) {
for (var i = 0; i <= data_all.length; i++) {
var id_name = data_item + i;
var x = i + 1
document.getElementById(id_name).innerHTML = '<small class="font-weight-normal">' + x + '.' + data_all[i]['name'] + '</small>';
document.getElementById(id_name).setAttribute("href", data_all[i]['url']);
for (var i = 0; i < data_all.length; i++) {
var currFileName = data_all[i].FileName;
var currSite = currFileName.replace(".json", "");
for (var j = 0; j < data_all[i].Content.length; j++) {
var id_name = currSite + j;
var x = i + 1
var $ele = $("#" + id_name);
if ($ele) {
var text = '<small class="font-weight-normal">' + x + '.' + data_all[i].Content[j].Name + '</small>';
$ele.html(text);
$ele.attr("href", data_all[i].Content[j].Url)
}
}
}
});
};
html_xr("baidusj");
html_xr("baidurd");
html_xr("weibo");
html_xr("tieba");
html_xr("vsite");
html_xr("zhihu");
})
</script>>
</body>
</html>
15 changes: 10 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,12 @@ func GetHotspot() {

/************** Http **************/
func handlerHome(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
byteHtml, err := ioutil.ReadFile("index.html")
if err != nil {
fmt.Fprintf(w, "get index.html failed. "+err.Error())
return
}
w.Write(byteHtml)
}

func handlerHotspot(w http.ResponseWriter, r *http.Request) {
Expand All @@ -253,10 +258,10 @@ func handlerHotspot(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
jsonEncoder := json.NewEncoder(w)
jsonEncoder.SetEscapeHTML(false)
err := jsonEncoder.Encode(result)
if err != nil {
log.Fatal("Encode result failed" + err.Error())
}
_ = jsonEncoder.Encode(result)
// if err != nil {
// log.Fatal("Encode result failed " + err.Error())
// }
}

/************** main func **************/
Expand Down

0 comments on commit a5e8ffc

Please sign in to comment.