implement config file
This commit is contained in:
parent
9ddca46ea7
commit
e12876b3ed
|
@ -44,7 +44,6 @@ query GetAllUsers {
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
## Mutation
|
## Mutation
|
||||||
Toute requếte visant à modifier une donnée est appellée une '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
|
## Postgres
|
||||||
|
@ -99,5 +97,9 @@ firstname TEXT,
|
||||||
lastname TEXT
|
lastname TEXT
|
||||||
);
|
);
|
||||||
|
|
||||||
|
> CREATE TABLE roles (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
name TEXT
|
||||||
|
);
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
{
|
||||||
|
"DB_HOST":"localhost",
|
||||||
|
"DB_PORT":"5432",
|
||||||
|
"DB_USER":"graphql",
|
||||||
|
"DB_PASSWORD":"graphql",
|
||||||
|
"DB_NAME":"graphql",
|
||||||
|
"JWT_SECRET":"cadoles"
|
||||||
|
}
|
12
main.go
12
main.go
|
@ -1,15 +1,17 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"cadoles/graphql/mutations"
|
|
||||||
"cadoles/graphql/postgres"
|
|
||||||
"cadoles/graphql/queries"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"cadoles/graphql/mutations"
|
||||||
|
"cadoles/graphql/postgres"
|
||||||
|
"cadoles/graphql/queries"
|
||||||
|
"cadoles/graphql/security"
|
||||||
|
|
||||||
"github.com/graphql-go/graphql"
|
"github.com/graphql-go/graphql"
|
||||||
"github.com/graphql-go/handler"
|
"github.com/graphql-go/handler"
|
||||||
"github.com/mnmtanish/go-graphiql"
|
graphiql "github.com/mnmtanish/go-graphiql"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -39,7 +41,7 @@ func main() {
|
||||||
postgres.DBConnect()
|
postgres.DBConnect()
|
||||||
defer postgres.DBClose()
|
defer postgres.DBClose()
|
||||||
|
|
||||||
http.Handle("/graphql", httpHandler)
|
http.Handle("/graphql", security.Handle(httpHandler))
|
||||||
http.HandleFunc("/", graphiql.ServeGraphiQL)
|
http.HandleFunc("/", graphiql.ServeGraphiQL)
|
||||||
log.Print("ready: listening...\n")
|
log.Print("ready: listening...\n")
|
||||||
|
|
||||||
|
|
|
@ -1,20 +1,13 @@
|
||||||
package postgres
|
package postgres
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cadoles/graphql/config"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
DB_HOST = "localhost"
|
|
||||||
DB_PORT = "5432"
|
|
||||||
DB_USER = "graphql"
|
|
||||||
DB_PASSWORD = "graphql"
|
|
||||||
DB_NAME = "graphql"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
DB *sql.DB
|
DB *sql.DB
|
||||||
)
|
)
|
||||||
|
@ -26,8 +19,9 @@ func checkErr(err error, DB *sql.DB) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func DBConnect() {
|
func DBConnect() {
|
||||||
|
conf := config.GetConfig()
|
||||||
dbinfo := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
|
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
|
var err error
|
||||||
DB, err = sql.Open("postgres", dbinfo)
|
DB, err = sql.Open("postgres", dbinfo)
|
||||||
checkErr(err, DB)
|
checkErr(err, DB)
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
// GetRootFields returns all the available queries.
|
// GetRootFields returns all the available queries.
|
||||||
func GetRootFields() graphql.Fields {
|
func GetRootFields() graphql.Fields {
|
||||||
return graphql.Fields{
|
return graphql.Fields{
|
||||||
"users": GetUserQuery(),
|
"user": GetUserQuery(),
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue