aboutsummaryrefslogtreecommitdiff
path: root/core.go
blob: b159fdd2807a7502c18766db08ba8e344a0728c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package sleepy

import (
	"encoding/json"
	"fmt"
	"net/http"
	"net/url"
)

const (
	GET    = "GET"
	POST   = "POST"
	PUT    = "PUT"
	DELETE = "DELETE"
)

type GetSupported interface {
	Get(values url.Values) (int, interface{})
}

type PostSupported interface {
	Post(values url.Values) (int, interface{})
}

type PutSupported interface {
	Put(values url.Values) (int, interface{})
}

type DeleteSupported interface {
	Delete(values url.Values) (int, interface{})
}

type API struct {
    mux *http.ServeMux
}

func NewAPI() *API {
    return &API{}
}

func (api *API) Abort(rw http.ResponseWriter, statusCode int) {
	rw.WriteHeader(statusCode)
	rw.Write([]byte(http.StatusText(statusCode)))
}

func (api *API) requestHandler(resource interface{}) http.HandlerFunc {
	return func(rw http.ResponseWriter, request *http.Request) {

        var err error
		var data interface{} = ""
		var code int = 405

		method := request.Method
		if request.ParseForm() != nil {
			api.Abort(rw, 400)
            return
		}
		values := request.Form

		switch method {
		case GET:
			if r, ok := resource.(GetSupported); ok {
				code, data = r.Get(values)
			} else {
                api.Abort(rw, 405)
                return
            }
		case POST:
			if r, ok := resource.(PostSupported); ok {
				code, data = r.Post(values)
			} else {
                api.Abort(rw, 405)
                return
            }
		case PUT:
			if r, ok := resource.(PutSupported); ok {
				code, data = r.Put(values)
			} else {
                api.Abort(rw, 405)
                return
            }
		case DELETE:
			if r, ok := resource.(DeleteSupported); ok {
				code, data = r.Delete(values)
			} else {
                api.Abort(rw, 405)
                return
            }
		default:
			api.Abort(rw, 405)
			return
		}

		content, err := json.Marshal(data)
		if err != nil {
			api.Abort(rw, 500)
		}
		rw.WriteHeader(code)
		rw.Write(content)
	}
}

func (api *API) AddResource(resource interface{}, path string) {
    if api.mux == nil {
        api.mux = http.NewServeMux()
    }
	api.mux.HandleFunc(path, api.requestHandler(resource))
}

func (api *API) Start(port int) error {
    if api.mux == nil {
        return &ErrorString{"You must add at least one resource to this API."}
    }
	portString := fmt.Sprintf(":%d", port)
	http.ListenAndServe(portString, api.mux)
    return nil
}
bgstack15