Skip to content

Commit

Permalink
Add basic structure for the WEB interface
Browse files Browse the repository at this point in the history
See #40
  • Loading branch information
rafaeljusto committed Sep 26, 2017
1 parent 6bafab1 commit 5491328
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 1 deletion.
62 changes: 62 additions & 0 deletions cmd/toglacier/toglacier-web.go
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
}
104 changes: 104 additions & 0 deletions cmd/toglacier/toglacier-web.tmpl.go
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">&times;</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>
`
7 changes: 7 additions & 0 deletions cmd/toglacier/toglacier.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/smtp"
"os"
"regexp"
Expand Down Expand Up @@ -391,9 +392,15 @@ func commandStart(c *cli.Context) error {

scheduler.Start()

var server *http.Server
if config.Current().WEB.Enabled {
server = startWEB()
}

stopped := make(chan bool)
cancelFunc = func() {
scheduler.Stop()
server.Shutdown(nil)
stopped <- true
}

Expand Down
13 changes: 12 additions & 1 deletion cmd/toglacier/toglacier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,15 @@ gcs:
# 3. Click in the CREATE SERVICE ACCOUNT link
# 4. Define a service account name, add permissions for all storage objects
# and check "Furnish a new private key" option (chosing JSON format)
account file: /etc/toglacier/toglacier-f926fc937f92.json
account file: /etc/toglacier/toglacier-f926fc937f92.json

# web contains all necessary information to run the web interface for
# communicating with the running scheduler.
web:
# enabled flag enables or disables the web interface. By default the web
# interface is enabled.
enabled: true

# address contains the IP address and port that the web server will listen to.
# By default it will listen on all interfaces on port 80.
address: 0.0.0.0:80
7 changes: 7 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ type Config struct {
Bucket string `yaml:"bucket"`
AccountFile string `yaml:"account file" split_words:"true"`
} `yaml:"gcs" envconfig:"gcs"`

WEB struct {
Enabled bool `yaml:"enabled"`
Address string `yaml:"address"`
} `yaml:"web" envconfig:"web"`
}

// Current return the actual system configuration, stored internally in a global
Expand Down Expand Up @@ -100,6 +105,8 @@ func Default() {
c.Database.File = path.Join("var", "log", "toglacier", "toglacier.db")
c.Log.Level = LogLevelError
c.Email.Format = EmailFormatHTML
c.WEB.Enabled = true
c.WEB.Address = "0.0.0.0:80"

Update(c)
}
Expand Down

0 comments on commit 5491328

Please sign in to comment.