Getting things going

This commit is contained in:
Brian Buller 2017-10-21 17:47:01 -05:00
parent 1905f2c009
commit 0850550be4
12 changed files with 59 additions and 37 deletions

View File

@ -11,7 +11,10 @@ func handleAdminClients(w http.ResponseWriter, req *http.Request, page *pageData
vars := mux.Vars(req)
page.SubTitle = "Clients"
clientId := vars["id"]
client := m.GetClient(clientId)
client, err := m.GetClient(clientId)
if err != nil {
client = NewClient(clientId)
}
clientIp, _, _ := net.SplitHostPort(req.RemoteAddr)
if clientId == "" {
type clientsPageData struct {

View File

@ -51,6 +51,7 @@ func handleAdminTeams(w http.ResponseWriter, req *http.Request, page *pageData)
mbrName := req.FormValue("newmembername")
mbr, err := NewTeamMember(tm.UUID, "")
if err == nil {
mbr.Name = mbrName
mbr.SlackId = req.FormValue("newmemberslackid")
mbr.Twitter = req.FormValue("newmembertwitter")
mbr.Email = req.FormValue("newmemberemail")

View File

@ -12113,16 +12113,16 @@ XzHfrrS2uzLN6JsOkTvo3Iny7fxf2KYCxs3I30osvf4OAAD//yR+jxHtAwAA
"/templates/admin-votes.html": {
local: "templates/admin-votes.html",
size: 837,
modtime: 1506689947,
size: 848,
modtime: 1508506407,
compressed: `
H4sIAAAJbogA/3xSTW/cIBC9768YWXusbam9RRip2lxy6SFd9Y7NJIuKwYJZS1WU/94Bm5Av5cTM4w3z
5g1CmxUmq2IcmoDxaim2k3ekjMPQyIO4fJenawjoCO63e9Ezdnh6gqDcI8LRfIPjCjcDdGecF6sIbxWp
bmfD8/MBoLIp0SnzjytXqHmnZBJD98r9ZeQmp7R2v9SMnIsxQC+3p9DpVFMj0fMYLJbUaBGMHprVE8Y2
502ZL/qwEZZrwPZ92I4+aAyoYeJh8/AAgi6otMzyBIUtyLA8mxkjqXkRPWevLk7WJLfubt9fpMmMe4wV
5yi/mZC9j6DR63+yGPK1xz+t/ZMGLQ6+kajlZuiL0ORiT/ojY1N8pz8h7CEn3takCpuSsqms83TxZqpy
9kprcp+p7rJn6PVj+xpfKvrarAoqZr2tYLQYJujBe/pkWbr8AIsPxN/B27goNzQ/miTMooPutyHsipdw
9qQs5PRjfz63PhykjyMP/wMAAP//x7l0qEUDAAA=
H4sIAAAJbogA/3xSPW/cMAzd/SsI48b6DLRbIAsoLkuWDsGhu2wxOaGyZEg8A0WQ/15KtqPcRzOJfHoU
Hx8ltJlhsCrGrg4Yz5ZiM3hHyjgMtazE6bs8nENAR/C83IuWsertDYJyrwg78w12Mzx0sD/iOFlF+KhI
7Vc2vL9XAIVNiU6Zv5u5Qo0rJZMYelbuDyMPOaV5/0uNyLnoA7RyeQqdTjUlEi2PwWJJ9RbB6K6ePWFs
cl5v80UfFsJ0Dthch03vg8aAGgYeNg8PIOiESsssT1BYggzLoxkxkhon0XL26eJgTXLr6fH6Ik1m3Gss
OEf5zYSsfQT1Xv+VmyFfe/zT2t9p0M3BC4laLoZ+CE0utqRvGYviJ32HsIaceFuSImxIyoZtnYeTN0OR
s1Zak/sMZZctQ58fW9f4UdGWZkXQZtZlBaObYYJevKc7y9LbD7D4QvwdvI2Tcl39o07CLLr/2wpHT8pC
Tm+l8Lm05CD9IVn9CwAA//8Mc47HUAMAAA==
`,
},

24
main.go
View File

@ -58,7 +58,6 @@ type menuItem struct {
var sessionSecret = "JCOP5e8ohkTcOzcSMe74"
var sessionStore = sessions.NewCookieStore([]byte(sessionSecret))
var site *siteData
var r *mux.Router
var m *model
@ -69,7 +68,7 @@ func main() {
}
loadConfig()
if err = site.SaveToDB(); err != nil {
if err = m.site.SaveToDB(); err != nil {
errorExit("Unable to save site config to DB: " + err.Error())
}
initialize()
@ -109,8 +108,8 @@ func main() {
chain := alice.New(loggingHandler).Then(r)
fmt.Printf("Listening on port %d\n", site.Port)
log.Fatal(http.ListenAndServe("127.0.0.1:"+strconv.Itoa(site.Port), chain))
fmt.Printf("Listening on port %d\n", m.site.Port)
log.Fatal(http.ListenAndServe("127.0.0.1:"+strconv.Itoa(m.site.Port), chain))
}
func loadConfig() {
@ -187,6 +186,7 @@ func initialize() {
gjName, _ := reader.ReadString('\n')
gjName = strings.TrimSpace(gjName)
m.jam.Name = gjName
assertError(m.jam.SaveToDB())
}
if m.jam.Name != "" {
@ -201,14 +201,14 @@ func loggingHandler(h http.Handler) http.Handler {
}
func InitPageData(w http.ResponseWriter, req *http.Request) *pageData {
if site.DevMode {
if m.site.DevMode {
w.Header().Set("Cache-Control", "no-cache")
}
p := new(pageData)
// Get session
var err error
var s *sessions.Session
if s, err = sessionStore.Get(req, site.SessionName); err != nil {
if s, err = sessionStore.Get(req, m.site.SessionName); err != nil {
http.Error(w, err.Error(), 500)
return p
}
@ -222,7 +222,7 @@ func InitPageData(w http.ResponseWriter, req *http.Request) *pageData {
// With a valid account
p.LoggedIn = m.isValidUserEmail(userEmail)
p.Site = site
p.Site = m.site
p.SubTitle = "GameJam Voting"
p.Stylesheets = make([]string, 0, 0)
p.Stylesheets = append(p.Stylesheets, "/assets/vendor/css/pure-min.css")
@ -258,7 +258,11 @@ func InitPageData(w http.ResponseWriter, req *http.Request) *pageData {
p.HideAdminMenu = true
p.ClientId = p.session.getClientId()
cl := m.GetClient(p.ClientId)
var cl *Client
if cl, err = m.GetClient(p.ClientId); err != nil {
// A new client
cl = NewClient(p.ClientId)
}
p.ClientIsAuth = cl.Auth
p.ClientIsServer = clientIsServer(req)
@ -290,8 +294,8 @@ func (p *pageData) show(tmplName string, w http.ResponseWriter) error {
// Spit out a template
func outputTemplate(tmplName string, tmplData interface{}, w http.ResponseWriter) error {
n := "/templates/" + tmplName
l := template.Must(template.New("layout").Parse(FSMustString(site.DevMode, n)))
t := template.Must(l.Parse(FSMustString(site.DevMode, n)))
l := template.Must(template.New("layout").Parse(FSMustString(m.site.DevMode, n)))
t := template.Must(l.Parse(FSMustString(m.site.DevMode, n)))
return t.Execute(w, tmplData)
}

View File

@ -127,23 +127,23 @@ func (m *model) SaveClient(cl *Client) error {
*/
// Return a client by it's UUID
func (m *model) GetClient(id string) *Client {
func (m *model) GetClient(id string) (*Client, error) {
for i := range m.clients {
if m.clients[i].UUID == id {
return &m.clients[i]
return &m.clients[i], nil
}
}
return nil
return nil, errors.New("Invalid Id")
}
// Return a client by it's IP address
func (m *model) GetClientByIp(ip string) *Client {
func (m *model) GetClientByIp(ip string) (*Client, error) {
for i := range m.clients {
if m.clients[i].IP == ip {
return &m.clients[i]
return &m.clients[i], nil
}
}
return nil
return nil, errors.New("Invalid Ip")
}
// Add/Update a client in the data model

View File

@ -61,6 +61,9 @@ func (gj *Gamejam) SaveToDB() error {
defer gj.m.closeDB()
var errs []error
if err := gj.m.bolt.SetValue(gj.mPath, "name", gj.Name); err != nil {
errs = append(errs, err)
}
// Save all Teams
for _, tm := range gj.Teams {
if err := gj.SaveTeam(&tm); err != nil {

View File

@ -3,6 +3,7 @@ package main
import (
"errors"
"strconv"
"strings"
)
/**
@ -59,16 +60,16 @@ func (s *siteData) LoadFromDB() error {
}
defer s.m.closeDB()
if title, err := s.m.bolt.GetValue(s.mPath, "title"); err == nil {
if title, _ := s.m.bolt.GetValue(s.mPath, "title"); strings.TrimSpace(title) != "" {
s.Title = title
}
if port, err := s.m.bolt.GetInt(s.mPath, "port"); err == nil {
s.Port = port
}
if sessionName, err := s.m.bolt.GetValue(s.mPath, "session-name"); err == nil {
if sessionName, _ := s.m.bolt.GetValue(s.mPath, "session-name"); strings.TrimSpace(sessionName) != "" {
s.SessionName = sessionName
}
if serverDir, err := s.m.bolt.GetValue(s.mPath, "server-dir"); err == nil {
if serverDir, _ := s.m.bolt.GetValue(s.mPath, "server-dir"); strings.TrimSpace(serverDir) != "" {
s.ServerDir = serverDir
}
s.changed = false

View File

@ -2,6 +2,7 @@ package main
import (
"errors"
"fmt"
"github.com/pborman/uuid"
)
@ -23,8 +24,11 @@ func NewTeam(id string) *Team {
if id == "" {
id = uuid.New()
}
// Create an emtpy game for the team
gm, _ := NewGame(id)
return &Team{
UUID: id,
Game: gm,
mPath: []string{"jam", "teams", id},
}
}
@ -114,7 +118,8 @@ func (gj *Gamejam) LoadAllTeams() []Team {
var tmUUIDs []string
tmsPath := append(gj.mPath, "teams")
if tmUUIDs, err = m.bolt.GetBucketList(tmsPath); err != nil {
if tmUUIDs, err = gj.m.bolt.GetBucketList(tmsPath); err != nil {
fmt.Println(err.Error())
return ret
}
for _, v := range tmUUIDs {
@ -276,10 +281,10 @@ func (gj *Gamejam) DeleteTeamMember(tm *Team, mbr *TeamMember) error {
// Add a team
func (gj *Gamejam) AddTeam(tm *Team) error {
if _, err := gj.GetTeamById(tm.UUID); err != nil {
if _, err := gj.GetTeamById(tm.UUID); err == nil {
return errors.New("A team with that ID already exists")
}
if _, err := gj.GetTeamByName(tm.Name); err != nil {
if _, err := gj.GetTeamByName(tm.Name); err == nil {
return errors.New("A team with that Name already exists")
}
gj.Teams = append(gj.Teams, *tm)

View File

@ -85,13 +85,13 @@ func (gj *Gamejam) LoadAllVotes() []Vote {
votesPath := []string{"jam", "votes"}
var cliUUIDs []string
if cliUUIDs, err = m.bolt.GetBucketList(votesPath); err != nil {
if cliUUIDs, err = gj.m.bolt.GetBucketList(votesPath); err != nil {
return ret
}
for _, cId := range cliUUIDs {
vtsPth := append(votesPath, cId)
var times []string
if times, err = m.bolt.GetBucketList(vtsPth); err != nil {
if times, err = gj.m.bolt.GetBucketList(vtsPth); err != nil {
// Error reading this bucket, move on to the next
continue
}

View File

@ -43,7 +43,9 @@ func (p *pageSession) getClientId() string {
fmt.Println(" Client IP:" + clientIp)
if clientIp != "127.0.0.1" {
fmt.Println(" Pulling data by IP")
cli = m.GetClientByIp(clientIp)
if cli, err = m.GetClientByIp(clientIp); err != nil {
cli = NewClient(clientId)
}
}
if cli != nil {
clientId = cli.UUID

View File

@ -71,7 +71,10 @@ func handlePublicSaveVote(w http.ResponseWriter, req *http.Request) {
fmt.Println("Error parsing timestamp: " + ts)
redirect("/", w, req)
}
client := m.GetClient(page.ClientId)
client, err := m.GetClient(page.ClientId)
if err != nil {
client = NewClient(page.ClientId)
}
// voteSlice is an ordered string slice of the voters preferences
voteCSV := req.FormValue("uservote")

View File

@ -31,7 +31,7 @@
</tbody>
<tfoot>
<tr>
<td class="left" colspan="3">{{ len .Site.Votes }} Total Votes</td>
<td class="left" colspan="3">{{ len .TemplateData.AllVotes }} Total Votes</td>
</tr>
</tfoot>
</table>