-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
125 lines (106 loc) · 2.9 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"
"fmt"
"github.com/akamensky/argparse"
openfga "github.com/openfga/go-sdk"
"github.com/paulosuzart/fgamanager/db"
"github.com/rivo/tview"
"log"
"net/http"
"net/url"
"os"
"testing"
)
var (
parser = argparse.NewParser("fgamanager", "fgamanager")
apiUrl = parser.String("a", "apiUrl", &argparse.Options{Default: "http://localhost:8087", Help: "OpenFGA API Url"})
storeId = parser.String("s", "storeId", &argparse.Options{Required: true, Help: "The Store Id to connect to"})
pruneStale = parser.Flag("p", "prune", &argparse.Options{Required: false, Default: false, Help: "Causes fgamanager to prune stale entries on startup"})
)
func init() {
if testing.Testing() {
testId := "TESTID"
storeId = &testId
return
}
err := parser.Parse(os.Args)
if err != nil {
fmt.Printf("Error: %v", err)
os.Exit(1)
}
if _, err := url.Parse(*apiUrl); err != nil {
panic("Api URL is malformed")
os.Exit(1)
}
}
var (
fgaClient *openfga.APIClient
fga fgaService
)
type WatchUpdate struct {
Writes, Deletes int
Token *string
WatchEnabled string
}
type fgaService interface {
write(ctx context.Context, tuple *openfga.WriteRequestWrites) error
delete(ctx context.Context, deletes []openfga.TupleKeyWithoutCondition) (*http.Response, error)
}
type fgaWrapper struct {
fgaService
}
func (f *fgaWrapper) write(ctx context.Context, tuple *openfga.WriteRequestWrites) error {
_, _, err := fgaClient.OpenFgaApi.Write(ctx).
Body(openfga.WriteRequest{
Writes: tuple,
}).Execute()
return err
}
func (f *fgaWrapper) delete(ctx context.Context, deletes []openfga.TupleKeyWithoutCondition) (*http.Response, error) {
_, resp, err := fgaClient.OpenFgaApi.
Write(ctx).
Body(openfga.WriteRequest{
Deletes: &openfga.WriteRequestDeletes{
TupleKeys: deletes}}).Execute()
return resp, err
}
func main() {
// log to custom file
LOG_FILE := "/tmp/fgamanager.log"
// open log file
logFile, err := os.OpenFile(LOG_FILE, os.O_APPEND|os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
log.Panic(err)
}
defer func(logFile *os.File) {
err := logFile.Close()
if err != nil {
}
}(logFile)
// Set log out put and enjoy :)
log.SetOutput(logFile)
// optional: log date-time, filename, and line number
log.SetFlags(log.Lshortfile | log.LstdFlags)
db.SetupDb()
defer db.Close()
if pruneStale != nil && *pruneStale {
log.Printf("Will prune stale entries...")
rowsAffected := db.Repository.Prune()
log.Printf("%v rows pruned", rowsAffected)
}
configuration, err := openfga.NewConfiguration(openfga.Configuration{
ApiUrl: *apiUrl,
StoreId: *storeId,
})
fgaClient = openfga.NewAPIClient(configuration)
fga = &fgaWrapper{}
if err != nil {
log.Panic("Unable to create openfga config")
}
app := tview.NewApplication()
root := AddComponents(context.Background(), app)
if err := app.SetRoot(root, true).SetFocus(root).Run(); err != nil {
log.Panic(err)
}
}