-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
98 lines (81 loc) · 2.74 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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"flag"
"fmt"
"log"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/ravener/img-api/routes"
)
var port = flag.Int("p", 3030, "Change the port to listen to.")
var addr = flag.String("h", "127.0.0.1", "")
var secret = flag.String("s", "", "Set a password")
var dev = flag.Bool("d", false, "Start in development mode (disable browser cache)")
var (
version = "dev"
commit = "none"
)
func main() {
flag.Parse()
router := chi.NewRouter()
// Setup middlewares
router.Use(middleware.Logger)
router.Use(middleware.Recoverer)
if *secret != "" {
router.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Localhost requests are immune to Authorization.
if r.Host == fmt.Sprintf("127.0.0.1:%d", *port) || r.Host == fmt.Sprintf("localhost:%d", *port) {
next.ServeHTTP(w, r)
return
}
auth := r.Header.Get("Authorization")
if auth != *secret {
w.WriteHeader(http.StatusUnauthorized)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("{\"message\": \"Unauthorized\"}"))
return
}
next.ServeHTTP(w, r)
})
})
}
if *dev {
router.Use(middleware.NoCache)
router.Mount("/debug", middleware.Profiler())
log.Print("Starting in development mode. (Browser cache will be disabled and profiler will be mounted.)")
}
// Initialize all routes.
router.Get("/ping", routes.Ping)
router.Get("/stats", routes.Stats)
// Image routes.
router.Get("/religion", routes.ImageReligion)
router.Get("/beautiful", routes.ImageBeautiful)
router.Get("/fear", routes.ImageFear)
router.Get("/sacred", routes.ImageSacred)
router.Get("/painting", routes.ImagePainting)
router.Get("/color", routes.ImageColor)
router.Get("/delete", routes.ImageDelete)
router.Get("/garbage", routes.ImageGarbage)
router.Get("/tom", routes.ImageTom)
router.Get("/bed", routes.ImageBed)
router.Get("/crush", routes.ImageCrush)
router.Get("/patrick", routes.ImagePatrick)
router.Get("/respect", routes.ImageRespect)
router.Get("/dipshit", routes.ImageDipshit)
router.Get("/picture", routes.ImagePicture)
router.Get("/tweet", routes.ImageTweet)
router.Get("/truth", routes.ImageTruth)
router.Get("/bobross", routes.ImageBobross)
router.Get("/mask", routes.ImageMask)
router.Get("/father", routes.ImageFather)
router.Get("/achievement", routes.ImageAchievement)
router.Get("/kaguya", routes.ImageKaguya)
// Non-Image routes.
router.Get("/dominantColor", routes.DominantColor)
fmt.Printf("Img API Version %s (Commit: %s)\n", version, commit)
fmt.Printf("Running on http://%s:%d\n", *addr, *port)
// Start the server.
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", *addr, *port), router))
}