diff --git a/README.md b/README.md index b0fa5ca..46c580a 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,6 @@ query GetAllUsers { ``` - ## Mutation Toute requếte visant à modifier une donnée est appellée une 'Mutation' @@ -69,7 +68,6 @@ mutation CreateUser($firstname: String!, $lastname: String!) { } } - ``` ## Postgres @@ -99,5 +97,9 @@ firstname TEXT, lastname TEXT ); +> CREATE TABLE roles ( +id SERIAL PRIMARY KEY, +name TEXT + ); ``` diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..db15135 --- /dev/null +++ b/config/config.go @@ -0,0 +1,25 @@ +package config + +import ( + "log" + + "github.com/tkanos/gonfig" +) + +type Configuration struct { + DB_HOST string + DB_PORT string + DB_USER string + DB_PASSWORD string + DB_NAME string + JWT_SECRET string +} + +func GetConfig() Configuration { + configuration := Configuration{} + err := gonfig.GetConf("config/config.json", &configuration) + if err != nil { + log.Fatalf("Failed to load configuration file, error: %v", err) + } + return configuration +} diff --git a/config/config.json b/config/config.json new file mode 100644 index 0000000..4f504e8 --- /dev/null +++ b/config/config.json @@ -0,0 +1,8 @@ +{ + "DB_HOST":"localhost", + "DB_PORT":"5432", + "DB_USER":"graphql", + "DB_PASSWORD":"graphql", + "DB_NAME":"graphql", + "JWT_SECRET":"cadoles" +} \ No newline at end of file diff --git a/graphql b/graphql index 618c0df..072e08b 100755 Binary files a/graphql and b/graphql differ diff --git a/main.go b/main.go index 7b4b039..a80f52b 100644 --- a/main.go +++ b/main.go @@ -1,15 +1,17 @@ package main import ( - "cadoles/graphql/mutations" - "cadoles/graphql/postgres" - "cadoles/graphql/queries" "log" "net/http" + "cadoles/graphql/mutations" + "cadoles/graphql/postgres" + "cadoles/graphql/queries" + "cadoles/graphql/security" + "github.com/graphql-go/graphql" "github.com/graphql-go/handler" - "github.com/mnmtanish/go-graphiql" + graphiql "github.com/mnmtanish/go-graphiql" ) func main() { @@ -39,7 +41,7 @@ func main() { postgres.DBConnect() defer postgres.DBClose() - http.Handle("/graphql", httpHandler) + http.Handle("/graphql", security.Handle(httpHandler)) http.HandleFunc("/", graphiql.ServeGraphiQL) log.Print("ready: listening...\n") diff --git a/postgres/postgres.go b/postgres/postgres.go index 8647c60..b75302f 100644 --- a/postgres/postgres.go +++ b/postgres/postgres.go @@ -1,20 +1,13 @@ package postgres import ( + "cadoles/graphql/config" "database/sql" "fmt" _ "github.com/lib/pq" ) -const ( - DB_HOST = "localhost" - DB_PORT = "5432" - DB_USER = "graphql" - DB_PASSWORD = "graphql" - DB_NAME = "graphql" -) - var ( DB *sql.DB ) @@ -26,8 +19,9 @@ func checkErr(err error, DB *sql.DB) { } func DBConnect() { + conf := config.GetConfig() dbinfo := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", - DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, DB_NAME) + conf.DB_HOST, conf.DB_PORT, conf.DB_USER, conf.DB_PASSWORD, conf.DB_NAME) var err error DB, err = sql.Open("postgres", dbinfo) checkErr(err, DB) diff --git a/queries/queries.go b/queries/queries.go index e0f40b1..30f0e84 100644 --- a/queries/queries.go +++ b/queries/queries.go @@ -7,6 +7,7 @@ import ( // GetRootFields returns all the available queries. func GetRootFields() graphql.Fields { return graphql.Fields{ - "users": GetUserQuery(), + "user": GetUserQuery(), } + }