This repository was archived by the owner on Nov 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
executable file
·111 lines (95 loc) · 4.17 KB
/
Copy pathmain.go
File metadata and controls
executable file
·111 lines (95 loc) · 4.17 KB
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
package main
import (
"os"
"log"
"context"
"net/http"
"github.com/apuigsech/rest-layer-sql"
"github.com/rs/rest-layer/resource"
"github.com/rs/rest-layer/rest"
"github.com/rs/rest-layer/schema"
_ "github.com/mattn/go-sqlite3"
)
const (
DB_DRIVER = "sqlite3"
DB_SOURCE = "file:data.db"
)
var task = schema.Schema{
Fields: schema.Fields{
"id": schema.IDField,
"created": schema.CreatedField,
"updated": schema.UpdatedField,
"name": { Required: true, Validator: &schema.String{ MaxLen: 150 } },
"info": { Required: true, Validator: &schema.String{} },
"exp": { Required: true, Default: 1, Sortable: true, Filterable: true, Validator: &schema.Integer{} },
"added": { Required: true, Validator: &schema.String{ MaxLen: 20 } },
"confirmed": { Required: true, Validator: &schema.String{ MaxLen: 20 } },
"catid": { Required: true, Validator: &schema.String{ MaxLen: 20 } },
"score": { Required: true, Default: 0, Sortable: true, Filterable: true, Validator: &schema.Integer{} },
},
}
var category = schema.Schema{
Fields: schema.Fields{
"id": schema.IDField,
"created": schema.CreatedField,
"updated": schema.UpdatedField,
"name": { Required: true, Validator: &schema.String{ MaxLen: 150 } },
"info": { Required: true, Validator: &schema.String{} },
"icon": { Required: true, Validator: &schema.String{} },
},
}
var assign = schema.Schema{
Fields: schema.Fields{
"id": schema.IDField,
"created": schema.CreatedField,
"updated": schema.UpdatedField,
"taskid": { Required: true, Validator: &schema.String{ MaxLen: 20 } },
"userid": { Required: true, Validator: &schema.String{ MaxLen: 20 } },
"done": { Required: true, Validator: &schema.Bool{}, Filterable: true, Default: false },
},
}
var user = schema.Schema{
Fields: schema.Fields{
"id": schema.IDField,
"created": schema.CreatedField,
"updated": schema.UpdatedField,
"username": { Required: true, Validator: &schema.String{ MaxLen: 150 }, Filterable: true },
"name": { Required: true, Validator: &schema.String{} },
"pass": { Required: true, Validator: &schema.String{}, Hidden: true, Filterable: true },
"exp": { Required: true, Default: 0, Sortable: true, Filterable: true, Validator: &schema.Integer{} },
"admin": { Required: true, Default: false, Filterable: true, Validator: &schema.Bool{} },
},
}
func addBind(name string, sch schema.Schema, index resource.Index) (*resource.Resource, resource.Storer) {
cfg := sqlStorage.Config{ 2, map[string]string{} }
s, err := sqlStorage.NewHandler(DB_DRIVER, DB_SOURCE, name, &cfg)
if err != nil { log.Fatalf("[%s] Error connecting database: %s", name, err) }
err = s.Create(context.TODO(), &sch)
if err != nil { log.Fatalf("[%s] Error creating table: %s", name, err) }
return index.Bind(name, sch, s, resource.DefaultConf), s
}
func middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type,Accept")
w.Header().Set("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, HEAD, PATCH, OPTIONS")
next.ServeHTTP(w, r)
})
}
func main() {
index := resource.NewIndex()
_, assignsS := addBind("assigns", assign, index)
tasks, tasksS := addBind("tasks", task, index)
users, _ := addBind("users", user, index)
cats, _ := addBind("categories", category, index)
users.Bind("tasks", "userid", assign, assignsS, resource.DefaultConf)
cats.Bind("tasks", "catid", task, tasksS, resource.DefaultConf)
tasks.Bind("users", "taskid", assign, assignsS, resource.DefaultConf)
api, err := rest.NewHandler(index)
if err != nil { log.Fatalf("Invalid API configuration: %s", err) }
http.Handle("/", middleware(api))
port := os.Getenv("PORT")
if port == "" { log.Fatal("$PORT must be set") }
log.Print("Serving API on :" + port)
if err := http.ListenAndServe(":" + port, nil); err != nil { log.Fatal(err) }
}