From f85da1db9194638995108696b8980ba994cef3f8 Mon Sep 17 00:00:00 2001 From: siddontang Date: Thu, 17 Jul 2014 15:19:46 +0800 Subject: [PATCH] add base http framework --- bootstrap.sh | 1 + etc/ledis.json | 1 + server/app.go | 44 +++++++++++++++++++++++++++++++++++++------- server/config.go | 2 ++ server/httpd.go | 22 ++++++++++++++++++++++ 5 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 server/httpd.go diff --git a/bootstrap.sh b/bootstrap.sh index 17eeb35..dbbf060 100644 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -3,4 +3,5 @@ . ./dev.sh go get -u github.com/siddontang/go-log/log +go get -u github.com/siddontang/go-websocket/websocket go get -u github.com/siddontang/go-snappy/snappy \ No newline at end of file diff --git a/etc/ledis.json b/etc/ledis.json index 0d93e88..46b0b87 100644 --- a/etc/ledis.json +++ b/etc/ledis.json @@ -1,5 +1,6 @@ { "addr": "127.0.0.1:6380", + "http_addr": "127.0.0.1:11181", "data_dir": "/tmp/ledis_server", "db": { "data_db" : { diff --git a/server/app.go b/server/app.go index 9f155c1..fb3f45a 100644 --- a/server/app.go +++ b/server/app.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/siddontang/ledisdb/ledis" "net" + "net/http" "path" "strings" ) @@ -11,7 +12,8 @@ import ( type App struct { cfg *Config - listener net.Listener + listener net.Listener + httpListener net.Listener ldb *ledis.Ledis @@ -25,6 +27,14 @@ type App struct { m *master } +func netType(s string) string { + if strings.Contains(s, "/") { + return "unix" + } else { + return "tcp" + } +} + func NewApp(cfg *Config) (*App, error) { if len(cfg.DataDir) == 0 { return nil, fmt.Errorf("must set data_dir first") @@ -44,14 +54,14 @@ func NewApp(cfg *Config) (*App, error) { var err error - if strings.Contains(cfg.Addr, "/") { - app.listener, err = net.Listen("unix", cfg.Addr) - } else { - app.listener, err = net.Listen("tcp", cfg.Addr) + if app.listener, err = net.Listen(netType(cfg.Addr), cfg.Addr); err != nil { + return nil, err } - if err != nil { - return nil, err + if len(cfg.HttpAddr) > 0 { + if app.httpListener, err = net.Listen(netType(cfg.HttpAddr), cfg.HttpAddr); err != nil { + return nil, err + } } if len(cfg.AccessLog) > 0 { @@ -86,6 +96,10 @@ func (app *App) Close() { app.listener.Close() + if app.httpListener != nil { + app.httpListener.Close() + } + app.m.Close() if app.access != nil { @@ -100,6 +114,8 @@ func (app *App) Run() { app.slaveof(app.cfg.SlaveOf) } + go app.httpServe() + for !app.closed { conn, err := app.listener.Accept() if err != nil { @@ -110,6 +126,20 @@ func (app *App) Run() { } } +func (app *App) httpServe() { + if app.httpListener == nil { + return + } + + mux := http.NewServeMux() + + mux.Handle("/ws", &wsHandler{app}) + mux.Handle("/", &cmdHandler{app}) + + svr := http.Server{Handler: mux} + svr.Serve(app.httpListener) +} + func (app *App) Ledis() *ledis.Ledis { return app.ldb } diff --git a/server/config.go b/server/config.go index 89aba2c..3c030be 100644 --- a/server/config.go +++ b/server/config.go @@ -9,6 +9,8 @@ import ( type Config struct { Addr string `json:"addr"` + HttpAddr string `json:"http_addr"` + DataDir string `json:"data_dir"` //if you not set db path, use data_dir diff --git a/server/httpd.go b/server/httpd.go new file mode 100644 index 0000000..29cf14a --- /dev/null +++ b/server/httpd.go @@ -0,0 +1,22 @@ +package server + +import ( + "net/http" + //"github.com/siddontang/go-websocket/websocket" +) + +type cmdHandler struct { + app *App +} + +func (h *cmdHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("cmd handler")) +} + +type wsHandler struct { + app *App +} + +func (h *wsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("ws handler")) +}