-
Notifications
You must be signed in to change notification settings - Fork 4
/
liddly.go
127 lines (104 loc) · 2.79 KB
/
liddly.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
126
127
package main
import (
"fmt"
"log"
"net/http"
"context"
"os"
"os/signal"
"time"
)
const lockfile = "./liddly.lock"
const watchfile = "./liddly.shutdown"
var repo TiddlerRepo
var srv = http.Server{
Addr: ":8080",
}
func main() {
lock, err := Acquire(lockfile)
if err != nil {
os.Create(watchfile)
log.Println("Lock file exists. Initialized remote shutdown.")
return
}
defer lock.Release()
folderWatch, err := NewFolderWatch()
if err != nil {
log.Println("Could not start watch on current dir")
return
}
defer folderWatch.Close()
register("/", strictPath(allowOnly(index, "GET", "OPTIONS")))
register("/status", strictPath(allowOnly(status, "GET")))
register("/recipes/all/tiddlers.json", strictPath(allowOnly(list, "GET")))
register("/recipes/all/tiddlers/", allowOnly(detail, "GET", "PUT"))
register("/bags/bag/tiddlers/", allowOnly(remove, "DELETE"))
shutdownOnCreate(folderWatch, watchfile, asyncShutdown)
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt)
go func() {
<-c
asyncShutdown()
}()
repo = NewSqliteRepo("./tiddlers.db")
log.Println(srv.ListenAndServe())
}
func asyncShutdown() {
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
if err := srv.Shutdown(ctx); err != nil {
log.Panic(err)
}
}
func register(pattern string, handler func(string) http.HandlerFunc) {
http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
log.Println(r)
handler(pattern)(w, r)
})
}
func strictPath(handler func(string) http.HandlerFunc) func(string) http.HandlerFunc {
return func(pattern string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != pattern {
log.Println("Request not allowed for strict path", r.URL.Path)
http.Error(w, fmt.Sprintf("Path not mapped: %v", r.URL.Path), http.StatusNotFound)
return
}
handler(pattern)(w, r)
}
}
}
func allowOnly(handler func(string) http.HandlerFunc, methods ...string) func(string) http.HandlerFunc {
return func(pattern string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var allowed = false
for _, method := range methods {
if r.Method == method {
allowed = true
}
}
if !allowed {
log.Printf("Method '%v' not allowed for path '%v'", r.Method, r.URL.Path)
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
handler(pattern)(w, r)
}
}
}
func shutdownOnCreate(folderWatch *FolderWatch, createdFile string, shutdownCallback func()) {
go func() {
for {
if e := <-folderWatch.Events; e != createdFile {
continue
} else {
defer os.Remove(createdFile)
shutdownCallback()
return
}
}
}()
}
func jsonResponse(w http.ResponseWriter) http.ResponseWriter {
w.Header().Set("Content-Type", "application/json")
return w
}