From 100f4fd5bdccf63208639ef77409bba7d222b70e Mon Sep 17 00:00:00 2001 From: fzerorubigd Date: Fri, 18 Oct 2019 00:41:12 +0200 Subject: [PATCH] stub for static api the only implemented api is the daily one. the idea is to generate **all possible combination** (even empty ones) of a query (no search/no range) the API is only for GET, no other method or query parameter is supported This PR only contain daily events. --- cmd/thetool/api.go | 87 +++++++++++++++++++++++++++++++++++++++++ cmd/thetool/generate.go | 4 +- 2 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 cmd/thetool/api.go diff --git a/cmd/thetool/api.go b/cmd/thetool/api.go new file mode 100644 index 0000000..12df0e3 --- /dev/null +++ b/cmd/thetool/api.go @@ -0,0 +1,87 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "os" + "path/filepath" + "strings" +) + +type categoryHandler interface { + Group(fl *File, ret map[string][]*Event) map[string][]*Event +} + +type dailyCat struct { + root string +} + +func (dc dailyCat) path(name string, month, day int) string { + return filepath.Join(dc.root, strings.ToLower(name), fmt.Sprint(month), fmt.Sprint(day)) +} + +func (dc dailyCat) Group(fl *File, ret map[string][]*Event) map[string][]*Event { + for m := range fl.Months.Name { + for d := 0; d < fl.Months.Leap[m] || d < fl.Months.Normal[m]; d++ { + path := dc.path(fl.Name, m+1, d+1) + if _, ok := ret[path]; !ok { + ret[path] = []*Event{} + } + } + } + + for i := range fl.Events { + path := dc.path(fl.Name, fl.Events[i].Month, fl.Events[i].Day) + ret[path] = append(ret[path], &fl.Events[i]) + } + + return ret +} + +func allHandlers(root string) []categoryHandler { + return []categoryHandler{ + dailyCat{root: root}, + } +} + +func categorize(fl *File, handlers ...categoryHandler) map[string][]*Event { + ret := make(map[string][]*Event) + for i := range handlers { + ret = handlers[i].Group(fl, ret) + } + return ret +} + +func writeFiles(m map[string][]*Event) error { + for i := range m { + b, err := json.MarshalIndent(m[i], "", " ") + if err != nil { + return fmt.Errorf("convert to json failed: %w", err) + } + + path := i + ".json" + dir := filepath.Dir(path) + if err := os.MkdirAll(dir, 0740); err != nil { + return fmt.Errorf("creating directory %q failed: %w", dir, err) + } + + if err := ioutil.WriteFile(path, b, 0600); err != nil { + return fmt.Errorf("write file %q failed: %w", path, err) + } + } + + return nil +} + +func writeStaticApi(fls []*File, root string) error { + handlers := allHandlers(root) + for i := range fls { + m := categorize(fls[i], handlers...) + if err := writeFiles(m); err != nil { + return err + } + } + + return nil +} diff --git a/cmd/thetool/generate.go b/cmd/thetool/generate.go index c73a949..ab3ba6b 100644 --- a/cmd/thetool/generate.go +++ b/cmd/thetool/generate.go @@ -49,7 +49,7 @@ func compareAndWrite(fl string, data []byte) error { func generate(cmd *command, fls []*File) error { // its time for calculating the event key - if err := unique(nil ,fls); err != nil { + if err := unique(nil, fls); err != nil { return err } @@ -79,7 +79,7 @@ func generate(cmd *command, fls []*File) error { } } - return nil + return writeStaticApi(fls, *dist) } func init() {