-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
143 lines (120 loc) · 3.83 KB
/
main.go
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// this basic example shows how to set up optic routes and call them with the provided client
package main
import (
"bytes"
"fmt"
"net/http"
"time"
"github.com/nanvenomous/optic"
)
const (
port = "4040"
host = "127.0.0.1"
userOpticRoute = "/api/optic/"
)
type solution struct {
Answer int
}
type subtraction struct {
First int
Second int
}
type division struct {
Top int
Bottom int
}
type userHTTPError struct {
Message string
Code int
}
// GetCode method returns the http.StatusCode for the HTTPError
// necessary to send the http code in a response
func (e *userHTTPError) GetCode() int {
return e.Code
}
func subtract(recieved *subtraction, r *http.Request) (*solution, optic.HTTPError) {
// Do something with a header (like check Authorization, or get a cookie)
fmt.Println(r.Header.Get("Authorization"))
return &solution{Answer: recieved.First - recieved.Second}, nil
}
func divide(recieved *division, _ *http.Request) (*solution, optic.HTTPError) {
if recieved.Bottom == 0 { // return an error
return nil, &userHTTPError{Code: http.StatusUnprocessableEntity, Message: "Impossible to divide by Zero"}
}
return &solution{Answer: recieved.Top / recieved.Bottom}, nil
}
func setupService() {
var (
err error
encodeErr = &userHTTPError{Code: http.StatusInternalServerError, Message: "Failed to encode your response."}
decodeErr = &userHTTPError{Code: http.StatusNotAcceptable, Message: "Failed to decode your request body."}
mux *http.ServeMux
)
mux = http.NewServeMux()
optic.SetupService(port, userOpticRoute, encodeErr, decodeErr, mux)
// An optical mirror simply recieves information and sends information back
optic.Mirror(subtract, "/RunSubtraction/")
optic.Mirror(divide) // by default optic will use function name as route
// Add other routes not handled by optic, as you would with any net/http service
mux.HandleFunc("/health-check/", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
// Add any net/http middleware
optic.RegisterMiddleware(exampleMiddleware)
// for this example, run the service in the background
go func() {
err = optic.Serve() // run the service
if err != nil {
panic(err)
}
}()
}
func main() {
setupService()
waitServiceUp() // only for this example
optic.SetupClient(host, port, userOpticRoute, false)
// Make requests
var (
err error // internal error
httpErr *userHTTPError // service exception
sln solution // output
)
httpErr, err = optic.Glance[userHTTPError]("/RunSubtraction/", &subtraction{First: 1, Second: 2}, &sln)
fmt.Println(err, httpErr) // <nil> <nil>
fmt.Println(sln.Answer) // -1
httpErr, err = optic.Glance[userHTTPError]("/divide/", &division{Top: 1, Bottom: 0}, &sln)
fmt.Println(err, httpErr) // <nil> &{ Impossible to divide by Zero 422}
fmt.Println(sln) // <nil>
// send receive
httpErr, err = optic.Glance[userHTTPError]("/divide/", &division{Top: 4, Bottom: 2}, &sln)
fmt.Println(err, httpErr) // <nil> <nil>
fmt.Println(sln.Answer) // 2
}
func exampleMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// add some middleware (like CORS for example)
w.Header().Set("Access-Control-Allow-Origin", "*")
next.ServeHTTP(w, r)
})
}
// this method is to wait for the service to initialize
func waitServiceUp() error {
var (
err error
req *http.Request
)
req, err = http.NewRequest(http.MethodGet, fmt.Sprintf("http://%s:%s/health-check/", host, port), bytes.NewBuffer([]byte{}))
if err != nil {
return err
}
for {
time.Sleep(time.Millisecond * 50)
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
} else if res.StatusCode == http.StatusOK {
break
}
}
return nil
}