Web app
This commit is contained in:
parent
847c0d2c9e
commit
9b92796375
17
server.go
17
server.go
|
@ -6,16 +6,21 @@ import (
|
|||
"io/ioutil"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/GeertJohan/go.rice"
|
||||
)
|
||||
|
||||
type (
|
||||
server struct {
|
||||
manager *Manager
|
||||
port int
|
||||
manager *Manager
|
||||
port int
|
||||
staticHandler http.Handler
|
||||
}
|
||||
)
|
||||
|
||||
func (s *server) start() {
|
||||
s.staticHandler = http.FileServer(rice.MustFindBox("static").HTTPBox())
|
||||
|
||||
portStr := ":" + strconv.Itoa(s.port)
|
||||
|
||||
fmt.Println("Configuration server is available at http://127.0.0.1" + portStr)
|
||||
|
@ -25,7 +30,15 @@ func (s *server) start() {
|
|||
}
|
||||
|
||||
func (s *server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
// Rewrite like a boss
|
||||
if req.URL.Path == "/" {
|
||||
req.URL.Path = "/config.html"
|
||||
}
|
||||
|
||||
// Route like a boss
|
||||
switch req.URL.Path {
|
||||
case "/config.html", "/app.js", "/bootstrap.min.css":
|
||||
s.staticHandler.ServeHTTP(w, req)
|
||||
case "/fields.json":
|
||||
s.fieldsHandler(w, req)
|
||||
case "/save":
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* Confection App
|
||||
*/
|
||||
|
||||
function loadFields(callback) {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', '/fields.json', true);
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === 4) {
|
||||
if (xhr.status === 200) {
|
||||
var fields = JSON.parse(xhr.responseText);
|
||||
callback(fields);
|
||||
}
|
||||
}
|
||||
};
|
||||
xhr.send(null);
|
||||
}
|
||||
|
||||
function drawForm(fields) {
|
||||
var container = document.getElementById('fields');
|
||||
for (var i = 0; i < fields.length; i++) {
|
||||
var field = fields[i],
|
||||
formGroup = document.createElement('div'),
|
||||
label = document.createElement('label'),
|
||||
input;
|
||||
|
||||
formGroup.setAttribute('class', 'form-group');
|
||||
label.setAttribute('for', field.path);
|
||||
label.appendChild(document.createTextNode(field.title));
|
||||
formGroup.appendChild(label);
|
||||
|
||||
if (field.options !== null) {
|
||||
input = document.createElement('select');
|
||||
|
||||
if (field.is_required === false) {
|
||||
var option = document.createElement('option');
|
||||
|
||||
option.setAttribute('value', '');
|
||||
option.appendChild(document.createTextNode('Not selected'));
|
||||
input.appendChild(option);
|
||||
}
|
||||
|
||||
for (var j = 0; j < field.options.length; j++) {
|
||||
var value = field.options[j],
|
||||
option = document.createElement('option');
|
||||
|
||||
option.setAttribute('value', value);
|
||||
option.appendChild(document.createTextNode(value));
|
||||
input.appendChild(option);
|
||||
}
|
||||
} else {
|
||||
input = document.createElement('input');
|
||||
}
|
||||
|
||||
input.setAttribute('value', field.value);
|
||||
input.setAttribute('id', field.path);
|
||||
input.setAttribute('class', 'form-control');
|
||||
if (field.is_readonly) {
|
||||
input.setAttribute('readonly', 'readonly');
|
||||
}
|
||||
|
||||
switch (field.type) {
|
||||
case "string":
|
||||
input.setAttribute('type', 'text');
|
||||
break;
|
||||
default:
|
||||
console.log("Invalid field type: "+ field.type, field)
|
||||
}
|
||||
|
||||
formGroup.appendChild(input);
|
||||
container.appendChild(formGroup);
|
||||
}
|
||||
}
|
||||
|
||||
loadFields(drawForm);
|
||||
|
||||
// TODO: Support for various types
|
||||
// bool
|
||||
// string
|
||||
// int int8 int16 int32 int64
|
||||
// uint uint8 uint16 uint32 uint64
|
||||
// float32 float64
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,26 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Configuration Management</title>
|
||||
<link rel="stylesheet" href="/bootstrap.min.css">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col-xs-6 col-xs-offset-3">
|
||||
<form>
|
||||
<div id="fields"> </div>
|
||||
<button type="submit" class="btn btn-default">Save</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="/app.js"></script>
|
||||
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue