-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
50 lines (45 loc) · 1.15 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"fmt"
"Golang-Graphql-withCICD-PSQL/db"
"Golang-Graphql-withCICD-PSQL/schema"
"log"
"net/http"
"os"
"github.com/neelance/graphql-go/relay"
"github.com/neelance/graphql-go"
"github.com/ichtrojan/thoth"
"github.com/joho/godotenv"
)
func main() {
//initialize logger
logger, _ := thoth.Init("log")
if err := godotenv.Load(); err != nil {
logger.Log(fmt.Errorf("NO .env file Found"))
log.Fatal("No env file found")
}
//get port from .env
port, ok := os.LookupEnv("PORT")
if !ok {
logger.Log(fmt.Errorf("PORT not Found in .env"))
log.Fatal("PORT not set in .env")
}
// Connect to DB
_, err := db.Connect()
if err != nil {
log.Fatal("Error connecting to Database, Exiting...", err)
}
//initalize the defined graphql schema
parsedSchema, err := graphql.ParseSchema(schema.Schema, &schema.RootResolver{})
if err != nil {
fmt.Println("Error parsing schema", err)
}
//our routes
http.Handle("/api", &relay.Handler{Schema: parsedSchema})
http.HandleFunc("/graphiql", schema.GraphiQLHandler)
//connect to port
err = http.ListenAndServe(":"+port, nil)
if err != nil {
fmt.Println("Cannot Listen to the port 8000")
}
}