-
Notifications
You must be signed in to change notification settings - Fork 0
/
locker.go
49 lines (44 loc) · 854 Bytes
/
locker.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
package cloudlocker
import (
"fmt"
"github.com/syndtr/goleveldb/leveldb"
"golang.org/x/net/context"
"net/http"
"os"
"time"
)
type LockerServer struct {
db *leveldb.DB
server *http.Server
}
func NewLockerServer(path, url string) (*LockerServer, error) {
db, err := leveldb.OpenFile(path, nil)
if err != nil {
return nil, err
}
l := &LockerServer{
db: db,
}
l.server = &http.Server{
Addr: url,
Handler: NewRouter(l),
}
return l, nil
}
func (l *LockerServer) Start() {
if err := l.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
fmt.Println(err)
os.Exit(-1)
}
}
func (l *LockerServer) Stop() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := l.server.Shutdown(ctx); err != nil {
//log
}
err := l.db.Close()
if err != nil {
//log
}
}