-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
48 lines (42 loc) · 1.13 KB
/
server.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
package main
import (
"localtracker/app/database"
"localtracker/app/repository"
"localtracker/graph"
"localtracker/graph/generated"
"log"
"net/http"
"os"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/joho/godotenv"
)
const defaultPort = "8080"
func main() {
godotenv.Load()
config := &database.Config{
Host: os.Getenv("DB_HOST"),
Port: os.Getenv("DB_PORT"),
Password: os.Getenv("DB_PASS"),
User: os.Getenv("DB_USER"),
SSLMode: os.Getenv("DB_SSLMODE"),
DBName: os.Getenv("DB_NAME"),
}
db, err := database.NewConnection(config)
if err != nil {
panic(err)
}
database.Migrate(db)
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
repo := repository.NewAnimeService(db)
srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{
AnimeRepository: repo,
}}))
http.Handle("/", playground.Handler("GraphQL playground", "/query"))
http.Handle("/query", srv)
log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}