websocket hinzugefügt

This commit is contained in:
konrad 2017-08-30 22:23:35 +02:00 committed by kolaente
parent fae7eec5ea
commit fc139036c0
15 changed files with 87 additions and 27 deletions

View File

@ -9,6 +9,6 @@ TODO::
* ~~Manuell (einzeln)~~ * ~~Manuell (einzeln)~~
* CSV-Import * CSV-Import
* ~~Kofis löschen~~ * ~~Kofis löschen~~
* Konfis auf der Frontseite mit Websockets updaten
* ~~Logout~~ * ~~Logout~~
* Konfis auf der Frontseite mit Websockets updaten
* Mode hinzufügen: in der Config soll man zwischen Gemeinden und Konfis umschalten können, so dass entweder die Gemeinden oder Konfis gegeneinander spielen * Mode hinzufügen: in der Config soll man zwischen Gemeinden und Konfis umschalten können, so dass entweder die Gemeinden oder Konfis gegeneinander spielen

View File

@ -13,4 +13,3 @@ type Template struct {
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error { func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data) return t.templates.ExecuteTemplate(w, name, data)
} }

4
add.go
View File

@ -16,8 +16,8 @@ func addKonfi(c echo.Context) error {
sess := GlobalSessions.SessionStart(rw, r) sess := GlobalSessions.SessionStart(rw, r)
logged := sess.Get("login") logged := sess.Get("login")
//Wenn das password stimmt //Wenn eingeloggt
if logged != nil{ if logged != nil {
kofi := new(Kofi) kofi := new(Kofi)
kofi.Name = c.FormValue("name") kofi.Name = c.FormValue("name")
kofi.Gemeinde = c.FormValue("gemeinde") kofi.Gemeinde = c.FormValue("gemeinde")

View File

@ -9,7 +9,7 @@ type Loggedin struct {
Loggedin bool Loggedin bool
} }
func adminHandler (c echo.Context) error { func adminHandler(c echo.Context) error {
rw := c.Response() rw := c.Response()
r := c.Request() r := c.Request()

View File

@ -6,4 +6,24 @@ setInterval(function() {
$( "#konfis" ).append('<tr> <td>' + item.Name + '</td> <td>' + item.Gemeinde + '</td> <td>' + item.KCoins + '</td></tr>'); $( "#konfis" ).append('<tr> <td>' + item.Name + '</td> <td>' + item.Gemeinde + '</td> <td>' + item.KCoins + '</td></tr>');
}); });
}); });
}, 1000); }, 1000);
/*
var loc = window.location;
var uri = 'ws:';
if (loc.protocol === 'https:') {
uri = 'wss:';
}
uri += '//' + loc.host;
uri += loc.pathname + 'ws';
ws = new WebSocket(uri)
ws.onopen = function() {
console.log('Connected');
ws.send('get');
};
ws.onmessage = function(evt) {
console.log(evt.data);
};*/

View File

@ -8,8 +8,8 @@ import (
//Configuration Struct //Configuration Struct
type Configuration struct { type Configuration struct {
AdminPassword string AdminPassword string
Interface string Interface string
DBFile string DBFile string
} }
var SiteConf Configuration = Configuration{} var SiteConf Configuration = Configuration{}

12
db.go
View File

@ -1,15 +1,15 @@
package main package main
import ( import (
_ "github.com/mattn/go-sqlite3"
"github.com/go-xorm/xorm"
"github.com/go-xorm/core"
"fmt" "fmt"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
_ "github.com/mattn/go-sqlite3"
) )
var db *xorm.Engine var db *xorm.Engine
func DBinit() *xorm.Engine{ func DBinit() *xorm.Engine {
SiteConf := initConfig() SiteConf := initConfig()
db, err := xorm.NewEngine("sqlite3", SiteConf.DBFile) db, err := xorm.NewEngine("sqlite3", SiteConf.DBFile)
if err != nil { if err != nil {
@ -17,7 +17,7 @@ func DBinit() *xorm.Engine{
} }
db.SetMapper(core.SameMapper{}) db.SetMapper(core.SameMapper{})
db.ShowSQL(true) db.ShowSQL(false)
db.Logger().SetLevel(core.LOG_DEBUG) db.Logger().SetLevel(core.LOG_DEBUG)
return db return db
} }

View File

@ -18,7 +18,7 @@ func deleteKonfi(c echo.Context) error {
logged := sess.Get("login") logged := sess.Get("login")
//Wenn das password stimmt //Wenn das password stimmt
if logged != nil{ if logged != nil {
id, _ := strconv.Atoi(c.FormValue("id")) id, _ := strconv.Atoi(c.FormValue("id"))
//Löschen //Löschen

View File

@ -1,9 +1,9 @@
package main package main
import ( import (
"fmt"
"github.com/labstack/echo" "github.com/labstack/echo"
"net/http" "net/http"
"fmt"
) )
func getList(c echo.Context) error { func getList(c echo.Context) error {

View File

@ -17,12 +17,11 @@ func login(c echo.Context) error {
var pass string = c.FormValue("password") var pass string = c.FormValue("password")
//Wenn das password stimmt, einloggen //Wenn das password stimmt, einloggen
if SiteConf.AdminPassword == pass{ if SiteConf.AdminPassword == pass {
sess.Set("login", true) sess.Set("login", true)
return c.JSON(http.StatusOK, Message{"success"}) return c.JSON(http.StatusOK, Message{"success"})
} else { } else {
return c.JSON(http.StatusOK, Message{"fail"}) return c.JSON(http.StatusOK, Message{"fail"})
} }
} }

View File

@ -3,12 +3,12 @@ package main
import ( import (
"github.com/labstack/echo" "github.com/labstack/echo"
"github.com/labstack/echo/middleware" "github.com/labstack/echo/middleware"
"html/template"
"github.com/labstack/gommon/log" "github.com/labstack/gommon/log"
"html/template"
"fmt"
"github.com/astaxie/session" "github.com/astaxie/session"
_ "github.com/astaxie/session/providers/memory" _ "github.com/astaxie/session/providers/memory"
"fmt"
) )
//Initialize Session //Initialize Session
@ -64,6 +64,7 @@ func main() {
e.GET("/list", getList) e.GET("/list", getList)
e.GET("/admin", adminHandler) e.GET("/admin", adminHandler)
e.GET("/logout", logout) e.GET("/logout", logout)
e.GET("/ws", ws)
e.POST("/login", login) e.POST("/login", login)
e.POST("/update", update) e.POST("/update", update)

View File

@ -5,7 +5,7 @@ import (
"net/http" "net/http"
) )
func showList (c echo.Context) error { func showList(c echo.Context) error {
//Template //Template
return c.Render(http.StatusOK, "index", Message{"schinken"}) return c.Render(http.StatusOK, "index", Message{"schinken"})
} }

View File

@ -18,7 +18,7 @@ func update(c echo.Context) error {
logged := sess.Get("login") logged := sess.Get("login")
//Wenn das password stimmt //Wenn das password stimmt
if logged != nil{ if logged != nil {
id, _ := strconv.Atoi(c.FormValue("id")) id, _ := strconv.Atoi(c.FormValue("id"))
addcoins, _ := strconv.Atoi(c.FormValue("addcoins")) addcoins, _ := strconv.Atoi(c.FormValue("addcoins"))

View File

@ -3,10 +3,10 @@ package main
import "log" import "log"
type Kofi struct { type Kofi struct {
ID int `xorm:"pk autoincr"` ID int `xorm:"pk autoincr"`
Name string Name string
Gemeinde string Gemeinde string
KCoins int KCoins int
} }
type Message struct { type Message struct {
@ -15,7 +15,7 @@ type Message struct {
type UpdatedMessage struct { type UpdatedMessage struct {
Message string Message string
Kofi Kofi Kofi Kofi
} }
//CheckError //CheckError
@ -23,4 +23,4 @@ func checkErr(err error) {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

41
ws.go Normal file
View File

@ -0,0 +1,41 @@
package main
import (
"github.com/labstack/echo"
"golang.org/x/net/websocket"
"fmt"
"encoding/json"
)
func ws(c echo.Context) error {
//Datenbankverbindung aufbauen
db := DBinit()
// Websocket
websocket.Handler(func(ws *websocket.Conn) {
defer ws.Close()
for {
msg := ""
err := websocket.Message.Receive(ws, &msg)
if err != nil {
c.Logger().Error(err)
}
fmt.Println(msg)
//Daten holen und anzeigen
var kofi []Kofi
err = db.OrderBy("KCoins DESC").Find(&kofi)
if err != nil {
fmt.Println(err)
}
kofiJson, err := json.Marshal(kofi)
// Wegschicken
err = websocket.Message.Send(ws, string(kofiJson))
if err != nil {
c.Logger().Error(err)
}
}
}).ServeHTTP(c.Response(), c.Request())
return nil
}