This repository has been archived by the owner on Dec 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
110 lines (81 loc) · 2.83 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
99
100
101
102
103
104
105
106
107
108
109
110
package main
// Import dependencies.
import (
// internals
"database/sql"
"html/template"
"log"
"net/http"
"os"
// externals
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
)
var db *sql.DB
var err error
var clients = make(map[*websocket.Conn]wsSession)
// Configure the upgrader
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
var templates = template.Must(template.ParseFiles("views/auth/login.html", "views/auth/signup.html", "views/index.html", "views/header.html", "views/footer.html", "views/communities.html", "views/post.html", "views/create_post.html", "views/create_comment.html", "views/comment_preview.html", "views/profile_sidebar.html", "views/user.html"))
// Main function.
func main() {
// Connect to the database.
db, err = sql.Open("mysql", "root:@unix(/tmp/mysql.sock)/slaj?parseTime=true&charset=utf8mb4,utf8")
if err != nil {
// we were unable to connect to the database
log.Printf("[err]: unable to connect to the database...\n")
log.Printf(" %v\n", err)
os.Exit(1)
}
// ping the database once to ensure that it is available
err = db.Ping()
if err != nil {
// we were unable to ping it
log.Printf("[err]: unable to ping the database...\n")
log.Printf(" %v\n", err)
os.Exit(1)
}
// close the database connection after this function exits
defer db.Close()
// Initialize routes.
r := mux.NewRouter()
// Index route.
r.HandleFunc("/", index)
// Auth routes.
r.HandleFunc("/signup", signup)
r.HandleFunc("/login", login)
r.HandleFunc("/act/logout", logout)
// User routes.
r.HandleFunc("/users/{username}", showUser)
// Post routes.
r.HandleFunc("/posts/{id:[0-9]+}", showPost)
r.HandleFunc("/posts/{id:[0-9]+}/comments", createComment)
// Yeah routes.
r.HandleFunc("/posts/{id:[0-9]+}/yeah", createPostYeah)
r.HandleFunc("/posts/{id:[0-9]+}/yeahu", deletePostYeah)
r.HandleFunc("/comments/{id:[0-9]+}/yeah", createCommentYeah)
r.HandleFunc("/comments/{id:[0-9]+}/yeahu", deleteCommentYeah)
// Follow routes.
r.HandleFunc("/users/{username}/follow", createFollow)
r.HandleFunc("/users/{username}/unfollow", deleteFollow)
// Community routes.
r.HandleFunc("/communities/{id:[0-9]+}", showCommunity)
r.HandleFunc("/communities/{id:[0-9]+}/posts", createPost).Methods("POST")
// Upload image.
r.HandleFunc("/upload", uploadImage).Methods("POST")
// Test websocket route.
r.HandleFunc("/ws", handleConnections)
// Serve static assets.
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets"))))
// tell the http server to handle routing with the router we just made
http.Handle("/", r)
// tell the person who started this that we have started the server
log.Printf("listening on :8080")
// start the server
http.ListenAndServe(":8080", nil)
}