9 changed files with 175 additions and 62 deletions
-
2assets/index.html
-
1assets/qurl.css
-
24assets/submit.html
-
3load.go
-
13main.go
-
79pages/submit.go
-
22qurl/qurl.go
-
2static/static.go
-
91storage/bolt/qurl.go
@ -0,0 +1,24 @@ |
|||||
|
<!DOCTYPE html> |
||||
|
<html lang="en"> |
||||
|
<head> |
||||
|
<meta charset="utf-8"> |
||||
|
<title>qurl.org</title> |
||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> |
||||
|
<link rel="stylesheet" media="screen" href="qurl.css"> |
||||
|
</head> |
||||
|
<body> |
||||
|
<div id="c"> |
||||
|
<div id="m"> |
||||
|
<div>{{if .Message}}{{.Message}}{{end}}</div> |
||||
|
<div>{{if .URL}}<a href="{{.URL}}">{{.URL}}</a>{{end}}</div> |
||||
|
</div> |
||||
|
|
||||
|
qurl.org is a simple url shortening service, in the same vein as |
||||
|
<a href="https://bit.ly">bit.ly</a> and |
||||
|
<a href="https://tinyurl.com">tinyurl.com</a>. |
||||
|
qurl.org is <a href="http://binarythought.com/qurl/LICENSE">open source</a>, |
||||
|
it's code is <a href="https://binarythought.com/fossils/qurl/">freely available</a> |
||||
|
and has an <a href="api/index.html">easy to use API</a>. |
||||
|
</div> |
||||
|
</body> |
||||
|
</html> |
@ -0,0 +1,79 @@ |
|||||
|
package pages |
||||
|
|
||||
|
import ( |
||||
|
"bytes" |
||||
|
"fmt" |
||||
|
"html/template" |
||||
|
"net/http" |
||||
|
"qurl/qurl" |
||||
|
"qurl/static" |
||||
|
"qurl/storage" |
||||
|
"time" |
||||
|
) |
||||
|
|
||||
|
type SubmitHandler struct { |
||||
|
Storage storage.Storage |
||||
|
template *template.Template |
||||
|
} |
||||
|
|
||||
|
type submitPage struct { |
||||
|
Message string |
||||
|
URL string |
||||
|
} |
||||
|
|
||||
|
func (ctx *SubmitHandler) Init() error { |
||||
|
ctx.template = template.New("submit.html") |
||||
|
|
||||
|
_, err := ctx.template.Parse(string(static.Assets["submit.html"])) |
||||
|
if err != nil { |
||||
|
return err |
||||
|
} |
||||
|
return nil |
||||
|
} |
||||
|
|
||||
|
func (ctx *SubmitHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
||||
|
var pg submitPage |
||||
|
|
||||
|
u := r.FormValue("url") |
||||
|
if u == "" { |
||||
|
pg.Message = "Not a valid URL." |
||||
|
} else { |
||||
|
q, err := ctx.Storage.GetQURLByURL(u) |
||||
|
if err != nil { |
||||
|
http.Error(w, fmt.Sprintf("Database error: %s", err.Error()), |
||||
|
http.StatusInternalServerError) |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
if q != nil { |
||||
|
pg.Message = "URL already exists." |
||||
|
pg.URL = fmt.Sprintf("https://qurl.org/%s", qurl.ToString(q.ID)) |
||||
|
} else { |
||||
|
q = &qurl.QURL{ |
||||
|
URL: u, |
||||
|
Created: time.Now(), |
||||
|
} |
||||
|
|
||||
|
err := ctx.Storage.AddQURL(q) |
||||
|
if err != nil { |
||||
|
http.Error(w, fmt.Sprintf("Database error: %s", err.Error()), |
||||
|
http.StatusInternalServerError) |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
pg.Message = "URL Added." |
||||
|
pg.URL = fmt.Sprintf("https://qurl.org/%s", qurl.ToString(q.ID)) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
var buf bytes.Buffer |
||||
|
err := ctx.template.Execute(&buf, pg) |
||||
|
if err != nil { |
||||
|
http.Error(w, fmt.Sprintf("Template execute error: %s", err.Error()), |
||||
|
http.StatusInternalServerError) |
||||
|
return |
||||
|
} |
||||
|
w.Header().Set("Content-Length", fmt.Sprintf("%d", buf.Len())) |
||||
|
w.Header().Set("Content-Type", "text/html") |
||||
|
w.Write(buf.Bytes()) |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue