Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
jagdeep committed Jun 7, 2021
1 parent 353052f commit 1f5a3a7
Showing 4 changed files with 21 additions and 22 deletions.
14 changes: 7 additions & 7 deletions auth.go
Original file line number Diff line number Diff line change
@@ -72,7 +72,7 @@ func (config *Config) AuthorizeApiKey(apiKey string, accountIDs []string) (autho
stmt.Params["service_method"] = strings.ToLower(config.Service.Method)

ctx := context.Background()
iter := SpannerClient.Single().Query(ctx, stmt)
iter := DB.Single().Query(ctx, stmt)
defer iter.Stop()

err = iter.Do(func(r *spanner.Row) error {
@@ -113,7 +113,7 @@ func (config *Config) AuthorizeToken(userEmail string, accountIDs []string) (aut
stmt.Params["service_method"] = strings.ToLower(config.Service.Method)

ctx := context.Background()
iter := SpannerClient.Single().Query(ctx, stmt)
iter := DB.Single().Query(ctx, stmt)
defer iter.Stop()

err = iter.Do(func(r *spanner.Row) error {
@@ -167,18 +167,18 @@ func VerifyToken(jwtToken string) (userID string, err error) {

// token.Claims : map[
// auth_time:1.610686521e+09
// email:giri@gmail.com
// email:someone@gmail.com
// email_verified:false
// firebase:map[
// identities:map[
// email:[giri@gmail.com]
// phone:[+919540327688]
// email:[someone@gmail.com]
// phone:[+919293949596]
// ]
// sign_in_provider:password
// ]
// name:giri giri
// phone_number:+919540327688
// user_id:8SyEzOpSizc1IrIK36zigfy6Ou32
// phone_number:+919293949596
// user_id:8SyEz1pSizc1IrIK36zigfy6Ou12
// ]

return fmt.Sprintf("%v", token.Claims["email"]), nil
18 changes: 7 additions & 11 deletions db.go
Original file line number Diff line number Diff line change
@@ -6,19 +6,15 @@ import (
"cloud.google.com/go/spanner"
)

func dbString(config *Config) string {
return "projects/" + config.Spanner.Project + "/instances/" + config.Spanner.Instance + "/databases/" + config.Spanner.Database
}

var SpannerClient *spanner.Client

// SpannerClient ...
func (config *Config) SpannerClient() {
var err error
ctx := context.Background()
// spannerClient ...
func (config *Config) newSpannerClient() *spanner.Client {
dbString := "projects/" + config.Spanner.Project +
"/instances/" + config.Spanner.Instance +
"/databases/" + config.Spanner.Database

SpannerClient, err = spanner.NewClient(ctx, dbString(config))
client, err := spanner.NewClient(context.Background(), dbString)
if err != nil {
panic("Error! spanner.NewClient: " + err.Error())
}
return client
}
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -19,7 +19,6 @@ cloud.google.com/go v0.75.0/go.mod h1:VGuuCn7PG0dwsd5XPVm2Mm3wlh3EL55/79EKB6hlPT
cloud.google.com/go v0.78.0/go.mod h1:QjdrLG0uq+YwhjoVOLsS1t7TW8fs36kLs4XO5R5ECHg=
cloud.google.com/go v0.79.0 h1:oqqswrt4x6b9OGBnNqdssxBl1xf0rSUNjU2BR4BZar0=
cloud.google.com/go v0.79.0/go.mod h1:3bzgcEeQlzbuEAYu4mrWhKqWjmpprinYgKJLgKHnbb8=
cloud.google.com/go v0.83.0 h1:bAMqZidYkmIsUqe6PtkEPT7Q+vfizScn+jfNA6jwK9c=
cloud.google.com/go/bigquery v1.0.1/go.mod h1:i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbfbpx4poX+o=
cloud.google.com/go/bigquery v1.3.0/go.mod h1:PjpwJnslEMmckchkHFfq+HTD2DmtT67aNFKH1/VBDHE=
cloud.google.com/go/bigquery v1.4.0/go.mod h1:S8dzgnTigyfTmLBfrtrhyYhwRxG72rYxvftPBK2Dvzc=
@@ -43,7 +42,6 @@ cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RX
cloud.google.com/go/storage v1.10.0 h1:STgFzyU5/8miMl0//zKh2aQeTyeaUH3WN9bSUiJ09bA=
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
firebase.google.com/go v1.0.2 h1:MCEmjmlwZiQ0s+z7EDVX6e3KHHvpGdF2pJBiQAXVXao=
firebase.google.com/go v3.13.0+incompatible h1:3TdYC3DDi6aHn20qoRkxwGqNgdjtblwVAyRLQwGn/+4=
firebase.google.com/go v3.13.0+incompatible/go.mod h1:xlah6XbEyW6tbfSklcfe5FHJIwjt8toICdV5Wh9ptHs=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
@@ -111,8 +109,10 @@ github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
github.com/google/martian/v3 v3.1.0 h1:wCKgOCHuUEVfsaQLpPSJb7VdYCdTVZQAuOdYm1yc/60=
github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
7 changes: 5 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
@@ -4,13 +4,16 @@ import (
"fmt"
"log"
"net/http"

"cloud.google.com/go/spanner"
)

type ServiceInterface interface {
Controller(config *Config, w http.ResponseWriter, req *http.Request)
}

var service ServiceInterface
var DB *spanner.Client

// StartService ...
func StartService(srv ServiceInterface) {
@@ -20,8 +23,8 @@ func StartService(srv ServiceInterface) {
config := ReadConfig()

// Create a new Spanner client
config.SpannerClient()
defer SpannerClient.Close()
DB = config.newSpannerClient()
defer DB.Close()

http.HandleFunc(config.Service.Path, config.Router) // Load all the routes

0 comments on commit 1f5a3a7

Please sign in to comment.