package main import ( "log" "net/http" "os" "github.com/99designs/gqlgen/graphql/handler" "github.com/99designs/gqlgen/graphql/playground" "github.com/gorilla/handlers" "github.com/gorilla/mux" "github.com/jmoiron/sqlx" _ "github.com/mattn/go-sqlite3" "github.com/moebiusband/cc-jobarchive/generated" "github.com/moebiusband/cc-jobarchive/graph" ) const defaultPort = "8080" func main() { port := os.Getenv("PORT") if port == "" { port = defaultPort } db, err := sqlx.Open("sqlite3", "./job.db") if err != nil { log.Fatal(err) } defer db.Close() r := mux.NewRouter() loggedRouter := handlers.LoggingHandler(os.Stdout, r) srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{DB: db}})) r.HandleFunc("/", playground.Handler("GraphQL playground", "/query")) r.Handle("/query", srv) log.Printf("connect to http://localhost:%s/ for GraphQL playground", port) log.Fatal(http.ListenAndServe("127.0.0.1:8080", handlers.CORS(handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization"}), handlers.AllowedMethods([]string{"GET", "POST", "HEAD", "OPTIONS"}), handlers.AllowedOrigins([]string{"*"}))(loggedRouter))) }