-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (40 loc) · 1.8 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
package main
import (
"github.com/gorilla/mux"
"net/http"
"log"
"encoding/json"
"os"
"github.com/gorilla/handlers"
)
func main() {
PORT := os.Getenv("PORT")
if PORT == "" {
PORT = "8080"
}
router := mux.NewRouter().StrictSlash(true)
// "Root" / "Home" route
router.HandleFunc("/", home).Methods(http.MethodGet)
APIRouter := router.PathPrefix("/api").Subrouter()
//APIRouter.Path("/patch-notes").HandlerFunc(goverwatch.PatchNoteHandler).Methods(http.MethodGet)
APIRouter.Path("/search/{tag}").HandlerFunc(SearchHandler).Methods(http.MethodGet)
PRTRouter := APIRouter.PathPrefix("/{platform}/{region}/{tag}").Subrouter()
PRTRouter.Handle("/profile", Use(http.HandlerFunc(ProfileHandler), PRTMiddleware, PlayerNotFoundMiddleware)).Methods(http.MethodGet)
PRTRouter.Handle("/achievements", Use(http.HandlerFunc(AchievementsHandler), PRTMiddleware, PlayerNotFoundMiddleware)).Methods(http.MethodGet)
// Any route under "/{platform}/{region}/{tag}/{mode}"
PRTMRouter := APIRouter.PathPrefix("/{platform}/{region}/{tag}/{mode}").Subrouter()
PRTMRouter.Handle("/all-hero-stats", Use(http.HandlerFunc(AllHeroStatsHandler), PRTMMiddleware, PlayerNotFoundMiddleware)).Methods(http.MethodGet)
PRTMRouter.Handle("/heros-breakdown", Use(http.HandlerFunc(HerosHandler), PRTMMiddleware, PlayerNotFoundMiddleware)).Methods(http.MethodGet)
// TODO: Hero name validation
PRTMRouter.Handle("/hero/{name}", Use(http.HandlerFunc(HeroHandler), PRTMMiddleware, PlayerNotFoundMiddleware)).Methods(http.MethodGet)
log.Println("Listening on " + PORT)
log.Fatal(http.ListenAndServe(":"+PORT, handlers.CORS()(router)))
}
func home(w http.ResponseWriter, r *http.Request) {
response, err := json.Marshal("API is online")
if err != nil {
log.Println(err)
}
w.Header().Set("Content-Type", "application/json")
w.Write(response)
}