Initial commit
This commit is contained in:
105
app/controllers/base_controller.go
Normal file
105
app/controllers/base_controller.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/gorilla/sessions"
|
||||
"gorm.io/driver/postgres"
|
||||
"moretcgshop/app/models"
|
||||
"os"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
DB *gorm.DB
|
||||
Router *mux.Router
|
||||
AppConfig *AppConfig
|
||||
}
|
||||
|
||||
type AppConfig struct {
|
||||
AppName string
|
||||
AppEnv string
|
||||
AppPort string
|
||||
AppURL string
|
||||
}
|
||||
|
||||
type DBConfig struct {
|
||||
DBHost string
|
||||
DBUser string
|
||||
DBPassword string
|
||||
DBName string
|
||||
DBPort string
|
||||
DBDriver string
|
||||
}
|
||||
|
||||
var store = sessions.NewCookieStore([]byte(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.initializeAppConfig(appConfig)
|
||||
server.initializeRoutes()
|
||||
}
|
||||
|
||||
func (server *Server) Run(addr string) {
|
||||
fmt.Printf("Listening to port %s", addr)
|
||||
log.Fatal(http.ListenAndServe(addr, server.Router))
|
||||
}
|
||||
|
||||
func (s Server) InitCommands(config AppConfig, config2 DBConfig) {
|
||||
|
||||
}
|
||||
|
||||
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)
|
||||
server.DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||
if err != nil {
|
||||
panic("Failed on connecting to the database server")
|
||||
}
|
||||
}
|
||||
func (server *Server) dbMigrate() {
|
||||
/*for _, model := range models.RegisterModels() {
|
||||
err := server.DB.Debug().AutoMigrate(model.Model)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("Database migrated successfully.")
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
func (server *Server) initializeAppConfig(config AppConfig) {
|
||||
server.AppConfig = &config
|
||||
}
|
||||
|
||||
func IsLoggedIn(r *http.Request) bool {
|
||||
session, _ := store.Get(r, sessionUser)
|
||||
if session.Values["id"] == nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (server *Server) CurrentUser(w http.ResponseWriter, r *http.Request) *models.User {
|
||||
if !IsLoggedIn(r) {
|
||||
return nil
|
||||
}
|
||||
|
||||
session, _ := store.Get(r, sessionUser)
|
||||
|
||||
userModel := models.User{}
|
||||
|
||||
_ = session
|
||||
_ = userModel
|
||||
return nil
|
||||
}
|
||||
19
app/controllers/home_controller.go
Normal file
19
app/controllers/home_controller.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/unrolled/render"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (server *Server) Home(w http.ResponseWriter, r *http.Request) {
|
||||
render := render.New(render.Options{
|
||||
Layout: "layout",
|
||||
Extensions: []string{".html", ".tmpl"},
|
||||
})
|
||||
|
||||
user := server.CurrentUser(w, r)
|
||||
|
||||
_ = render.HTML(w, http.StatusOK, "home", map[string]interface{}{
|
||||
"user": user,
|
||||
})
|
||||
}
|
||||
15
app/controllers/routes.go
Normal file
15
app/controllers/routes.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/gorilla/mux"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (server *Server) initializeRoutes() {
|
||||
server.Router = mux.NewRouter()
|
||||
server.Router.HandleFunc("/", server.Home).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