github.com/EngineerKamesh/gofullstack@v0.0.0-20180609171605-d41341d7d4ee/volume2/section7/gopherfaceq/gopherface.go (about) 1 package main 2 3 import ( 4 "log" 5 "os" 6 7 "github.com/EngineerKamesh/gofullstack/volume2/section7/gopherfaceq/common" 8 "github.com/EngineerKamesh/gofullstack/volume2/section7/gopherfaceq/common/asyncq" 9 "github.com/EngineerKamesh/gofullstack/volume2/section7/gopherfaceq/common/datastore" 10 "github.com/EngineerKamesh/gofullstack/volume2/section7/gopherfaceq/handlers" 11 "github.com/EngineerKamesh/gofullstack/volume2/section7/gopherfaceq/middleware" 12 13 "net/http" 14 15 ghandlers "github.com/gorilla/handlers" 16 "github.com/gorilla/mux" 17 "github.com/justinas/alice" 18 ) 19 20 const ( 21 WEBSERVERPORT = ":8443" 22 ) 23 24 func main() { 25 26 asyncq.StartTaskDispatcher(9) 27 28 db, err := datastore.NewDatastore(datastore.MYSQL, "gopherface:gopherface@/gopherfacedb") 29 //db, err := datastore.NewDatastore(datastore.MONGODB, "localhost:27017") 30 //db, err := datastore.NewDatastore(datastore.REDIS, "localhost:6379") 31 32 if err != nil { 33 log.Print(err) 34 } 35 36 defer db.Close() 37 38 env := common.Env{DB: db} 39 40 r := mux.NewRouter() 41 42 r.HandleFunc("/", handlers.HomeHandler) 43 r.Handle("/signup", handlers.SignUpHandler(&env)).Methods("GET", "POST") 44 45 r.Handle("/login", handlers.LoginHandler(&env)).Methods("GET", "POST") 46 r.HandleFunc("/logout", handlers.LogoutHandler).Methods("GET", "POST") 47 48 r.Handle("/feed", middleware.GatedContentHandler(handlers.FeedHandler)).Methods("GET") 49 r.Handle("/friends", middleware.GatedContentHandler(handlers.FriendsHandler)).Methods("GET") 50 r.Handle("/find", middleware.GatedContentHandler(handlers.FindHandler)).Methods("GET,POST") 51 r.Handle("/profile", middleware.GatedContentHandler(handlers.MyProfileHandler)).Methods("GET") 52 r.Handle("/profile/{username}", middleware.GatedContentHandler(handlers.ProfileHandler)).Methods("GET") 53 r.Handle("/postpreview", middleware.GatedContentHandler(handlers.PostPreviewHandler)).Methods("GET", "POST") 54 r.Handle("/upload-image", middleware.GatedContentHandler(handlers.UploadImageHandler)).Methods("GET", "POST") 55 r.Handle("/upload-video", middleware.GatedContentHandler(handlers.UploadVideoHandler)).Methods("GET", "POST") 56 57 r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static")))) 58 59 loggedRouter := ghandlers.LoggingHandler(os.Stdout, r) 60 stdChain := alice.New(middleware.PanicRecoveryHandler) 61 http.Handle("/", stdChain.Then(loggedRouter)) 62 63 err = http.ListenAndServeTLS(WEBSERVERPORT, "certs/gopherfacecert.pem", "certs/gopherfacekey.pem", nil) 64 if err != nil { 65 log.Fatal("ListenAndServe: ", err) 66 } 67 68 }