Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Staging #13

Merged
merged 18 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"))

Check warning on line 16 in auth/claims.go

View check run for this annotation

Codecov / codecov/patch

auth/claims.go#L15-L16

Added lines #L15 - L16 were not covered by tests
}
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)

Check warning on line 11 in auth/utils.go

View check run for this annotation

Codecov / codecov/patch

auth/utils.go#L10-L11

Added lines #L10 - L11 were not covered by tests
}

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",
},
}

Check warning on line 23 in auth/utils.go

View check run for this annotation

Codecov / codecov/patch

auth/utils.go#L14-L23

Added lines #L14 - L23 were not covered by tests
}

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")

Check warning on line 36 in auth/utils.go

View check run for this annotation

Codecov / codecov/patch

auth/utils.go#L26-L36

Added lines #L26 - L36 were not covered by tests
}
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 @@
InitMod,
InitIdeHelper,
InitLighthouseYml,
InitResolver,

Check warning on line 35 in command/cli/generate/initialize/one/one.go

View check run for this annotation

Codecov / codecov/patch

command/cli/generate/initialize/one/one.go#L35

Added line #L35 was not covered by tests
}

for _, initFunc := range initFunctions {
Expand Down Expand Up @@ -167,3 +168,20 @@
}
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)

Check warning on line 186 in command/cli/generate/initialize/one/one.go

View check run for this annotation

Codecov / codecov/patch

command/cli/generate/initialize/one/one.go#L172-L186

Added lines #L172 - L186 were not covered by tests
}
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 @@
}
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")

Check warning on line 174 in command/run.go

View check run for this annotation

Codecov / codecov/patch

command/run.go#L166-L174

Added lines #L166 - L174 were not covered by tests
}
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
Loading