/
main.go
115 lines (92 loc) · 2.77 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
DESCRIPTION:
main.go handles the setup and initialisation of the website,
as welll as handles the home/index page requests.
AUTHORS:
David Sutton <dsutton1202@gmail.com>
LICENSE:
The following code is licensed under the MIT license.
See LICENSE for more.
COPYRIGHT 2023 - DAVID SUTTON
*/
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"os"
"strconv"
_ "github.com/lib/pq"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
const version = "0.0.1-a"
var tmpl *template.Template
var (
db *gorm.DB
host = os.Getenv("DB_HOST")
port, _ = strconv.Atoi(os.Getenv("DB_PORT"))
user = os.Getenv("DB_USER")
sapassword = os.Getenv("DB_PWORD")
database = os.Getenv("DB")
)
func main() {
// Connect to database.
var err error
db, err = dbConnect()
if err != nil {
log.Fatalln(err)
}
// Print version number.
log.Println("Running version", version)
mux := http.NewServeMux()
// Redirect file requests to static dir.
mux.Handle("/static/", http.StripPrefix("/static", http.FileServer(http.Dir("static"))))
// Assign function handlers to each page.
mux.HandleFunc("/", indexHandler)
mux.HandleFunc("/login/", loginHandler)
mux.HandleFunc("/logout/", logoutHandler)
mux.HandleFunc("/login/form", loginFormHandler)
mux.HandleFunc("/signup/form", signupFormHandler)
mux.HandleFunc("/favicon.ico", faviconHandler)
// Create a HTTP server.
log.Println("listening on localhost:8080")
log.Fatal(http.ListenAndServe(":8080", mux))
}
// dbConnect initialises a connection with the database and returns a reference to a gorm.DB.
func dbConnect() (*gorm.DB, error) {
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Australia/Sydney", host, user, sapassword, database, port)
for i := 0; i < 5; i++ {
gormDB, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err == nil {
log.Printf("connected to database: %s", database)
return gormDB, err
}
log.Printf("failed to connect to %s: attempt %d", database, i+1)
}
return new(gorm.DB), fmt.Errorf("exceeded max retries and couldn't connect to database")
}
// indexHandler handles requests to the index (home) page.
func indexHandler(w http.ResponseWriter, r *http.Request) {
valid, username := validateCookie(r)
if !valid {
w.Header().Set("Location", "/login")
w.WriteHeader(http.StatusSeeOther)
return
}
tmpl = template.Must(template.ParseFiles("static/html/index.html"))
log.Println(r.URL.Path)
var indexData = struct {
Title string
Uname string
Version string
}{Title: "Escapyr", Uname: username, Version: version}
err := tmpl.Execute(w, indexData)
if err != nil {
http.Error(w, "failed to write template", 500)
}
}
func faviconHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/images/favicon.ico")
}