-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
52 lines (40 loc) · 1.05 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
49
50
51
52
package main
import (
"log"
"net/http"
"strings"
cloudinary "blog/cloudinary"
photosblog "blog/photos-blog"
)
func main() {
initDependencies()
setUpEndpoints()
runServer()
}
func initDependencies() {
cloudinary.Init()
}
func runServer() {
log.Println("Server started at http://localhost:8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}
func corsMiddleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.Host, "localhost:") {
w.Header().Set("Access-Control-Allow-Origin", "*")
} else {
w.Header().Set("Access-Control-Allow-Origin", "https://rkimmi.github.io")
}
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
if r.Method == "OPTIONS" {
return
}
next(w, r)
}
}
func setUpEndpoints() {
http.HandleFunc("/api/photos-blog/thumbnails", corsMiddleware(photosblog.GetThumbnailsHandler))
}