create basic frontend

This commit is contained in:
sentriz
2019-04-15 23:59:04 +01:00
parent 87efb3b3c5
commit 64fb0fdf82
17 changed files with 372 additions and 22 deletions

77
handler/admin.go Normal file
View File

@@ -0,0 +1,77 @@
package handler
import (
"net/http"
"github.com/sentriz/gonic/db"
)
func (c *Controller) ServeLogin(w http.ResponseWriter, r *http.Request) {
session, _ := c.SStore.Get(r, "gonic")
renderTemplate(w, r, session, "login", &templateData{})
}
func (c *Controller) ServeAuthenticate(w http.ResponseWriter, r *http.Request) {
session, _ := c.SStore.Get(r, "gonic")
username := r.FormValue("username")
password := r.FormValue("password")
if username == "" || password == "" {
session.AddFlash("please provide both a username and password")
session.Save(r, w)
http.Redirect(w, r, "/admin/login", 303)
return
}
var user db.User
c.DB.Where("name = ?", username).First(&user)
if !(username == user.Name && password == user.Password) {
session.AddFlash("invalid username / password")
session.Save(r, w)
http.Redirect(w, r, "/admin/login", 303)
return
}
session.Values["authenticated"] = true
session.Values["user"] = user.ID
session.Save(r, w)
http.Redirect(w, r, "/admin/home", 303)
}
func (c *Controller) ServeHome(w http.ResponseWriter, r *http.Request) {
session, _ := c.SStore.Get(r, "gonic")
authed, _ := session.Values["authenticated"].(bool)
if !authed {
session.AddFlash("you are not authenticated")
session.Save(r, w)
http.Redirect(w, r, "/admin/login", 303)
return
}
var data templateData
var user db.User
c.DB.First(&user, session.Values["user"])
data.UserID = user.ID
data.Username = user.Name
c.DB.Table("album_artists").Count(&data.ArtistCount)
c.DB.Table("albums").Count(&data.AlbumCount)
c.DB.Table("tracks").Count(&data.TrackCount)
c.DB.Find(&data.Users)
renderTemplate(w, r, session, "home", &data)
}
func (c *Controller) ServeCreateUser(w http.ResponseWriter, r *http.Request) {
session, _ := c.SStore.Get(r, "gonic")
authed, _ := session.Values["authenticated"].(bool)
if !authed {
session.AddFlash("you are not authenticated")
session.Save(r, w)
http.Redirect(w, r, "/admin/login", 303)
return
}
renderTemplate(w, r, session, "create_user", &templateData{})
}
func (c *Controller) ServeLogout(w http.ResponseWriter, r *http.Request) {
session, _ := c.SStore.Get(r, "gonic")
delete(session.Values, "authenticated")
delete(session.Values, "user")
session.Save(r, w)
http.Redirect(w, r, "/admin/login", 303)
}

View File

@@ -4,16 +4,55 @@ import (
"encoding/json"
"encoding/xml"
"fmt"
"html/template"
"log"
"net/http"
"path/filepath"
"strconv"
"github.com/jinzhu/gorm"
"github.com/gorilla/sessions"
"github.com/sentriz/gonic/db"
"github.com/sentriz/gonic/subsonic"
"github.com/jinzhu/gorm"
"github.com/wader/gormstore"
)
var (
templates = make(map[string]*template.Template)
)
func init() {
templates["login"] = template.Must(template.ParseFiles(
filepath.Join("templates", "layout.tmpl"),
filepath.Join("templates", "admin", "login.tmpl"),
))
templates["home"] = template.Must(template.ParseFiles(
filepath.Join("templates", "layout.tmpl"),
filepath.Join("templates", "user.tmpl"),
filepath.Join("templates", "admin", "home.tmpl"),
))
templates["create_user"] = template.Must(template.ParseFiles(
filepath.Join("templates", "layout.tmpl"),
filepath.Join("templates", "user.tmpl"),
filepath.Join("templates", "admin", "create_user.tmpl"),
))
}
type Controller struct {
DB *gorm.DB
DB *gorm.DB
SStore *gormstore.Store
Templates map[string]*template.Template
}
type templateData struct {
Flashes []interface{}
UserID uint
Username string
ArtistCount uint
AlbumCount uint
TrackCount uint
Users []*db.User
}
func getStrParam(r *http.Request, key string) string {
@@ -40,7 +79,8 @@ func getIntParamOr(r *http.Request, key string, or int) int {
return val
}
func respondRaw(w http.ResponseWriter, r *http.Request, code int, sub *subsonic.Response) {
func respondRaw(w http.ResponseWriter, r *http.Request,
code int, sub *subsonic.Response) {
res := subsonic.MetaResponse{
Response: sub,
}
@@ -73,12 +113,26 @@ func respondRaw(w http.ResponseWriter, r *http.Request, code int, sub *subsonic.
}
}
func respond(w http.ResponseWriter, r *http.Request, sub *subsonic.Response) {
func respond(w http.ResponseWriter, r *http.Request,
sub *subsonic.Response) {
respondRaw(w, r, http.StatusOK, sub)
}
func respondError(w http.ResponseWriter, r *http.Request, code uint64, message string) {
func respondError(w http.ResponseWriter, r *http.Request,
code uint64, message string) {
respondRaw(w, r, http.StatusBadRequest, subsonic.NewError(
code, message,
))
}
func renderTemplate(w http.ResponseWriter, r *http.Request,
s *sessions.Session, name string, data *templateData) {
flashes := s.Flashes()
s.Save(r, w)
data.Flashes = flashes
err := templates[name].ExecuteTemplate(w, "layout", data)
if err != nil {
http.Error(w, fmt.Sprintf("500 when executing: %v", err), 500)
return
}
}

View File

@@ -62,7 +62,7 @@ func (c *Controller) CheckParameters(next http.HandlerFunc) http.HandlerFunc {
return
}
user := db.User{
Username: username,
Name: username,
}
err := c.DB.Where(user).First(&user).Error
if gorm.IsRecordNotFoundError(err) {