-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add server code, db interface and config.
- Loading branch information
Bo Du
committed
Nov 19, 2018
1 parent
f10bfa5
commit 9c3e5bf
Showing
6 changed files
with
130 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/xichen2020/eventdb/storage" | ||
) | ||
|
||
// Options for a server. | ||
type Options struct { | ||
Port int | ||
} | ||
|
||
// Server exposes http endpoints that handle reads and writes. | ||
// Uses the default servemux under the hood. | ||
type Server struct { | ||
opts Options | ||
store storage.Storage | ||
} | ||
|
||
// New returns a new server instance. | ||
func New(opts Options, store storage.Storage) *Server { | ||
return &Server{ | ||
opts: opts, | ||
store: store, | ||
} | ||
} | ||
|
||
// Serve runs the server. | ||
func (s *Server) Serve() error { | ||
return http.ListenAndServe(fmt.Sprintf(":%d", s.opts.Port), nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package config | ||
|
||
import ( | ||
"github.com/xichen2020/eventdb/server" | ||
"github.com/xichen2020/eventdb/storage" | ||
) | ||
|
||
// Config holds all eventdb config options. | ||
type Config struct { | ||
Storage storage.Options `yaml"storage"` | ||
Server server.Options `yaml:"server"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,42 @@ | ||
package main | ||
|
||
import "fmt" | ||
import ( | ||
"flag" | ||
|
||
m3config "github.com/m3db/m3x/config" | ||
"github.com/m3db/m3x/log" | ||
|
||
"github.com/xichen2020/eventdb/server" | ||
"github.com/xichen2020/eventdb/services/eventdb/config" | ||
"github.com/xichen2020/eventdb/storage" | ||
) | ||
|
||
var ( | ||
configFilePath string | ||
logger = log.SimpleLogger | ||
) | ||
|
||
func main() { | ||
fmt.Println("hello world!") | ||
// Parse command line args. | ||
flag.Parse() | ||
|
||
var cfg config.Config | ||
if err := m3config.LoadFile(&cfg, configFilePath, m3config.Options{}); err != nil { | ||
logger.Fatalf("logstore load config error: %v", err) | ||
} | ||
|
||
// Instantiate DB. | ||
db := storage.New(cfg.Storage) | ||
|
||
// Spin up server. | ||
s := server.New(cfg.Server, db) | ||
go func() { | ||
if err := s.Serve(); err != nil { | ||
logger.Fatalf("Failed to serve HTTP endpoints: %v", err) | ||
} | ||
}() | ||
} | ||
|
||
func init() { | ||
flag.StringVar(&configFilePath, "config", "eventdb.yaml", "path to the eventdb config file") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package storage | ||
|
||
// Options to configure a database instance. | ||
type Options struct{} | ||
|
||
// Storage exposes methods to write and query data. | ||
type Storage interface { | ||
WriteBatch([][]byte) error | ||
Write([]byte) error | ||
// TODO: Implement query package and then method. | ||
//Query() | ||
} | ||
|
||
// Store implements the Storage interface. | ||
type Store struct { | ||
opts Options | ||
} | ||
|
||
// New reutrns a new database instance. | ||
func New(opts Options) *Store { | ||
return &Store{ | ||
opts: opts, | ||
} | ||
} | ||
|
||
// WriteBatch persists multiple events. | ||
func (s *Store) WriteBatch(events [][]byte) error { | ||
return nil | ||
} | ||
|
||
// Write persists a single event. | ||
func (s *Store) Write(event []byte) error { | ||
return s.WriteBatch([][]byte{event}) | ||
} |