-
Notifications
You must be signed in to change notification settings - Fork 11
/
hello-rest.go
57 lines (45 loc) · 1.29 KB
/
hello-rest.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
// Copyright 2018 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build ignore
package main
import (
"io/ioutil"
"log"
"mime"
"net/http"
"time"
"github.com/chai2010/pbgo/examples/hello.pb"
)
type HelloService struct{}
func (p *HelloService) Hello(request *hello_pb.String, reply *hello_pb.String) error {
reply.Value = "hello:" + request.GetValue()
return nil
}
func (p *HelloService) Echo(request *hello_pb.Message, reply *hello_pb.Message) error {
*reply = *request
return nil
}
func (p *HelloService) Static(request *hello_pb.String, reply *hello_pb.StaticFile) error {
data, err := ioutil.ReadFile("./testdata/" + request.Value)
if err != nil {
return err
}
reply.ContentType = mime.TypeByExtension(request.Value)
reply.ContentBody = data
return nil
}
func someMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(wr http.ResponseWriter, r *http.Request) {
timeStart := time.Now()
defer func() {
timeElapsed := time.Since(timeStart)
log.Println(r.Method, r.URL, timeElapsed)
}()
next.ServeHTTP(wr, r)
})
}
func main() {
router := hello_pb.HelloServiceHandler(new(HelloService))
log.Fatal(http.ListenAndServe(":8080", someMiddleware(router)))
}