-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
62 lines (51 loc) · 1.46 KB
/
main.go
File metadata and controls
62 lines (51 loc) · 1.46 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"strconv"
"github.com/gorilla/mux"
"github.com/microhod/randflix-api/api"
"github.com/microhod/randflix-api/config"
"github.com/microhod/randflix-api/storage"
"github.com/rs/cors"
)
func main() {
cfg, err := config.GetConfig()
if err != nil {
log.Fatalf("failed to get config: %s\n", err)
}
log.Printf("config: %s\n", cfg)
store, err := storage.CreateStorage(cfg)
if err != nil {
log.Fatalf("failed to create storage: %s\n", err)
}
api := api.API{Storage: store}
defer store.Disconnect()
r := mux.NewRouter()
r.HandleFunc("/title/random", api.RandomTitleHandler).
Methods(http.MethodGet).
Schemes("http")
r.HandleFunc("/title", api.TitleHandler).
Methods(http.MethodPost, http.MethodGet).
Schemes("http")
r.HandleFunc("/title/{id}", api.TitleHandler).
Methods(http.MethodGet, http.MethodPut).
Schemes("http")
cors := cors.New(cors.Options{
AllowedOrigins: cfg.CorsAllowedOrigins,
AllowedHeaders: cfg.CorsAllowedHeaders,
AllowedMethods: cfg.CorsAllowedMethods,
})
handler := cors.Handler(r)
// handle case where azure functions needs to set the port
if port, ok := os.LookupEnv("FUNCTIONS_CUSTOMHANDLER_PORT"); ok {
cfg.Port, err = strconv.Atoi(port)
if err != nil {
log.Fatalf("failed to parse FUNCTIONS_CUSTOMHANDLER_PORT to int: %s", err)
}
}
log.Printf("(http): starting http server on port %d", cfg.Port)
http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", cfg.Port), handler)
}