Fixed lint
continuous-integration/drone/push Build is passing Details

This commit is contained in:
kolaente 2019-09-05 21:35:07 +02:00
parent fa9e254240
commit 4de30c61a0
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
15 changed files with 49 additions and 15 deletions

View File

@ -19,7 +19,7 @@ GOFMT ?= gofmt -s
GOFLAGS := -i -v
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/))
SOURCES ?= $(shell find . -name "*.go" -type f)

View File

@ -8,10 +8,8 @@ import (
"net/http"
)
// A single Broker will be created in this program. It is responsible
// for keeping a list of which clients (browsers) are currently attached
// and broadcasting events (messages) to those clients.
//
// Broker is is responsible for keeping a list of which clients (browsers)
// are currently attached and broadcasting events (messages) to those clients.
type Broker struct {
// 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)
}
// MessageKind represents a message kind
type MessageKind string
// These are all valid message kinds
const (
KindInit MessageKind = `init`
KindUpdate MessageKind = `update`
@ -47,6 +47,7 @@ const (
KindDelete MessageKind = `delete`
)
// Message represents a message
type Message struct {
Kind MessageKind `json:"kind"`
Data interface{} `json:"data"`
@ -54,6 +55,7 @@ type Message struct {
var broker *Broker
// Init creates a new local broker
func Init(init func(c echo.Context) (interface{}, error)) {
broker = &Broker{
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
// the addition & removal of clients, as well as the broadcasting
// of messages out to clients that are currently attached.
//
// Start starts a new goroutine. It handles the addition & removal of clients,
// as well as the broadcasting of messages out to clients that are currently attached.
func Start() {
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 {
rw := c.Response().Writer
@ -183,6 +184,7 @@ func Serve(c echo.Context) error {
return nil
}
// SendMessage lets any package send a message to clients
func SendMessage(m Message) {
broker.messages <- m
}

View File

@ -17,6 +17,7 @@ type Configuration struct {
var siteConf = &Configuration{}
// InitConfig parses the config and maps out values
func InitConfig() {
err := ini.MapTo(siteConf, "./config.ini")
if err != nil {
@ -24,30 +25,37 @@ func InitConfig() {
}
}
// GetConfig returns the full config
func GetConfig() *Configuration {
return siteConf
}
// GetMode returns the mode
func GetMode() int {
return siteConf.Mode
}
// GetInterface returns the interface the server listens on
func GetInterface() string {
return siteConf.Interface
}
// GetAdminPassword returns the admin password
func GetAdminPassword() string {
return siteConf.AdminPassword
}
// GetDBFile returns the path to the db file
func GetDBFile() string {
return siteConf.DBFile
}
// GetOpenWindows returns whether to open electron windows or not
func GetOpenWindows() bool {
return siteConf.OpenWindows
}
// GetOpenBrowser returns whether to open browser windows or not
func GetOpenBrowser() bool {
return siteConf.OpenBrowser
}

View File

@ -1,5 +1,6 @@
package models
// Community represents a community
type Community struct {
ID int64 `xorm:"pk autoincr" json:"id" form:"id"`
Name string `xorm:"text" json:"name" form:"name"`
@ -9,16 +10,19 @@ type Community struct {
CoinsQuota float64 `xorm:"-" json:"coins_quota"`
}
// Create creates a new community
func (c *Community) Create() (err error) {
_, err = x.Insert(c)
return
}
// Delete removes a community
func (c *Community) Delete() (err error) {
_, err = x.Delete(c)
return
}
// ReadAll returns all communites
func (c *Community) ReadAll(orderby string) (interface{}, error) {
orderbyStmt := "CoinsQuota DESC"
@ -44,6 +48,7 @@ func (c *Community) ReadAll(orderby string) (interface{}, error) {
return communities, nil
}
// Update updates an existing community
func (c *Community) Update(moreCoins int64) (err error) {
// Check if it exists
exists, err := x.Where("id = ?", c.ID).Get(c)

View File

@ -4,12 +4,13 @@ import (
"git.kolaente.de/konrad/Konfi-Castle-Kasino/pkg/config"
"github.com/go-xorm/xorm"
"github.com/labstack/gommon/log"
_ "github.com/mattn/go-sqlite3"
_ "github.com/mattn/go-sqlite3" // Needed for sqlite compatibility
"xorm.io/core"
)
var x *xorm.Engine
// DBinit creates the initial db connection and does migrations
func DBinit() {
var err error
x, err = xorm.NewEngine("sqlite3", config.GetDBFile())

View File

@ -2,10 +2,12 @@ package models
import "fmt"
// ErrKofiDoesNotExist represents an error where a kofi does not exist
type ErrKofiDoesNotExist struct {
ID int64
}
// Error is Go's error implementation
func (err ErrKofiDoesNotExist) Error() string {
return fmt.Sprintf("This kofi does not exist [ID: %n]", err.ID)
}

View File

@ -1,5 +1,6 @@
package models
// Kofi represents a kofi
type Kofi struct {
ID int64 `xorm:"pk autoincr" json:"id" form:"id"`
Name string `xorm:"text" json:"name" form:"name"`
@ -7,16 +8,19 @@ type Kofi struct {
KCoins int64 `xorm:"bigint(11)" json:"kcoins"`
}
// Create creates a new kofi
func (k *Kofi) Create() (err error) {
_, err = x.Insert(k)
return
}
// Delete removes a kofi
func (k *Kofi) Delete() (err error) {
_, err = x.Delete(k)
return
}
// Update updates an existing kofi
func (k *Kofi) Update(moreCoins int64) (err error) {
// Check if it exists
exists, err := x.Where("id = ?", k.ID).Get(k)
@ -34,6 +38,7 @@ func (k *Kofi) Update(moreCoins int64) (err error) {
return
}
// ReadAll returns all kofis
func (k *Kofi) ReadAll(orderby string) (interface{}, error) {
var orderbyStmt = "k_coins DESC"
if orderby == "name" {

View File

@ -1,5 +1,6 @@
package models
// Managable represents an interface which can be managed
type Managable interface {
ReadAll(string) (interface{}, error)
Create() error

View File

@ -1,7 +1,4 @@
package models
var Version = "+1.1-2-g353bf3b"
type Message struct {
Message string
}
// Version is the current version, gets overridden by make build
var Version = "1.3"

View File

@ -6,10 +6,12 @@ import (
"io"
)
// Template represents a template
type Template struct {
templates *template.Template
}
// Render implements echo's template handler
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}

View File

@ -10,6 +10,7 @@ import (
"git.kolaente.de/konrad/Konfi-Castle-Kasino/pkg/config"
)
// AdminInfos represents stuff about an admin
type AdminInfos struct {
Loggedin bool
Mode int

View File

@ -10,11 +10,13 @@ import (
"github.com/labstack/gommon/log"
)
// Handler represents a web handler which is able to do stuff
type Handler struct {
str func() models.Managable
broker *broker.Broker
}
// UpdatedMessage is a message which holds updated data
type UpdatedMessage struct {
Message string `json:"message"`
Data models.Managable `json:"data"`
@ -35,6 +37,7 @@ func (h *Handler) readAll(c echo.Context) (interface{}, error) {
return data, nil
}
// ReadAll is the web handler for getting everything
func (h *Handler) ReadAll(c echo.Context) error {
data, err := h.readAll(c)
@ -44,6 +47,7 @@ func (h *Handler) ReadAll(c echo.Context) error {
return c.JSON(http.StatusOK, data)
}
// Create is the web handler to create
func (h *Handler) Create(c echo.Context) error {
str := h.str()
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")
}
// Delete is the web handler to delete something
func (h *Handler) Delete(c echo.Context) error {
str := h.str()
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")
}
// Update is the handler to update an entry
func (h *Handler) Update(c echo.Context) error {
str := h.str()
if err := c.Bind(str); err != nil {

View File

@ -14,6 +14,7 @@ import (
"github.com/labstack/gommon/log"
)
// NewEcho creates a new echo instance
func NewEcho() *echo.Echo {
e := echo.New()
e.HideBanner = true
@ -37,6 +38,7 @@ func NewEcho() *echo.Echo {
return e
}
// RegisterRoutes registers all routes
func RegisterRoutes(e *echo.Echo) {
//Static ontent
e.Static("/assets", "assets")

View File

@ -6,6 +6,7 @@ import (
"github.com/pkg/browser"
)
// OpenNativeWindows opens browser windows
func OpenNativeWindows() {
err := browser.OpenURL("http://127.0.0.1" + config.GetInterface())
if err != nil {

View File

@ -6,6 +6,7 @@ import (
"github.com/labstack/gommon/log"
)
// OpenWindows opens electron windows
func OpenWindows() {
// Webview viewer
/* err := webview.Open("Konfi@Castle Viewer", "http://127.0.0.1:8080", 800, 600, true)