Skip to content

Commit

Permalink
Merge pull request #13 from light-speak/staging
Browse files Browse the repository at this point in the history
Staging
  • Loading branch information
light-speak authored Nov 4, 2024
2 parents b5fe63d + 90c5c51 commit 2bf46be
Show file tree
Hide file tree
Showing 70 changed files with 1,514 additions and 759 deletions.
17 changes: 17 additions & 0 deletions auth/claims.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package auth

import (
"github.com/golang-jwt/jwt/v5"
"github.com/light-speak/lighthouse/env"
)

type claim struct {
UserId int64 `json:"user_id"`
jwt.RegisteredClaims
}

var key []byte

func init() {
key = []byte(env.GetEnv("JWT_SECRET", "IWY@*3JUI#d309HhefzX2WpLtPKtD!hn"))
}
37 changes: 37 additions & 0 deletions auth/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package auth

import (
"errors"
"time"

"github.com/golang-jwt/jwt/v5"
)

func GetToken(userId int64) (string, error) {
return jwt.NewWithClaims(jwt.SigningMethodHS256, buildClaims(userId)).SignedString(key)
}

func buildClaims(userId int64) *claim {
now := time.Now()
return &claim{
UserId: userId,
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(now.Add(time.Hour * 24 * 30)),
IssuedAt: jwt.NewNumericDate(now),
Issuer: "lighthouse",
},
}
}

func GetUserId(token string) (int64, error) {
t, err := jwt.ParseWithClaims(token, &claim{}, func(token *jwt.Token) (interface{}, error) {
return key, nil
})
if err != nil {
return 0, err
}
if claims, ok := t.Claims.(*claim); ok && t.Valid {
return claims.UserId, nil
}
return 0, errors.New("invalid token")
}
18 changes: 18 additions & 0 deletions command/cli/generate/initialize/one/one.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func Run(module string) error {
InitMod,
InitIdeHelper,
InitLighthouseYml,
InitResolver,
}

for _, initFunc := range initFunctions {
Expand Down Expand Up @@ -167,3 +168,20 @@ func InitLighthouseYml() error {
}
return template.Render(options)
}

func InitResolver() error {
resolverTemplate, err := oneFs.ReadFile("tpl/resolver.tpl")
if err != nil {
return err
}
options := &template.Options{
Path: filepath.Join(projectName, "resolver"),
Template: string(resolverTemplate),
FileName: "resolver",
FileExt: "go",
Package: "resolver",
Editable: true,
SkipIfExists: true,
}
return template.Render(options)
}
5 changes: 0 additions & 5 deletions command/cli/generate/initialize/one/tpl/ide-helper.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ type Query
type Mutation
type Subscription

directive @external repeatable on FIELD_DEFINITION
directive @requires(fields: [String!]!) repeatable on FIELD_DEFINITION
directive @provides(fields: [String!]!) repeatable on FIELD_DEFINITION
directive @key(fields: [String!]!) repeatable on OBJECT | INTERFACE
directive @extends repeatable on OBJECT

directive @paginate(scopes: [String!]) on FIELD_DEFINITION
directive @skip(if: Boolean!) on FIELD_DEFINITION
Expand Down
6 changes: 6 additions & 0 deletions command/cli/generate/initialize/one/tpl/resolver.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type Resolver struct {
}

func (r *Resolver) IsResolver() bool {
return true
}
2 changes: 1 addition & 1 deletion command/cli/generate/schema/schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 7 additions & 10 deletions command/cli/version/version.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 9 additions & 13 deletions command/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,14 @@ func validateRequiredFlags(cmd Command, flagValues map[string]interface{}) error
}
return nil
}

func printLogo() {
fmt.Print("\033[33m")
fmt.Printf(`
_ _ _ _ _
| ) | | |
| _ __ |__ _ |_ |__ _ _ _ ___ __
| | ' \ \ | \ \ | | __| _ \
|___ | | | | | |_ | | | | |_ | __ \ '__
| | ' - | | | / | | '- / / / |
|
\__ / %s by @light-speak
`, version.Version)
fmt.Print("\033[0m\n")
fmt.Printf("\033[38;5;196m _ _ _ _ _\n")
fmt.Printf("\033[38;5;202m | ) | | |\n")
fmt.Printf("\033[38;5;208m | _ __ |__ _ |_ |__ _ _ _ ___ __\n")
fmt.Printf("\033[38;5;214m | | ' \\ \\ | \\ \\ | | __| _ \\\n")
fmt.Printf("\033[38;5;220m |___ | | | | | |_ | | | | |_ | __ \\ '__\n")
fmt.Printf("\033[38;5;226m | | ' - | | | / | | '- / / / |\n")
fmt.Printf("\033[38;5;190m |\n")
fmt.Printf("\033[38;5;154m \\__ / %s by @light-speak\n\n", version.Version)
fmt.Print("\033[0m")
}
4 changes: 2 additions & 2 deletions config/lighthouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type Config struct {
}

type SchemaConfig struct {
Ext []string `yaml:"ext"`
Path []string `yaml:"path"`
Ext []string `yaml:"ext"`
Path []string `yaml:"path"`
}

func ReadConfig(path string) (*Config, error) {
Expand Down
1 change: 0 additions & 1 deletion env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,3 @@ func TestGetEnvInt(t *testing.T) {
}
}
}

1 change: 1 addition & 0 deletions example/user/cmd/migrate/migrate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions example/user/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/joho/godotenv v1.5.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions example/user/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
Expand Down
6 changes: 0 additions & 6 deletions example/user/ide-helper.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ type Query
type Mutation
type Subscription

directive @external repeatable on FIELD_DEFINITION
directive @requires(fields: [String!]!) repeatable on FIELD_DEFINITION
directive @provides(fields: [String!]!) repeatable on FIELD_DEFINITION
directive @key(fields: [String!]!) repeatable on OBJECT | INTERFACE
directive @extends repeatable on OBJECT

directive @skip(if: Boolean!) on FIELD_DEFINITION
directive @include(if: Boolean!) on FIELD_DEFINITION

Expand Down
58 changes: 40 additions & 18 deletions example/user/models/enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions example/user/models/interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2bf46be

Please sign in to comment.