-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
53 lines (43 loc) · 1.11 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
package main
import (
"context"
"log"
"net/http"
"os"
"time"
"github.com/ShauryaAg/ProductAPI/models/db"
"github.com/ShauryaAg/ProductAPI/models/db/seed"
"github.com/ShauryaAg/ProductAPI/routes"
"github.com/rs/cors"
)
var (
seedDb = os.Getenv("SEED_DB")
)
func main() {
if seedDb == "true" {
seed.Seed()
}
r := routes.GetRoutes()
ctx := context.Background()
client, err := db.InitDatabase("mongo", ctx)
defer client.Disconnect(ctx)
if err != nil {
log.Fatal("err", err)
}
db.DBCon = client.Database("mongo")
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"POST", "GET", "PATCH", "OPTIONS"},
AllowedHeaders: []string{"Access-Control-Allow-Origin", "Accept", "Accept-Language", "Content-Type", "Authorization"},
AllowCredentials: true,
Debug: true,
})
srv := &http.Server{
Addr: ":" + os.Getenv("PORT"),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
Handler: c.Handler(r),
}
log.Fatal(srv.ListenAndServe())
}