find path from executable

This commit is contained in:
divyam234 2023-08-25 23:02:05 +05:30
parent d584a8a500
commit 68a03aeb83
6 changed files with 32 additions and 11 deletions

View file

@ -7,7 +7,7 @@ builds:
flags: flags:
- -trimpath - -trimpath
ldflags: ldflags:
- -s -w - -s -w -extldflags "-static"'
- -X {{ .ModulePath }}/pkg/consts.Version={{ .Version }} - -X {{ .ModulePath }}/pkg/consts.Version={{ .Version }}
- -X {{ .ModulePath }}/pkg/consts.Commit={{ .ShortCommit }} - -X {{ .ModulePath }}/pkg/consts.Commit={{ .ShortCommit }}
- -X {{ .ModulePath }}/pkg/consts.CommitDate={{ .CommitDate }} - -X {{ .ModulePath }}/pkg/consts.CommitDate={{ .CommitDate }}

View file

@ -3,6 +3,7 @@ package database
import ( import (
"log" "log"
"os" "os"
"path/filepath"
"time" "time"
"github.com/divyam234/teldrive/utils" "github.com/divyam234/teldrive/utils"
@ -64,11 +65,13 @@ func InitDB() {
func migrate() { func migrate() {
config := utils.GetConfig()
if err := goose.SetDialect("postgres"); err != nil { if err := goose.SetDialect("postgres"); err != nil {
panic(err) panic(err)
} }
db, _ := DB.DB() db, _ := DB.DB()
if err := goose.Up(db, "database/migrations"); err != nil { if err := goose.Up(db, filepath.Join(config.ExecDir, "database", "migrations")); err != nil {
panic(err) panic(err)
} }
} }

10
main.go
View file

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"path/filepath"
"time" "time"
"github.com/divyam234/teldrive/cache" "github.com/divyam234/teldrive/cache"
@ -13,7 +14,6 @@ import (
"github.com/divyam234/teldrive/utils/cron" "github.com/divyam234/teldrive/utils/cron"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/go-co-op/gocron" "github.com/go-co-op/gocron"
"github.com/joho/godotenv"
) )
func main() { func main() {
@ -22,9 +22,6 @@ func main() {
router := gin.Default() router := gin.Default()
godotenv.Load()
godotenv.Load("teldrive.env")
utils.InitConfig() utils.InitConfig()
utils.InitializeLogger() utils.InitializeLogger()
@ -55,10 +52,11 @@ func main() {
routes.GetRoutes(router) routes.GetRoutes(router)
ok, _ := utils.PathExists("./sslcerts")
config := utils.GetConfig() config := utils.GetConfig()
certDir := filepath.Join(config.ExecDir, "sslcerts")
ok, _ := utils.PathExists(certDir)
if ok && config.Https { if ok && config.Https {
router.RunTLS(fmt.Sprintf(":%d", config.Port), "./sslcerts/cert.pem", "./sslcerts/key.pem") router.RunTLS(fmt.Sprintf(":%d", config.Port), filepath.Join(certDir, "cert.pem"), filepath.Join(certDir, "key.pem"))
} else { } else {
router.Run(fmt.Sprintf(":%d", config.Port)) router.Run(fmt.Sprintf(":%d", config.Port))
} }

View file

@ -1,6 +1,9 @@
package utils package utils
import ( import (
"path/filepath"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig" "github.com/kelseyhightower/envconfig"
) )
@ -24,15 +27,23 @@ type Config struct {
TgClientLangPack string `envconfig:"TG_CLIENT_LANG_PACK" default:"webk"` TgClientLangPack string `envconfig:"TG_CLIENT_LANG_PACK" default:"webk"`
RunMigrations bool `envconfig:"RUN_MIGRATIONS" default:"true"` RunMigrations bool `envconfig:"RUN_MIGRATIONS" default:"true"`
Port int `envconfig:"PORT" default:"8080"` Port int `envconfig:"PORT" default:"8080"`
ExecDir string
} }
var config Config var config Config
func InitConfig() { func InitConfig() {
execDir := getExecutableDir()
godotenv.Load(filepath.Join(execDir, ".env"))
godotenv.Load(filepath.Join(execDir, "teldrive.env"))
err := envconfig.Process("", &config) err := envconfig.Process("", &config)
if err != nil { if err != nil {
panic(err) panic(err)
} }
config.ExecDir = execDir
} }
func GetConfig() *Config { func GetConfig() *Config {

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"os" "os"
"path/filepath"
"regexp" "regexp"
"strings" "strings"
"time" "time"
@ -101,3 +102,12 @@ func PathExists(path string) (bool, error) {
} }
return false, err return false, err
} }
func getExecutableDir() string {
path, _ := os.Executable()
executableDir := filepath.Dir(path)
return executableDir
}

View file

@ -54,7 +54,7 @@ func GetBotClient(clientName string) *telegram.Client {
config := GetConfig() config := GetConfig()
sessionStorage := &telegram.FileSessionStorage{ sessionStorage := &telegram.FileSessionStorage{
Path: filepath.Join("sessions", clientName+".json"), Path: filepath.Join(config.ExecDir, "sessions", clientName+".json"),
} }
middlewares := []telegram.Middleware{floodwait.NewSimpleWaiter()} middlewares := []telegram.Middleware{floodwait.NewSimpleWaiter()}
@ -186,9 +186,8 @@ func InitBotClients() {
Workloads = make(map[int]int) Workloads = make(map[int]int)
if config.MultiClient { if config.MultiClient {
sessionDir := "sessions"
if err := os.MkdirAll(sessionDir, 0700); err != nil { if err := os.MkdirAll(filepath.Join(config.ExecDir, "sessions"), 0700); err != nil {
return return
} }