Skip to content

Commit

Permalink
Fixed json messages, implemented delete button
Browse files Browse the repository at this point in the history
  • Loading branch information
kellegous committed Jul 3, 2015
1 parent 737e215 commit c0e3858
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 44 deletions.
4 changes: 2 additions & 2 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const (

// Route ...
type Route struct {
URL string
Time time.Time
URL string `json:"url"`
Time time.Time `json:"time"`
}

//
Expand Down
38 changes: 27 additions & 11 deletions pub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ var load = function() {
url: '/api/url/' + name,
dataType: 'json'
}).always(function(data) {
var url = data.url || '';
$('#url').val(url)
.focus();
if (!data.ok) {
// TODO(knorton): Error
return;
}

var route = data.route,
url = route.url || '';
$('#url').val(url).focus();
});
}

Expand All @@ -41,24 +46,35 @@ var form = $('form').on('submit', function(e) {
var name = nameFrom(location.pathname),
url = $('#url').val().trim();

if (!url) {
return;
}

$.ajax({
type: 'POST',
url : '/api/url/' + name,
data : JSON.stringify({ url : url }),
dataType : 'json'
}).success(function(data) {
var url = data.url || '';
}).always(function(data, txt, xhr) {
if (!data.ok) {
return;
}

var route = data.route;
if (!route) {
// deleted
}

var url = route.url || '',
name = route.name || '';
if (url) {
history.replaceState({}, null, '/edit/' + data.name);
showLink(data.name);
history.replaceState({}, null, '/edit/' + name);
showLink(name);
}
});
});

$('#cls').on('click', function(e) {
$('#url').val('');
$('form').submit();
});

window.addEventListener('resize', resize);
resize();
load();
Expand Down
4 changes: 2 additions & 2 deletions web/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 47 additions & 29 deletions web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ func parseName(base, path string) string {
return t[:ix]
}

type routeWithName struct {
Name string `json:"name"`
*context.Route
}

type msg struct {
Ok bool `json:"ok"`
Error string `json:"error,omitempty"`
Route *routeWithName `json:"route,omitempty"`
}

func writeJSON(w http.ResponseWriter, data interface{}, status int) {
w.Header().Set("Content-Type", "application/json;charset=utf-8")
w.WriteHeader(status)
Expand All @@ -60,28 +71,32 @@ func writeJSON(w http.ResponseWriter, data interface{}, status int) {
}
}

func writeJSONError(w http.ResponseWriter, error string, status int) {
writeJSON(w, map[string]interface{}{
"error": error,
}, status)
func writeJSONRoute(w http.ResponseWriter, name string, rt *context.Route) {
writeJSON(w, &msg{
Ok: true,
Route: &routeWithName{
Name: name,
Route: rt,
},
}, http.StatusOK)
}

func writeJSONNotFound(w http.ResponseWriter) {
writeJSON(w, nil, http.StatusNotFound)
func writeJSONOk(w http.ResponseWriter) {
writeJSON(w, &msg{
Ok: true,
}, http.StatusOK)
}

func writeJSONRoute(w http.ResponseWriter, name string, rt *context.Route) {
res := struct {
Name string `json:"name"`
URL string `json:"url"`
Time time.Time `json:"time"`
}{
name,
rt.URL,
rt.Time,
}
func writeJSONError(w http.ResponseWriter, err string) {
writeJSON(w, &msg{
Ok: false,
Error: err,
}, http.StatusOK)
}

writeJSON(w, &res, http.StatusOK)
func writeJSONBackendError(w http.ResponseWriter, err error) {
log.Printf("[error] %s", err)
writeJSONError(w, "backend error")
}

func serveAsset(w http.ResponseWriter, r *http.Request, name string) {
Expand Down Expand Up @@ -126,33 +141,36 @@ func apiPost(ctx *context.Context, w http.ResponseWriter, r *http.Request) {
}

if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeJSONError(w, "invalid json", http.StatusBadRequest)
writeJSONError(w, "invalid json")
return
}

// Handle delete requests
if req.URL == "" {
if p == "" {
writeJSONError(w, "url required", http.StatusBadRequest)
writeJSONError(w, "url required")
return
}

if err := ctx.Del(p); err != nil {
log.Panic(err)
writeJSONBackendError(w, err)
return
}

return
}

if !validURL(req.URL) {
writeJSONError(w, "invalid URL", http.StatusBadRequest)
writeJSONError(w, "invalid URL")
return
}

// If no name is specified, an ID must be generate.
if p == "" {
id, err := ctx.NextID()
if err != nil {
log.Panic(err)
writeJSONBackendError(w, err)
return
}
p = encodeID(id)
}
Expand All @@ -163,7 +181,8 @@ func apiPost(ctx *context.Context, w http.ResponseWriter, r *http.Request) {
}

if err := ctx.Put(p, &rt); err != nil {
log.Panic(err)
writeJSONBackendError(w, err)
return
}

writeJSONRoute(w, p, &rt)
Expand All @@ -173,16 +192,17 @@ func apiGet(ctx *context.Context, w http.ResponseWriter, r *http.Request) {
p := parseName("/api/url/", r.URL.Path)

if p == "" {
writeJSONNotFound(w)
writeJSON(w, nil, http.StatusNotFound)
return
}

rt, err := ctx.Get(p)
if err == leveldb.ErrNotFound {
writeJSONNotFound(w)
writeJSON(w, nil, http.StatusNotFound)
return
} else if err != nil {
log.Panic(err)
writeJSONBackendError(w, err)
return
}

writeJSONRoute(w, p, rt)
Expand All @@ -195,9 +215,7 @@ func (h *apiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
case "GET":
apiGet(h.ctx, w, r)
default:
writeJSONError(w,
http.StatusText(http.StatusMethodNotAllowed),
http.StatusMethodNotAllowed)
writeJSONError(w, http.StatusText(http.StatusMethodNotAllowed))
}
}

Expand Down

0 comments on commit c0e3858

Please sign in to comment.