Skip to content

Commit

Permalink
web: overhaul config page (resolve #9, #11)
Browse files Browse the repository at this point in the history
* Changed the layout of the config page in an attempt to be easier to
  use and understand
* Eliminated most of the tooltips. Did this instead of removing them
  entirely. Some of them are still useful and it doesn't look as dumb
  without the enable all help thing.
* Fix possible data race in package config
* Fixed position status message
* Did I change anything else?
  • Loading branch information
moshee committed Sep 18, 2015
1 parent f16041c commit 87aca4d
Show file tree
Hide file tree
Showing 8 changed files with 438 additions and 230 deletions.
4 changes: 2 additions & 2 deletions cmd/airliftd/bindata_files/static.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions cmd/airliftd/bindata_files/templates.go

Large diffs are not rendered by default.

74 changes: 32 additions & 42 deletions cmd/airliftd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,80 +232,70 @@ func getConfigOverview(g *gas.Gas) (int, gas.Outputter) {
}

func postConfig(g *gas.Gas) (int, gas.Outputter) {
var form struct {
Host string `form:"host"`
Directory string `form:"directory"`
NewPass string `form:"newpass"`
Password string `form:"password"`
Port int `form:"port"`
HashLen int `form:"hash-len"`
MaxAge int `form:"max-age"`
MaxSize int64 `form:"max-size"`
AppendExt bool `form:"append-ext"`
TwitterCard bool `form:"twitter-card"`
Handle string `form:"twitter-handle"`
}
conf := config.Get()
newconf := config.Config{}

if err := g.UnmarshalForm(&form); err != nil {
if err := g.UnmarshalForm(&newconf); err != nil {
return 400, out.JSON(&Resp{Err: err.Error()})
}

conf := config.Get()

pass := g.FormValue("password")
newpass := g.FormValue("newpass")
if conf.Password != nil {
if form.Password == "" {
if pass == "" {
return 403, out.JSON(&Resp{Err: "you forgot your password"})
}
if !auth.VerifyHash([]byte(form.Password), conf.Password, conf.Salt) {
if !auth.VerifyHash([]byte(pass), conf.Password, conf.Salt) {
return 403, out.JSON(&Resp{Err: "incorrect password"})
}
if form.NewPass != "" {
conf.SetPass(form.NewPass)
if newpass != "" {
conf.SetPass(newpass)
}
} else {
if form.NewPass == "" {
if newpass == "" {
return 400, out.JSON(&Resp{Err: "cannot set empty password"})
} else {
conf.SetPass(form.NewPass)
if err := auth.SignIn(g, conf, form.NewPass); err != nil {
conf.SetPass(newpass)
if err := auth.SignIn(g, conf, newpass); err != nil {
return 400, out.JSON(&Resp{Err: err.Error()})
}
}
}

conf.Host = form.Host
conf.Directory = form.Directory
conf.Port = form.Port
conf.HashLen = form.HashLen
conf.Age = form.MaxAge
conf.Size = form.MaxSize
conf.AppendExt = form.AppendExt
conf.TwitterCardEnable = form.TwitterCard
conf.TwitterHandle = form.Handle
newconf.Password = conf.Password
newconf.Salt = conf.Salt
newconf.Port = conf.Port

if err := config.Set(conf); err != nil {
if newconf.HashLen < 1 {
newconf.HashLen = 1
} else if newconf.HashLen > 64 {
newconf.HashLen = 64
}

if newconf.TwitterCardEnable && newconf.TwitterHandle == "" {
return 400, out.JSON(&Resp{Err: "you must provide a Twitter handle to use Twitter Cards"})
}

if err := config.Set(&newconf); err != nil {
log.Println(g.Request.Method, "postConfig:", err)
return 500, out.JSON(&Resp{Err: err.Error()})
}

if conf.MaxSize() > 0 {
_, err := fileCache.CutToSize(conf.MaxSize() * 1024 * 1024)
conf = config.Get()

if conf.MaxSizeEnable {
_, err := fileCache.CutToSize(conf.Size * 1024 * 1024)
if err != nil {
log.Print(err)
}
}
if conf.MaxAge() > 0 {
cutoff := time.Now().Add(-time.Duration(conf.MaxAge()) * 24 * time.Hour)
if conf.MaxAgeEnable {
cutoff := time.Now().Add(-time.Duration(conf.Age) * 24 * time.Hour)
_, err := fileCache.RemoveOlderThan(cutoff)
if err != nil {
log.Print(err)
}
}
if conf.HashLen < 1 {
conf.HashLen = 1
} else if conf.HashLen > 64 {
conf.HashLen = 64
}

return 204, nil
}
Expand Down
146 changes: 129 additions & 17 deletions cmd/airliftd/static/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,18 @@ function makesvg(elem) {
return document.createElementNS("http://www.w3.org/2000/svg", elem);
}

function showMessage(root, msg, classname) {
if (root == null) return;
function showMessage(msg, classname) {
if (timeout != null) window.clearTimeout(timeout);

var box = $('#message-box');
if (box == null) {
box = document.createElement('div');
box.id = 'message-box';
} else {
box.parentNode.removeChild(box);
}

root.insertBefore(box, root.firstChild.nextSibling);
box.className = classname;
box.innerText = msg;
box.style.display = 'block';
box.classList.add(classname);
box.classList.add('active')
timeout = window.setTimeout(hideMessage, 5000);
}

function hideMessage() {
$('#message-box').style.display = 'none';
$('#message-box').classList.remove('active');
}

function cb(e) {
Expand All @@ -65,9 +56,9 @@ function cb(e) {
} else {
var resp = JSON.parse(e.target.responseText);
if (resp != null && resp.Err != null) {
showMessage($('#section-overview'), 'Error: ' + resp.Err, 'bad');
showMessage('Error: ' + resp.Err, 'bad');
} else {
showMessage($('#section-overview'), 'An unknown error occurred (status ' + e.target.status + ')', 'bad');
showMessage('An unknown error occurred (status ' + e.target.status + ')', 'bad');
}
}
}
Expand Down Expand Up @@ -199,14 +190,14 @@ function uploadFiles(fileList) {
try {
if (this.status !== 201) {
var err = JSON.parse(this.responseText);
showMessage($('#upload'), err.Err, 'bad');
showMessage(err.Err, 'bad');
} else {
var resp = JSON.parse(this.responseText);
result.push(window.location.protocol + '//' + resp.URL);
setTimeout(next, 1, i+1, result, totalLoaded);
}
} catch (e) {
showMessage($('#upload'), 'Server Error: ' + this.statusText, 'bad');
showMessage('Server Error: ' + this.statusText, 'bad');
console.error('error parsing response: ' + e);
}
}, false);
Expand Down Expand Up @@ -253,3 +244,124 @@ function disable() {
function clickPicker() {
picker.click();
}

var oldMaxSize, oldMaxAge, sampleID, sampleExt, idSize, addExt;

function reloadOverview() {
var x = new XMLHttpRequest();
x.open('GET', '/-/config/overview', true);
x.addEventListener('load', function(e) {
if (e.target.status === 200) {
$('#section-overview').innerHTML = e.target.response;
}
});
x.send();
}

function updateSample() {
var a = new Array(parseInt(idSize.value));
for (var i = 0; i < a.length; i++) {
a[i] = 'X';
}
sampleID.textContent = sampleID.innerText = a.join('');

if (addExt.checked) {
sampleExt.classList.add('show');
} else {
sampleExt.classList.remove('show');
}
}

function configPage() {
var buttons = $$('button'), host = $('#host');
oldMaxSize = parseInt($('#max-size').value);
oldMaxAge = parseInt($('#max-age').value);
sampleID = $('#sample-id');
sampleExt = $('#sample-ext');
idSize = $('#id-size');
addExt = $('#append-ext');

if (host.value === '') {
host.value = window.location.host;
}

updateSample();
// IE and webkit seem to have different change and input impls
idSize.addEventListener('change', updateSample, false);
idSize.addEventListener('input', updateSample, false);
addExt.addEventListener('change', updateSample, false);

var boxes = $$('.check-enable');
for (var i = 0, b; b = boxes[i]; i++) {
var hider = b.querySelector('.hider');
hider.hidee = b.querySelector('.hidee input');
hider.addEventListener('click', function() {
this.hidee.disabled = !this.checked;
}, false);
}

$('#submit').addEventListener('click', function() {
for (var i = 0, button; button = buttons[i]; i++) button.setAttribute('disabled', true);
var maxSize = parseInt($('#max-size').value);
var maxAge = parseInt($('#max-age').value);
var delta = 0;

var f = function(url, val) {
var fd = new FormData();
fd.append('N', val);
var x = new XMLHttpRequest();
x.open('POST', url, false);
x.send(fd);

if (x.status == 200) {
var n = JSON.parse(x.response).N;
if (n > delta) delta = n;
return true;
} else {
var err = JSON.parse(x.response);
showMessage('Server error: ' + err.Err + ' (' + x.status + ')', 'bad');
return false;
}
}

if (maxSize > 0 && (oldMaxSize == 0 || maxSize < oldMaxSize)) {
if (!f('/-/config/size', maxSize)) return;
}
if (maxAge > 0 && (oldMaxAge == 0 || maxAge < oldMaxAge)) {
if (!f('/-/config/age', maxAge)) return;
}
if (delta > 0) {
if (!confirm('Changes made to age or size limits mean that ' + delta + ' old file(s) will be pruned. Continue?')) {
return;
}
}

oldMaxAge = maxAge;
oldMaxSize = maxSize;

var host = $('#host');
host.value = host.value.replace(/\w+:\/\//, '');
var fd = new FormData($('#config'));
var x = new XMLHttpRequest();

x.addEventListener('load', function(e) {
$('#password').value = '';
for (var i = 0, button; button = buttons[i]; i++) button.removeAttribute('disabled');
if (e.target.status === 204) {
showMessage('Configuration updated.', 'good');
$('#newpass').value = '';
reloadOverview();
} else {
var resp = JSON.parse(x.responseText);
if (resp != null && resp.Err != null) {
showMessage('Error: ' + resp.Err, 'bad');
} else {
showMessage('An unknown error occurred (status ' + e.target.status + ')', 'bad');
}
}
}, false);

x.open('POST', '/-/config', true);
x.send(fd);
}, false);
}
Loading

0 comments on commit 87aca4d

Please sign in to comment.