Skip to content

Commit

Permalink
Add server code and DB interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bo Du committed Nov 19, 2018
1 parent f10bfa5 commit c656215
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 6 deletions.
15 changes: 12 additions & 3 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package: github.com/xichen2020/eventdb
import: []
import:
- package: gopkg.in/validator.v2
testImport:
- package: github.com/stretchr/testify
version: ^1.2.2
Expand Down
32 changes: 32 additions & 0 deletions services/eventdb/database/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package database

type Options struct{}

// Database exposes methods to write and query data.
type Database interface {
WriteBatch([][]byte) error
Write([]byte) error
// TODO: Implement query package and then method.
//Query()
}

// DB implements the Database interface.
type DB struct {
opts Options
}

func New(opts Options) *DB {
return &DB{
opts: opts,
}
}

// WriteBatch persists multiple events.
func (d *DB) WriteBatch(events [][]byte) error {
return nil
}

// Write persists a single event.
func (d *DB) Write(event []byte) error {
return d.WriteBatch([][]byte{event})
}
12 changes: 12 additions & 0 deletions services/eventdb/main/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"github.com/xichen2020/eventdb/services/eventdb/database"
"github.com/xichen2020/eventdb/services/eventdb/server"
)

// Config holds all eventdb config options.
type Config struct {
Database database.Options `yaml"database"`
Server server.Options `yaml:"server"`
}
37 changes: 35 additions & 2 deletions services/eventdb/main/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,40 @@
package main

import "fmt"
import (
"flag"

"github.com/m3db/m3x/config"
"github.com/m3db/m3x/log"
"github.com/xichen2020/eventdb/services/eventdb/database"
"github.com/xichen2020/eventdb/services/eventdb/server"
)

var (
configFilePath string
logger = log.SimpleLogger
)

func main() {
fmt.Println("hello world!")
// Parse command line args.
flag.Parse()

var cfg Config
if err := config.LoadFile(&cfg, configFilePath, config.Options{}); err != nil {
logger.Fatalf("logstore load config error: %v", err)
}

// Instantiate DB.
db := database.New(cfg.Database)

// 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")
}
33 changes: 33 additions & 0 deletions services/eventdb/server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package server

import (
"fmt"
"net/http"

"github.com/xichen2020/eventdb/services/eventdb/database"
)

// 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
db database.Database
}

// New returns a new server instance.
func New(opts Options, db database.Database) *Server {
return &Server{
opts: opts,
db: db,
}
}

// Serve runs the server.
func (s *Server) Serve() error {
return http.ListenAndServe(fmt.Sprintf(":%d", s.opts.Port), nil)
}

0 comments on commit c656215

Please sign in to comment.