From f53fa2f2f9eb445527e0a1b29b9e37c224499233 Mon Sep 17 00:00:00 2001 From: Horus3 Date: Thu, 19 Feb 2015 02:38:34 +0100 Subject: Reorder files and parses templates. --- .gitignore | 4 +- Makefile | 17 ------- app/Makefile | 21 +++++++++ app/db.go | 40 +++++++++++++++++ app/fetch.go | 9 ++++ app/handler.go | 55 +++++++++++++++++++++++ app/main.go | 26 +++++++++++ app/struct.go | 37 +++++++++++++++ app/utilities.go | 97 ++++++++++++++++++++++++++++++++++++++++ db.go | 40 ----------------- fetch.go | 9 ---- handler.go | 63 -------------------------- main.go | 25 ----------- struct.go | 37 --------------- templates/admin.html | 0 templates/admin/admin.html | 0 templates/index.html | 0 templates/index/index.html | 0 templates/login.html | 0 templates/login/login.html | 0 templates/register.html | 0 templates/register/register.html | 0 utilities.go | 96 --------------------------------------- 23 files changed, 288 insertions(+), 288 deletions(-) delete mode 100644 Makefile create mode 100644 app/Makefile create mode 100644 app/db.go create mode 100644 app/fetch.go create mode 100644 app/handler.go create mode 100644 app/main.go create mode 100644 app/struct.go create mode 100644 app/utilities.go delete mode 100644 db.go delete mode 100644 fetch.go delete mode 100644 handler.go delete mode 100644 main.go delete mode 100644 struct.go create mode 100644 templates/admin.html delete mode 100644 templates/admin/admin.html create mode 100644 templates/index.html delete mode 100644 templates/index/index.html create mode 100644 templates/login.html delete mode 100644 templates/login/login.html create mode 100644 templates/register.html delete mode 100644 templates/register/register.html delete mode 100644 utilities.go diff --git a/.gitignore b/.gitignore index 37ab148..e055c24 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ *.swp -*.db +db/ +statuspage + diff --git a/Makefile b/Makefile deleted file mode 100644 index 64c916d..0000000 --- a/Makefile +++ /dev/null @@ -1,17 +0,0 @@ -export STATUS_DB_DRIVER:=sqlite3 -export STATUS_DB_CREDENTIALS:=status.db - -all: kill build run - -clean: - rm status.db - rm statuspage - -build: - go build - -run: - ./statuspage & - -kill: - pkill statuspage || true diff --git a/app/Makefile b/app/Makefile new file mode 100644 index 0000000..f246e69 --- /dev/null +++ b/app/Makefile @@ -0,0 +1,21 @@ +export STATUS_DB_DRIVER:=sqlite3 +export STATUS_DB_CREDENTIALS:=../db/status.db + +all: kill build run + +clean: + @echo "Removing sqlite3 database..." + @rm $$STATUS_DB_CREDENTIALS + @echo "Removing binary..." + @rm statuspage + @echo "Done" + +build: + go build -o statuspage + +run: + ./statuspage & + +kill: + @echo "Killing running instances..." + @pkill statuspage || true diff --git a/app/db.go b/app/db.go new file mode 100644 index 0000000..dc9a15d --- /dev/null +++ b/app/db.go @@ -0,0 +1,40 @@ +package main + +import ( + "github.com/jinzhu/gorm" + _ "github.com/mattn/go-sqlite3" + "log" + "os" +) + +//var Db gorm.DB +var dbdriver = os.Getenv("STATUS_DB_DRIVER") +var dbcred = os.Getenv("STATUS_DB_CREDENTIALS") +var Db, dberr = gorm.Open(dbdriver, dbcred) + +func InitDB() { + /* + dbdriver := os.Getenv("STATUS_DB_DRIVER") + dbcred := os.Getenv("STATUS_DB_CREDENTIALS") + Db, err := gorm.Open(dbdriver, dbcred) + */ + if dberr != nil { + log.Panic(dberr) + } + Db.LogMode(true) + if err := Db.DB().Ping(); err != nil { + log.Panic(err) + } + + // u := User{} + h := Host{} + // Db.Debug().AutoMigrate(&u) + db := Db + log.Println(db) + db.Debug().AutoMigrate(&h) + + /* + Db.Model(&u).AddUniqueIndex("idx_user_name", "name") + Db.Model(&u).AddUniqueIndex("idx_user_email", "email") + */ +} diff --git a/app/fetch.go b/app/fetch.go new file mode 100644 index 0000000..27aa628 --- /dev/null +++ b/app/fetch.go @@ -0,0 +1,9 @@ +package main + +/* +import ( + "fmt" +) + +func +*/ diff --git a/app/handler.go b/app/handler.go new file mode 100644 index 0000000..f87e92a --- /dev/null +++ b/app/handler.go @@ -0,0 +1,55 @@ +package main + +import ( + "fmt" + // "html/template" + "log" + "net/http" +) + +func IndexHandler(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "Hello World! \n") + // w.Write() +} + +func RegisterHandler(w http.ResponseWriter, r *http.Request) { + log.Println("Processing registration!") + fmt.Fprintf(w, "Processing registration! \n") + // w.Write() +} + +func PrintRegisterHandler(w http.ResponseWriter, r *http.Request) { + + fmt.Fprintf(w, "Printing register etc! \n") + // w.Write() +} + +func PrintNewJobHandler(w http.ResponseWriter, r *http.Request) { + log.Println("Printing job") + + job := mainTempl.Lookup("jobs.html") + + err := job.ExecuteTemplate(w, "jobs.html", nil) + if err != nil { + log.Panic(err) + } +} + +func AddNewJobHandler(w http.ResponseWriter, r *http.Request) { + log.Printf("Add new job") + + err := r.ParseForm() + if err != nil { + log.Panic(err) + } + + host := &Host{} + err = decoder.Decode(host, r.PostForm) + if err != nil { + log.Panic(err) + } + + log.Printf("%v", host) + fmt.Fprintf(w, "%s", host.Url) + Db.Debug().Save(host) +} diff --git a/app/main.go b/app/main.go new file mode 100644 index 0000000..0a8b7dd --- /dev/null +++ b/app/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "github.com/gorilla/mux" + "github.com/gorilla/schema" + "html/template" + "net/http" +) + +var decoder = schema.NewDecoder() + +//var mainTempl = template.Must(template.New("global").ParseFiles("templates/header.html", "templates/navbar.html", "templates/footer.html")) +var mainTempl = template.Must(template.New("global").ParseGlob("../templates/*.html")) + +func main() { + InitDB() + + r := mux.NewRouter() + r.HandleFunc("/", IndexHandler) + r.HandleFunc("/register", RegisterHandler).Methods("POST") + r.HandleFunc("/register", PrintRegisterHandler).Methods("GET") + r.HandleFunc("/new", AddNewJobHandler).Methods("POST") + r.HandleFunc("/new", PrintNewJobHandler).Methods("GET") + http.Handle("/", r) + http.ListenAndServe(":8080", nil) +} diff --git a/app/struct.go b/app/struct.go new file mode 100644 index 0000000..26c462b --- /dev/null +++ b/app/struct.go @@ -0,0 +1,37 @@ +package main + +import ( + "time" +) + +/* Maybe worth saving uptime history? */ + +type Host struct { + Id int64 + UserId int64 + Url string + Protocoll string // e.g. http + Private bool + Response int64 + /* + Date time.Time + Success bool + Include string // Website must include this string + Except string // Website must not include this string + Reason string // Include, Exclude, Connection failure + Alert bool // True to send alert on failure + */ + CreatedAt time.Time + DeletedAt time.Time + UpdatedAt time.Time +} + +type User struct { + Id int64 + Name string + Email string + Password string + CreatedAt time.Time + DeletedAt time.Time + UpdatedAt time.Time +} diff --git a/app/utilities.go b/app/utilities.go new file mode 100644 index 0000000..bc6c0ca --- /dev/null +++ b/app/utilities.go @@ -0,0 +1,97 @@ +package main + +import ( + "crypto/md5" + "fmt" + // "github.com/garyburd/redigo/redis" + "golang.org/x/crypto/bcrypt" + "io" + "io/ioutil" + "log" + "math/rand" + "net/http" + // "time" +) + +// Returns headers as map and the content of a webpage as string +func HttpGet(url string) (http.Header, string, error) { + response, err := http.Get(url) + if err != nil { + return nil, "Get request failed.", err + } + + defer response.Body.Close() + contents, err := ioutil.ReadAll(response.Body) + if err != nil { + return nil, "Reading body failed.", err + } + + return response.Header, string(contents), nil +} + +// Hashs and returns a string (md5) +func Md5Hash(content string) string { + h := md5.New() + io.WriteString(h, content) + hash := fmt.Sprintf("%x", h.Sum(nil)) + + return hash +} + +// Creates a random string +func RandomKey() string { + letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") + key := make([]rune, 40) + for i := range key { + key[i] = letters[rand.Intn(len(letters))] + } + + return string(key) +} + +//var pool = newPool() + +/* +// Creates a pool with connections to Redis +func newPool() *redis.Pool { + return &redis.Pool{ + MaxIdle: 3, + IdleTimeout: 240 * time.Second, + Dial: func() (redis.Conn, error) { + //c, err := redis.Dial("tcp", ":6379") + c, err := redis.Dial("tcp", revel.Config.StringDefault("redis.server", "127.0.0.1")+":"+revel.Config.StringDefault("redis.port", "6379")) + if err != nil { + return nil, err + } + return c, err + }, + TestOnBorrow: func(c redis.Conn, t time.Time) error { + _, err := c.Do("PING") + return err + }, + } +} +*/ + +// Hashs password with bcrypt and returns the string +func HashPassword(password string) (string, error) { + if password == "" { + return "", nil + } + p := []byte(password) + hash, err := bcrypt.GenerateFromPassword(p, 10) + if err != nil { + return "", err + } + return string(hash), nil +} + +// Verify password and hash +func VerifyPassword(password, hash string) bool { + err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) + if err != nil { + log.Printf("%s \n", err) + return false + } + return true +} diff --git a/db.go b/db.go deleted file mode 100644 index dc9a15d..0000000 --- a/db.go +++ /dev/null @@ -1,40 +0,0 @@ -package main - -import ( - "github.com/jinzhu/gorm" - _ "github.com/mattn/go-sqlite3" - "log" - "os" -) - -//var Db gorm.DB -var dbdriver = os.Getenv("STATUS_DB_DRIVER") -var dbcred = os.Getenv("STATUS_DB_CREDENTIALS") -var Db, dberr = gorm.Open(dbdriver, dbcred) - -func InitDB() { - /* - dbdriver := os.Getenv("STATUS_DB_DRIVER") - dbcred := os.Getenv("STATUS_DB_CREDENTIALS") - Db, err := gorm.Open(dbdriver, dbcred) - */ - if dberr != nil { - log.Panic(dberr) - } - Db.LogMode(true) - if err := Db.DB().Ping(); err != nil { - log.Panic(err) - } - - // u := User{} - h := Host{} - // Db.Debug().AutoMigrate(&u) - db := Db - log.Println(db) - db.Debug().AutoMigrate(&h) - - /* - Db.Model(&u).AddUniqueIndex("idx_user_name", "name") - Db.Model(&u).AddUniqueIndex("idx_user_email", "email") - */ -} diff --git a/fetch.go b/fetch.go deleted file mode 100644 index 27aa628..0000000 --- a/fetch.go +++ /dev/null @@ -1,9 +0,0 @@ -package main - -/* -import ( - "fmt" -) - -func -*/ diff --git a/handler.go b/handler.go deleted file mode 100644 index 16640ae..0000000 --- a/handler.go +++ /dev/null @@ -1,63 +0,0 @@ -package main - -import ( - "fmt" - "html/template" - "log" - "net/http" -) - -func IndexHandler(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "Hello World! \n") - // w.Write() -} - -func RegisterHandler(w http.ResponseWriter, r *http.Request) { - log.Println("Processing registration!") - fmt.Fprintf(w, "Processing registration! \n") - // w.Write() -} - -func PrintRegisterHandler(w http.ResponseWriter, r *http.Request) { - - fmt.Fprintf(w, "Printing register etc! \n") - // w.Write() -} - -func PrintNewJobHandler(w http.ResponseWriter, r *http.Request) { - log.Printf("Printing job") - //t := template.Must(template.ParseFiles("templates/header.html", "templates/navbar.html", "templates/jobs.html", "templates/footer.html")) - t := template.Must(template.ParseFiles("templates/jobs.html", "templates/header.html", "templates/navbar.html", "templates/footer.html")) - //t := template.New("job") - //t := templ - //fmt.Printf("%s", t) - //t, err := template.ParseFiles("templates/jobs.html", "templates/header.html") - //t, err := template.ParseFiles("templates/jobs.html") - //t, err := t.ParseFiles("templates/jobs.html") - //if err != nil { - // log.Panic(err) - //} - err := t.Execute(w, nil) - if err != nil { - log.Panic(err) - } -} - -func AddNewJobHandler(w http.ResponseWriter, r *http.Request) { - log.Printf("Add new job") - - err := r.ParseForm() - if err != nil { - log.Panic(err) - } - - host := &Host{} - err = decoder.Decode(host, r.PostForm) - if err != nil { - log.Panic(err) - } - - log.Printf("%v", host) - fmt.Fprintf(w, "%s", host.Url) - Db.Debug().Save(host) -} diff --git a/main.go b/main.go deleted file mode 100644 index fcbeaf5..0000000 --- a/main.go +++ /dev/null @@ -1,25 +0,0 @@ -package main - -import ( - "github.com/gorilla/mux" - "github.com/gorilla/schema" - //"html/template" - "net/http" -) - -var decoder = schema.NewDecoder() - -//var templ = template.Must(template.ParseFiles("templates/header.html", "templates/navbar.html", "templates/footer.html")) - -func main() { - InitDB() - - r := mux.NewRouter() - r.HandleFunc("/", IndexHandler) - r.HandleFunc("/register", RegisterHandler).Methods("POST") - r.HandleFunc("/register", PrintRegisterHandler).Methods("GET") - r.HandleFunc("/new", AddNewJobHandler).Methods("POST") - r.HandleFunc("/new", PrintNewJobHandler).Methods("GET") - http.Handle("/", r) - http.ListenAndServe(":8080", nil) -} diff --git a/struct.go b/struct.go deleted file mode 100644 index 26c462b..0000000 --- a/struct.go +++ /dev/null @@ -1,37 +0,0 @@ -package main - -import ( - "time" -) - -/* Maybe worth saving uptime history? */ - -type Host struct { - Id int64 - UserId int64 - Url string - Protocoll string // e.g. http - Private bool - Response int64 - /* - Date time.Time - Success bool - Include string // Website must include this string - Except string // Website must not include this string - Reason string // Include, Exclude, Connection failure - Alert bool // True to send alert on failure - */ - CreatedAt time.Time - DeletedAt time.Time - UpdatedAt time.Time -} - -type User struct { - Id int64 - Name string - Email string - Password string - CreatedAt time.Time - DeletedAt time.Time - UpdatedAt time.Time -} diff --git a/templates/admin.html b/templates/admin.html new file mode 100644 index 0000000..e69de29 diff --git a/templates/admin/admin.html b/templates/admin/admin.html deleted file mode 100644 index e69de29..0000000 diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..e69de29 diff --git a/templates/index/index.html b/templates/index/index.html deleted file mode 100644 index e69de29..0000000 diff --git a/templates/login.html b/templates/login.html new file mode 100644 index 0000000..e69de29 diff --git a/templates/login/login.html b/templates/login/login.html deleted file mode 100644 index e69de29..0000000 diff --git a/templates/register.html b/templates/register.html new file mode 100644 index 0000000..e69de29 diff --git a/templates/register/register.html b/templates/register/register.html deleted file mode 100644 index e69de29..0000000 diff --git a/utilities.go b/utilities.go deleted file mode 100644 index 75f153f..0000000 --- a/utilities.go +++ /dev/null @@ -1,96 +0,0 @@ -package main - -import ( - "crypto/md5" - "fmt" - // "github.com/garyburd/redigo/redis" - "golang.org/x/crypto/bcrypt" - "io" - "io/ioutil" - "math/rand" - "net/http" - // "time" -) - -// Returns headers as map and the content of a webpage as string -func HttpGet(url string) (http.Header, string, error) { - response, err := http.Get(url) - if err != nil { - return nil, "Get request failed.", err - } - - defer response.Body.Close() - contents, err := ioutil.ReadAll(response.Body) - if err != nil { - return nil, "Reading body failed.", err - } - - return response.Header, string(contents), nil -} - -// Hashs and returns a string (md5) -func Hash(content string) string { - h := md5.New() - io.WriteString(h, content) - hash := fmt.Sprintf("%x", h.Sum(nil)) - - return hash -} - -// Creates a random string -func RandomKey() string { - letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") - key := make([]rune, 40) - for i := range key { - key[i] = letters[rand.Intn(len(letters))] - } - - return string(key) -} - -//var pool = newPool() - -/* -// Creates a pool with connections to Redis -func newPool() *redis.Pool { - return &redis.Pool{ - MaxIdle: 3, - IdleTimeout: 240 * time.Second, - Dial: func() (redis.Conn, error) { - //c, err := redis.Dial("tcp", ":6379") - c, err := redis.Dial("tcp", revel.Config.StringDefault("redis.server", "127.0.0.1")+":"+revel.Config.StringDefault("redis.port", "6379")) - if err != nil { - return nil, err - } - return c, err - }, - TestOnBorrow: func(c redis.Conn, t time.Time) error { - _, err := c.Do("PING") - return err - }, - } -} -*/ - -// Hashs password with bcrypt and returns the string -func HashPassword(password string) (string, error) { - if password == "" { - return "", nil - } - p := []byte(password) - hash, err := bcrypt.GenerateFromPassword(p, 10) - if err != nil { - return "", err - } - return string(hash), nil -} - -// Verify password and hash -func VerifyPassword(password, hash string) bool { - err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) - if err != nil { - revel.ERROR.Printf("%s \n", err) - return false - } - return true -} -- cgit v1.2.3