This repository was archived by the owner on Feb 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
57 lines (42 loc) · 1.39 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
package main
import (
"log"
"net/http"
"os"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
func handleRequests() {
// Create a new instance of a mux router ✨
myRouter := mux.NewRouter().StrictSlash(true)
// Home page ✨
myRouter.HandleFunc("/", serverHomePage)
// Return all tweets ✨
myRouter.HandleFunc("/tweets", returnAllTweets)
// Delete a tweet ✨
myRouter.HandleFunc("/tweet/{id}", checkBearerToken(deleteATweet)).Methods("DELETE")
// Return a single Tweet ✨
myRouter.HandleFunc("/tweet/{id}", returnSingleTweet)
// Create a tweet ✨
myRouter.HandleFunc("/tweet", checkBearerToken(createATweet)).Methods("POST")
// Save the user, if they're authenticated ✨
myRouter.HandleFunc("/save-user-info", checkBearerToken(saveUserInfo)).Methods("POST")
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
if err := http.ListenAndServe(":"+port, handlers.CORS(
handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization"}),
handlers.AllowedMethods([]string{"GET", "POST", "PUT", "HEAD", "OPTIONS"}),
handlers.AllowedOrigins([]string{"*"}))(myRouter)); err != nil {
log.Fatal(err)
}
}
func main() {
// Tweets array will be accessible by all .go files
Tweets = []Tweet{
Tweet{ID: "1", Copy: "This is our first default tweet!", Author: "[email protected]"},
}
handleRequests()
}