1
Fork 0
photoview/api/server/server.go

70 lines
1.8 KiB
Go

package main
import (
"log"
"net/http"
"os"
"github.com/go-chi/chi"
"github.com/go-chi/cors"
"github.com/joho/godotenv"
"github.com/viktorstrate/photoview/api/database"
"github.com/viktorstrate/photoview/api/graphql/auth"
"github.com/viktorstrate/photoview/api/routes"
"github.com/99designs/gqlgen/handler"
photoview_graphql "github.com/viktorstrate/photoview/api/graphql"
"github.com/viktorstrate/photoview/api/graphql/resolvers"
)
const defaultPort = "4001"
func main() {
if err := godotenv.Load(); err != nil {
log.Println("No .env file found")
}
port := os.Getenv("API_PORT")
if port == "" {
port = defaultPort
}
db := database.SetupDatabase()
defer db.Close()
// Migrate database
if err := database.MigrateDatabase(db); err != nil {
log.Fatalf("Could not migrate database: %s\n", err)
}
router := chi.NewRouter()
router.Use(auth.Middleware(db))
router.Use(cors.New(cors.Options{
AllowedOrigins: []string{"http://localhost:4001", "http://localhost:1234", "*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type"},
AllowCredentials: true,
Debug: false,
}).Handler)
graphqlResolver := resolvers.Resolver{Database: db}
graphqlDirective := photoview_graphql.DirectiveRoot{}
graphqlDirective.IsAdmin = photoview_graphql.IsAdmin(db)
graphqlConfig := photoview_graphql.Config{
Resolvers: &graphqlResolver,
Directives: graphqlDirective,
}
router.Handle("/", handler.Playground("GraphQL playground", "/graphql"))
router.Handle("/graphql", handler.GraphQL(photoview_graphql.NewExecutableSchema(graphqlConfig)))
router.Mount("/photo", routes.PhotoRoutes(db))
log.Printf("🚀 Graphql playground ready at http://localhost:%s/", port)
log.Fatal(http.ListenAndServe(":"+port, router))
}