From b3fa125ed0292f8ca37e91bf19c8cae7cdacc506 Mon Sep 17 00:00:00 2001 From: horus_arch Date: Tue, 21 Apr 2015 01:47:42 +0200 Subject: Add API documentation and better defaults. --- .gitignore | 1 + config/conf.json_ | 14 ----- config/default.conf.json | 2 +- main.go | 30 ++++++++-- run.sh | 1 + server/server.go | 7 ++- server/templateHandler.go | 1 - static/js/bootstrap.file-input.js | 4 +- templates/api.html | 114 +++++++++++++++++++++++++++++++++++++- templates/cli.html | 2 +- templates/header.html | 1 - templates/index.html | 3 +- 12 files changed, 153 insertions(+), 27 deletions(-) delete mode 100644 config/conf.json_ delete mode 100644 server/templateHandler.go mode change 120000 => 100644 templates/api.html 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 = ' ' + 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 = ' ' + 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 deleted file mode 120000 index 7b03479..0000000 --- a/templates/api.html +++ /dev/null @@ -1 +0,0 @@ -cli.html \ No newline at end of file diff --git a/templates/api.html b/templates/api.html new file mode 100644 index 0000000..a88e584 --- /dev/null +++ b/templates/api.html @@ -0,0 +1,113 @@ +{{template "header.html" "Upload images from file!"}} +{{template "navbar.html"}} +{{template "modal_uploaded.html"}} +{{template "modal_error.html"}} + +
Interfacing with Mandible is extremely simple:
+ + +++ +Upload an image file:
++
POST {{.OriginUrl}}/api/v1/filewith the following multi-part/form-data
++
+- +
image- file
++ +Upload an image from a URL:
++
POST {{.OriginUrl}}/api/v1/urlwith the following multi-part/form-data
++
+- +
image- file
++ +Upload an image from base64 data:
++
POST {{.OriginUrl}}/api/v1/base64with the following multi-part/form-data
++
+- +
image- image encoded as base64 data
++ +Thumbnail Generation:
+To generate thumbnails with the request, pass the following JSON as form-data, keyed under
+thumbs+{ + "name1": { + "width": x, + "height": y, + "shape": ("square" | "thumb" | "circle") + }, + "name2": { + "width": x2, + "height": y2, + "shape": ("square" | "thumb" | "circle") + }, + ... +}Note: Square thumbnails don't preserve aspect ratio, whereas the 'thumb' type does.
+
++ +URL Upload with thumbnails:
++ +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"}}'Response:
++{ + "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 +}
++File Upload with thumbnails:
++ +curl -i {{.OriginUrl}}/api/v1/file \ +-F 'image=@/tmp/cat.gif' \ +-F 'thumbs={"small": {"width": 20, "height": 20, "shape": "square"}}'Response:
++{ + "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 +}
TODO: Read the documentation here.
+We provice a interface for easy access without your web browser.
+ Read here more! »