/
server.go
79 lines (63 loc) · 1.72 KB
/
server.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package server
import (
"fmt"
"log"
"net/http"
"github.com/graph-gophers/graphql-go/relay"
"gorm.io/gorm"
"github.com/musobarlab/gorengan/config"
"github.com/musobarlab/gorengan/database"
"github.com/musobarlab/gorengan/internal/server/middleware"
"github.com/musobarlab/gorengan/pkg/shared"
)
// HTTPServer struct
type HTTPServer struct {
port int
graphQLHandlerV1 *relay.Handler
db *gorm.DB
}
// NewHTTPServer echo server constructor
func NewHTTPServer(port int) (*HTTPServer, error) {
db, err := database.GetGormConn(config.DBHost, config.DBUser, config.DBName, config.DBPassword, config.DBPort)
if err != nil {
return nil, err
}
// schema V1
graphQLHandlerV1, err := initGraphqlSchemaV1(db)
if err != nil {
return nil, err
}
return &HTTPServer{
port: port,
graphQLHandlerV1: graphQLHandlerV1,
db: db,
}, nil
}
// Run function
func (s *HTTPServer) Run() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
shared.BuildJSONResponse(w, shared.Response[shared.EmptyJSON]{
Success: true,
Code: 200,
Message: "server up and running",
Data: shared.EmptyJSON{},
}, 200)
})
// secure graphql route with Basic Auth
mux.Handle("/graphql/v1", middleware.BasicAuth(
middleware.NewBasicAuthConfig(config.BasicAuthUsername, config.BasicAuthPassword),
s.graphQLHandlerV1,
))
log.Printf("Http server running on port %d\n", s.port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", s.port), mux))
}
func (s *HTTPServer) Exit() {
log.Print("exiting Http server\n")
db, err := s.db.DB()
if err != nil {
log.Printf("error loading DB %v\n", err)
}
log.Print("closing database\n")
db.Close()
}