From 4de30c61a0715055d5e8e8b2bb439e6cbf0fcc8c Mon Sep 17 00:00:00 2001 From: kolaente Date: Thu, 5 Sep 2019 21:35:07 +0200 Subject: [PATCH] Fixed lint --- Makefile | 2 +- pkg/broker/broker.go | 18 ++++++++++-------- pkg/config/config.go | 8 ++++++++ pkg/models/community.go | 5 +++++ pkg/models/db.go | 3 ++- pkg/models/errors.go | 2 ++ pkg/models/kofi.go | 5 +++++ pkg/models/managable.go | 1 + pkg/models/utils.go | 7 ++----- pkg/router/TemplateRender.go | 2 ++ pkg/router/admin.go | 1 + pkg/router/handler.go | 6 ++++++ pkg/router/router.go | 2 ++ pkg/windows/native.go | 1 + pkg/windows/windows.go | 1 + 15 files changed, 49 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index a21f5d0..c90211f 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/pkg/broker/broker.go b/pkg/broker/broker.go index 3675b90..0d55364 100644 --- a/pkg/broker/broker.go +++ b/pkg/broker/broker.go @@ -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 } diff --git a/pkg/config/config.go b/pkg/config/config.go index 75a193b..66da4b7 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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 } diff --git a/pkg/models/community.go b/pkg/models/community.go index 79e8670..ca171e0 100644 --- a/pkg/models/community.go +++ b/pkg/models/community.go @@ -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) diff --git a/pkg/models/db.go b/pkg/models/db.go index 7dd902a..e4900cb 100644 --- a/pkg/models/db.go +++ b/pkg/models/db.go @@ -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()) diff --git a/pkg/models/errors.go b/pkg/models/errors.go index 011b9f0..1447775 100644 --- a/pkg/models/errors.go +++ b/pkg/models/errors.go @@ -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) } diff --git a/pkg/models/kofi.go b/pkg/models/kofi.go index 7ba71c8..4aa2360 100644 --- a/pkg/models/kofi.go +++ b/pkg/models/kofi.go @@ -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" { diff --git a/pkg/models/managable.go b/pkg/models/managable.go index b1833a1..a142353 100644 --- a/pkg/models/managable.go +++ b/pkg/models/managable.go @@ -1,5 +1,6 @@ package models +// Managable represents an interface which can be managed type Managable interface { ReadAll(string) (interface{}, error) Create() error diff --git a/pkg/models/utils.go b/pkg/models/utils.go index a779f2b..7661048 100644 --- a/pkg/models/utils.go +++ b/pkg/models/utils.go @@ -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" diff --git a/pkg/router/TemplateRender.go b/pkg/router/TemplateRender.go index e2388ff..083a9aa 100644 --- a/pkg/router/TemplateRender.go +++ b/pkg/router/TemplateRender.go @@ -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) } diff --git a/pkg/router/admin.go b/pkg/router/admin.go index 7760622..c5a0fd2 100644 --- a/pkg/router/admin.go +++ b/pkg/router/admin.go @@ -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 diff --git a/pkg/router/handler.go b/pkg/router/handler.go index 903d893..8ec5c30 100644 --- a/pkg/router/handler.go +++ b/pkg/router/handler.go @@ -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 { diff --git a/pkg/router/router.go b/pkg/router/router.go index 5046695..655be5c 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -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") diff --git a/pkg/windows/native.go b/pkg/windows/native.go index 219912c..50fbba8 100644 --- a/pkg/windows/native.go +++ b/pkg/windows/native.go @@ -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 { diff --git a/pkg/windows/windows.go b/pkg/windows/windows.go index baee915..5cce823 100644 --- a/pkg/windows/windows.go +++ b/pkg/windows/windows.go @@ -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)