Заимплементил Set в интеграции #4
1 changed files with 67 additions and 3 deletions
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
|
||||||
|
@ -96,15 +97,61 @@ func (model StorageClientInfo) GetSecret() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (model StorageClientInfo) GetDomain() string {
|
func (model StorageClientInfo) GetDomain() string {
|
||||||
// implement as in-memory
|
return model.GetDomain()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (model StorageClientInfo) IsPublic() bool {
|
func (model StorageClientInfo) IsPublic() bool {
|
||||||
// implement as in-memory
|
return model.IsPublic()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (model StorageClientInfo) GetUserID() string {
|
func (model StorageClientInfo) GetUserID() string {
|
||||||
// implement as in-memory
|
return model.GetUserID()
|
||||||
|
}
|
||||||
|
|
||||||
|
type InMemoryClient struct {
|
||||||
|
Id string
|
||||||
|
Domain string
|
||||||
|
IsPublic bool
|
||||||
|
}
|
||||||
|
|
||||||
|
var clients map[string]InMemoryClient
|
||||||
|
|
||||||
|
func addInMemoryClient(id string, domain string, isPublic bool) error {
|
||||||
|
result := InMemoryClient{
|
||||||
|
Id: id,
|
||||||
|
Domain: domain,
|
||||||
|
IsPublic: isPublic,
|
||||||
|
}
|
||||||
|
_, contains := clients[id]
|
||||||
|
if contains {
|
||||||
|
return fmt.Errorf("client with id %s already exist", id)
|
||||||
|
} else {
|
||||||
|
clients[id] = result
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getInMemoryClient(id string) (*InMemoryClient, error) {
|
||||||
|
result, contains := clients[id]
|
||||||
|
if contains {
|
||||||
|
return &result, nil
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("client with id %s not found", id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateInMemoryClientById(id string, client InMemoryClient) error {
|
||||||
|
_, contains := clients[id]
|
||||||
|
if !contains || id != client.Id {
|
||||||
|
return fmt.Errorf("client with id %s not found", id)
|
||||||
|
} else {
|
||||||
|
clients[id] = client
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func deleteInMemoryClient(id string) {
|
||||||
|
delete(clients, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -144,6 +191,13 @@ func main() {
|
||||||
|
|
||||||
http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
|
||||||
srv.HandleTokenRequest(w, r)
|
srv.HandleTokenRequest(w, r)
|
||||||
|
|
||||||
|
id := r.Header.Get("client_id")
|
||||||
|
_, err := getInMemoryClient(id)
|
||||||
|
if err != nil {
|
||||||
|
slog.Warn("Client with id " + id + "not found")
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
http.HandleFunc("/register", func(writer http.ResponseWriter, request *http.Request) {
|
http.HandleFunc("/register", func(writer http.ResponseWriter, request *http.Request) {
|
||||||
|
@ -170,6 +224,16 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
client, _ := clientStore.GetByID(context.Background(), id)
|
||||||
|
err = addInMemoryClient(
|
||||||
|
client.GetID(),
|
||||||
|
client.GetDomain(),
|
||||||
|
client.IsPublic())
|
||||||
|
if err != nil {
|
||||||
|
slog.Error(err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
writer.WriteHeader(http.StatusOK)
|
writer.WriteHeader(http.StatusOK)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue