forked from crowd-sh/crowd-sh
/
main.go
123 lines (94 loc) · 2.67 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"fmt"
"github.com/coreos/go-etcd/etcd"
// "github.com/crowdmob/goamz/aws"
// "github.com/crowdmob/goamz/exp/mturk"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
_ "github.com/lib/pq"
"log"
"net/http"
"os"
)
const (
DatabaseUrlKey = "/workmachine/database_url"
)
var (
Db gorm.DB
// MTurk mturk.MTurk
)
func dbConnect(databaseUrl string) {
log.Println("Connecting to database:", databaseUrl)
var err error
Db, err = gorm.Open("postgres", databaseUrl)
if err != nil {
log.Println(err)
}
Db.LogMode(true)
}
func mturkConnect() {
// auth := aws.Auth{AccessKey: "abc", SecretKey: "123"}
// MTurk = mturk.New(auth, true)
}
func init() {
etcdHosts := os.Getenv("ETCD_HOSTS")
if etcdHosts == "" {
etcdHosts = "http://127.0.0.1:4001"
}
etcdClient := etcd.NewClient([]string{etcdHosts})
resp, err := etcdClient.Get(DatabaseUrlKey, false, false)
if err != nil {
panic(err)
}
databaseUrl := resp.Node.Value
dbConnect(databaseUrl)
}
func main() {
// go AssignmentExpirer()
log.Println("WorkMachine Starting...")
r := mux.NewRouter()
r.HandleFunc("/v1/workflows", func(w http.ResponseWriter, r *http.Request) {
workflow := NewWorkflow(r.Body)
fmt.Fprintln(w, workflow.Id)
}).Methods("POST")
r.HandleFunc("/v1/workflows/{workflow}", func(w http.ResponseWriter, r *http.Request) {
}).Methods("GET")
r.HandleFunc("/v1/workflows/{workflow}/tasks", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
workflow := Workflow{}
Db.First(&workflow, vars["workflow"])
workflow.Parse()
t := workflow.AddTask(r.Body)
fmt.Fprintln(w, t.Id)
}).Methods("POST")
r.HandleFunc("/v1/workflows/{workflow}/tasks/{tasks}", func(w http.ResponseWriter, r *http.Request) {
}).Methods("GET")
//r.HandleFunc("/v1/workflow/{workflow}/tests", TaskHandler).Methods("PUT")
// r.HandleFunc("/v1/assignments", func(w http.ResponseWriter, req *http.Request) {
// assign := AvailableAssignments.GetUnfinished()
// if assign == nil {
// renderJson(w, false)
// return
// }
// if !assign.TryToAssign() {
// renderJson(w, false)
// return
// }
// renderJson(w, assign)
// }).Methods("GET")
// r.HandleFunc("/v1/assignments", func(w http.ResponseWriter, req *http.Request) {
// log.Println("Posting", req.FormValue("id"))
// assign := AvailableAssignments.Find(req.FormValue("id"))
// if assign != nil {
// assign.Finish(req.FormValue(assign.InputField.Value))
// }
// renderJson(w, true)
// }).Methods("POST")
r.HandleFunc("/static/", func(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL.Path[1:])
http.ServeFile(w, r, r.URL.Path[1:])
})
http.Handle("/", r)
http.ListenAndServe(":3002", nil)
}