2021-03-31 07:23:48 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/99designs/gqlgen/graphql/handler"
|
|
|
|
"github.com/99designs/gqlgen/graphql/playground"
|
2021-03-31 08:50:53 +02:00
|
|
|
"github.com/ClusterCockpit/cc-jobarchive/graph"
|
|
|
|
"github.com/ClusterCockpit/cc-jobarchive/graph/generated"
|
2021-03-31 07:23:48 +02:00
|
|
|
"github.com/gorilla/handlers"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2021-04-12 14:01:59 +02:00
|
|
|
log.Fatal(http.ListenAndServe("127.0.0.1:" + port,
|
2021-03-31 07:23:48 +02:00
|
|
|
handlers.CORS(handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization"}),
|
|
|
|
handlers.AllowedMethods([]string{"GET", "POST", "HEAD", "OPTIONS"}),
|
|
|
|
handlers.AllowedOrigins([]string{"*"}))(loggedRouter)))
|
|
|
|
}
|