diff options
| -rw-r--r-- | README.md | 9 | ||||
| -rw-r--r-- | cmd/main.go (renamed from main.go) | 2 | ||||
| -rw-r--r-- | torpedo.go | 88 |
3 files changed, 97 insertions, 2 deletions
| @@ -1 +1,8 @@ | |||
| 1 | Implementation details can be found here: http://www.subsonic.org/pages/api.jsp \ No newline at end of file | 1 | Implementation details can be found here: http://www.subsonic.org/pages/api.jsp |
| 2 | |||
| 3 | To start dev clone this repo into $GOPATH/src/codesaloon.com/torpedo | ||
| 4 | |||
| 5 | and start the server with: | ||
| 6 | |||
| 7 | cd cmd | ||
| 8 | go run main.go \ No newline at end of file | ||
| @@ -2,10 +2,10 @@ package main | |||
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "bytes" | 4 | "bytes" |
| 5 | "codesaloon.com/torpedo" | ||
| 5 | "encoding/xml" | 6 | "encoding/xml" |
| 6 | "flag" | 7 | "flag" |
| 7 | "fmt" | 8 | "fmt" |
| 8 | "github.com/sideshowdave7/torpedo" | ||
| 9 | "net/url" | 9 | "net/url" |
| 10 | ) | 10 | ) |
| 11 | 11 | ||
diff --git a/torpedo.go b/torpedo.go new file mode 100644 index 0000000..e0c1b99 --- /dev/null +++ b/torpedo.go | |||
| @@ -0,0 +1,88 @@ | |||
| 1 | package torpedo | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "fmt" | ||
| 5 | "net/http" | ||
| 6 | "net/url" | ||
| 7 | ) | ||
| 8 | |||
| 9 | const ( | ||
| 10 | GET = "GET" | ||
| 11 | POST = "POST" | ||
| 12 | PUT = "PUT" | ||
| 13 | DELETE = "DELETE" | ||
| 14 | ) | ||
| 15 | |||
| 16 | type Resource interface { | ||
| 17 | Get(values url.Values) (int, string) | ||
| 18 | Post(values url.Values) (int, string) | ||
| 19 | Put(values url.Values) (int, string) | ||
| 20 | Delete(values url.Values) (int, string) | ||
| 21 | } | ||
| 22 | |||
| 23 | type ( | ||
| 24 | GetNotSupported struct{} | ||
| 25 | PostNotSupported struct{} | ||
| 26 | PutNotSupported struct{} | ||
| 27 | DeleteNotSupported struct{} | ||
| 28 | ) | ||
| 29 | |||
| 30 | func (GetNotSupported) Get(values url.Values) (int, string) { | ||
| 31 | return 405, "" | ||
| 32 | } | ||
| 33 | |||
| 34 | func (PostNotSupported) Post(values url.Values) (int, string) { | ||
| 35 | return 405, "" | ||
| 36 | } | ||
| 37 | |||
| 38 | func (PutNotSupported) Put(values url.Values) (int, string) { | ||
| 39 | return 405, "" | ||
| 40 | } | ||
| 41 | |||
| 42 | func (DeleteNotSupported) Delete(values url.Values) (int, string) { | ||
| 43 | return 405, "" | ||
| 44 | } | ||
| 45 | |||
| 46 | type API struct{} | ||
| 47 | |||
| 48 | func (api *API) Abort(rw http.ResponseWriter, statusCode int) { | ||
| 49 | rw.WriteHeader(statusCode) | ||
| 50 | } | ||
| 51 | |||
| 52 | func (api *API) requestHandler(resource Resource) http.HandlerFunc { | ||
| 53 | return func(rw http.ResponseWriter, request *http.Request) { | ||
| 54 | |||
| 55 | var data string | ||
| 56 | var code int | ||
| 57 | |||
| 58 | request.ParseForm() | ||
| 59 | method := request.Method | ||
| 60 | values := request.Form | ||
| 61 | |||
| 62 | switch method { | ||
| 63 | case GET: | ||
| 64 | code, data = resource.Get(values) | ||
| 65 | case POST: | ||
| 66 | code, data = resource.Post(values) | ||
| 67 | case PUT: | ||
| 68 | code, data = resource.Put(values) | ||
| 69 | case DELETE: | ||
| 70 | code, data = resource.Delete(values) | ||
| 71 | default: | ||
| 72 | api.Abort(rw, 405) | ||
| 73 | return | ||
| 74 | } | ||
| 75 | |||
| 76 | rw.WriteHeader(code) | ||
| 77 | rw.Write([]byte(data)) | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | func (api *API) AddResource(resource Resource, path string) { | ||
| 82 | http.HandleFunc(path, api.requestHandler(resource)) | ||
| 83 | } | ||
| 84 | |||
| 85 | func (api *API) Start(port int) { | ||
| 86 | portString := fmt.Sprintf(":%d", port) | ||
| 87 | http.ListenAndServe(portString, nil) | ||
| 88 | } | ||