-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathservice.go
More file actions
41 lines (34 loc) · 789 Bytes
/
service.go
File metadata and controls
41 lines (34 loc) · 789 Bytes
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
package api
import (
"net/http"
"time"
"github.com/pkg/errors"
"github.com/CovenantSQL/CovenantSQL/api/models"
"github.com/CovenantSQL/CovenantSQL/rpc/jsonrpc"
)
var (
rpc = jsonrpc.NewHandler()
server *jsonrpc.WebsocketServer
)
func init() {
server = &jsonrpc.WebsocketServer{
Server: http.Server{
ReadTimeout: 30 * time.Second,
WriteTimeout: 60 * time.Second,
},
}
}
// Serve runs an API server on the specified address and database file.
func Serve(addr, dbFile string) error {
// setup database
if err := models.InitModels(dbFile); err != nil {
return errors.WithMessage(err, "api: init models failed")
}
server.Addr = addr
server.RPCHandler = rpc
return server.Serve()
}
// StopService stops the API server.
func StopService() {
server.Stop()
}