websocket hinzugefügt
This commit is contained in:
parent
fae7eec5ea
commit
fc139036c0
|
@ -9,6 +9,6 @@ TODO::
|
|||
* ~~Manuell (einzeln)~~
|
||||
* CSV-Import
|
||||
* ~~Kofis löschen~~
|
||||
* Konfis auf der Frontseite mit Websockets updaten
|
||||
* ~~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
|
|
@ -13,4 +13,3 @@ type Template struct {
|
|||
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
|
||||
return t.templates.ExecuteTemplate(w, name, data)
|
||||
}
|
||||
|
||||
|
|
4
add.go
4
add.go
|
@ -16,8 +16,8 @@ func addKonfi(c echo.Context) error {
|
|||
sess := GlobalSessions.SessionStart(rw, r)
|
||||
logged := sess.Get("login")
|
||||
|
||||
//Wenn das password stimmt
|
||||
if logged != nil{
|
||||
//Wenn eingeloggt
|
||||
if logged != nil {
|
||||
kofi := new(Kofi)
|
||||
kofi.Name = c.FormValue("name")
|
||||
kofi.Gemeinde = c.FormValue("gemeinde")
|
||||
|
|
2
admin.go
2
admin.go
|
@ -9,7 +9,7 @@ type Loggedin struct {
|
|||
Loggedin bool
|
||||
}
|
||||
|
||||
func adminHandler (c echo.Context) error {
|
||||
func adminHandler(c echo.Context) error {
|
||||
rw := c.Response()
|
||||
r := c.Request()
|
||||
|
||||
|
|
|
@ -6,4 +6,24 @@ setInterval(function() {
|
|||
$( "#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);
|
||||
};*/
|
|
@ -8,8 +8,8 @@ import (
|
|||
//Configuration Struct
|
||||
type Configuration struct {
|
||||
AdminPassword string
|
||||
Interface string
|
||||
DBFile string
|
||||
Interface string
|
||||
DBFile string
|
||||
}
|
||||
|
||||
var SiteConf Configuration = Configuration{}
|
||||
|
|
12
db.go
12
db.go
|
@ -1,15 +1,15 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"github.com/go-xorm/xorm"
|
||||
"github.com/go-xorm/core"
|
||||
"fmt"
|
||||
"github.com/go-xorm/core"
|
||||
"github.com/go-xorm/xorm"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
var db *xorm.Engine
|
||||
|
||||
func DBinit() *xorm.Engine{
|
||||
func DBinit() *xorm.Engine {
|
||||
SiteConf := initConfig()
|
||||
db, err := xorm.NewEngine("sqlite3", SiteConf.DBFile)
|
||||
if err != nil {
|
||||
|
@ -17,7 +17,7 @@ func DBinit() *xorm.Engine{
|
|||
}
|
||||
|
||||
db.SetMapper(core.SameMapper{})
|
||||
db.ShowSQL(true)
|
||||
db.ShowSQL(false)
|
||||
db.Logger().SetLevel(core.LOG_DEBUG)
|
||||
return db
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ func deleteKonfi(c echo.Context) error {
|
|||
logged := sess.Get("login")
|
||||
|
||||
//Wenn das password stimmt
|
||||
if logged != nil{
|
||||
if logged != nil {
|
||||
id, _ := strconv.Atoi(c.FormValue("id"))
|
||||
|
||||
//Löschen
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func getList(c echo.Context) error {
|
||||
|
|
3
login.go
3
login.go
|
@ -17,12 +17,11 @@ func login(c echo.Context) error {
|
|||
var pass string = c.FormValue("password")
|
||||
|
||||
//Wenn das password stimmt, einloggen
|
||||
if SiteConf.AdminPassword == pass{
|
||||
if SiteConf.AdminPassword == pass {
|
||||
sess.Set("login", true)
|
||||
return c.JSON(http.StatusOK, Message{"success"})
|
||||
} else {
|
||||
return c.JSON(http.StatusOK, Message{"fail"})
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
5
main.go
5
main.go
|
@ -3,12 +3,12 @@ package main
|
|||
import (
|
||||
"github.com/labstack/echo"
|
||||
"github.com/labstack/echo/middleware"
|
||||
"html/template"
|
||||
"github.com/labstack/gommon/log"
|
||||
"html/template"
|
||||
|
||||
"fmt"
|
||||
"github.com/astaxie/session"
|
||||
_ "github.com/astaxie/session/providers/memory"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
//Initialize Session
|
||||
|
@ -64,6 +64,7 @@ func main() {
|
|||
e.GET("/list", getList)
|
||||
e.GET("/admin", adminHandler)
|
||||
e.GET("/logout", logout)
|
||||
e.GET("/ws", ws)
|
||||
|
||||
e.POST("/login", login)
|
||||
e.POST("/update", update)
|
||||
|
|
|
@ -5,7 +5,7 @@ import (
|
|||
"net/http"
|
||||
)
|
||||
|
||||
func showList (c echo.Context) error {
|
||||
func showList(c echo.Context) error {
|
||||
//Template
|
||||
return c.Render(http.StatusOK, "index", Message{"schinken"})
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ func update(c echo.Context) error {
|
|||
logged := sess.Get("login")
|
||||
|
||||
//Wenn das password stimmt
|
||||
if logged != nil{
|
||||
if logged != nil {
|
||||
id, _ := strconv.Atoi(c.FormValue("id"))
|
||||
addcoins, _ := strconv.Atoi(c.FormValue("addcoins"))
|
||||
|
||||
|
|
10
utils.go
10
utils.go
|
@ -3,10 +3,10 @@ package main
|
|||
import "log"
|
||||
|
||||
type Kofi struct {
|
||||
ID int `xorm:"pk autoincr"`
|
||||
Name string
|
||||
ID int `xorm:"pk autoincr"`
|
||||
Name string
|
||||
Gemeinde string
|
||||
KCoins int
|
||||
KCoins int
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
|
@ -15,7 +15,7 @@ type Message struct {
|
|||
|
||||
type UpdatedMessage struct {
|
||||
Message string
|
||||
Kofi Kofi
|
||||
Kofi Kofi
|
||||
}
|
||||
|
||||
//CheckError
|
||||
|
@ -23,4 +23,4 @@ func checkErr(err error) {
|
|||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue