-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
43 lines (35 loc) · 1020 Bytes
/
handlers.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
package main
import (
"github.com/laplaceon/httprouter"
"fmt"
"net/http"
)
func DefaultError() string {
return "error";
}
func NotFound(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, DefaultError())
}
// takes in a function as a parameter and returns a handlerfunction which can be used by the router
// this way, the original function can still be used when accessed over ZMQ, and the handler can be used when accessed over HTTP
func GenerateHandler(fn (func(*http.Request, httprouter.Params) string)) func(http.ResponseWriter, *http.Request, httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
fmt.Fprint(w, fn(r, ps))
}
}
func Index(_ *http.Request, _ httprouter.Params) string {
return "Welcome"
}
func UserFind(r *http.Request, ps httprouter.Params) string {
var id string = ""
if(r != nil) {
r.ParseForm()
id = r.PostFormValue("id")
} else {
id = ps.ByName("id")
}
if(id == "") {
return "No id given"
}
return "Id is " + id
}