~xenrox/10man-api

ref: 7d7099856ce985345d3e1f3226769c8d94948fe8 10man-api/server.go -rw-r--r-- 853 bytes
7d709985Thorben Günther Make port configurable 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main

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

	"git.xenrox.net/~xenrox/10man-api/config"
	"git.xenrox.net/~xenrox/10man-api/database"
	"git.xenrox.net/~xenrox/10man-api/graph"
	"git.xenrox.net/~xenrox/10man-api/graph/generated"
	"github.com/99designs/gqlgen/graphql/handler"
)

func main() {
	port := os.Getenv("PORT")
	if port == "" {
		port = config.Port
	}

	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

	err := database.Open()
	if err != nil {
		log.Fatal(err)
	}

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

	http.Handle("/query", srv)

	go func() {
		log.Fatal(http.ListenAndServe(":"+port, nil))
	}()

	shutdown(<-sigs)
}

func shutdown(sig os.Signal) {
	database.Close()
	log.Println("API was shut down")
}