Skip to content

Commit

Permalink
added lec 77 setup test environment package render and a first test
Browse files Browse the repository at this point in the history
  • Loading branch information
jagottsicher committed Nov 3, 2023
1 parent 91aa4d0 commit 95b45d2
Show file tree
Hide file tree
Showing 277 changed files with 283 additions and 46,681 deletions.
12 changes: 0 additions & 12 deletions .gitignore
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,3 @@

# Dependency directories (remove the comment below to include it)
# vendor/

# an update script in production
update.sh

# log directory in production
logs/

# Exclude my Database Setup
database.yml

# ignore the git language inclusion
.gitattributes
Empty file modified LICENSE
100755 → 100644
Empty file.
26 changes: 1 addition & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,5 @@
# myGoWebApplication ![Actions Status](https://github.com/jagottsicher/myGoWebApplication/workflows/Go/badge.svg)
# myGoWebApplication

🇬🇧 An Udemy course accompanying educational repository to build web applications with Go (golang).

🇩🇪 Ein einen Udemy-Kurs begleitendes Lehr-Repository, um Webanwendungen mit Go (golang) zu entwickeln.

🇬🇧 Dependencies / 🇩🇪 Anhängigkeiten:
* [github.com/go-chi/chi/v5](https://github.com/go-chi/chi/v5) | Router
* [github.com/alexedwards/scs/v2](https://github.com/alexedwards/scs/v2) | Sessions
* [github.com/justinas/nosurf](https://github.com/justinas/nosurf) | CSRF-Token
* [github.com/asaskevich/govalidator](https://github.com/asaskevich/govalidator) | Validator (server-sided)
* [github.com/jackc/pgx/v5](https://github.com/jackc/pgx/v5) | PostgreSQL Driver & Toolkit
* [github.com/xhit/go-simple-mail](https://github.com/xhit/go-simple-mail) | Golang package for sending e-mail
* [Caddy 2](https://caddyserver.com/l) | a powerful, enterprise-ready, open source web server with automatic HTTPS written in Go

🇬🇧 Also playing a part / 🇩🇪 ebenfalls eine Rolle spielen:
* [github.com/twbs/bootstrap](https://github.com/twbs/bootstrap) | Bootstrap - HTML, CSS, and JavaScript framework (no jQuery)
* [RoyalUI-Free-Bootstrap-Admin-Template](https://github.com/BootstrapDash/RoyalUI-Free-Bootstrap-Admin-Template) | Free Bootstrap 4 Admin Template
* [github.com/fiduswriter/Simple-DataTables](https://github.com/fiduswriter/Simple-DataTables) | DataTables but in TypeScript transpiled to Vanilla JS
* [github.com/postgres/postgres](https://github.com/postgres/postgres) | PostgreSQL Server (mirror only)
* [github.com/gobuffalo/pop](github.com/gobuffalo/pop) | Soda/Migrations - standardization of database tasks
* [github.com/dbeaver/dbeaver](https://github.com/dbeaver/dbeaver) | Dbeaver - free multi-platform database tool
* [github.com/mymth/vanillajs-datepicker](https://github.com/mymth/vanillajs-datepicker) | Vanilla JavaScript datepicker
* [github.com/jaredreich/notie](https://github.com/jaredreich/notie) | unobtrusive notifications - clean and simple JavaScript
* [github.com/jackc/pgx/v5](https://github.com/sweetalert2/sweetalert2) | SweetAlert2 - so many options for JavaScript popups
* [github.com/mailhog/MailHog](https://github.com/mailhog/MailHog) | MailHog - Web and API based SMTP testing
* [Foundation for Emails 2](https://get.foundation/emails.html) | Quickly create responsive HTML e-mails that work
* [Cobra](https://cobra.dev) | A Framework for Modern CLI Apps in Go
* [GoDotEnv](https://github.com/joho/godotenv) | A Go port of Ruby's dotenv library
77 changes: 8 additions & 69 deletions cmd/web/main.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,30 @@ package main

import (
"encoding/gob"
"flag"
"fmt"
"log"
"net/http"
"os"
"time"

"github.com/alexedwards/scs/v2"
"github.com/jagottsicher/myGoWebApplication/internal/config"
"github.com/jagottsicher/myGoWebApplication/internal/driver"
"github.com/jagottsicher/myGoWebApplication/internal/handlers"
"github.com/jagottsicher/myGoWebApplication/internal/helpers"
"github.com/jagottsicher/myGoWebApplication/internal/models"
"github.com/jagottsicher/myGoWebApplication/internal/render"
)

const portNumber = ":8080"
const versionNumber = "v1.0.176"

var app config.AppConfig
var session *scs.SessionManager
var infoLog *log.Logger
var errorLog *log.Logger

// main is the main function
func main() {
db, err := run()
err := run()
if err != nil {
log.Fatal(err)
}

defer db.SQL.Close()

defer close(app.MailChan)

fmt.Println("Starting E-Mail listener")
listenForMail()

fmt.Println(fmt.Sprintf("Starting application on port %s", portNumber))

srv := &http.Server{
Expand All @@ -54,49 +40,12 @@ func main() {

}

func run() (*driver.DB, error) {
func run() error {
// Data to be available in the session
gob.Register(models.Reservation{})
gob.Register(models.User{})
gob.Register(models.Bungalow{})
gob.Register(models.BungalowRestriction{})
gob.Register(models.Restriction{})
gob.Register(map[string]int{})

// read flags as arguments from the command line
inProduction := flag.Bool("production", true, "Application is in production mode")
useCache := flag.Bool("cache", true, "Use template cache")
dbHost := flag.String("dbhost", "localhost", "Database host")
dbName := flag.String("dbname", "", "Database name")
dbUser := flag.String("dbuser", "", "Database user")
dbPass := flag.String("dbpass", "", "Database password")
dbPort := flag.String("dbport", "5432", "Database port")
dbSSL := flag.String("dbssl", "disable", "Database ssl settings (disable, prefer, require)")
version := flag.Bool("version", false, "Prints the version number")

flag.Parse()

if *dbName == "" || *dbUser == "" {
fmt.Println("Required flags missing - no user credentials for db access?")
os.Exit(1)
}

if *version == true {
fmt.Println(versionNumber)
}

mailChan := make(chan models.MailData)
app.MailChan = mailChan

// don't forget to change to true in Production!
app.InProduction = *inProduction
app.UseCache = *useCache

infoLog = log.New(os.Stdout, "[INFO]\t", log.Ldate|log.Ltime)
app.InfoLog = infoLog

errorLog = log.New(os.Stdout, "[ERROR]\t", log.Ldate|log.Ltime|log.Lshortfile)
app.ErrorLog = errorLog
app.InProduction = false

session = scs.New()
session.Lifetime = 24 * time.Hour
Expand All @@ -106,28 +55,18 @@ func run() (*driver.DB, error) {

app.Session = session

// connecting to database
log.Println("Connecting to database...")
connectionString := fmt.Sprintf("host=%s port=%s dbname=%s user=%s password=%s sslmode=%s", *dbHost, *dbPort, *dbName, *dbUser, *dbPass, *dbSSL)
db, err := driver.ConnectSQL(connectionString)
if err != nil {
log.Fatal("No connection to database! Terminating ...")
}
log.Println("Successfully connected to database.")

tc, err := render.CreateTemplateCache()
if err != nil {
log.Fatal("cannot create template cache")
return nil, err
return err
}

app.TemplateCache = tc
app.UseCache = false

repo := handlers.NewRepo(&app, db)
repo := handlers.NewRepo(&app)
handlers.NewHandlers(repo)

render.NewRenderer(&app)

helpers.NewHelpers(&app)
return db, nil
render.NewTemplates(&app)
return nil
}
2 changes: 1 addition & 1 deletion cmd/web/main_test.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import "testing"

func TestRun(t *testing.T) {
_, err := run()
err := run()
if err != nil {
t.Error("Test did not pass: EPIC FAIL")
}
Expand Down
13 changes: 0 additions & 13 deletions cmd/web/middleware.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"net/http"

"github.com/jagottsicher/myGoWebApplication/internal/helpers"
"github.com/justinas/nosurf"
)

Expand All @@ -25,15 +24,3 @@ func NoSurf(next http.Handler) http.Handler {
func SessionLoad(next http.Handler) http.Handler {
return session.LoadAndSave(next)
}

// Auth redirects non-authenticated requests
func Auth(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !helpers.IsAuthenticated(r) {
session.Put(r.Context(), "error", "Log in first!")
http.Redirect(w, r, "/user/login", http.StatusSeeOther)
return
}
next.ServeHTTP(w, r)
})
}
Empty file modified cmd/web/middleware_test.go
100755 → 100644
Empty file.
18 changes: 0 additions & 18 deletions cmd/web/routes.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,9 @@ func routes(app *config.AppConfig) http.Handler {
mux.Get("/reservation", handlers.Repo.Reservation)
mux.Post("/reservation", handlers.Repo.PostReservation)
mux.Post("/reservation-json", handlers.Repo.ReservationJSON)
mux.Get("/choose-bungalow/{id}", handlers.Repo.ChooseBungalow)
mux.Get("/book-bungalow", handlers.Repo.BookBungalow)
mux.Get("/make-reservation", handlers.Repo.MakeReservation)
mux.Post("/make-reservation", handlers.Repo.PostMakeReservation)
mux.Get("/reservation-overview", handlers.Repo.ReservationOverview)
mux.Get("/user/login", handlers.Repo.ShowLogin)
mux.Post("/user/login", handlers.Repo.PostShowLogin)
mux.Get("/user/logout", handlers.Repo.Logout)

mux.Route("/admin", func(mux chi.Router) {
mux.Use(Auth)
mux.Get("/dashboard", handlers.Repo.AdminDashboard)
mux.Get("/reservations-new", handlers.Repo.AdminNewReservations)
mux.Get("/reservations-all", handlers.Repo.AdminAllReservations)
mux.Get("/reservations-calendar", handlers.Repo.AdminReservationsCalendar)
mux.Post("/reservations-calendar", handlers.Repo.AdminPostReservationsCalendar)
mux.Get("/reservations/{src}/{id}/show", handlers.Repo.AdminShowReservation)
mux.Post("/reservations/{src}/{id}", handlers.Repo.AdminPostShowReservation)
mux.Get("/process-reservation/{src}/{id}/do", handlers.Repo.AdminProcessReservation)
mux.Get("/delete-reservation/{src}/{id}/do", handlers.Repo.AdminDeleteReservation)
})

fileServer := http.FileServer(http.Dir("./static/"))
mux.Handle("/static/*", http.StripPrefix("/static", fileServer))
Expand Down
Empty file modified cmd/web/routes_test.go
100755 → 100644
Empty file.
43 changes: 0 additions & 43 deletions cmd/web/send-mail.go

This file was deleted.

Empty file modified cmd/web/setup_test.go
100755 → 100644
Empty file.
13 changes: 0 additions & 13 deletions database.yml_example

This file was deleted.

11 changes: 1 addition & 10 deletions go.mod
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,8 @@ go 1.20

require (
github.com/alexedwards/scs/v2 v2.5.1
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2
github.com/go-chi/chi/v5 v5.0.8
github.com/jackc/pgx/v5 v5.4.1
github.com/justinas/nosurf v1.1.1
)

require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/toorop/go-dkim v0.0.0-20201103131630-e1cd1a0a5208 // indirect
github.com/xhit/go-simple-mail/v2 v2.16.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/text v0.9.0 // indirect
)
require github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
25 changes: 0 additions & 25 deletions go.sum
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,7 @@ github.com/alexedwards/scs/v2 v2.5.1 h1:EhAz3Kb3OSQzD8T+Ub23fKsiuvE0GzbF5Lgn0uTw
github.com/alexedwards/scs/v2 v2.5.1/go.mod h1:ToaROZxyKukJKT/xLcVQAChi5k6+Pn1Gvmdl7h3RRj8=
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.4.1 h1:oKfB/FhuVtit1bBM3zNRRsZ925ZkMN3HXL+LgLUM9lE=
github.com/jackc/pgx/v5 v5.4.1/go.mod h1:q6iHT8uDNXWiFNOlRqJzBTaSH3+2xCXkokxHZC5qWFY=
github.com/justinas/nosurf v1.1.1 h1:92Aw44hjSK4MxJeMSyDa7jwuI9GR2J/JCQiaKvXXSlk=
github.com/justinas/nosurf v1.1.1/go.mod h1:ALpWdSbuNGy2lZWtyXdjkYv4edL23oSEgfBT1gPJ5BQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/toorop/go-dkim v0.0.0-20201103131630-e1cd1a0a5208 h1:PM5hJF7HVfNWmCjMdEfbuOBNXSVF2cMFGgQTPdKCbwM=
github.com/toorop/go-dkim v0.0.0-20201103131630-e1cd1a0a5208/go.mod h1:BzWtXXrXzZUvMacR0oF/fbDDgUPO8L36tDMmRAf14ns=
github.com/xhit/go-simple-mail/v2 v2.16.0 h1:ouGy/Ww4kuaqu2E2UrDw7SvLaziWTB60ICLkIkNVccA=
github.com/xhit/go-simple-mail/v2 v2.16.0/go.mod h1:b7P5ygho6SYE+VIqpxA6QkYfv4teeyG4MKqB3utRu98=
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Empty file modified html-source/about.html
100755 → 100644
Empty file.
Empty file modified html-source/check-availability.html
100755 → 100644
Empty file.
Empty file modified html-source/contact.html
100755 → 100644
Empty file.
Empty file modified html-source/couple.html
100755 → 100644
Empty file.
Empty file modified html-source/eremite.html
100755 → 100644
Empty file.
Empty file modified html-source/family.html
100755 → 100644
Empty file.
Empty file modified html-source/index.html
100755 → 100644
Empty file.
Empty file modified html-source/make-reservation.html
100755 → 100644
Empty file.
Empty file modified html-source/static/css/styles.css
100755 → 100644
Empty file.
Empty file modified html-source/static/images/couple-3br.jpg
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified html-source/static/images/couple-bedroom.jpg
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified html-source/static/images/couple-hallway.jpg
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified html-source/static/images/couple-kitchen.jpg
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified html-source/static/images/eremit-2br.jpg
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified html-source/static/images/eremit-bedroom.jpg
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified html-source/static/images/eremit-eating.jpg
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified html-source/static/images/family-5br.jpg
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified html-source/static/images/family-bedroom.jpg
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified html-source/static/images/family-kitchen.jpg
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified html-source/static/images/family-living-room.jpg
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified html-source/static/images/tray.jpg
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified html-source/static/images/woman.jpg
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 0 additions & 3 deletions internal/config/config.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ import (
"log"

"github.com/alexedwards/scs/v2"
"github.com/jagottsicher/myGoWebApplication/internal/models"
)

// AppConfig is a struct holding die application's configuration
type AppConfig struct {
TemplateCache map[string]*template.Template
UseCache bool
InfoLog *log.Logger
ErrorLog *log.Logger
InProduction bool
Session *scs.SessionManager
MailChan chan models.MailData
}
Loading

0 comments on commit 95b45d2

Please sign in to comment.