2021-03-31 07:23:48 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2021-09-21 16:06:41 +02:00
|
|
|
"flag"
|
2021-03-31 07:23:48 +02:00
|
|
|
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-09-21 16:06:41 +02:00
|
|
|
var port, staticFiles, jobDBFile string
|
|
|
|
|
|
|
|
flag.StringVar(&port, "port", "8080", "Port on which to listen")
|
|
|
|
flag.StringVar(&staticFiles, "static-files", "./frontend/public", "Directory who's contents shall be served as static files")
|
|
|
|
flag.StringVar(&jobDBFile, "job-db", "./job.db", "SQLite 3 Jobs Database File")
|
|
|
|
flag.Parse()
|
2021-03-31 07:23:48 +02:00
|
|
|
|
2021-09-21 16:06:41 +02:00
|
|
|
db, err := sqlx.Open("sqlite3", jobDBFile)
|
2021-03-31 07:23:48 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
r := mux.NewRouter()
|
|
|
|
loggedRouter := handlers.LoggingHandler(os.Stdout, r)
|
2021-09-21 16:06:41 +02:00
|
|
|
|
2021-03-31 07:23:48 +02:00
|
|
|
srv := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{DB: db}}))
|
2021-09-21 16:06:41 +02:00
|
|
|
r.HandleFunc("/graphql-playground", playground.Handler("GraphQL playground", "/query"))
|
2021-03-31 07:23:48 +02:00
|
|
|
r.Handle("/query", srv)
|
|
|
|
|
2021-09-21 16:06:41 +02:00
|
|
|
if len(staticFiles) != 0 {
|
|
|
|
r.PathPrefix("/").Handler(http.FileServer(http.Dir(staticFiles)))
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("connect to http://localhost:%s/graphql-playground for GraphQL playground", port)
|
2021-05-12 18:34:55 +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)))
|
|
|
|
}
|