Skip to content

Commit

Permalink
Caching! Makefile improvements! Update screen!
Browse files Browse the repository at this point in the history
  • Loading branch information
jaynagpaul committed Oct 23, 2017
1 parent 974294e commit b308f61
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ build: version deps assets-release
@$(foreach bit,$(BITS),$(foreach os, $(GOOSES), rm -rf dist/instahelper-$(v)-$(os)-$(bit);))

debug: assets
go run main.go
go run main.go -debug

test: deps assets
go test -v ./app/...
Expand Down
39 changes: 39 additions & 0 deletions app/config/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package config

import (
"time"
)

// Set is a quick interface to a key value cache.
// dur is the amount of time to cache the object before deleting
// if 0 then will cache infinitely
func Set(key string, value interface{}, dur time.Duration) error {

if err := DB.Set("cache", key, value); err != nil {
return err
}

if dur.Seconds() > 0 {
go func() {
time.Sleep(dur)
Delete(key)
}()
}

return nil
}

// Get a value from the key value cache.
// Returns nil if not found.
func Get(key string) interface{} {
var i interface{}

DB.Get("cache", key, &i)

return i
}

// Delete a value from the key value cache.
func Delete(key string) error {
return DB.Delete("cache", key)
}
3 changes: 3 additions & 0 deletions app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func Open() error {
createConfig()
}

// Refresh the cache
DB.Drop("cache")

return nil
}

Expand Down
10 changes: 10 additions & 0 deletions app/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ func init() {
Unlisted: true,
Handler: SettingsHandler,
},

"update": {
ID: 5,
Name: "Update",
Link: "/update",
Icon: "get_app",
Template: newTemplate("base.html", "update.html"),
Handler: UpdateHandler,
},
}
}

Expand Down Expand Up @@ -138,6 +147,7 @@ func AssetsHandler(w http.ResponseWriter, r *http.Request) {
}

// Error will display the error and promt the user to report it
// Do not use this for api endpoints. Only for user facing pages.
func Error(w http.ResponseWriter, e error) {
w.Write([]byte(
fmt.Sprintf("Error: %s\nIt would be appreciated if you could create an issue at https://github.com/socialplanner/instahelper/issues/new", e.Error()),
Expand Down
58 changes: 58 additions & 0 deletions app/handlers/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,71 @@ package handlers
import (
"encoding/json"
"net/http"
"time"

"github.com/go-chi/chi"
"github.com/sirupsen/logrus"
"github.com/socialplanner/instahelper/app/config"
"github.com/socialplanner/instahelper/app/update"
)

// UpdateHandler is the handler for /update
func UpdateHandler(w http.ResponseWriter, r *http.Request) {

var releases []update.Release
var err error

if r := config.Get("releases"); r == nil {
releases, err = update.ListReleases()
} else {
if rel, ok := r.([]update.Release); ok {
releases = rel
} else {
releases, err = update.ListReleases()
}
}

if err != nil {
logrus.Error(err)
w.WriteHeader(http.StatusInternalServerError)
Error(w, err)
return
}

for index, r := range releases {
// Ignore error to default to the raw date
t, err := time.Parse(time.RFC3339, r.PublishedAt)

if err == nil {
releases[index].PublishedAt = t.Format("Mon Jan 2, 3:04 PM ")
}
}

err = Template("update").Execute(w, map[string]interface{}{
"Releases": releases,
})

if err != nil {
logrus.Error(err)
w.WriteHeader(http.StatusInternalServerError)
Error(w, err)
}
}

// APIUpdateHandler is the handler used to update instahelper to the latest version
func APIUpdateHandler(w http.ResponseWriter, r *http.Request) {
asset, err := update.ToLatest(update.VERSION)

if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

b, _ := json.Marshal(asset)

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(b)
}

// APIUpdateToHandler is the handler used to update instahelper to a specific version
Expand All @@ -32,5 +89,6 @@ func APIUpdateToHandler(w http.ResponseWriter, r *http.Request) {
b, _ := json.Marshal(asset)

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(b)
}
44 changes: 14 additions & 30 deletions app/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,36 +79,14 @@ func ToLatest(version string) (*Asset, error) {
// Will return error if ver not found
// Update is allowed to update to a lower version
func To(ver string) (*Asset, error) {
semver1, err := semver.NewVersion(strings.Replace(
ver,
"v",
"",
-1,
))

if err != nil {
return nil, err
}

releases, err := ListReleases()

if err != nil {
return nil, err
}

for _, r := range releases {
semver2, err := semver.NewVersion(strings.Replace(
r.Version,
"v",
"",
-1,
))

if err != nil {
continue
}

if semver1.Equal(*semver2) {
if ver == r.Version {
if asset := pickAsset(r.Assets); asset != nil {
err := download(asset.DownloadURL)
return asset, err
Expand Down Expand Up @@ -322,14 +300,20 @@ func unzip(src, dest string) error {

// Release is a github release
type Release struct {
Name string `json:"name"`
Description string `json:"body"`
Version string `json:"tag_name"`
URL string `json:"url"`
ID int `json:"id"`
PreRelease bool `json:"prerelease"`
Name string `json:"name,omitempty"`
Description string `json:"body,omitempty"`
Version string `json:"tag_name,omitempty"`
URL string `json:"url,omitempty"`
ID int `json:"id,omitempty"`
PreRelease bool `json:"prerelease,omitempty"`
PublishedAt string `json:"published_at,omitempty"`
Assets []Asset `json:"assets,omitempty"`
InfoURL string `json:"html_url,omitempty"`
}

Assets []Asset `json:"assets"`
// Semver will return a semver.Version from the release version
func (r *Release) Semver() (*semver.Version, error) {
return semver.NewVersion(r.Version)
}

// Asset is a single download and it's various info for a github release
Expand Down
36 changes: 36 additions & 0 deletions assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,42 @@ function getWebSocketURL() {
return url;
}

$(".update-button").on("click", function() {
var button = $(this);
var version = button.attr("version");
var description = button.attr("description");
var url = "/api/update/to/" + version;

swal({
title: "Are you sure you want to update to {0}?".format(version),
html: "<b>Description: </b> description",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
confirmButtonText: "Yes, delete it!"
}).then(function() {
$.ajax({
url: url,
type: "POST",
success: function(d) {
showNotification(
"Awesome! Updated to " +
version +
"! All you need to do is restart the current running app.",
"top",
"left",
"success"
);
},

error: function(d) {
showNotification("Error: " + d.responseText, "top", "left", "danger");
}
});
});
});

$(".delete-account").on("click", function() {
var button = $(this);
var username = button.attr("username");
Expand Down
3 changes: 3 additions & 0 deletions assets/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@
<li>
<a href="https://github.com/socialplanner/instahelper">Github</a>
</li>
<li>
<a href="/update">Update</a>
</li>
</ul>
</nav>
<p class="copyright pull-right">
Expand Down
50 changes: 50 additions & 0 deletions assets/templates/update.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{{define "content"}}
<div class="row">
<div class="col-md-12">
<div class="card card-plain">
<div class="card-header" data-background-color="purple">
<h4 class="title">Update</h4>
<p class="category">You can update to a specific version by clicking the respective button.</p>
</div>
<div class="card-content table-responsive">
<table class="table table-hover">
<thead>
<th>Version</th>
<th>Name</th>
<th>Published At</th>
<th>Description</th>
<th>Actions</th>
</thead>
<tbody>
{{range .Releases}}
<tr>
<td>{{.Version}}</td>
<td>{{.Name}}</td>
<td>{{.PublishedAt}}</td>
<td>
{{if .PreRelease}}
<b>PRERELEASE:</b>
<br> {{end}} {{.Description}}
</td>
<td class="td-actions text-right">
<a href="{{.InfoURL}}">
<button type="button" rel="tooltip" class="btn btn-info" data-original-title="" title="">
<i class="material-icons">info</i>
<div class="ripple-container"></div>
</button>
</a>
<button description="{{.Description}}" version="{{.Version}}" type="button" rel="tooltip" class="btn btn-success update-button"
data-original-title="" title="">
<i class="material-icons">get_app</i>
<div class="ripple-container"></div>
</button>
</td>
</tr>
{{end}}
</tbody>
</table>
</div>
</div>
</div>
</div>
{{end}}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func main() {
debug := flag.Bool("debug", false, "Run in debug mode")

flag.Parse()

// To be removed on working prototype :)
fmt.Println("Rome wasn't built in a day.")

Expand Down

0 comments on commit b308f61

Please sign in to comment.