-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
125 lines (113 loc) · 3.47 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
124
125
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"regexp"
"strconv"
"time"
"github.com/ivanmartinez/boocat/boocat"
"github.com/ivanmartinez/boocat/boocat/mongodb"
"github.com/ivanmartinez/boocat/webserver"
)
func main() {
// Parse flags
url := flag.String("url", "localhost:80", "This boocat's base URL")
dbURI := flag.String("dburi", "mongodb://127.0.0.1:27017", "Database URI")
flag.Parse()
// Create channel for listening to OS signals and connect OS interrupts to
// the channel
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
ctx, cancel := context.WithCancel(context.Background())
go func() {
oscall := <-c
webserver.Info.Printf("received signal %v", oscall)
cancel()
}()
// Initialize database
db, err := mongodb.NewMongoDB(ctx, dbURI)
if err != nil {
webserver.Error.Fatal(err)
}
// Set formats
var bc boocat.Boocat
bc.SetFormat("author", boocat.Format{
Name: "author",
Fields: map[string]boocat.Validate{
"name": regExpValidator("^([A-Z][a-z]*)([ |-][A-Z][a-z]*)*$"),
"birthdate": validateYear,
"biography": nil,
},
Searchable: map[string]struct{}{"name": {}, "biography": {}},
})
bc.SetFormat("book", boocat.Format{
Name: "book",
Fields: map[string]boocat.Validate{
"name": regExpValidator("^([A-Z][a-z]*)([ |-][A-Z][a-z]*)*$"),
"year": validateYear,
"author": db.ReferenceValidator("author"),
"synopsis": nil,
},
Searchable: map[string]struct{}{"name": {}, "synopsis": {}},
})
// Make sure database collections match the defined formats
if err := db.InitializeCollections(ctx, bc.Formats()); err != nil {
webserver.Error.Fatal(err)
}
// Set database to use
bc.SetDatabase(db)
ws := webserver.Initialize(*url, &bc)
loadWebFiles(&ws)
ws.Start()
// Wait for ctx to be cancelled
<-ctx.Done()
// New context with timeout to shut the HTTP boocat down
ctxShutDown, cancel := context.WithTimeout(context.Background(), 5*time.Second)
// Shut services down
ws.Shutdown(ctxShutDown)
if err := db.Disconnect(ctxShutDown); err != nil {
webserver.Error.Print(err)
}
}
// reqExpValidator returns a validator that uses the regular expression passed as argument
func regExpValidator(regExpString string) boocat.Validate {
regExp, err := regexp.Compile(regExpString)
if err != nil {
webserver.Error.Fatal(err)
}
return func(_ context.Context, value interface{}) string {
stringValue := fmt.Sprintf("%v", value)
if !regExp.MatchString(stringValue) {
return fmt.Sprintf("does not match regular expression '%s'", regExpString)
}
return ""
}
}
// validateYear returns a validator that validates a year
func validateYear(_ context.Context, value interface{}) string {
stringValue := fmt.Sprintf("%v", value)
year, err := strconv.Atoi(stringValue)
if err != nil {
return "not a valid year number"
}
if year < 0 {
return "not a valid year number"
}
return ""
}
func loadWebFiles(ws *webserver.Webserver) {
ws.LoadStaticFile("bcweb", "/index.html")
ws.LoadTemplate("bcweb", "/author.tmpl", "author")
ws.LoadTemplate("bcweb", "/book.tmpl", "book")
ws.LoadTemplate("bcweb", "/new/author.tmpl", "author")
ws.LoadTemplate("bcweb", "/new/book.tmpl", "book")
ws.LoadTemplate("bcweb", "/edit/author.tmpl", "author")
ws.LoadTemplate("bcweb", "/edit/book.tmpl", "book")
ws.LoadTemplate("bcweb", "/list/author.tmpl", "author")
ws.LoadTemplate("bcweb", "/list/book.tmpl", "book")
ws.LoadTemplate("bcweb", "/search/author.tmpl", "author")
ws.LoadTemplate("bcweb", "/search/book.tmpl", "book")
}