-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (55 loc) · 1.92 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
package main
import (
"encoding/json"
"log"
"net/http"
"os"
"whos-that-pokemon/controllers"
_ "whos-that-pokemon/models"
application "whos-that-pokemon/app"
"github.com/gorilla/mux"
)
//App instance to build the app
type App struct {
Router *mux.Router
}
//UseRouter Method to initialize all routers of the app
func (app *App) UseRouter() {
app.Router = mux.NewRouter()
app.Router.HandleFunc("/", mainFunc).Methods("GET")
app.Router.HandleFunc("/api/users/signin", controllers.SignIn).Methods("POST")
app.Router.HandleFunc("/api/users/{id}/friendship", controllers.SearchAllFriends).Methods("GET")
app.Router.HandleFunc("/api/users/{id}/friendship/{friend_id}", controllers.CreateFriendship).Methods("POST")
app.Router.HandleFunc("/api/users/{friend_id}/friendship/{id}", controllers.AcceptRequest).Methods("PUT")
app.Router.HandleFunc("/api/users/{id}/friendship/{friend_id}", controllers.DeleteFriendship).Methods("DELETE")
app.Router.HandleFunc("/api/users/{id}/friendship/{friend_id}/games", controllers.GetAllGamesFromFriends).Methods("GET")
app.Router.HandleFunc("/api/users/{id}/games/{friend_id}", controllers.StartGameWithFriend).Methods("POST")
app.Router.HandleFunc("/api/users/{id}/games", controllers.GetAllUserGames).Methods("GET")
app.Router.HandleFunc("/api/games/{id}", controllers.GetSpecificGame).Methods("GET")
app.Router.HandleFunc("/api/games/{id}", controllers.UpdateGame).Methods("PUT")
app.Router.Use(application.Authentication)
}
func main() {
app := App{}
app.UseRouter()
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Println("listing on port:", port)
log.Fatal(http.ListenAndServe(":"+port, app.Router))
}
func mainFunc(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
x := struct {
Data string
}{
Data: "Matheus Otário",
}
err := json.NewEncoder(w).Encode(x)
log.Println(x)
if err != nil {
log.Fatal(err)
}
return
}