This commit is contained in:
parent
fa9e254240
commit
4de30c61a0
2
Makefile
2
Makefile
|
@ -19,7 +19,7 @@ GOFMT ?= gofmt -s
|
||||||
GOFLAGS := -i -v
|
GOFLAGS := -i -v
|
||||||
EXTRA_GOFLAGS ?=
|
EXTRA_GOFLAGS ?=
|
||||||
|
|
||||||
LDFLAGS := -X "main.Version=$(shell git describe --tags --always | sed 's/-/+/' | sed 's/^v//')" -X "main.Tags=$(TAGS)"
|
LDFLAGS := -X "models.Version=$(shell git describe --tags --always | sed 's/-/+/' | sed 's/^v//')" -X "main.Tags=$(TAGS)"
|
||||||
|
|
||||||
PACKAGES ?= $(filter-out git.mowie.cc/konrad/Konfi-Castle-Kasino/integrations,$(shell go list ./... | grep -v /vendor/))
|
PACKAGES ?= $(filter-out git.mowie.cc/konrad/Konfi-Castle-Kasino/integrations,$(shell go list ./... | grep -v /vendor/))
|
||||||
SOURCES ?= $(shell find . -name "*.go" -type f)
|
SOURCES ?= $(shell find . -name "*.go" -type f)
|
||||||
|
|
|
@ -8,10 +8,8 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A single Broker will be created in this program. It is responsible
|
// Broker is is responsible for keeping a list of which clients (browsers)
|
||||||
// for keeping a list of which clients (browsers) are currently attached
|
// are currently attached and broadcasting events (messages) to those clients.
|
||||||
// and broadcasting events (messages) to those clients.
|
|
||||||
//
|
|
||||||
type Broker struct {
|
type Broker struct {
|
||||||
|
|
||||||
// Create a map of clients, the keys of the map are the channels
|
// Create a map of clients, the keys of the map are the channels
|
||||||
|
@ -38,8 +36,10 @@ type Broker struct {
|
||||||
init func(c echo.Context) (interface{}, error)
|
init func(c echo.Context) (interface{}, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MessageKind represents a message kind
|
||||||
type MessageKind string
|
type MessageKind string
|
||||||
|
|
||||||
|
// These are all valid message kinds
|
||||||
const (
|
const (
|
||||||
KindInit MessageKind = `init`
|
KindInit MessageKind = `init`
|
||||||
KindUpdate MessageKind = `update`
|
KindUpdate MessageKind = `update`
|
||||||
|
@ -47,6 +47,7 @@ const (
|
||||||
KindDelete MessageKind = `delete`
|
KindDelete MessageKind = `delete`
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Message represents a message
|
||||||
type Message struct {
|
type Message struct {
|
||||||
Kind MessageKind `json:"kind"`
|
Kind MessageKind `json:"kind"`
|
||||||
Data interface{} `json:"data"`
|
Data interface{} `json:"data"`
|
||||||
|
@ -54,6 +55,7 @@ type Message struct {
|
||||||
|
|
||||||
var broker *Broker
|
var broker *Broker
|
||||||
|
|
||||||
|
// Init creates a new local broker
|
||||||
func Init(init func(c echo.Context) (interface{}, error)) {
|
func Init(init func(c echo.Context) (interface{}, error)) {
|
||||||
broker = &Broker{
|
broker = &Broker{
|
||||||
clients: make(map[chan Message]bool),
|
clients: make(map[chan Message]bool),
|
||||||
|
@ -64,10 +66,8 @@ func Init(init func(c echo.Context) (interface{}, error)) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This Broker method starts a new goroutine. It handles
|
// Start starts a new goroutine. It handles the addition & removal of clients,
|
||||||
// the addition & removal of clients, as well as the broadcasting
|
// as well as the broadcasting of messages out to clients that are currently attached.
|
||||||
// of messages out to clients that are currently attached.
|
|
||||||
//
|
|
||||||
func Start() {
|
func Start() {
|
||||||
|
|
||||||
if broker.init == nil {
|
if broker.init == nil {
|
||||||
|
@ -116,6 +116,7 @@ func Start() {
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Serve is the web handler clients use to connect to the broker
|
||||||
func Serve(c echo.Context) error {
|
func Serve(c echo.Context) error {
|
||||||
|
|
||||||
rw := c.Response().Writer
|
rw := c.Response().Writer
|
||||||
|
@ -183,6 +184,7 @@ func Serve(c echo.Context) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SendMessage lets any package send a message to clients
|
||||||
func SendMessage(m Message) {
|
func SendMessage(m Message) {
|
||||||
broker.messages <- m
|
broker.messages <- m
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ type Configuration struct {
|
||||||
|
|
||||||
var siteConf = &Configuration{}
|
var siteConf = &Configuration{}
|
||||||
|
|
||||||
|
// InitConfig parses the config and maps out values
|
||||||
func InitConfig() {
|
func InitConfig() {
|
||||||
err := ini.MapTo(siteConf, "./config.ini")
|
err := ini.MapTo(siteConf, "./config.ini")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -24,30 +25,37 @@ func InitConfig() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetConfig returns the full config
|
||||||
func GetConfig() *Configuration {
|
func GetConfig() *Configuration {
|
||||||
return siteConf
|
return siteConf
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetMode returns the mode
|
||||||
func GetMode() int {
|
func GetMode() int {
|
||||||
return siteConf.Mode
|
return siteConf.Mode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetInterface returns the interface the server listens on
|
||||||
func GetInterface() string {
|
func GetInterface() string {
|
||||||
return siteConf.Interface
|
return siteConf.Interface
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAdminPassword returns the admin password
|
||||||
func GetAdminPassword() string {
|
func GetAdminPassword() string {
|
||||||
return siteConf.AdminPassword
|
return siteConf.AdminPassword
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetDBFile returns the path to the db file
|
||||||
func GetDBFile() string {
|
func GetDBFile() string {
|
||||||
return siteConf.DBFile
|
return siteConf.DBFile
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetOpenWindows returns whether to open electron windows or not
|
||||||
func GetOpenWindows() bool {
|
func GetOpenWindows() bool {
|
||||||
return siteConf.OpenWindows
|
return siteConf.OpenWindows
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetOpenBrowser returns whether to open browser windows or not
|
||||||
func GetOpenBrowser() bool {
|
func GetOpenBrowser() bool {
|
||||||
return siteConf.OpenBrowser
|
return siteConf.OpenBrowser
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
|
// Community represents a community
|
||||||
type Community struct {
|
type Community struct {
|
||||||
ID int64 `xorm:"pk autoincr" json:"id" form:"id"`
|
ID int64 `xorm:"pk autoincr" json:"id" form:"id"`
|
||||||
Name string `xorm:"text" json:"name" form:"name"`
|
Name string `xorm:"text" json:"name" form:"name"`
|
||||||
|
@ -9,16 +10,19 @@ type Community struct {
|
||||||
CoinsQuota float64 `xorm:"-" json:"coins_quota"`
|
CoinsQuota float64 `xorm:"-" json:"coins_quota"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create creates a new community
|
||||||
func (c *Community) Create() (err error) {
|
func (c *Community) Create() (err error) {
|
||||||
_, err = x.Insert(c)
|
_, err = x.Insert(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Delete removes a community
|
||||||
func (c *Community) Delete() (err error) {
|
func (c *Community) Delete() (err error) {
|
||||||
_, err = x.Delete(c)
|
_, err = x.Delete(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadAll returns all communites
|
||||||
func (c *Community) ReadAll(orderby string) (interface{}, error) {
|
func (c *Community) ReadAll(orderby string) (interface{}, error) {
|
||||||
|
|
||||||
orderbyStmt := "CoinsQuota DESC"
|
orderbyStmt := "CoinsQuota DESC"
|
||||||
|
@ -44,6 +48,7 @@ func (c *Community) ReadAll(orderby string) (interface{}, error) {
|
||||||
return communities, nil
|
return communities, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update updates an existing community
|
||||||
func (c *Community) Update(moreCoins int64) (err error) {
|
func (c *Community) Update(moreCoins int64) (err error) {
|
||||||
// Check if it exists
|
// Check if it exists
|
||||||
exists, err := x.Where("id = ?", c.ID).Get(c)
|
exists, err := x.Where("id = ?", c.ID).Get(c)
|
||||||
|
|
|
@ -4,12 +4,13 @@ import (
|
||||||
"git.kolaente.de/konrad/Konfi-Castle-Kasino/pkg/config"
|
"git.kolaente.de/konrad/Konfi-Castle-Kasino/pkg/config"
|
||||||
"github.com/go-xorm/xorm"
|
"github.com/go-xorm/xorm"
|
||||||
"github.com/labstack/gommon/log"
|
"github.com/labstack/gommon/log"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3" // Needed for sqlite compatibility
|
||||||
"xorm.io/core"
|
"xorm.io/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
var x *xorm.Engine
|
var x *xorm.Engine
|
||||||
|
|
||||||
|
// DBinit creates the initial db connection and does migrations
|
||||||
func DBinit() {
|
func DBinit() {
|
||||||
var err error
|
var err error
|
||||||
x, err = xorm.NewEngine("sqlite3", config.GetDBFile())
|
x, err = xorm.NewEngine("sqlite3", config.GetDBFile())
|
||||||
|
|
|
@ -2,10 +2,12 @@ package models
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
|
// ErrKofiDoesNotExist represents an error where a kofi does not exist
|
||||||
type ErrKofiDoesNotExist struct {
|
type ErrKofiDoesNotExist struct {
|
||||||
ID int64
|
ID int64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Error is Go's error implementation
|
||||||
func (err ErrKofiDoesNotExist) Error() string {
|
func (err ErrKofiDoesNotExist) Error() string {
|
||||||
return fmt.Sprintf("This kofi does not exist [ID: %n]", err.ID)
|
return fmt.Sprintf("This kofi does not exist [ID: %n]", err.ID)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
|
// Kofi represents a kofi
|
||||||
type Kofi struct {
|
type Kofi struct {
|
||||||
ID int64 `xorm:"pk autoincr" json:"id" form:"id"`
|
ID int64 `xorm:"pk autoincr" json:"id" form:"id"`
|
||||||
Name string `xorm:"text" json:"name" form:"name"`
|
Name string `xorm:"text" json:"name" form:"name"`
|
||||||
|
@ -7,16 +8,19 @@ type Kofi struct {
|
||||||
KCoins int64 `xorm:"bigint(11)" json:"kcoins"`
|
KCoins int64 `xorm:"bigint(11)" json:"kcoins"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create creates a new kofi
|
||||||
func (k *Kofi) Create() (err error) {
|
func (k *Kofi) Create() (err error) {
|
||||||
_, err = x.Insert(k)
|
_, err = x.Insert(k)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Delete removes a kofi
|
||||||
func (k *Kofi) Delete() (err error) {
|
func (k *Kofi) Delete() (err error) {
|
||||||
_, err = x.Delete(k)
|
_, err = x.Delete(k)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update updates an existing kofi
|
||||||
func (k *Kofi) Update(moreCoins int64) (err error) {
|
func (k *Kofi) Update(moreCoins int64) (err error) {
|
||||||
// Check if it exists
|
// Check if it exists
|
||||||
exists, err := x.Where("id = ?", k.ID).Get(k)
|
exists, err := x.Where("id = ?", k.ID).Get(k)
|
||||||
|
@ -34,6 +38,7 @@ func (k *Kofi) Update(moreCoins int64) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadAll returns all kofis
|
||||||
func (k *Kofi) ReadAll(orderby string) (interface{}, error) {
|
func (k *Kofi) ReadAll(orderby string) (interface{}, error) {
|
||||||
var orderbyStmt = "k_coins DESC"
|
var orderbyStmt = "k_coins DESC"
|
||||||
if orderby == "name" {
|
if orderby == "name" {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
|
// Managable represents an interface which can be managed
|
||||||
type Managable interface {
|
type Managable interface {
|
||||||
ReadAll(string) (interface{}, error)
|
ReadAll(string) (interface{}, error)
|
||||||
Create() error
|
Create() error
|
||||||
|
|
|
@ -1,7 +1,4 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
var Version = "+1.1-2-g353bf3b"
|
// Version is the current version, gets overridden by make build
|
||||||
|
var Version = "1.3"
|
||||||
type Message struct {
|
|
||||||
Message string
|
|
||||||
}
|
|
||||||
|
|
|
@ -6,10 +6,12 @@ import (
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Template represents a template
|
||||||
type Template struct {
|
type Template struct {
|
||||||
templates *template.Template
|
templates *template.Template
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Render implements echo's template handler
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"git.kolaente.de/konrad/Konfi-Castle-Kasino/pkg/config"
|
"git.kolaente.de/konrad/Konfi-Castle-Kasino/pkg/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// AdminInfos represents stuff about an admin
|
||||||
type AdminInfos struct {
|
type AdminInfos struct {
|
||||||
Loggedin bool
|
Loggedin bool
|
||||||
Mode int
|
Mode int
|
||||||
|
|
|
@ -10,11 +10,13 @@ import (
|
||||||
"github.com/labstack/gommon/log"
|
"github.com/labstack/gommon/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Handler represents a web handler which is able to do stuff
|
||||||
type Handler struct {
|
type Handler struct {
|
||||||
str func() models.Managable
|
str func() models.Managable
|
||||||
broker *broker.Broker
|
broker *broker.Broker
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdatedMessage is a message which holds updated data
|
||||||
type UpdatedMessage struct {
|
type UpdatedMessage struct {
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
Data models.Managable `json:"data"`
|
Data models.Managable `json:"data"`
|
||||||
|
@ -35,6 +37,7 @@ func (h *Handler) readAll(c echo.Context) (interface{}, error) {
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadAll is the web handler for getting everything
|
||||||
func (h *Handler) ReadAll(c echo.Context) error {
|
func (h *Handler) ReadAll(c echo.Context) error {
|
||||||
|
|
||||||
data, err := h.readAll(c)
|
data, err := h.readAll(c)
|
||||||
|
@ -44,6 +47,7 @@ func (h *Handler) ReadAll(c echo.Context) error {
|
||||||
return c.JSON(http.StatusOK, data)
|
return c.JSON(http.StatusOK, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create is the web handler to create
|
||||||
func (h *Handler) Create(c echo.Context) error {
|
func (h *Handler) Create(c echo.Context) error {
|
||||||
str := h.str()
|
str := h.str()
|
||||||
if err := c.Bind(str); err != nil {
|
if err := c.Bind(str); err != nil {
|
||||||
|
@ -64,6 +68,7 @@ func (h *Handler) Create(c echo.Context) error {
|
||||||
return c.JSON(http.StatusOK, "success")
|
return c.JSON(http.StatusOK, "success")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Delete is the web handler to delete something
|
||||||
func (h *Handler) Delete(c echo.Context) error {
|
func (h *Handler) Delete(c echo.Context) error {
|
||||||
str := h.str()
|
str := h.str()
|
||||||
if err := c.Bind(str); err != nil {
|
if err := c.Bind(str); err != nil {
|
||||||
|
@ -84,6 +89,7 @@ func (h *Handler) Delete(c echo.Context) error {
|
||||||
return c.JSON(http.StatusOK, "success")
|
return c.JSON(http.StatusOK, "success")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update is the handler to update an entry
|
||||||
func (h *Handler) Update(c echo.Context) error {
|
func (h *Handler) Update(c echo.Context) error {
|
||||||
str := h.str()
|
str := h.str()
|
||||||
if err := c.Bind(str); err != nil {
|
if err := c.Bind(str); err != nil {
|
||||||
|
|
|
@ -14,6 +14,7 @@ import (
|
||||||
"github.com/labstack/gommon/log"
|
"github.com/labstack/gommon/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// NewEcho creates a new echo instance
|
||||||
func NewEcho() *echo.Echo {
|
func NewEcho() *echo.Echo {
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
e.HideBanner = true
|
e.HideBanner = true
|
||||||
|
@ -37,6 +38,7 @@ func NewEcho() *echo.Echo {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RegisterRoutes registers all routes
|
||||||
func RegisterRoutes(e *echo.Echo) {
|
func RegisterRoutes(e *echo.Echo) {
|
||||||
//Static ontent
|
//Static ontent
|
||||||
e.Static("/assets", "assets")
|
e.Static("/assets", "assets")
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"github.com/pkg/browser"
|
"github.com/pkg/browser"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// OpenNativeWindows opens browser windows
|
||||||
func OpenNativeWindows() {
|
func OpenNativeWindows() {
|
||||||
err := browser.OpenURL("http://127.0.0.1" + config.GetInterface())
|
err := browser.OpenURL("http://127.0.0.1" + config.GetInterface())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"github.com/labstack/gommon/log"
|
"github.com/labstack/gommon/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// OpenWindows opens electron windows
|
||||||
func OpenWindows() {
|
func OpenWindows() {
|
||||||
// Webview viewer
|
// Webview viewer
|
||||||
/* err := webview.Open("Konfi@Castle Viewer", "http://127.0.0.1:8080", 800, 600, true)
|
/* err := webview.Open("Konfi@Castle Viewer", "http://127.0.0.1:8080", 800, 600, true)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user