Skip to content

Commit

Permalink
Global struct removed
Browse files Browse the repository at this point in the history
  • Loading branch information
f1mishutka committed Apr 10, 2024
1 parent 177b419 commit 06a7e52
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 29 deletions.
12 changes: 4 additions & 8 deletions app/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ import (
var tgBot *tgbotapi.BotAPI

func InitApi() error {
if len(Settings.GuiCookieSecretKey) < 32 {
return errors.New("gui_cookie_secret_key should be at least 32 characters long")
}

if len(Settings.BotToken) == 0 {
return errors.New("bot_token required")
}
Expand All @@ -31,8 +27,8 @@ func InitApi() error {
return err
}

Global.BotInfo = tgBot.Self.FirstName + " " + tgBot.Self.LastName + " (@" + tgBot.Self.UserName + ")"
log.Printf("Authorized on telegram bot: %s\n", Global.BotInfo)
App.Global["BotInfo"] = tgBot.Self.FirstName + " " + tgBot.Self.LastName + " (@" + tgBot.Self.UserName + ")"
log.Printf("Authorized on telegram bot: %s\n", App.Global["BotInfo"].(string))

if Settings.BotChatID == 0 {
return errors.New("bot_chat_id required")
Expand All @@ -43,8 +39,8 @@ func InitApi() error {
return err
}

Global.ChatInfo = chat.Type + " \"" + chat.Title + "\", " + chat.InviteLink
log.Printf("Chat info: %s\n", Global.ChatInfo)
App.Global["ChatInfo"] = chat.Type + " \"" + chat.Title + "\", " + chat.InviteLink
log.Printf("Chat info: %s\n", App.Global["ChatInfo"].(string))

return nil
}
Expand Down
7 changes: 3 additions & 4 deletions app/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
</head>
<body>
<script type="application/javascript">
var mtAuth = {{.Auth}}
var mtAuth = {{.Auth}};
</script>

<div class="container border m-5 p-3 bg-light">
<h1>{{.AppInfo.AppName}}</h1>
<p class="small text-muted">v.{{.AppInfo.Version}}, commit {{.AppInfo.BuildCommit}}, built with {{.AppInfo.BuildWith}} at {{.AppInfo.BuildTime}}</p>
<p>
<b>Bot</b>: {{.Global.BotInfo}}<br>
<b>Chat</b>: {{.Global.ChatInfo}}
<b>Bot</b>: {{.AppInfo.Global.BotInfo}}<br>
<b>Chat</b>: {{.AppInfo.Global.ChatInfo}}
</p>

<div id="app">
Expand All @@ -26,7 +26,6 @@ <h1>{{.AppInfo.AppName}}</h1>
<component-main v-if="session"></component-main>
<component-auth v-else=""></component-auth>
</div>

</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.min.js" integrity="sha256-3gQJhtmj7YnV1fmtbVcnAV6eI4ws0Tr48bVZCThtCGQ=" crossorigin="anonymous"></script>
Expand Down
11 changes: 0 additions & 11 deletions app/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,6 @@ package app
import "github.com/mitoteam/goappbase"

var (
Global struct {
BotInfo string
ChatInfo string
}

App *goappbase.AppBase
Settings *AppSettingsType
)

func init() {
//defaults
Global.BotInfo = "[undefined]"
Global.ChatInfo = "[undefined]"
}
5 changes: 2 additions & 3 deletions app/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ package app
import "github.com/mitoteam/goappbase"

type AppSettingsType struct {
goappbase.AppSettingsBase
goappbase.AppSettingsBase `yaml:",inline"`

BotToken string `yaml:"bot_token" yaml_comment:"Bot authorization token"`
BotChatID int64 `yaml:"bot_chat_id" yaml_comment:"'chat_id' int64 value"`

GuiCookieSecretKey string `yaml:"gui_cookie_secret_key" yaml_comment:"At least 32 chars"`
GuiPassword string `yaml:"gui_password" yaml_comment:"GUI access password"`
GuiPassword string `yaml:"gui_password" yaml_comment:"GUI access password"`
}
1 change: 0 additions & 1 deletion app/web_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func WebIndex(c *gin.Context) {
session := sessions.Default(c)

data := &indexData{
Global: Global,
AppInfo: App,
Auth: session.Get("auth") == true,
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ require (
github.com/gin-gonic/gin v1.9.1
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/mitoteam/goappbase v0.0.0-20240409151548-3934980e7612
github.com/mitoteam/mttools v0.0.0-20240403084847-81ae7e0a4074
github.com/spf13/cobra v1.8.0
)

require (
Expand All @@ -31,9 +29,11 @@ require (
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitoteam/mttools v0.0.0-20240403084847-81ae7e0a4074 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.0 // indirect
github.com/spf13/cobra v1.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
)

func main() {
//default settings
app.Settings = &app.AppSettingsType{
GuiPassword: "mitoteam",
}
app.Settings.WebserverPort = 15080

//create app and set it up
app.App = goappbase.NewAppBase(app.Settings)

app.App.AppName = "mt-tgadmin"
Expand All @@ -29,5 +31,10 @@ func main() {
return err
}

//global state default values
app.App.Global["BotInfo"] = "[undefined]"
app.App.Global["ChatInfo"] = "[undefined]"

//do all the job
app.App.Run()
}

0 comments on commit 06a7e52

Please sign in to comment.