Browse Source

Added API usage instructions

master
Christopher Ramey 5 years ago
committed by cdramey
parent
commit
0216358dfb
  1. 2
      assets/index.html
  2. 47
      assets/usage.html
  3. 14
      pages/root.go

2
assets/index.html

@ -13,7 +13,7 @@
<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>.
and has an <a href="api/usage.html">easy to use API</a>.
<form method="post" action="submit.html">
<input type="text" id="u" name="url" placeholder="https://qurl.org" />

47
assets/usage.html

@ -0,0 +1,47 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>qurl.org - API usage</title>
<style>
body { font-size: 14px; }
pre { font-size: 12px; border-left: 3px solid blue; padding-left: 10px; }
ul { margin: 0px; padding: 0px; }
li { margin: 0px 0px 0px 20px; padding: 0px; }
</style>
</head>
<body>
<h1>API usage</h1>
<p>API URL: http://qurl.org/api/url</p>
<p>
<ul>Parameters (GET or POST)
<li><b>url</b> The url to be shortened</li>
</ul>
</p>
<p>
<ul>JSON Response
<li><b>url</b> <i>(string)</i> the shortened url (e.g. http://qurl.org/0)</li>
<li><b>exists</b> <i>(boolean)</i> true if the provided URL was already in the database</li>
<li><b>error</b> <i>(string)</i> error message if url could not be shortened (e.g. "Invalid URL")</li>
</ul>
</p>
<h1>API example</h1>
<p>
<b>Submit:</b><pre>http://qurl.org/api/url?url=http://fark.com</pre>
<b>Response:</b><pre>{"exists":true,"url":"http:\/\/qurl.org\/s"}</pre>
</p>
<br/>
<p>
<b>Submit:</b><pre>http://qurl.org/api/url?url=http://klasjdlh.com</pre>
<b>Response:</b><pre>{"exists":false,"url":"http:\/\/qurl.org\/Z1q"}</pre>
</p>
<br/>
<p>
<b>Submit:</b><pre>http://qurl.org/api/url?url=notaurl</pre>
<b>Response:</b><pre>{"error":"Not a valid URL."}</pre>
</p>
</body>
</html>

14
pages/root.go

@ -15,6 +15,7 @@ type RootHandler struct {
index *static.StaticContent
css *static.StaticContent
favi *static.StaticContent
usage *static.StaticContent
submit *template.Template
}
@ -36,6 +37,9 @@ func (ctx *RootHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
case "/favicon.ico":
ctx.favi.ServeHTTP(w, r)
case "/api/usage.html":
ctx.usage.ServeHTTP(w, r)
case "/api/url":
ctx.ServeAPI(w, r)
@ -73,7 +77,7 @@ func (ctx *RootHandler) Init() error {
}
ctx.css = css
// Initialize the static content object for the css
// Initialize the static content object favicon
favi := &static.StaticContent{Content: "favicon.ico"}
err = favi.Init()
if err != nil {
@ -81,6 +85,14 @@ func (ctx *RootHandler) Init() error {
}
ctx.favi = favi
// Initialize the api usage instructions
usage := &static.StaticContent{Content: "usage.html"}
err = usage.Init()
if err != nil {
return err
}
ctx.usage = usage
// Initialize submit page template
ctx.submit = template.New("submit.html")
_, err = ctx.submit.Parse(string(static.Assets["submit.html"]))

Loading…
Cancel
Save