ozon-task/cmd/server.go
2024-06-25 02:34:10 +03:00

36 lines
800 B
Go

package main
import (
"log"
"net/http"
"os"
"git.obamna.ru/erius/ozon-task/graph"
"git.obamna.ru/erius/ozon-task/internal/storage"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
)
const defaultPort = "8080"
var port = os.Getenv("APP_PORT")
func main() {
if port == "" {
port = defaultPort
}
s, err := storage.InitStorage()
if err != nil {
log.Panic("could not connect to database")
}
res := graph.InitResolver(s)
srv := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: res}))
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))
}