github.com/EngineerKamesh/gofullstack@v0.0.0-20180609171605-d41341d7d4ee/volume2/section6/gopherfaceauth/gopherface.go (about)

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