controller added
Registrierung hinzugefügt
This commit is contained in:
@@ -4,12 +4,11 @@ import (
|
||||
"fmt"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/gorilla/sessions"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/driver/postgres"
|
||||
"moretcgshop/app/models"
|
||||
"os"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"log"
|
||||
"moretcgshop/app/models"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
@@ -35,16 +34,49 @@ type DBConfig struct {
|
||||
DBDriver string
|
||||
}
|
||||
|
||||
var store = sessions.NewCookieStore([]byte(os.Getenv("SESSION_KEY")))
|
||||
var store = sessions.NewCookieStore([]byte("DamianFinja")) //os.Getenv("SESSION_KEY")))
|
||||
var sessionShoppingCart = "shopping-cart-session"
|
||||
var sessionFlash = "flash-session"
|
||||
var sessionUser = "user-session"
|
||||
|
||||
func (server *Server) Initialize(appConfig AppConfig, dbConfig DBConfig) {
|
||||
fmt.Println("Willkommen zu " + appConfig.AppName)
|
||||
//server.initializeDB(dbConfig)
|
||||
server.initializeDB(dbConfig)
|
||||
server.initializeAppConfig(appConfig)
|
||||
server.initializeRoutes()
|
||||
server.routeInit()
|
||||
}
|
||||
|
||||
func SetFlash(w http.ResponseWriter, r *http.Request, name string, value string) {
|
||||
session, err := store.Get(r, sessionFlash)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
session.AddFlash(value, name)
|
||||
err = session.Save(r, w)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func GetFlash(w http.ResponseWriter, r *http.Request, name string) []string {
|
||||
session, err := store.Get(r, sessionFlash)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return nil
|
||||
}
|
||||
fm := session.Flashes(name)
|
||||
if len(fm) < 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
session.Save(r, w)
|
||||
var flashes []string
|
||||
for _, fl := range fm {
|
||||
flashes = append(flashes, fl.(string))
|
||||
}
|
||||
return flashes
|
||||
}
|
||||
|
||||
func (server *Server) Run(addr string) {
|
||||
@@ -56,6 +88,15 @@ func (s Server) InitCommands(config AppConfig, config2 DBConfig) {
|
||||
|
||||
}
|
||||
|
||||
func MakePassword(password string) (string, error) {
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
return string(hashedPassword), err
|
||||
}
|
||||
|
||||
func ComparePassword(password string, hashedPassword string) bool {
|
||||
return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password)) == nil
|
||||
}
|
||||
|
||||
func (server *Server) initializeDB(dbConfig DBConfig) {
|
||||
var err error
|
||||
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Jakarta", dbConfig.DBHost, dbConfig.DBUser, dbConfig.DBPassword, dbConfig.DBName, dbConfig.DBPort)
|
||||
@@ -63,9 +104,10 @@ func (server *Server) initializeDB(dbConfig DBConfig) {
|
||||
if err != nil {
|
||||
panic("Failed on connecting to the database server")
|
||||
}
|
||||
server.dbMigrate()
|
||||
}
|
||||
func (server *Server) dbMigrate() {
|
||||
/*for _, model := range models.RegisterModels() {
|
||||
for _, model := range models.RegisterModels() {
|
||||
err := server.DB.Debug().AutoMigrate(model.Model)
|
||||
|
||||
if err != nil {
|
||||
@@ -74,8 +116,6 @@ func (server *Server) dbMigrate() {
|
||||
}
|
||||
|
||||
fmt.Println("Database migrated successfully.")
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
func (server *Server) initializeAppConfig(config AppConfig) {
|
||||
@@ -108,3 +148,19 @@ func (server *Server) CurrentUser(w http.ResponseWriter, r *http.Request) *model
|
||||
|
||||
return user
|
||||
}
|
||||
|
||||
func (server *Server) routeInit() {
|
||||
server.Router = mux.NewRouter()
|
||||
server.Router.HandleFunc("/", server.Home).Methods("GET")
|
||||
|
||||
server.Router.HandleFunc("/login", server.Login).Methods("GET")
|
||||
server.Router.HandleFunc("/login", server.DoLogin).Methods("POST")
|
||||
|
||||
server.Router.HandleFunc("/register", server.Register).Methods("GET")
|
||||
server.Router.HandleFunc("/register", server.DoRegister).Methods("POST")
|
||||
server.Router.HandleFunc("/logout", server.Logout).Methods("GET")
|
||||
|
||||
staticFileDirectory := http.Dir("./assets/")
|
||||
staticFileHandler := http.StripPrefix("/public/", http.FileServer(staticFileDirectory))
|
||||
server.Router.PathPrefix("/public/").Handler(staticFileHandler).Methods("GET")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user