Skip to content

Commit

Permalink
Add server code, db interface and config.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bo Du committed Nov 19, 2018
1 parent f10bfa5 commit 9c3e5bf
Show file tree
Hide file tree
Showing 6 changed files with 130 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
33 changes: 33 additions & 0 deletions 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/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)
}
12 changes: 12 additions & 0 deletions services/eventdb/config/config.go
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"`
}
39 changes: 37 additions & 2 deletions services/eventdb/main/main.go
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")
}
34 changes: 34 additions & 0 deletions storage/database.go
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})
}

0 comments on commit 9c3e5bf

Please sign in to comment.