Skip to content

Commit

Permalink
cmd server
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur1 committed Sep 10, 2023
1 parent 6136071 commit 426d100
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
46 changes: 46 additions & 0 deletions cmd/server/main.go
@@ -0,0 +1,46 @@
package main

import (
"log"
"net/http"
"os"

"entgo.io/ent/dialect"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/Arthur1/economical-graphql-server-demo/ent"
"github.com/Arthur1/economical-graphql-server-demo/graph"
"github.com/Arthur1/economical-graphql-server-demo/graph/generated"
"github.com/Arthur1/economical-graphql-server-demo/sqlite"
)

const (
defaultPort = "8080"
)

func init() {
sqlite.Init()
}

func main() {
client, err := ent.Open(dialect.SQLite, "file:db.sqlite?cache=shared&mode=ro")
if err != nil {
log.Fatalf("failed opening connection to sqlite: %v", err)
}
defer client.Close()

port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}

srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{
Client: client,
}}))

http.Handle("/", playground.Handler("GraphQL playground", "/graphql"))
http.Handle("/graphql", srv)

log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
3 changes: 3 additions & 0 deletions go.mod
Expand Up @@ -22,11 +22,14 @@ require (
github.com/go-openapi/inflect v0.19.0 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/hcl/v2 v2.13.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/vmihailenco/msgpack/v5 v5.0.0-beta.9 // indirect
github.com/vmihailenco/tagparser v0.1.2 // indirect
Expand Down

0 comments on commit 426d100

Please sign in to comment.