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:
- -trimpath
ldflags:
- -s -w
- -s -w -extldflags "-static"'
- -X {{ .ModulePath }}/pkg/consts.Version={{ .Version }}
- -X {{ .ModulePath }}/pkg/consts.Commit={{ .ShortCommit }}
- -X {{ .ModulePath }}/pkg/consts.CommitDate={{ .CommitDate }}

View file

@ -3,6 +3,7 @@ package database
import (
"log"
"os"
"path/filepath"
"time"
"github.com/divyam234/teldrive/utils"
@ -64,11 +65,13 @@ func InitDB() {
func migrate() {
config := utils.GetConfig()
if err := goose.SetDialect("postgres"); err != nil {
panic(err)
}
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)
}
}

10
main.go
View file

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

View file

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

View file

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"
@ -101,3 +102,12 @@ func PathExists(path string) (bool, error) {
}
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()
sessionStorage := &telegram.FileSessionStorage{
Path: filepath.Join("sessions", clientName+".json"),
Path: filepath.Join(config.ExecDir, "sessions", clientName+".json"),
}
middlewares := []telegram.Middleware{floodwait.NewSimpleWaiter()}
@ -186,9 +186,8 @@ func InitBotClients() {
Workloads = make(map[int]int)
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
}