-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
63 lines (52 loc) · 1.33 KB
/
server.go
File metadata and controls
63 lines (52 loc) · 1.33 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"log"
"net/http"
"os"
"pv-service/database"
"pv-service/graph"
"pv-service/graph/generated"
"pv-service/service"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
)
const port = "8080"
func main() {
cfg := loadConfig()
dbConnection, err := database.GetDBConnection(cfg.DBHost, cfg.DBUser, cfg.DBPassword)
if err != nil {
log.Fatalf("Error on startup has occured: %v", err)
}
processor := processing.New(dbConnection)
srv := handler.NewDefaultServer(
generated.NewExecutableSchema(
generated.Config{
Resolvers: &graph.Resolver{
Processor: processor,
},
},
),
)
http.Handle("/solar/query", srv)
http.Handle("/solar/query/health", health())
http.Handle("/solar/query/playground", playground.Handler("GraphQL playground", "/solar/query"))
log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
func health() http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(http.StatusOK)
}
}
type Config struct {
DBUser string
DBPassword string
DBHost string
}
func loadConfig() Config {
return Config{
DBUser: os.Getenv("DB_USER"),
DBPassword: os.Getenv("DB_PASSWORD"),
DBHost: os.Getenv("DB_HOST"),
}
}