removed mongodb
This commit is contained in:
1 parent
8976b50cc5
commit
f6ec518814
4 files changed
+3
-154
No files matched your search
@@ -1,88 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
/*
|
||||
Api structure
|
||||
|
||||
/weblabs
|
||||
POST: create a new weblab. The payload must be
|
||||
{
|
||||
"name": "web lab frendly name",
|
||||
"cases": ["case_name_1", "case_name_2", ..., "case_name_n"],
|
||||
"probabilities": [prob_1, prob_2, ..., prob_n], // the sum must be 1
|
||||
"private": false,
|
||||
}
|
||||
GET:
|
||||
returns the list of all the created weblabs
|
||||
|
||||
/weblabs/<id>
|
||||
GET: returns the weblab as
|
||||
|
||||
/weblabs/<id>/history
|
||||
GET: get the history
|
||||
*/
|
||||
|
||||
// the smallest value that makes two floats the same.
|
||||
const epsilon float64 = 0.000000001
|
||||
|
||||
type Cases struct {
|
||||
Name string `json:"name"`
|
||||
Probability float64 `json:"probability"`
|
||||
}
|
||||
|
||||
type WeblabDefinition struct {
|
||||
Id bson.ObjectId `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Cases []*Cases `json:"cases"`
|
||||
Private bool `json:"private"`
|
||||
}
|
||||
|
||||
func (w *WeblabDefinition) verifyCasesStructure() error {
|
||||
names := make(map[string]bool)
|
||||
for _, c := range w.Cases {
|
||||
if c.Probability+epsilon < 0 {
|
||||
return errors.New("probability cannot be less 0")
|
||||
}
|
||||
if c.Probability-epsilon > 1 {
|
||||
return errors.New("probability cannot be greater 0")
|
||||
}
|
||||
c.Name = strings.Trim(c.Name, " \t\n\r")
|
||||
if c.Name == "" {
|
||||
return errors.New("weblab cannot have empty names")
|
||||
}
|
||||
names[c.Name] = true
|
||||
}
|
||||
if len(names) != len(w.Cases) {
|
||||
return errors.New("multiple weblab cannot have the same name")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *WeblabDefinition) VerifyProbabilitySum() {
|
||||
|
||||
}
|
||||
func (w *WeblabDefinition) Verify() error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateNewWeblab(w http.ResponseWriter, request *http.Request) {
|
||||
d := json.NewDecoder(request.Body)
|
||||
wab := new(WeblabDefinition)
|
||||
if err := d.Decode(d); err != nil {
|
||||
w.WriteHeader(400)
|
||||
fmt.Fprint(w, err.Error())
|
||||
return
|
||||
}
|
||||
wab.Id = bson.NewObjectId()
|
||||
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"io/ioutil"
|
||||
|
||||
mgo "gopkg.in/mgo.v2"
|
||||
goyaml "gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
@@ -12,15 +11,9 @@ const PARAM_FILE string = "params.yml"
|
||||
type Params struct {
|
||||
Port int `yaml:"port,omitempty"`
|
||||
Host string `yaml:"host,omitempty"`
|
||||
Mongo *MongoParams `yaml:"mongo,omitempty"`
|
||||
MongoClient *mgo.Session `yaml:"-"`
|
||||
DB *mgo.Database `yaml:"-"`
|
||||
}
|
||||
|
||||
type MongoParams struct {
|
||||
Url string `yaml:"url,omitempty"`
|
||||
DatabaseName string `yaml:"database_name,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
func (this *Params) LoadFromYamlFile(fileName string) error {
|
||||
data, err := ioutil.ReadFile(fileName)
|
||||
@@ -43,36 +36,14 @@ func (this *Params) LoadFromYamlData(data []byte) error {
|
||||
if p.Port > 0 {
|
||||
this.Port = p.Port
|
||||
}
|
||||
if p.Mongo == nil {
|
||||
return nil
|
||||
}
|
||||
if p.Mongo.Url != "" {
|
||||
this.Mongo.Url = p.Mongo.Url
|
||||
}
|
||||
if p.Mongo.DatabaseName != "" {
|
||||
this.Mongo.DatabaseName = p.Mongo.DatabaseName
|
||||
}
|
||||
if p.Mongo.DatabaseName != "" {
|
||||
this.Mongo.DatabaseName = p.Mongo.DatabaseName
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *Params) initMongo() error {
|
||||
var err error
|
||||
this.MongoClient, err = mgo.Dial(this.Mongo.Url)
|
||||
this.DB = this.MongoClient.DB(this.Mongo.DatabaseName)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func GetDefaultParams() *Params {
|
||||
mongoParams := &MongoParams{
|
||||
Url: "localhost:27017",
|
||||
DatabaseName: "union",
|
||||
}
|
||||
return &Params{7777, "localhost", mongoParams, nil, nil}
|
||||
return &Params{7777, "localhost"}
|
||||
}
|
||||
|
||||
var params = GetDefaultParams()
|
||||
@@ -81,7 +52,4 @@ func init() {
|
||||
if err := params.LoadFromYamlFile(PARAM_FILE); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := params.initMongo(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
@@ -13,12 +13,3 @@ port: 1999
|
||||
# service
|
||||
host: 0.0.0.0 #localhost
|
||||
|
||||
|
||||
|
||||
# all the params relative to mongodb
|
||||
mongo:
|
||||
# the url where to get mongodb
|
||||
url: localhost:27017
|
||||
|
||||
# the name of the database to use
|
||||
database_name: amazon_union
|
||||
+1
-23
@@ -11,20 +11,7 @@ port: 9999
|
||||
|
||||
host: 0.0.0.0 #localhost
|
||||
|
||||
# all the params relative to mongodb
|
||||
mongo:
|
||||
# the url where to get mongodb
|
||||
url: localhost:1234
|
||||
|
||||
# the name of the database to use
|
||||
database_name: geography
|
||||
|
||||
# the name of the collection where all the street can be found
|
||||
street_collection: quebec_street_map
|
||||
|
||||
|
||||
# the collection where all the heat map point will be stored
|
||||
heat_map_collection: quebec_heat_map # quebec_heat_map
|
||||
|
||||
`
|
||||
|
||||
func TestLoadFromYamlData(t *testing.T) {
|
||||
@@ -39,14 +26,5 @@ func TestLoadFromYamlData(t *testing.T) {
|
||||
if p.Host != "0.0.0.0" {
|
||||
t.Error("host")
|
||||
}
|
||||
if p.Mongo.Url != "localhost:1234" {
|
||||
t.Error("mongo.url")
|
||||
}
|
||||
if p.Mongo.Url != "localhost:1234" {
|
||||
t.Error("mongo.url")
|
||||
}
|
||||
if p.Mongo.DatabaseName != "geography" {
|
||||
t.Error("mongo.DatabaseName")
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user