Static images now work again after they were accidentally broken when I added Gzip Compression.

Fixed a topic desync and eliminates a number of possible race conditions in the caches.
Optimised global.js by swapping out some of the unnecessary jQuery with native javascript code.
Updated jQuery to version 3.1.11
Fixed the CSS on the areyousure and error templates.
This commit is contained in:
Azareal 2017-02-16 06:47:55 +00:00
parent 61358b5449
commit 1d85224dbc
15 changed files with 72 additions and 54 deletions

View File

@ -18,6 +18,7 @@ type SFile struct
GzipData []byte GzipData []byte
Pos int64 Pos int64
Length int64 Length int64
GzipLength int64
Mimetype string Mimetype string
Info os.FileInfo Info os.FileInfo
FormattedModTime string FormattedModTime string
@ -77,8 +78,9 @@ func add_static_file(path string, prefix string) error {
log.Print("Adding the '" + path + "' static file") log.Print("Adding the '" + path + "' static file")
path = strings.TrimPrefix(path, prefix) path = strings.TrimPrefix(path, prefix)
log.Print("Added the '" + path + "' static file") log.Print("Added the '" + path + "' static file")
gzip_data := compress_bytes_gzip(data)
static_files["/static" + path] = SFile{data,compress_bytes_gzip(data),0,int64(len(data)),mime.TypeByExtension(filepath.Ext(prefix + path)),f,f.ModTime().UTC().Format(http.TimeFormat)} static_files["/static" + path] = SFile{data,gzip_data,0,int64(len(data)),int64(len(gzip_data)),mime.TypeByExtension(filepath.Ext(prefix + path)),f,f.ModTime().UTC().Format(http.TimeFormat)}
return nil return nil
} }

View File

@ -138,7 +138,9 @@ func init_static_files() {
path = strings.TrimPrefix(path,"public/") path = strings.TrimPrefix(path,"public/")
log.Print("Added the '" + path + "' static file.") log.Print("Added the '" + path + "' static file.")
static_files["/static/" + path] = SFile{data,compress_bytes_gzip(data),0,int64(len(data)),mime.TypeByExtension(filepath.Ext("/public/" + path)),f,f.ModTime().UTC().Format(http.TimeFormat)} gzip_data := compress_bytes_gzip(data)
static_files["/static/" + path] = SFile{data,gzip_data,0,int64(len(data)),int64(len(gzip_data)),mime.TypeByExtension(filepath.Ext("/public/" + path)),f,f.ModTime().UTC().Format(http.TimeFormat)}
return nil return nil
}) })
if err != nil { if err != nil {

View File

@ -59,7 +59,7 @@ func route_edit_topic(w http.ResponseWriter, r *http.Request) {
err = topics.Load(tid) err = topics.Load(tid)
if err != nil { if err != nil {
LocalError("This topic no longer exists!",w,r,user) LocalErrorJSQ("This topic no longer exists!",w,r,user,is_js)
return return
} }
@ -153,8 +153,12 @@ func route_stick_topic(w http.ResponseWriter, r *http.Request) {
InternalError(err,w,r) InternalError(err,w,r)
return return
} }
//topic.Sticky = true
topic.Sticky = true err = topics.Load(tid)
if err != nil {
LocalError("This topic doesn't exist!",w,r,user)
return
}
http.Redirect(w,r,"/topic/" + strconv.Itoa(tid),http.StatusSeeOther) http.Redirect(w,r,"/topic/" + strconv.Itoa(tid),http.StatusSeeOther)
} }
@ -188,8 +192,12 @@ func route_unstick_topic(w http.ResponseWriter, r *http.Request) {
InternalError(err,w,r) InternalError(err,w,r)
return return
} }
//topic.Sticky = false
topic.Sticky = false err = topics.Load(tid)
if err != nil {
LocalError("This topic doesn't exist!",w,r,user)
return
}
http.Redirect(w,r,"/topic/" + strconv.Itoa(tid),http.StatusSeeOther) http.Redirect(w,r,"/topic/" + strconv.Itoa(tid),http.StatusSeeOther)
} }

View File

@ -4,13 +4,13 @@ function post_link(event)
{ {
event.preventDefault(); event.preventDefault();
var form_action = $(event.target).closest('a').attr("href"); var form_action = $(event.target).closest('a').attr("href");
console.log("Form Action: " + form_action); //console.log("Form Action: " + form_action);
$.ajax({ url: form_action, type: "POST", dataType: "json", data: { js: "1" } }); $.ajax({ url: form_action, type: "POST", dataType: "json", data: { js: "1" } });
} }
$(document).ready(function(){ $(document).ready(function(){
$(".open_edit").click(function(event){ $(".open_edit").click(function(event){
console.log("Clicked on edit"); //console.log("Clicked on edit");
event.preventDefault(); event.preventDefault();
$(".hide_on_edit").hide(); $(".hide_on_edit").hide();
$(".show_on_edit").show(); $(".show_on_edit").show();
@ -29,9 +29,9 @@ $(document).ready(function(){
var topic_status_input = $('.topic_status_input').val(); var topic_status_input = $('.topic_status_input').val();
var topic_content_input = $('.topic_content_input').val(); var topic_content_input = $('.topic_content_input').val();
var form_action = $(this).closest('form').attr("action"); var form_action = $(this).closest('form').attr("action");
console.log("New Topic Name: " + topic_name_input); //console.log("New Topic Name: " + topic_name_input);
console.log("New Topic Status: " + topic_status_input); //console.log("New Topic Status: " + topic_status_input);
console.log("Form Action: " + form_action); //console.log("Form Action: " + form_action);
$.ajax({ $.ajax({
url: form_action, url: form_action,
data: { data: {
@ -45,7 +45,6 @@ $(document).ready(function(){
}); });
}); });
$(".post_link").click(post_link);
$(".delete_item").click(function(event) $(".delete_item").click(function(event)
{ {
post_link(event); post_link(event);
@ -69,7 +68,7 @@ $(document).ready(function(){
block.html(newContent); block.html(newContent);
var form_action = $(this).closest('a').attr("href"); var form_action = $(this).closest('a').attr("href");
console.log("Form Action: " + form_action); //console.log("Form Action: " + form_action);
$.ajax({ url: form_action, type: "POST", dataType: "json", data: { is_js: "1", edit_item: newContent } $.ajax({ url: form_action, type: "POST", dataType: "json", data: { is_js: "1", edit_item: newContent }
}); });
}); });
@ -91,7 +90,7 @@ $(document).ready(function(){
block.html(newContent); block.html(newContent);
var form_action = $(this).closest('a').attr("href"); var form_action = $(this).closest('a').attr("href");
console.log("Form Action: " + form_action); //console.log("Form Action: " + form_action);
$.ajax({ $.ajax({
url: form_action + "?session=" + session, url: form_action + "?session=" + session,
type: "POST", type: "POST",
@ -108,10 +107,10 @@ $(document).ready(function(){
block_parent.find('.hide_on_edit').hide(); block_parent.find('.hide_on_edit').hide();
block_parent.find('.editable_block').show(); block_parent.find('.editable_block').show();
block_parent.find('.editable_block').each(function(){ block_parent.find('.editable_block').each(function(){
var field_name = $(this).data("field"); var field_name = this.getAttribute("data-field");
var field_type = $(this).data("type"); var field_type = this.getAttribute("data-type");
if(field_type=="list") { if(field_type=="list") {
var field_value = $(this).data("value"); var field_value = this.getAttribute("data-value");
if(field_name in form_vars) var it = form_vars[field_name]; if(field_name in form_vars) var it = form_vars[field_name];
else var it = ['No','Yes']; else var it = ['No','Yes'];
var itLen = it.length; var itLen = it.length;
@ -121,9 +120,9 @@ $(document).ready(function(){
else sel = ""; else sel = "";
out += "<option "+sel+"value='"+i+"'>"+it[i]+"</option>"; out += "<option "+sel+"value='"+i+"'>"+it[i]+"</option>";
} }
$(this).html("<select data-field='"+field_name+"' name='"+field_name+"'>" + out + "</select>"); this.innerHTML = "<select data-field='"+field_name+"' name='"+field_name+"'>" + out + "</select>";
} }
else $(this).html("<input name='"+field_name+"' value='" + $(this).text() + "' type='text'/>"); else this.innerHTML = "<input name='"+field_name+"' value='" + this.textContent + "' type='text'/>";
}); });
block_parent.find('.show_on_edit').eq(0).show(); block_parent.find('.show_on_edit').eq(0).show();
@ -133,38 +132,38 @@ $(document).ready(function(){
var out_data = {is_js: "1"} var out_data = {is_js: "1"}
var block_parent = $(this).closest('.editable_parent'); var block_parent = $(this).closest('.editable_parent');
var block = block_parent.find('.editable_block').each(function(){ var block = block_parent.find('.editable_block').each(function(){
var field_name = $(this).data("field"); var field_name = this.getAttribute("data-field");
var field_type = $(this).data("type"); var field_type = this.getAttribute("data-type");
if(field_type == "list") var newContent = $(this).find('select :selected').text(); if(field_type == "list") var newContent = $(this).find('select :selected').text();
else var newContent = $(this).find('input').eq(0).val(); else var newContent = $(this).find('input').eq(0).val();
$(this).html(newContent); this.innerHTML = newContent;
out_data[field_name] = newContent out_data[field_name] = newContent
}); });
var form_action = $(this).closest('a').attr("href"); var form_action = $(this).closest('a').attr("href");
console.log("Form Action: " + form_action); //console.log("Form Action: " + form_action);
console.log(out_data); //console.log(out_data);
$.ajax({ url: form_action + "?session=" + session, type:"POST", dataType:"json", data: out_data }); $.ajax({ url: form_action + "?session=" + session, type:"POST", dataType:"json", data: out_data });
block_parent.find('.hide_on_edit').show(); block_parent.find('.hide_on_edit').show();
block_parent.find('.show_on_edit').hide(); block_parent.find('.show_on_edit').hide();
}); });
}); });
$(this).find(".ip_item").each(function(){ $(".ip_item").each(function(){
var ip = $(this).text(); var ip = this.textContent;
console.log("IP: " + ip); //console.log("IP: " + ip);
if(ip.length > 10){ if(ip.length > 10){
$(this).html("Show IP"); this.innerHTML = "Show IP";
$(this).click(function(event){ this.onclick = function(event){
event.preventDefault(); event.preventDefault();
$(this).text(ip); this.textContent = ip;
}); };
} }
}); });
$(this).keyup(function(event){ this.onkeyup = function(event){
if(event.which == 37) $("#prevFloat a")[0].click(); if(event.which == 37) this.querySelectorAll("#prevFloat a")[0].click();
if(event.which == 39) $("#nextFloat a")[0].click(); if(event.which == 39) this.querySelectorAll("#nextFloat a")[0].click();
}); };
}); });

4
public/jquery-3.1.1.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -41,13 +41,14 @@ func route_static(w http.ResponseWriter, r *http.Request){
h := w.Header() h := w.Header()
h.Set("Last-Modified", file.FormattedModTime) h.Set("Last-Modified", file.FormattedModTime)
h.Set("Content-Type", file.Mimetype) h.Set("Content-Type", file.Mimetype)
h.Set("Content-Length", strconv.FormatInt(file.Length, 10)) // Avoid doing a type conversion every time?
//http.ServeContent(w,r,r.URL.Path,file.Info.ModTime(),file) //http.ServeContent(w,r,r.URL.Path,file.Info.ModTime(),file)
//w.Write(file.Data) //w.Write(file.Data)
if strings.Contains(r.Header.Get("Accept-Encoding"),"gzip") { if strings.Contains(r.Header.Get("Accept-Encoding"),"gzip") {
h.Set("Content-Encoding","gzip") h.Set("Content-Encoding","gzip")
h.Set("Content-Length", strconv.FormatInt(file.GzipLength, 10))
io.Copy(w, bytes.NewReader(file.GzipData)) // Use w.Write instead? io.Copy(w, bytes.NewReader(file.GzipData)) // Use w.Write instead?
} else { } else {
h.Set("Content-Length", strconv.FormatInt(file.Length, 10)) // Avoid doing a type conversion every time?
io.Copy(w, bytes.NewReader(file.Data)) io.Copy(w, bytes.NewReader(file.Data))
} }
//io.CopyN(w, bytes.NewReader(file.Data), static_files[r.URL.Path].Length) //io.CopyN(w, bytes.NewReader(file.Data), static_files[r.URL.Path].Length)

View File

@ -6,9 +6,8 @@ var header_0 []byte = []byte(`<!doctype html>
<title>`) <title>`)
var header_1 []byte = []byte(`</title> var header_1 []byte = []byte(`</title>
<link href="/static/main.css" rel="stylesheet" type="text/css"> <link href="/static/main.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="/static/jquery-1.12.3.min.js"></script> <script type="text/javascript" src="/static/jquery-3.1.1.min.js"></script>
<script type="text/javascript"> <script type="text/javascript">var session = "`)
var session = "`)
var header_2 []byte = []byte(`"; var header_2 []byte = []byte(`";
</script> </script>
<script type="text/javascript" src="/static/global.js"></script> <script type="text/javascript" src="/static/global.js"></script>
@ -47,7 +46,8 @@ var menu_7 []byte = []byte(`
<div style="clear: both;"></div> <div style="clear: both;"></div>
</div>`) </div>`)
var header_3 []byte = []byte(` var header_3 []byte = []byte(`
<div id="back"><div id="main">`) <div id="back"><div id="main">
`)
var header_4 []byte = []byte(`<div class="alert">`) var header_4 []byte = []byte(`<div class="alert">`)
var header_5 []byte = []byte(`</div>`) var header_5 []byte = []byte(`</div>`)
var topic_0 []byte = []byte(`<div id="prevFloat" class="prev_button"><a class="prev_link" href="/topic/`) var topic_0 []byte = []byte(`<div id="prevFloat" class="prev_button"><a class="prev_link" href="/topic/`)

View File

@ -1,6 +1,6 @@
{{template "header.html" . }} {{template "header.html" . }}
<div class="rowblock"> <div class="rowblock">
<div class="rowitem"><a>Are you sure?</a></div> <div class="rowitem rowhead"><a>Are you sure?</a></div>
</div> </div>
<div class="rowblock"> <div class="rowblock">
<div class="rowitem passive">{{.Something.Message}}<br /><br /> <div class="rowitem passive">{{.Something.Message}}<br /><br />

View File

@ -1,6 +1,6 @@
{{template "header.html" . }} {{template "header.html" . }}
<div class="rowblock"> <div class="rowblock">
<div class="rowitem"><a>An error has occured</a></div> <div class="rowitem rowhead"><a>An error has occured</a></div>
</div> </div>
<div class="rowblock"> <div class="rowblock">
<div class="rowitem passive">{{.Something}}</div> <div class="rowitem passive">{{.Something}}</div>

View File

@ -3,9 +3,8 @@
<head> <head>
<title>{{.Title}}</title> <title>{{.Title}}</title>
<link href="/static/main.css" rel="stylesheet" type="text/css"> <link href="/static/main.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="/static/jquery-1.12.3.min.js"></script> <script type="text/javascript" src="/static/jquery-3.1.1.min.js"></script>
<script type="text/javascript"> <script type="text/javascript">var session = "{{.CurrentUser.Session}}";
var session = "{{.CurrentUser.Session}}";
</script> </script>
<script type="text/javascript" src="/static/global.js"></script> <script type="text/javascript" src="/static/global.js"></script>
<meta name="viewport" content="width=device-width,initial-scale = 1.0, maximum-scale=1.0,user-scalable=no" /> <meta name="viewport" content="width=device-width,initial-scale = 1.0, maximum-scale=1.0,user-scalable=no" />
@ -13,4 +12,5 @@
<body> <body>
<div class="container"> <div class="container">
{{template "menu.html" .}} {{template "menu.html" .}}
<div id="back"><div id="main">{{range .NoticeList}}<div class="alert">{{.}}</div>{{end}} <div id="back"><div id="main">
{{range .NoticeList}}<div class="alert">{{.}}</div>{{end}}

View File

@ -104,8 +104,9 @@ func add_theme_static_files(themeName string) {
path = strings.TrimPrefix(path,"themes/" + themeName + "/public") path = strings.TrimPrefix(path,"themes/" + themeName + "/public")
log.Print("Added the '" + path + "' static file for default theme " + themeName + ".") log.Print("Added the '" + path + "' static file for default theme " + themeName + ".")
gzip_data := compress_bytes_gzip(data)
static_files["/static" + path] = SFile{data,compress_bytes_gzip(data),0,int64(len(data)),mime.TypeByExtension(filepath.Ext("/themes/" + themeName + "/public" + path)),f,f.ModTime().UTC().Format(http.TimeFormat)} static_files["/static" + path] = SFile{data,gzip_data,0,int64(len(data)),int64(len(gzip_data)),mime.TypeByExtension(filepath.Ext("/themes/" + themeName + "/public" + path)),f,f.ModTime().UTC().Format(http.TimeFormat)}
return nil return nil
}) })
if err != nil { if err != nil {

View File

@ -174,7 +174,6 @@ hr { color: silver; border: 1px solid silver; }
.rowhead span { display: block; padding-top: 5px; } .rowhead span { display: block; padding-top: 5px; }
.colhead a { color: white; display: block; padding-top: 5px; } .colhead a { color: white; display: block; padding-top: 5px; }
.colhead span { display: block; padding-top: 5px; } .colhead span { display: block; padding-top: 5px; }
.open_edit { display: none !important; }
.show_on_edit { display: none; } .show_on_edit { display: none; }
.rowhead .topic_status_e { display: none !important; } .rowhead .topic_status_e { display: none !important; }
.topic_button { float: right; position: relative; top: -22px; margin-right: 2px; border-style: solid !important; } .topic_button { float: right; position: relative; top: -22px; margin-right: 2px; border-style: solid !important; }

View File

@ -171,7 +171,6 @@ hr { color: silver; border: 1px solid silver; }
.rowhead span { display: block; padding-top: 5px; } .rowhead span { display: block; padding-top: 5px; }
.colhead a { color: white; display: block; padding-top: 5px; } .colhead a { color: white; display: block; padding-top: 5px; }
.colhead span { display: block; padding-top: 5px; } .colhead span { display: block; padding-top: 5px; }
.open_edit { display: none !important; }
.show_on_edit { display: none; } .show_on_edit { display: none; }
.rowhead .topic_status_e { display: none !important; } .rowhead .topic_status_e { display: none !important; }
.topic_button { float: right; position: relative; top: -22px; margin-right: 2px; border-style: solid !important; } .topic_button { float: right; position: relative; top: -22px; margin-right: 2px; border-style: solid !important; }

View File

@ -144,13 +144,15 @@ func (sts *StaticTopicStore) Load(id int) error {
err := get_topic_stmt.QueryRow(id).Scan(&topic.Title, &topic.Content, &topic.CreatedBy, &topic.CreatedAt, &topic.Is_Closed, &topic.Sticky, &topic.ParentID, &topic.IpAddress, &topic.PostCount, &topic.LikeCount) err := get_topic_stmt.QueryRow(id).Scan(&topic.Title, &topic.Content, &topic.CreatedBy, &topic.CreatedAt, &topic.Is_Closed, &topic.Sticky, &topic.ParentID, &topic.IpAddress, &topic.PostCount, &topic.LikeCount)
if err == nil { if err == nil {
sts.Set(topic) sts.Set(topic)
} else {
sts.Remove(id)
} }
return err return err
} }
func (sts *StaticTopicStore) Set(item *Topic) error { func (sts *StaticTopicStore) Set(item *Topic) error {
sts.mu.Lock() sts.mu.Lock()
item, ok := sts.items[item.ID] _, ok := sts.items[item.ID]
if ok { if ok {
sts.items[item.ID] = item sts.items[item.ID] = item
} else if sts.length >= sts.capacity { } else if sts.length >= sts.capacity {
@ -158,9 +160,9 @@ func (sts *StaticTopicStore) Set(item *Topic) error {
return ErrStoreCapacityOverflow return ErrStoreCapacityOverflow
} else { } else {
sts.items[item.ID] = item sts.items[item.ID] = item
sts.length++
} }
sts.mu.Unlock() sts.mu.Unlock()
sts.length++
return nil return nil
} }

View File

@ -118,6 +118,7 @@ func (sts *StaticUserStore) Load(id int) error {
user := &User{ID:id,Loggedin:true} user := &User{ID:id,Loggedin:true}
err := get_full_user_stmt.QueryRow(id).Scan(&user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Email, &user.Avatar, &user.Message, &user.URLPrefix, &user.URLName, &user.Level, &user.Score, &user.Last_IP) err := get_full_user_stmt.QueryRow(id).Scan(&user.Name, &user.Group, &user.Is_Super_Admin, &user.Session, &user.Email, &user.Avatar, &user.Message, &user.URLPrefix, &user.URLName, &user.Level, &user.Score, &user.Last_IP)
if err != nil { if err != nil {
sts.Remove(id)
return err return err
} }
@ -136,7 +137,7 @@ func (sts *StaticUserStore) Load(id int) error {
func (sts *StaticUserStore) Set(item *User) error { func (sts *StaticUserStore) Set(item *User) error {
sts.mu.Lock() sts.mu.Lock()
item, ok := sts.items[item.ID] _, ok := sts.items[item.ID]
if ok { if ok {
sts.items[item.ID] = item sts.items[item.ID] = item
} else if sts.length >= sts.capacity { } else if sts.length >= sts.capacity {
@ -144,9 +145,9 @@ func (sts *StaticUserStore) Set(item *User) error {
return ErrStoreCapacityOverflow return ErrStoreCapacityOverflow
} else { } else {
sts.items[item.ID] = item sts.items[item.ID] = item
sts.length++
} }
sts.mu.Unlock() sts.mu.Unlock()
sts.length++
return nil return nil
} }