2014-05-16 11:03:23 +04:00
|
|
|
package server
|
2014-05-02 13:08:20 +04:00
|
|
|
|
|
|
|
import (
|
2014-06-06 07:25:13 +04:00
|
|
|
"fmt"
|
2014-05-16 11:03:23 +04:00
|
|
|
"github.com/siddontang/ledisdb/ledis"
|
2014-07-22 07:31:56 +04:00
|
|
|
. "github.com/siddontang/ledisdb/server/http"
|
2014-05-02 13:08:20 +04:00
|
|
|
"net"
|
2014-07-17 11:19:46 +04:00
|
|
|
"net/http"
|
2014-06-11 12:48:11 +04:00
|
|
|
"path"
|
2014-05-02 13:08:20 +04:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type App struct {
|
|
|
|
cfg *Config
|
|
|
|
|
2014-07-17 11:19:46 +04:00
|
|
|
listener net.Listener
|
|
|
|
httpListener net.Listener
|
2014-05-02 13:08:20 +04:00
|
|
|
|
2014-05-20 04:41:24 +04:00
|
|
|
ldb *ledis.Ledis
|
2014-05-08 06:54:33 +04:00
|
|
|
|
|
|
|
closed bool
|
2014-06-06 10:57:18 +04:00
|
|
|
|
|
|
|
quit chan struct{}
|
2014-06-08 12:43:59 +04:00
|
|
|
|
2014-06-11 12:48:11 +04:00
|
|
|
access *accessLog
|
|
|
|
|
2014-06-08 12:43:59 +04:00
|
|
|
//for slave replication
|
2014-06-09 13:23:32 +04:00
|
|
|
m *master
|
2014-05-02 13:08:20 +04:00
|
|
|
}
|
|
|
|
|
2014-07-17 11:19:46 +04:00
|
|
|
func netType(s string) string {
|
|
|
|
if strings.Contains(s, "/") {
|
|
|
|
return "unix"
|
|
|
|
} else {
|
|
|
|
return "tcp"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:08:20 +04:00
|
|
|
func NewApp(cfg *Config) (*App, error) {
|
2014-06-06 07:25:13 +04:00
|
|
|
if len(cfg.DataDir) == 0 {
|
|
|
|
return nil, fmt.Errorf("must set data_dir first")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cfg.DB.DataDir) == 0 {
|
|
|
|
cfg.DB.DataDir = cfg.DataDir
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:08:20 +04:00
|
|
|
app := new(App)
|
|
|
|
|
2014-06-06 10:57:18 +04:00
|
|
|
app.quit = make(chan struct{})
|
|
|
|
|
2014-05-08 06:54:33 +04:00
|
|
|
app.closed = false
|
|
|
|
|
2014-05-02 13:08:20 +04:00
|
|
|
app.cfg = cfg
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
2014-07-17 11:19:46 +04:00
|
|
|
if app.listener, err = net.Listen(netType(cfg.Addr), cfg.Addr); err != nil {
|
|
|
|
return nil, err
|
2014-05-02 13:08:20 +04:00
|
|
|
}
|
|
|
|
|
2014-07-17 11:19:46 +04:00
|
|
|
if len(cfg.HttpAddr) > 0 {
|
|
|
|
if app.httpListener, err = net.Listen(netType(cfg.HttpAddr), cfg.HttpAddr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-02 13:08:20 +04:00
|
|
|
}
|
|
|
|
|
2014-06-11 12:48:11 +04:00
|
|
|
if len(cfg.AccessLog) > 0 {
|
|
|
|
if path.Dir(cfg.AccessLog) == "." {
|
|
|
|
app.access, err = newAcessLog(path.Join(cfg.DataDir, cfg.AccessLog))
|
|
|
|
} else {
|
|
|
|
app.access, err = newAcessLog(cfg.AccessLog)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-04 11:45:23 +04:00
|
|
|
if app.ldb, err = ledis.Open(&cfg.DB); err != nil {
|
2014-05-02 13:08:20 +04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-06-09 13:23:32 +04:00
|
|
|
app.m = newMaster(app)
|
|
|
|
|
2014-05-02 13:08:20 +04:00
|
|
|
return app, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) Close() {
|
2014-05-08 06:54:33 +04:00
|
|
|
if app.closed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-06-06 10:57:18 +04:00
|
|
|
app.closed = true
|
|
|
|
|
|
|
|
close(app.quit)
|
|
|
|
|
2014-05-02 13:08:20 +04:00
|
|
|
app.listener.Close()
|
|
|
|
|
2014-07-17 11:19:46 +04:00
|
|
|
if app.httpListener != nil {
|
|
|
|
app.httpListener.Close()
|
|
|
|
}
|
|
|
|
|
2014-06-09 13:23:32 +04:00
|
|
|
app.m.Close()
|
|
|
|
|
2014-06-11 12:48:11 +04:00
|
|
|
if app.access != nil {
|
|
|
|
app.access.Close()
|
|
|
|
}
|
|
|
|
|
2014-05-20 04:41:24 +04:00
|
|
|
app.ldb.Close()
|
2014-05-02 13:08:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) Run() {
|
2014-06-08 12:43:59 +04:00
|
|
|
if len(app.cfg.SlaveOf) > 0 {
|
2014-06-09 13:23:32 +04:00
|
|
|
app.slaveof(app.cfg.SlaveOf)
|
2014-06-06 10:57:18 +04:00
|
|
|
}
|
|
|
|
|
2014-07-17 11:19:46 +04:00
|
|
|
go app.httpServe()
|
|
|
|
|
2014-05-08 06:54:33 +04:00
|
|
|
for !app.closed {
|
2014-05-02 13:08:20 +04:00
|
|
|
conn, err := app.listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-06-06 10:57:18 +04:00
|
|
|
newClient(conn, app)
|
2014-05-02 13:08:20 +04:00
|
|
|
}
|
|
|
|
}
|
2014-06-11 10:52:34 +04:00
|
|
|
|
2014-07-17 11:19:46 +04:00
|
|
|
func (app *App) httpServe() {
|
|
|
|
if app.httpListener == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
2014-07-22 07:31:56 +04:00
|
|
|
mux.Handle("/ws", &WsHandler{app.Ledis()})
|
|
|
|
mux.Handle("/", &CmdHandler{app.Ledis()})
|
2014-07-17 11:19:46 +04:00
|
|
|
|
|
|
|
svr := http.Server{Handler: mux}
|
|
|
|
svr.Serve(app.httpListener)
|
|
|
|
}
|
|
|
|
|
2014-06-11 12:48:11 +04:00
|
|
|
func (app *App) Ledis() *ledis.Ledis {
|
2014-06-11 10:52:34 +04:00
|
|
|
return app.ldb
|
|
|
|
}
|