2018-06-10 01:31:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
2018-06-10 03:44:47 +00:00
|
|
|
"strconv"
|
2018-06-10 01:31:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func RunHTTPServer() {
|
2018-06-10 01:36:21 +00:00
|
|
|
fmt.Println("Fusioner HTTP Server running on http://localhost:8080")
|
2018-06-10 01:31:13 +00:00
|
|
|
css := http.StripPrefix("/css/", http.FileServer(cssBox.HTTPBox()))
|
|
|
|
js := http.StripPrefix("/js/", http.FileServer(jsBox.HTTPBox()))
|
|
|
|
http.Handle("/", http.HandlerFunc(IndexHandler))
|
|
|
|
http.Handle("/css/", css)
|
|
|
|
http.Handle("/js/", js)
|
|
|
|
http.Handle("/setup", http.HandlerFunc(SetupHandler))
|
|
|
|
http.Handle("/setup/save", http.HandlerFunc(ProcessSetupHandler))
|
|
|
|
http.Handle("/dashboard", http.HandlerFunc(DashboardHandler))
|
|
|
|
http.Handle("/login", http.HandlerFunc(LoginHandler))
|
|
|
|
http.Handle("/logout", http.HandlerFunc(LogoutHandler))
|
|
|
|
//http.Handle("/auth", http.HandlerFunc(AuthenticateHandler))
|
2018-06-10 03:44:47 +00:00
|
|
|
http.Handle("/users/create", http.HandlerFunc(CreateUserHandler))
|
|
|
|
http.Handle("/services/create", http.HandlerFunc(CreateServiceHandler))
|
|
|
|
http.Handle("/services", http.HandlerFunc(ServicesHandler))
|
2018-06-10 01:31:13 +00:00
|
|
|
http.Handle("/users", http.HandlerFunc(UsersHandler))
|
2018-06-10 01:36:21 +00:00
|
|
|
http.ListenAndServe(":8080", nil)
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func LogoutHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
session, _ := store.Get(r, "apizer_auth")
|
|
|
|
session.Values["authenticated"] = false
|
|
|
|
session.Save(r, w)
|
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoginHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
session, _ := store.Get(r, "apizer_auth")
|
|
|
|
r.ParseForm()
|
|
|
|
username := r.PostForm.Get("username")
|
|
|
|
password := r.PostForm.Get("password")
|
2018-06-10 03:44:47 +00:00
|
|
|
user, auth := AuthUser(username, password)
|
|
|
|
fmt.Println(user)
|
|
|
|
fmt.Println(auth)
|
2018-06-10 01:31:13 +00:00
|
|
|
if auth {
|
|
|
|
session.Values["authenticated"] = true
|
|
|
|
session.Save(r, w)
|
|
|
|
http.Redirect(w, r, "/dashboard", http.StatusSeeOther)
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(502)
|
|
|
|
w.Header().Set("Content-Type", "plain/text")
|
|
|
|
fmt.Fprintln(w, "bad")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//func AuthenticateHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// r.ParseForm()
|
|
|
|
// key := r.PostForm.Get("key")
|
|
|
|
// secret := r.PostForm.Get("secret")
|
|
|
|
// token := SelectToken(key, secret)
|
|
|
|
// if token.Id != 0 {
|
|
|
|
// go token.Hit(r)
|
|
|
|
// w.WriteHeader(200)
|
|
|
|
// w.Header().Set("Content-Type", "plain/text")
|
|
|
|
// fmt.Fprintln(w, token.Id)
|
|
|
|
// } else {
|
|
|
|
// w.WriteHeader(502)
|
|
|
|
// w.Header().Set("Content-Type", "plain/text")
|
|
|
|
// fmt.Fprintln(w, "bad")
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
|
|
|
|
func CreateUserHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.ParseForm()
|
|
|
|
username := r.PostForm.Get("username")
|
|
|
|
password := r.PostForm.Get("password")
|
|
|
|
user := &User{
|
|
|
|
Username: username,
|
|
|
|
Password: password,
|
|
|
|
}
|
|
|
|
user.Create()
|
|
|
|
http.Redirect(w, r, "/users", http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateServiceHandler(w http.ResponseWriter, r *http.Request) {
|
2018-06-10 03:44:47 +00:00
|
|
|
r.ParseForm()
|
|
|
|
name := r.PostForm.Get("name")
|
|
|
|
domain := r.PostForm.Get("domain")
|
|
|
|
method := r.PostForm.Get("method")
|
|
|
|
expected := r.PostForm.Get("expected")
|
|
|
|
status, _ := strconv.Atoi(r.PostForm.Get("expected_status"))
|
|
|
|
interval, _ := strconv.Atoi(r.PostForm.Get("interval"))
|
|
|
|
|
|
|
|
service := &Service{
|
|
|
|
Name: name,
|
|
|
|
Domain: domain,
|
|
|
|
Method: method,
|
|
|
|
Expected: expected,
|
|
|
|
ExpectedStatus: status,
|
|
|
|
Interval: interval,
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(service)
|
|
|
|
|
|
|
|
service.Create()
|
2018-06-10 01:31:13 +00:00
|
|
|
http.Redirect(w, r, "/services", http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetupHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
setupFile, err := tmplBox.String("setup.html")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
setupTmpl, err := template.New("message").Parse(setupFile)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
setupTmpl.Execute(w, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
type index struct {
|
2018-06-10 04:09:20 +00:00
|
|
|
Project string
|
|
|
|
Services []*Service
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func IndexHandler(w http.ResponseWriter, r *http.Request) {
|
2018-06-10 01:47:57 +00:00
|
|
|
if core == nil {
|
2018-06-10 01:31:13 +00:00
|
|
|
http.Redirect(w, r, "/setup", http.StatusSeeOther)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-10 01:47:57 +00:00
|
|
|
indexFile, err := tmplBox.String("index.html")
|
2018-06-10 01:31:13 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-06-10 01:47:57 +00:00
|
|
|
indexTmpl, err := template.New("message").Funcs(template.FuncMap{
|
2018-06-10 01:31:13 +00:00
|
|
|
"js": func(html string) template.JS {
|
|
|
|
return template.JS(html)
|
|
|
|
},
|
2018-06-10 01:47:57 +00:00
|
|
|
}).Parse(indexFile)
|
2018-06-10 04:09:20 +00:00
|
|
|
out := index{core.Name, services}
|
2018-06-10 01:47:57 +00:00
|
|
|
indexTmpl.Execute(w, out)
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 00:20:42 +00:00
|
|
|
type dashboard struct {
|
|
|
|
Services []*Service
|
|
|
|
Core *Core
|
|
|
|
CountOnline int
|
|
|
|
CountServices int
|
|
|
|
Count24Failures int
|
|
|
|
}
|
|
|
|
|
2018-06-10 01:31:13 +00:00
|
|
|
func DashboardHandler(w http.ResponseWriter, r *http.Request) {
|
2018-06-10 03:44:47 +00:00
|
|
|
session, _ := store.Get(r, "apizer_auth")
|
|
|
|
if auth, ok := session.Values["authenticated"].(bool); !ok || !auth {
|
|
|
|
loginFile, err := tmplBox.String("login.html")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
loginTmpl, err := template.New("message").Parse(loginFile)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
loginTmpl.Execute(w, nil)
|
|
|
|
} else {
|
|
|
|
dashboardFile, err := tmplBox.String("dashboard.html")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
dashboardTmpl, err := template.New("message").Parse(dashboardFile)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-06-11 00:20:42 +00:00
|
|
|
out := dashboard{services, core, CountOnline(), len(services), CountFailures()}
|
2018-06-10 03:44:47 +00:00
|
|
|
dashboardTmpl.Execute(w, out)
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func ServicesHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
session, _ := store.Get(r, "apizer_auth")
|
|
|
|
if auth, ok := session.Values["authenticated"].(bool); !ok || !auth {
|
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tokensFile, err := tmplBox.String("services.html")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
tokensTmpl, err := template.New("message").Parse(tokensFile)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-06-10 04:09:20 +00:00
|
|
|
tokensTmpl.Execute(w, services)
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func UsersHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
session, _ := store.Get(r, "apizer_auth")
|
|
|
|
if auth, ok := session.Values["authenticated"].(bool); !ok || !auth {
|
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
usersFile, err := tmplBox.String("users.html")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
usersTmpl, err := template.New("message").Parse(usersFile)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
usersTmpl.Execute(w, SelectAllUsers())
|
|
|
|
}
|
|
|
|
|
|
|
|
func PermissionsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
session, _ := store.Get(r, "apizer_auth")
|
|
|
|
if auth, ok := session.Values["authenticated"].(bool); !ok || !auth {
|
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
permsFile, err := tmplBox.String("permissions.html")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
permsTmpl, err := template.New("message").Parse(permsFile)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
permsTmpl.Execute(w, SelectAllUsers())
|
|
|
|
}
|