-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic structure for the WEB interface
See #40
- Loading branch information
1 parent
6bafab1
commit 5491328
Showing
5 changed files
with
192 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"html/template" | ||
"net/http" | ||
|
||
"github.com/rafaeljusto/toglacier/internal/config" | ||
"github.com/rafaeljusto/toglacier/internal/storage" | ||
) | ||
|
||
func startWEB() *http.Server { | ||
server := &http.Server{ | ||
Addr: config.Current().WEB.Address, | ||
} | ||
|
||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
t := template.New("homepage") | ||
t, err := t.Parse(webHomepageTemplate) | ||
|
||
if err != nil { | ||
logger.Warningf("error parsing homepage template. details: %s", err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if err = t.Execute(w, nil); err != nil { | ||
logger.Warningf("error executing homepage template. details: %s", err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
}) | ||
|
||
http.HandleFunc("/backups", func(w http.ResponseWriter, r *http.Request) { | ||
backups, err := toGlacier.ListBackups(false) | ||
if err != nil { | ||
logger.Warningf("error retrieving backups. details: %s", err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
if backups == nil { | ||
// initialize slice to avoid showing null in json | ||
backups = make(storage.Backups, 0) | ||
} | ||
|
||
encoder := json.NewEncoder(w) | ||
if err = encoder.Encode(backups); err != nil { | ||
logger.Warningf("error marshalling backups. details: %s", err) | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
}) | ||
|
||
go func() { | ||
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { | ||
logger.Warningf("error listening web server. details: %s", err) | ||
} | ||
}() | ||
|
||
return server | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package main | ||
|
||
var webHomepageTemplate = `<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | ||
<title>toglacier</title> | ||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous"> | ||
</head> | ||
<body> | ||
<nav class="navbar navbar-inverse bg-primary"> | ||
<a class="navbar-brand" href="#">toglacier</a> | ||
</nav> | ||
<div class="container-fluid"> | ||
<div class="row"> | ||
<div class="col"> | ||
<div class="alert alert-danger alert-dismissible fade show mt-2" role="alert"> | ||
<button type="button" class="close" data-dismiss="alert" aria-label="Close"> | ||
<span aria-hidden="true">×</span> | ||
</button> | ||
<p></p> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="row"> | ||
<div class="col"> | ||
<ul class="nav nav-tabs mt-2" role="tablist"> | ||
<li class="nav-item"> | ||
<a class="nav-link active" data-toggle="tab" href="#backups" role="tab">Backups</a> | ||
</li> | ||
<li class="nav-item"> | ||
<a class="nav-link" data-toggle="tab" href="#settings" role="tab">Settings</a> | ||
</li> | ||
</ul> | ||
</div> | ||
</div> | ||
<div class="row"> | ||
<div class="col"> | ||
<div class="tab-content"> | ||
<div class="tab-pane fade show active" id="backups" role="tabpanel"> | ||
<table class="table"> | ||
<thead> | ||
<tr> | ||
<th>#</th> | ||
<th>Date</th> | ||
</tr> | ||
</thead> | ||
<tbody id="backups"></tbody> | ||
</table> | ||
</div> | ||
<div class="tab-pane fade" id="settings" role="tabpanel"> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script> | ||
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script> | ||
<script type="text/javascript"> | ||
function backups() { | ||
$.ajax("/backups") | ||
.done(function(backups, textStatus, jqXHR) { | ||
$("#backups").empty(); | ||
$.each(JSON.parse(backups), function(index, backup) { | ||
$("#backups").append( | ||
$("<tr>").append( | ||
$("<th>") | ||
.attr("scope", "row") | ||
.text(index), | ||
$("<td>").text(backup.Backup.CreatedAt) | ||
) | ||
); | ||
}); | ||
}) | ||
.fail(function(jqXHR, textStatus, errorThrown) { | ||
$(".alert-danger > p").text("Error loading backups"); | ||
$(".alert").alert(); | ||
}) | ||
.always(function() { | ||
$(".alert-danger > p").text(""); | ||
$(".alert").alert("close"); | ||
setTimeout(backups, 5000); | ||
}); | ||
} | ||
$(function() { | ||
backups(); | ||
}); | ||
</script> | ||
</body> | ||
</html> | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters