-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
48 lines (36 loc) · 1.21 KB
/
main.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
package main
import (
"encoding/json"
"log"
"net/http"
"os"
"github.com/bradpurchase/grocerytime-backend/handlers"
// Autoload env variables from .env
_ "github.com/joho/godotenv/autoload"
"github.com/bradpurchase/grocerytime-backend/internal/pkg/db"
"github.com/gorilla/mux"
)
func main() {
db.Factory()
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/", heartbeat)
router.Handle("/graphql", corsHandler(handlers.GraphQLHandler()))
port := os.Getenv("PORT")
log.Println("[main] ⚡️...Listening on port " + port)
log.Fatal(http.ListenAndServe(":"+port, router))
}
type heartbeatResponse struct {
Status string `json:"status"`
Code int `json:"code"`
}
func heartbeat(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(heartbeatResponse{Status: "OK", Code: 200})
}
func corsHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Accept, Authorization, Content-Type, Content-Length, Accept-Encoding")
h.ServeHTTP(w, r)
})
}