-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
124 lines (98 loc) · 2.62 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
package main
import (
"context"
"encoding/json"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/joho/godotenv"
openfgaclient "github.com/openfga/go-sdk/client"
"github.com/sambego/fga-demo-api/data"
"github.com/sambego/fga-demo-api/router"
)
func main() {
// Load the .env file
// load .env file
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Error loading .env file")
}
// Check for environment variables
apiUrl, ok := os.LookupEnv("FGA_API_URL")
if !ok {
log.Fatal("'FGA_STORE_ID' environment variable must be set")
}
// Initialise the Auth0 FGA middleware
fgaClient, err := openfgaclient.NewSdkClient(&openfgaclient.ClientConfiguration{
ApiUrl: apiUrl,
})
if err != nil {
log.Fatal(err)
}
log.Printf("FGA API URL: %v", apiUrl)
// Create a new FGA Store
body := openfgaclient.ClientCreateStoreRequest{Name: "FGA Demo"}
fgaStore, err := fgaClient.CreateStore(context.Background()).Body(body).Execute()
// Set the new store as the store ID for the Open FGA Client
fgaClient.SetStoreId(fgaStore.Id)
log.Printf("NEW FGA STORE ID: %v", fgaStore.Id)
if err != nil {
log.Fatal(err)
}
// Read the fga-model.json file, containing our model
jsonData, err := os.ReadFile("./fga-model.json")
if err != nil {
log.Fatal(err)
}
// Convert to a ClientWriteAuthorizationModelRequest
var fgaModel openfgaclient.ClientWriteAuthorizationModelRequest
err = json.Unmarshal(jsonData, &fgaModel)
if err != nil {
log.Fatal(err)
}
// Create a new FGA Model with the contents of our fga-model.json file
fgaModelData, err := fgaClient.WriteAuthorizationModel(context.Background()).Body(fgaModel).Execute()
if err != nil {
log.Fatal(err)
}
log.Printf("NEW FGA MODEL ID: %v", fgaModelData.AuthorizationModelId)
// Create a new data store
var documents []data.Document
store := data.Store{
Documents: documents,
}
// Create a new routerservice
service := router.Service{
Store: &store,
FGAClient: fgaClient,
}
// Create router and routes
r := router.CreateRouter(service)
// Port
var port = ":4000"
// Start the server
srv := &http.Server{
Addr: port,
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: r,
}
log.Printf("SERVER URL: http://localhost%s", port)
// Error handling
go func() {
if err := srv.ListenAndServe(); err != nil {
if err != http.ErrServerClosed {
log.Fatal(err)
}
}
}()
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, os.Interrupt)
<-shutdown
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
srv.Shutdown(ctx)
}