-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
59 lines (52 loc) · 1.56 KB
/
server.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
package gobaserver
import (
"encoding/json"
"github.com/fasthttp/router"
"github.com/gomuddle/goba"
"github.com/valyala/fasthttp"
)
// A Server provides a web interface for interacting with a goba.Goba.
type Server struct {
goba *goba.Goba
router *router.Router
credentials []Credentials
}
// New creates a new configured Server and returns it.
func New(goba goba.Goba, validCredentials ...Credentials) *Server {
s := Server{
goba: &goba,
router: newRouter(),
credentials: validCredentials,
}
s.routes()
return &s
}
// ListenAndServe serves HTTP requests from the given TCP address.
func (s Server) ListenAndServe(addr string) error {
return s.fasthttpServer().ListenAndServe(addr)
}
// ListenAndServeTLS serves HTTP requests from the given TCP address.
// certFile and keyFile are paths to TLS certificate and key files.
func (s Server) ListenAndServeTLS(addr, certFile, keyFile string) error {
return s.fasthttpServer().ListenAndServeTLS(addr, certFile, keyFile)
}
// fasthttpServer returns a configured fasthttp.Server.
func (s Server) fasthttpServer() *fasthttp.Server {
server := &fasthttp.Server{
Handler: s.router.Handler,
}
server.ReadBufferSize = 8192
return server
}
// writeJSON sets the response's "Content-Type" header to
// "application/json" and writes the JSON representation of
// v to the response's body.
func (s Server) writeJSON(ctx *fasthttp.RequestCtx, v interface{}) error {
data, err := json.Marshal(v)
if err != nil {
return err
}
ctx.SetContentType("application/json")
_, err = ctx.Write(data)
return nil
}