/
doc.go
128 lines (103 loc) · 3.02 KB
/
doc.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
// Copyright 2014 Takatoshi Matsumoto. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
Package dou is tiny and flexible toolkit for creating a api server.
Simple usage. If you want to see more example, check github.com/ToQoz/dou/example/full
package main
import (
"errors"
"github.com/ToQoz/dou"
_ "github.com/ToQoz/dou/jsonapi"
"log"
"net"
"net/http"
"os"
"os/signal"
"time"
)
// --- API Error type ---
type apiError struct {
Message string `json:"message"`
}
func newAPIError(err error) *apiError {
return &apiError{Message: err.Error()}
}
type apiErrors struct {
Errors []*apiError `json:"errors"`
}
func newAPIErrors(errs []error) *apiErrors {
aErrs := &apiErrors{}
for _, err := range errs {
aErrs.Errors = append(aErrs.Errors, newAPIError(err))
}
return aErrs
}
func main() {
defer teardown()
api, err := dou.NewAPI("jsonapi")
if err != nil {
log.Fatal(err)
}
api.ReadTimeout = 10 * time.Second
api.WriteTimeout = 10 * time.Second
api.MaxHeaderBytes = 1 << 20
// --- Map routes ---
api.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/":
api.Ok(w, map[string]string{"hello": "world"}, http.StatusOK)
case "/error":
err := errors.New("some error occur")
api.Error(w, newAPIError(err), http.StatusInternalServerError)
case "/errors":
var errs []error
errs = append(errs, errors.New("1 error occur"))
errs = append(errs, errors.New("2 error occur"))
api.Error(w, newAPIErrors(errs), http.StatusInternalServerError)
default:
api.Error(w, map[string]string{"message": http.StatusText(http.StatusNotFound)}, http.StatusNotFound)
}
})
// --- Create listener ---
// You can use utility, for example github.com/lestrrat/go-server-starter-listener etc.
l, err := net.Listen("tcp", ":8099")
if err != nil {
log.Fatalf("Could not listen: %s", ":8099")
}
log.Printf("Listen: %s", ":8099")
// --- Handle C-c ---
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for sig := range c {
log.Print("Stopping the server...")
switch sig {
case os.Interrupt:
// --- Stop Server ---
api.Stop()
return
default:
log.Print("Receive unknown signal...")
}
}
}()
// --- Run Server ---
api.Run(l)
}
func teardown() {
log.Print("Tearing down...")
log.Print("Finished - bye bye. ;-)")
}
You can creating a custom plugin in accordance with your api type or domain-specific use-case.
The plugin should keep following interface.
type Plugin interface {
OnPanic(w http.ResponseWriter, r *http.Request)
BeforeDispatch(w http.ResponseWriter, r *http.Request) (http.ResponseWriter, *http.Request)
AfterDispatch(w http.ResponseWriter, r *http.Request) (http.ResponseWriter, *http.Request)
Marshal(v interface{}) ([]byte, error)
Unmarshal(data []byte, v interface{}) error
APIStatus(w http.ResponseWriter, code int)
}
*/
package dou