Browse files

Another shot at moving things around to make dependencies simple

  • Loading branch information...
1 parent bdbf68b commit e0c112bc5a3200faf7f1d08a272ffa55b6c6a503 @brendonh committed Aug 11, 2012
View
91 src/drift/accounts/account.go
@@ -2,110 +2,47 @@ package accounts
import (
. "drift/common"
- "drift/services"
-
- "fmt"
-
"code.google.com/p/go.crypto/bcrypt"
)
-type User struct {
+type Account struct {
Name string
PasswordHash []byte
Admin bool
}
-func (user *User) StorageKey() string {
- return user.Name
+func (account *Account) StorageKey() string {
+ return account.Name
}
-func NewUser(name string, password string) *User {
+func NewAccount(name string, password string) *Account {
hash, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
- return &User{name, hash, false}
+ return &Account{name, hash, false}
}
// XXX BGH TODO: Serialize to avoid Riak races
-func CreateUser(name string, password string, context *ServerContext) (*User, bool) {
- var client = context.StorageClient
+func CreateAccount(name string, password string, context ServerContext) (*Account, bool) {
+ var client = context.Storage()
- existing := User{Name: name}
- if client.Get(&existing) {
+ existing := &Account{Name: name}
+ if client.Get(existing) {
return nil, false
}
- user := NewUser(name, password)
- if !client.Put(user) {
+ account := NewAccount(name, password)
+ if !client.Put(account) {
return nil, false
}
- return user, true
+ return account, true
}
-func (user *User) CheckPassword(given string) bool {
+func (account *Account) CheckPassword(given string) bool {
var err = bcrypt.CompareHashAndPassword(
- user.PasswordHash,
+ account.PasswordHash,
[]byte(given))
return err == nil
}
-// ------------------------------------------
-// Service endpoints
-// ------------------------------------------
-
-func GetService() *services.Service {
- service := services.NewService("accounts")
- service.AddMethod(
- "register",
- []APIArg{
- APIArg{Name: "name", ArgType: services.StringArg},
- APIArg{Name: "password", ArgType: services.StringArg},
- },
- method_register)
-
- service.AddMethod(
- "login",
- []APIArg{
- APIArg{Name: "name", ArgType: services.StringArg},
- APIArg{Name: "password", ArgType: services.StringArg},
- },
- method_login)
-
- return service
-}
-
-
-func method_register(args APIData, context *ServerContext) (bool, APIData) {
- var response = make(APIData)
-
- user, ok := CreateUser(
- args["name"].(string),
- args["password"].(string),
- context)
-
- if !ok {
- response["message"] = "User exists"
- return false, response
- }
-
- fmt.Printf(
- "Registered account: %s\n",
- user.Name)
-
- return true, response
-}
-
-func method_login(args APIData, context *ServerContext) (bool, APIData) {
- var response = make(APIData)
-
- var client = context.StorageClient
-
- var user = User{Name: args["name"].(string)}
-
- if !client.Get(&user) || !user.CheckPassword(args["password"].(string)) {
- response["message"] = "Invalid credentials"
- return false, response
- }
- return true, response
-}
View
70 src/drift/accounts/service.go
@@ -0,0 +1,70 @@
+package accounts
+
+import (
+ . "drift/common"
+ "drift/services"
+
+ "fmt"
+)
+
+// ------------------------------------------
+// Service endpoints
+// ------------------------------------------
+
+func GetService() *services.Service {
+ service := services.NewService("accounts")
+ service.AddMethod(
+ "register",
+ []APIArg{
+ APIArg{Name: "name", ArgType: StringArg},
+ APIArg{Name: "password", ArgType: StringArg},
+ },
+ method_register)
+
+ service.AddMethod(
+ "login",
+ []APIArg{
+ APIArg{Name: "name", ArgType: StringArg},
+ APIArg{Name: "password", ArgType: StringArg},
+ },
+ method_login)
+
+ return service
+}
+
+
+func method_register(args APIData, context ServerContext) (bool, APIData) {
+ var response = make(APIData)
+
+ account, ok := CreateAccount(
+ args["name"].(string),
+ args["password"].(string),
+ context)
+
+ if !ok {
+ response["message"] = "User exists"
+ return false, response
+ }
+
+ fmt.Printf(
+ "Registered account: %s\n",
+ account.Name)
+
+ return true, response
+}
+
+
+func method_login(args APIData, context ServerContext) (bool, APIData) {
+ var response = make(APIData)
+
+ var client = context.Storage()
+
+ var account = Account{Name: args["name"].(string)}
+
+ if !client.Get(&account) || !account.CheckPassword(args["password"].(string)) {
+ response["message"] = "Invalid credentials"
+ return false, response
+ }
+
+ return true, response
+}
View
87 src/drift/common/interfaces.go
@@ -0,0 +1,87 @@
+package common
+
+
+type StorageClient interface {
+ GenerateID() string
+
+ Get(Storable) bool
+ Put(Storable) bool
+ IndexLookup(obj Storable, results interface{}, index string) bool
+
+ GetKey(bucket string, key string, target interface{}) bool
+ PutNew(bucket string, val interface{}) (string, bool)
+ PutKey(bucket string, key string, val interface{}) bool
+
+ Delete(bucket string, key string) bool
+
+ Keys(bucket string) ([]string, bool)
+}
+
+
+type Storable interface {
+ StorageKey() string
+}
+
+
+// ------------------------------------------
+// Server
+// ------------------------------------------
+
+type Endpoint interface {
+ Start() bool
+ Stop() bool
+}
+
+type ServerContext interface {
+ Storage() StorageClient
+ API() API
+}
+
+
+
+// ------------------------------------------
+// API
+// ------------------------------------------
+
+const (
+ IntArg = iota
+ FloatArg
+ StringArg
+ NestedArg
+ RawArg
+)
+
+type APIArg struct {
+ Name string
+ ArgType int
+ Required bool
+ Default interface{}
+ Extra interface{}
+}
+
+type APIMethod struct {
+ Name string
+ ArgSpec []APIArg
+ Handler APIHandler
+}
+
+type APIData map[string]interface{}
+
+type APIHandler func(APIData, ServerContext) (bool, APIData)
+
+
+// ------------------------------------------
+// Services
+// ------------------------------------------
+
+type APIService interface {
+ Name() string
+ AddMethod(string, []APIArg, APIHandler)
+ FindMethod(string) *APIMethod
+}
+
+type API interface {
+ AddService(APIService)
+ HandleRequest(APIData, ServerContext) APIData
+ HandleCall(string, string, APIData, ServerContext) (bool, []string, APIData)
+}
View
50 src/drift/common/types.go
@@ -1,56 +1,6 @@
package common
-import (
- "drift/storage"
-)
-
type SectorCoords struct {
X int
Y int
-}
-
-type ServerContext struct {
- StorageClient storage.StorageClient
- Services API
-}
-
-
-// ------------------------------------------
-// API
-// ------------------------------------------
-
-
-type APIArg struct {
- Name string
- ArgType int
- Required bool
- Default interface{}
- Extra interface{}
-}
-
-type APIMethod struct {
- Name string
- ArgSpec []APIArg
- Handler APIHandler
-}
-
-type APIData map[string]interface{}
-
-type APIHandler func(APIData, *ServerContext) (bool, APIData)
-
-
-// ------------------------------------------
-// Services
-// ------------------------------------------
-
-type APIService interface {
- Name() string
- AddMethod(string, []APIArg, APIHandler)
- FindMethod(string) *APIMethod
-}
-
-type API interface {
- AddService(APIService)
- HandleRequest(APIData, *ServerContext) APIData
- HandleCall(string, string, APIData, *ServerContext) (bool, []string, APIData)
}
View
16 src/drift/endpoints/httprpc.go
@@ -15,13 +15,12 @@ import (
type HttpRpcEndpoint struct {
Address string
listener net.Listener
-
- context *ServerContext
+ context ServerContext
}
-func NewHttpRpcEndpoint(address string, context *ServerContext) Endpoint {
- return &HttpRpcEndpoint{
+func NewHttpRpcEndpoint(address string, context ServerContext) Endpoint {
+ return &HttpRpcEndpoint{
Address: address,
context: context,
}
@@ -39,7 +38,7 @@ func (endpoint *HttpRpcEndpoint) Start() bool {
}
endpoint.listener = listener
-
+
mux := http.NewServeMux()
mux.HandleFunc("/favicon.ico", http.NotFound)
mux.Handle("/", endpoint)
@@ -48,6 +47,7 @@ func (endpoint *HttpRpcEndpoint) Start() bool {
return true
}
+
func (endpoint *HttpRpcEndpoint) Stop() bool {
if endpoint.listener == nil {
return true
@@ -62,8 +62,8 @@ func (endpoint *HttpRpcEndpoint) Stop() bool {
return true
}
-func (endpoint *HttpRpcEndpoint) ServeHTTP(response http.ResponseWriter, req *http.Request) {
+func (endpoint *HttpRpcEndpoint) ServeHTTP(response http.ResponseWriter, req *http.Request) {
bits := strings.SplitN(req.URL.Path[1:], "/", 2)
if len(bits) != 2 {
@@ -78,7 +78,7 @@ func (endpoint *HttpRpcEndpoint) ServeHTTP(response http.ResponseWriter, req *ht
form[k] = v[0]
}
- ok, errors, resp := endpoint.context.Services.HandleCall(
+ ok, errors, resp := endpoint.context.API().HandleCall(
bits[0], bits[1], form, endpoint.context)
if errors != nil {
@@ -91,6 +91,4 @@ func (endpoint *HttpRpcEndpoint) ServeHTTP(response http.ResponseWriter, req *ht
response.Header().Add("Content-Length", strconv.Itoa(len(jsonReply)))
response.Write(jsonReply)
-
-
}
View
6 src/drift/endpoints/interfaces.go
@@ -1,6 +0,0 @@
-package endpoints
-
-type Endpoint interface {
- Start() bool
- Stop() bool
-}
View
39 src/drift/server/server.go
@@ -1,40 +1,57 @@
package server
import (
- "drift/common"
- "drift/endpoints"
+ . "drift/common"
"os"
)
+
type Server struct {
- Context *common.ServerContext
- Endpoints []endpoints.Endpoint
+ storage StorageClient
+ services API
+ endpoints []Endpoint
stopper chan os.Signal
}
-func NewServer(context *common.ServerContext) *Server {
- httpRpc := endpoints.NewHttpRpcEndpoint(":9999", context)
+
+func NewServer(
+ storage StorageClient,
+ services API) *Server {
return &Server {
- Context: context,
- Endpoints: []endpoints.Endpoint{ httpRpc },
+ storage: storage,
+ services: services,
+ endpoints: make([]Endpoint, 0),
}
}
+func (server *Server) AddEndpoint(endpoint Endpoint) {
+ server.endpoints = append(server.endpoints, endpoint)
+}
+
func (server *Server) Start() {
- for _, endpoint := range server.Endpoints {
+ for _, endpoint := range server.endpoints {
endpoint.Start()
}
}
func (server *Server) Stop() {
- for _, endpoint := range server.Endpoints {
+ for _, endpoint := range server.endpoints {
endpoint.Stop()
}
}
-
+// ------------------------------------------
+// Context API
+// ------------------------------------------
+
+func (server *Server) Storage() StorageClient {
+ return server.storage
+}
+func (server *Server) API() API {
+ return server.services
+}
View
9 src/drift/services/argparser.go
@@ -2,18 +2,11 @@ package services
import (
. "drift/common"
+
"fmt"
"container/list"
)
-const (
- IntArg = iota
- FloatArg
- StringArg
- NestedArg
- RawArg
-)
-
func Parse(argspec []APIArg, args APIData) (
bool, *list.List, APIData) {
View
4 src/drift/services/service.go
@@ -67,7 +67,7 @@ var requestArgSpec = []APIArg {
APIArg{Name: "data", ArgType: RawArg},
}
-func (collection ServiceCollection) HandleRequest(request APIData, context *ServerContext) APIData {
+func (collection ServiceCollection) HandleRequest(request APIData, context ServerContext) APIData {
ok, resolutionErrors, args := Parse(requestArgSpec, request)
if !ok {
return ErrorResponse(ListToStringSlice(resolutionErrors))
@@ -85,7 +85,7 @@ func (collection ServiceCollection) HandleCall(
serviceName string,
methodName string,
data APIData,
- context *ServerContext) (bool, []string, APIData) {
+ context ServerContext) (bool, []string, APIData) {
service, ok := collection.Services[serviceName]
View
21 src/drift/storage/interfaces.go
@@ -1,21 +0,0 @@
-package storage
-
-type StorageClient interface {
- GenerateID() string
-
- Get(Storable) bool
- Put(Storable) bool
- IndexLookup(obj Storable, results interface{}, index string) bool
-
- GetKey(bucket string, key string, target interface{}) bool
- PutNew(bucket string, val interface{}) (string, bool)
- PutKey(bucket string, key string, val interface{}) bool
-
- Delete(bucket string, key string) bool
-
- Keys(bucket string) ([]string, bool)
-}
-
-type Storable interface {
- StorageKey() string
-}
View
3 src/drift/storage/riak_client.go
@@ -1,6 +1,8 @@
package storage
import (
+ . "drift/common"
+
"bytes"
"encoding/json"
"encoding/base64"
@@ -16,6 +18,7 @@ import (
"code.google.com/p/go-uuid/uuid"
)
+
type RiakClient struct {
httpc *http.Client
baseURL string
View
11 src/sandbox/sandbox.go
@@ -1,11 +1,11 @@
package main
import (
- "drift/common"
"drift/storage"
"drift/services"
"drift/accounts"
"drift/server"
+ "drift/endpoints"
"flag"
"fmt"
@@ -42,11 +42,10 @@ func startServer() {
serviceCollection := services.NewServiceCollection()
serviceCollection.AddService(accounts.GetService())
- var context = &common.ServerContext{
- StorageClient: client,
- Services: serviceCollection,
- }
- var s = server.NewServer(context)
+ var s = server.NewServer(client, serviceCollection)
+
+ httpRpc := endpoints.NewHttpRpcEndpoint(":9999", s)
+ s.AddEndpoint(httpRpc)
var stopper = make(chan os.Signal, 1)
signal.Notify(stopper)

0 comments on commit e0c112b

Please sign in to comment.