summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhorus_arch2015-04-21 01:47:42 +0200
committerhorus_arch2015-04-21 01:47:42 +0200
commitb3fa125ed0292f8ca37e91bf19c8cae7cdacc506 (patch)
tree61079b97ed26bf4c9483eb14dee7205c3c349b90
parent01e9a34952bd6ddd383680b0ca2312e476ad07a6 (diff)
downloadmandible-b3fa125ed0292f8ca37e91bf19c8cae7cdacc506.tar.gz
Add API documentation and better defaults.
-rw-r--r--.gitignore1
-rw-r--r--config/conf.json_14
-rw-r--r--config/default.conf.json2
-rw-r--r--main.go30
-rwxr-xr-xrun.sh1
-rw-r--r--server/server.go7
-rw-r--r--server/templateHandler.go1
-rw-r--r--static/js/bootstrap.file-input.js4
-rw-r--r--[l---------]templates/api.html114
-rw-r--r--templates/cli.html2
-rw-r--r--templates/header.html1
-rw-r--r--templates/index.html3
12 files changed, 153 insertions, 27 deletions
diff --git a/.gitignore b/.gitignore
index b634adb..1fe2226 100644
--- a/.gitignore
+++ b/.gitignore
@@ -27,5 +27,6 @@ ImgurGo
tags
mandible
config/conf.json
+files/
*.sw[o-p]
diff --git a/config/conf.json_ b/config/conf.json_
deleted file mode 100644
index a03249e..0000000
--- a/config/conf.json_
+++ /dev/null
@@ -1,14 +0,0 @@
-{
- "Port": 8080,
- "MaxFileSize": 20971520,
- "HashLength": 7,
- "UserAgent": "Mandible",
- "Stores" : [
- {
- "Type" : "local",
- "StoreRoot": "/home/horus/projects/golang/src/mandible/files",
- "NamePathRegex" : "^([a-zA-Z0-9])([a-zA-Z0-9]).*",
- "NamePathMap" : "${ImageSize}/${1}/${2}/${ImageName}"
- }
- ]
-}
diff --git a/config/default.conf.json b/config/default.conf.json
index 0372b3d..2b7520e 100644
--- a/config/default.conf.json
+++ b/config/default.conf.json
@@ -2,7 +2,7 @@
"Port": 8080,
"MaxFileSize": 20971520,
"HashLength": 7,
- "UserAgent": "ImgurGo (https://github.com/gophergala/ImgurGo)",
+ "UserAgent": "Mandible",
"Stores" : [
{
"Type" : "local",
diff --git a/main.go b/main.go
index 67ef2ad..e1f7e68 100644
--- a/main.go
+++ b/main.go
@@ -1,10 +1,7 @@
package main
import (
- /*
- mandibleConf "mandible/config"
- mandible "mandible/server"
- */
+ "fmt"
"mandible/config"
mandible "mandible/server"
"os"
@@ -12,8 +9,33 @@ import (
func main() {
configFile := os.Getenv("IMGUR_GO_CONF")
+ if configFile == "" {
+ configFile = "config/conf.json"
+ }
config := config.NewConfiguration(configFile)
+
+ port := ":" + os.Getenv("PORT")
+ if port == ":" {
+ port = fmt.Sprintf(":%d", config.Port)
+ }
+
+ if os.Getenv("UPLOAD_URL") == "" {
+ os.Setenv("UPLOAD_URL", "http://127.0.0.1"+port)
+ }
+
+ if os.Getenv("ORIGIN_URL") == "" {
+ os.Setenv("ORIGIN_URL", os.Getenv("UPLOAD_URL"))
+ }
+
+ if os.Getenv("STATIC_DIR") == "" {
+ os.Setenv("STATIC_DIR", (config.Stores[0]["StoreRoot"])+"/../static")
+ }
+
+ if os.Getenv("UPLOAD_DIR") == "" {
+ os.Setenv("UPLOAD_DIR", (config.Stores[0]["StoreRoot"]))
+ }
+
server := mandible.CreateServer(config)
server.Start()
}
diff --git a/run.sh b/run.sh
index ab368b0..27c86ae 100755
--- a/run.sh
+++ b/run.sh
@@ -26,4 +26,5 @@ STATIC_DIR=$(pwd)/static \
IMGUR_GO_CONF=$(pwd)/config/conf.json \
UPLOAD_DIR=$(pwd)/files \
UPLOAD_URL=http://i.iamfabulous.de:8080 \
+ORIGIN_URL=https://images.iamfabulous.de \
./$APP
diff --git a/server/server.go b/server/server.go
index 4cfafe7..e55d2e1 100644
--- a/server/server.go
+++ b/server/server.go
@@ -194,7 +194,10 @@ func (s *Server) Start() {
apiHandler := func(w http.ResponseWriter, r *http.Request) {
templ := mainTempl.Lookup("api.html")
- err := templ.ExecuteTemplate(w, "api.html", nil)
+ err := templ.ExecuteTemplate(w, "api.html", struct {
+ OriginUrl string
+ ResponseUrl string
+ }{os.Getenv("ORIGIN_URL"), os.Getenv("UPLOAD_URL")})
if err != nil {
ErrorResponse(w, err.Error(), http.StatusInternalServerError)
return
@@ -231,7 +234,7 @@ func (s *Server) Start() {
port = fmt.Sprintf(":%d", s.Config.Port)
}
- http.ListenAndServe(port, nil)
+ log.Panic(http.ListenAndServe(port, nil))
}
func (s *Server) download(url string) (io.ReadCloser, error) {
diff --git a/server/templateHandler.go b/server/templateHandler.go
deleted file mode 100644
index abb4e43..0000000
--- a/server/templateHandler.go
+++ /dev/null
@@ -1 +0,0 @@
-package server
diff --git a/static/js/bootstrap.file-input.js b/static/js/bootstrap.file-input.js
index c56ea33..54d7c5b 100644
--- a/static/js/bootstrap.file-input.js
+++ b/static/js/bootstrap.file-input.js
@@ -29,6 +29,7 @@ $.fn.bootstrapFileInput = function() {
if (typeof $elem.attr('title') != 'undefined') {
buttonWord = $elem.attr('title');
}
+ buttonWord = '<span class="glyphicon glyphicon-picture"></span> ' + buttonWord;
var className = '';
@@ -105,7 +106,8 @@ $.fn.bootstrapFileInput = function() {
var selectedFileNamePlacement = $(this).data('filename-placement');
if (selectedFileNamePlacement === 'inside') {
// Print the fileName inside
- $(this).siblings('span').html(fileName);
+ fileTitle = '<span class="glyphicon glyphicon-picture"></span> ' + fileName;
+ $(this).siblings('span').html(fileTitle);
$(this).attr('title', fileName);
} else {
// Print the fileName aside (right after the the button)
diff --git a/templates/api.html b/templates/api.html
index 7b03479..a88e584 120000..100644
--- a/templates/api.html
+++ b/templates/api.html
@@ -1 +1,113 @@
-cli.html \ No newline at end of file
+{{template "header.html" "Upload images from file!"}}
+{{template "navbar.html"}}
+{{template "modal_uploaded.html"}}
+{{template "modal_error.html"}}
+
+<div class="container">
+ <div class="row">
+ <div class="col-md-12 well">
+
+ <h1>Rest API:</h1>
+ <p>Interfacing with Mandible is extremely simple:</p>
+ <p></p>
+
+ <blockquote>
+ <h2>Upload an image file:</h2>
+ <p><code>POST {{.OriginUrl}}/api/v1/file</code></p>
+ <p>with the following multi-part/form-data</p>
+ <ul>
+ <li><code>image</code> - file</li>
+ </ul>
+ </blockquote>
+
+ <blockquote>
+ <h2>Upload an image from a URL:</h2>
+ <p><code>POST {{.OriginUrl}}/api/v1/url</code></p>
+ <p>with the following multi-part/form-data</p>
+ <ul>
+ <li><code>image</code> - file</li>
+ </ul>
+ </blockquote>
+
+ <blockquote>
+ <h2>Upload an image from base64 data:</h2>
+ <p><code>POST {{.OriginUrl}}/api/v1/base64</code></p>
+ <p>with the following multi-part/form-data</p>
+ <ul>
+ <li><code>image</code> - image encoded as base64 data</li>
+ </ul>
+ </blockquote>
+
+ <blockquote>
+ <h2>Thumbnail Generation:</h2>
+ <p>To generate thumbnails with the request, pass the following JSON as form-data, keyed under <code>thumbs</code></p>
+ <pre><code>{
+ "name1": {
+ "width": x,
+ "height": y,
+ "shape": ("square" | "thumb" | "circle")
+ },
+ "name2": {
+ "width": x2,
+ "height": y2,
+ "shape": ("square" | "thumb" | "circle")
+ },
+ ...
+}</code></pre>
+ <p>Note: Square thumbnails don't preserve aspect ratio, whereas the 'thumb' type does.</p>
+ </blockquote>
+
+ <h1>Example Usage:</h1>
+ <blockquote>
+ <h2>URL Upload with thumbnails:</h2>
+ <pre><code>curl -i {{.OriginUrl}}/api/v1/url \
+-d 'image=http://i.imgur.com/s9zxmYe.jpg' \
+-d 'thumbs={"small": {"width": 20, "height": 20, "shape": "square"}, "profile": {"width": 50, "height": 50, "shape": "circle"}}'</code></pre>
+
+ <h2>Response:</h2>
+ <pre><code>{
+ "data": {
+ "width": 380,
+ "height": 430,
+ "link": "{{.ResponseUrl}}/i/Y/h/Yhs6srW",
+ "mime": "image/jpeg",
+ "name": "",
+ "size": 82199,
+ "thumbs": {
+ "profile":"{{.ResponseUrl}}/t/Y/h/Yhs6srW/profile",
+ "small": "{{.ResponseUrl}}/t/Y/h/Yhs6srW/small"
+ }
+ },
+ "status": 200,
+ "success": true
+}</pre></code>
+ </blockquote>
+
+ <blockquote>
+ <h2>File Upload with thumbnails:</h2>
+ <pre><code>curl -i {{.OriginUrl}}/api/v1/file \
+-F 'image=@/tmp/cat.gif' \
+-F 'thumbs={"small": {"width": 20, "height": 20, "shape": "square"}}'</pre></code>
+
+ <h2>Response:</h2>
+ <pre><code>{
+ "data": {
+ "width": 354,
+ "height": 200,
+ "link": "{{.ResponseUrl}}/i/U/v/UvfuzBW",
+ "mime": "image/gif",
+ "name": "cat.gif",
+ "size": 3511100,
+ "thumbs": {
+ "small":"{{.ResponseUrl}}/t/U/v/UvfuzBW/small"
+ }
+ },
+ "status": 200,
+ "success": true
+}</pre></code>
+ </blockquote>
+ </div>
+ </div>
+</div>
+
+{{template "footer.html"}}
diff --git a/templates/cli.html b/templates/cli.html
index d6dfa84..9d50adb 100644
--- a/templates/cli.html
+++ b/templates/cli.html
@@ -5,7 +5,7 @@
<div class="container">
<div class="row">
- <div class="col-md-12">
+ <div class="col-md-12 well">
<h1>TODO!</h1>
diff --git a/templates/header.html b/templates/header.html
index 2e0b604..8269f3d 100644
--- a/templates/header.html
+++ b/templates/header.html
@@ -13,6 +13,5 @@
<link rel='stylesheet' type='text/css' href='/static/css/style.css'>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<link rel='shortcut icon' type='image/png' href='/static/img/favicon.ico'>
- <!--noscript><style>.navbar{margin-bottom:0;}.noscript{margin-bottom:20;}</style></noscript-->
</head>
<body>
diff --git a/templates/index.html b/templates/index.html
index 21180b7..44f6ace 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -61,7 +61,8 @@
<i class="fa fa-exchange"></i>
REST-Like API</h2>
- <p><strong>TODO:</strong> Read the documentation here.</p>
+ <p>We provice a interface for easy access without your web browser. <br>
+ <a href="/api" title="API Documentation" class="btn btn-material-indigo-A700"><span class="fa fa-info-circle"></span> Read here more! &raquo;</a></p>
</div>
</div>
<div class="row">