diff --git a/controllers/getController.go b/controllers/getController.go new file mode 100644 index 0000000..5a2aeed --- /dev/null +++ b/controllers/getController.go @@ -0,0 +1,28 @@ +package controllers + +import ( + "GoWeb/app" + "GoWeb/templating" + "net/http" +) + +// GetController is a wrapper struct for the App struct +type GetController struct { + App *app.App +} + +func (getController *GetController) ShowHome(w http.ResponseWriter, r *http.Request) { + type dataStruct struct { + Test string + } + + data := dataStruct{ + Test: "Hello World!", + } + + templating.RenderTemplate(getController.App, w, "templates/pages/home.html", data) +} + +func (getController *GetController) ShowRegister(w http.ResponseWriter, r *http.Request) { + templating.RenderTemplate(getController.App, w, "templates/pages/register.html", nil) +} diff --git a/controllers/postController.go b/controllers/postController.go new file mode 100644 index 0000000..7031d07 --- /dev/null +++ b/controllers/postController.go @@ -0,0 +1,42 @@ +package controllers + +import ( + "GoWeb/app" + "GoWeb/database/models" + "log" + "net/http" + "time" +) + +// PostController is a wrapper struct for the App struct +type PostController struct { + App *app.App +} + +func (postController *PostController) Register(w http.ResponseWriter, r *http.Request) { + username := r.FormValue("username") + password := r.FormValue("password") + createdAt := time.Now() + updatedAt := time.Now() + + if username == "" || password == "" { + log.Println("Tried to create user with empty username or password") + http.Redirect(w, r, "/register", http.StatusFound) + } + + models.CreateUser(postController.App, username, password, createdAt, updatedAt) + + http.Redirect(w, r, "/login", http.StatusFound) +} + +func (postController *PostController) Login(w http.ResponseWriter, r *http.Request) { + username := r.FormValue("username") + password := r.FormValue("password") + + if username == "" || password == "" { + log.Println("Tried to create user with empty username or password") + http.Redirect(w, r, "/register", http.StatusFound) + } + + http.Redirect(w, r, "/login", http.StatusFound) +} diff --git a/database/databaseConnection.go b/database/databaseConnection.go new file mode 100644 index 0000000..7802000 --- /dev/null +++ b/database/databaseConnection.go @@ -0,0 +1,33 @@ +package database + +import ( + "GoWeb/app" + "database/sql" + "fmt" + _ "github.com/lib/pq" + "log" +) + +// ConnectDB returns a new database connection +func ConnectDB(app *app.App) *sql.DB { + // Set connection parameters from config + postgresConfig := fmt.Sprintf("host=%s port=%s user=%s "+ + "password=%s dbname=%s sslmode=disable", + app.Config.Db.Ip, app.Config.Db.Port, app.Config.Db.User, app.Config.Db.Password, app.Config.Db.Name) + + // Create connection + db, err := sql.Open("postgres", postgresConfig) + if err != nil { + panic(err) + } + + // Test connection + err = db.Ping() + if err != nil { + panic(err) + } + + log.Println("Connected to database successfully on " + app.Config.Db.Ip + ":" + app.Config.Db.Port + " using database " + app.Config.Db.Name) + + return db +} diff --git a/routes/getRoutes.go b/routes/getRoutes.go new file mode 100644 index 0000000..c7a946f --- /dev/null +++ b/routes/getRoutes.go @@ -0,0 +1,24 @@ +package routes + +import ( + "GoWeb/app" + "GoWeb/controllers" + "log" + "net/http" +) + +// GetRoutes defines all project get routes +func GetRoutes(app *app.App) { + // Get controller struct initialize + getController := controllers.GetController{ + App: app, + } + + // Serve static files + http.Handle("/file/", http.FileServer(http.Dir("./static"))) + log.Println("Serving static files from: ./static") + + // Pages + http.HandleFunc("/", getController.ShowHome) + http.HandleFunc("/register", getController.ShowRegister) +} diff --git a/routes/postRoutes.go b/routes/postRoutes.go new file mode 100644 index 0000000..b082b47 --- /dev/null +++ b/routes/postRoutes.go @@ -0,0 +1,17 @@ +package routes + +import ( + "GoWeb/app" + "GoWeb/controllers" + "net/http" +) + +func PostRoutes(app *app.App) { + // Get controller struct initialize + postController := controllers.PostController{ + App: app, + } + + http.HandleFunc("/register-handle", postController.Register) + http.HandleFunc("/login-handle", postController.Login) +} diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..30b537a --- /dev/null +++ b/templates/base.html @@ -0,0 +1,13 @@ + + +
+ +