@@ -2,117 +2,15 @@ package main
22
33import (
44 "net/http"
5- "github.com/gorilla/mux"
6- "github.com/gorilla/sessions"
7- "github.com/go-redis/redis"
8- "golang.org/x/crypto/bcrypt"
9- "html/template"
5+ "./routes"
6+ "./models"
7+ "./utils"
108)
119
12- var client * redis.Client
13- var store = sessions .NewCookieStore ([]byte ("t0p-s3cr3t" ))
14- var templates * template.Template
15-
1610func main () {
17- client = redis .NewClient (& redis.Options {
18- Addr : "localhost:6379" ,
19- })
20- templates = template .Must (template .ParseGlob ("templates/*.html" ))
21- r := mux .NewRouter ()
22- r .HandleFunc ("/" , AuthRequired (indexGetHandler )).Methods ("GET" )
23- r .HandleFunc ("/" , AuthRequired (indexPostHandler )).Methods ("POST" )
24- r .HandleFunc ("/login" , loginGetHandler ).Methods ("GET" )
25- r .HandleFunc ("/login" , loginPostHandler ).Methods ("POST" )
26- r .HandleFunc ("/register" , registerGetHandler ).Methods ("GET" )
27- r .HandleFunc ("/register" , registerPostHandler ).Methods ("POST" )
28- fs := http .FileServer (http .Dir ("./static/" ))
29- r .PathPrefix ("/static/" ).Handler (http .StripPrefix ("/static/" , fs ))
11+ models .Init ()
12+ utils .LoadTemplates ("templates/*.html" )
13+ r := routes .NewRouter ()
3014 http .Handle ("/" , r )
3115 http .ListenAndServe (":8080" , nil )
3216}
33-
34- func AuthRequired (handler http.HandlerFunc ) http.HandlerFunc {
35- return func (w http.ResponseWriter , r * http.Request ) {
36- session , _ := store .Get (r , "session" )
37- _ , ok := session .Values ["username" ]
38- if ! ok {
39- http .Redirect (w , r , "/login" , 302 )
40- return
41- }
42- handler .ServeHTTP (w , r )
43- }
44- }
45-
46- func indexGetHandler (w http.ResponseWriter , r * http.Request ) {
47- comments , err := client .LRange ("comments" , 0 , 10 ).Result ()
48- if err != nil {
49- w .WriteHeader (http .StatusInternalServerError )
50- w .Write ([]byte ("Internal server error" ))
51- return
52- }
53- templates .ExecuteTemplate (w , "index.html" , comments )
54- }
55-
56- func indexPostHandler (w http.ResponseWriter , r * http.Request ) {
57- r .ParseForm ()
58- comment := r .PostForm .Get ("comment" )
59- err := client .LPush ("comments" , comment ).Err ()
60- if err != nil {
61- w .WriteHeader (http .StatusInternalServerError )
62- w .Write ([]byte ("Internal server error" ))
63- return
64- }
65- http .Redirect (w , r , "/" , 302 )
66- }
67-
68- func loginGetHandler (w http.ResponseWriter , r * http.Request ) {
69- templates .ExecuteTemplate (w , "login.html" , nil )
70- }
71-
72- func loginPostHandler (w http.ResponseWriter , r * http.Request ) {
73- r .ParseForm ()
74- username := r .PostForm .Get ("username" )
75- password := r .PostForm .Get ("password" )
76- hash , err := client .Get ("user:" + username ).Bytes ()
77- if err == redis .Nil {
78- templates .ExecuteTemplate (w , "login.html" , "unknown user" )
79- return
80- } else if err != nil {
81- w .WriteHeader (http .StatusInternalServerError )
82- w .Write ([]byte ("Internal server error" ))
83- return
84- }
85- err = bcrypt .CompareHashAndPassword (hash , []byte (password ))
86- if err != nil {
87- templates .ExecuteTemplate (w , "login.html" , "invalid login" )
88- return
89- }
90- session , _ := store .Get (r , "session" )
91- session .Values ["username" ] = username
92- session .Save (r , w )
93- http .Redirect (w , r , "/" , 302 )
94- }
95-
96- func registerGetHandler (w http.ResponseWriter , r * http.Request ) {
97- templates .ExecuteTemplate (w , "register.html" , nil )
98- }
99-
100- func registerPostHandler (w http.ResponseWriter , r * http.Request ) {
101- r .ParseForm ()
102- username := r .PostForm .Get ("username" )
103- password := r .PostForm .Get ("password" )
104- cost := bcrypt .DefaultCost
105- hash , err := bcrypt .GenerateFromPassword ([]byte (password ), cost )
106- if err != nil {
107- w .WriteHeader (http .StatusInternalServerError )
108- w .Write ([]byte ("Internal server error" ))
109- return
110- }
111- err = client .Set ("user:" + username , hash , 0 ).Err ()
112- if err != nil {
113- w .WriteHeader (http .StatusInternalServerError )
114- w .Write ([]byte ("Internal server error" ))
115- return
116- }
117- http .Redirect (w , r , "/login" , 302 )
118- }
0 commit comments