refactored frontend for better organization

This commit is contained in:
devinchristianson
2019-12-27 02:51:27 +00:00
parent 8d6cb5ff88
commit 958b351064
4 changed files with 91 additions and 52 deletions
+26
View File
@@ -0,0 +1,26 @@
package main
import (
"fmt"
"log"
"database/sql"
_ "github.com/lib/pq"
)
var db *sql.DB
const (
DB_USER = "postgres"
DB_PASSWORD = "postgres"
DB_NAME = "test"
)
func initDB() {
dbcreds := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable",
DB_USER, DB_PASSWORD, DB_NAME)
db, dberr := sql.Open("roach", dbcreds)
if dberr != nil {
log.Fatal(dberr)
}
defer db.Close()
}
+29
View File
@@ -0,0 +1,29 @@
package main
import (
"fmt"
"net/http"
"strings"
)
//FileSystem implements http.Filesystem, with Open() that doesnt allow Dirs
type FileSystem struct {
fs http.FileSystem
}
//Open overrides http.FileSystem.Open in order to prevent Directory access
func (cfs FileSystem) Open(path string) (http.File, error) {
fmt.Println("filepath",path)
f, err := cfs.fs.Open(path)
if err != nil {
return nil, err
}
st, err := f.Stat()
if st.IsDir() {
fmt.Println("Dir", path, "is not allowed")
index := strings.TrimSuffix(path, "/") + "/index.html"
if _, err := cfs.fs.Open(index); err != nil {
return nil, err
}
}
return f, nil
}
+34
View File
@@ -0,0 +1,34 @@
package main
import (
"fmt"
"log"
"net/http"
"html/template"
)
//Page struct to hold per-page data
type Page struct {
Location string
Name string
}
func homePage(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
t, err := template.ParseFiles("index.gohtml")
if err != nil {
log.Fatal("Parse failed: ", err)
}
home := Page{Location: "/", Name: "Home"}
search := Page{Location: "/search", Name: "Search"}
data := struct { Pages []*Page }{ []*Page{ &home, &search } }
if err := t.ExecuteTemplate(w, "index", data); err != nil {
log.Fatal("ExecuteTemplate failed:", err)
}
} else {
r.ParseForm()
// logic part of log in
fmt.Println("username:", r.Form["username"])
fmt.Println("password:", r.Form["password"])
}
}
+2 -52
View File
@@ -1,16 +1,14 @@
package main
import (
"fmt"
"log"
"net/http"
"strings"
"strconv"
"html/template"
"github.com/lib/pq"
_ "github.com/lib/pq"
)
func main() {
initDB()
var port = 8080
mux := http.NewServeMux()
fileserver := http.FileServer(FileSystem{http.Dir("./assets")})
@@ -21,51 +19,3 @@ func main() {
log.Fatal("ListenAndServe failed with error: ", err)
}
}
//Page struct to hold per-page data
type Page struct {
Location string
Name string
}
func homePage(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
t, err := template.ParseFiles("index.gohtml")
if err != nil {
log.Fatal("Parse failed: ", err)
}
home := Page{Location: "/", Name: "Home"}
search := Page{Location: "/search", Name: "Search"}
data := struct { Pages []*Page }{ []*Page{ &home, &search } }
if err := t.ExecuteTemplate(w, "index", data); err != nil {
log.Fatal("ExecuteTemplate failed:", err)
}
} else {
r.ParseForm()
// logic part of log in
fmt.Println("username:", r.Form["username"])
fmt.Println("password:", r.Form["password"])
}
}
//FileSystem implements http.Filesystem, with Open() that doesnt allow Dirs
type FileSystem struct {
fs http.FileSystem
}
//Open overrides http.FileSystem.Open in order to prevent Directory access
func (cfs FileSystem) Open(path string) (http.File, error) {
fmt.Println("filepath",path)
f, err := cfs.fs.Open(path)
if err != nil {
return nil, err
}
st, err := f.Stat()
if st.IsDir() {
fmt.Println("Dir", path, "is not allowed")
index := strings.TrimSuffix(path, "/") + "/index.html"
if _, err := cfs.fs.Open(index); err != nil {
return nil, err
}
}
return f, nil
}