diff --git a/.env b/.env new file mode 100644 index 0000000..105fba8 --- /dev/null +++ b/.env @@ -0,0 +1,3 @@ +CONFIG=. +DATA=./assets +CHECK_FREQUENCY = 10 \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..932a1f4 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,33 @@ +ARG GO_VERSION=1.15.2 + +FROM golang:${GO_VERSION}-alpine AS builder + +RUN apk update && apk add alpine-sdk git && rm -rf /var/cache/apk/* + +RUN mkdir -p /api +WORKDIR /api + +COPY go.mod . +COPY go.sum . +RUN go mod download + +COPY . . +RUN go build -o ./app ./main.go + +FROM alpine:latest + +RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/* + +RUN mkdir -p /api +WORKDIR /api +COPY --from=builder /api/app . +COPY client ./client +RUN mkdir /config +RUN mkdir /assets +ENV CONFIG=/config +ENV DATA=/assets +#COPY --from=builder /api/test.db . + +EXPOSE 8080 +VOLUME ["/config", "/assets"] +ENTRYPOINT ["./app"] \ No newline at end of file diff --git a/db/db.go b/db/db.go index 1c35c66..a80b600 100644 --- a/db/db.go +++ b/db/db.go @@ -2,6 +2,8 @@ package db import ( "fmt" + "os" + "path" "gorm.io/driver/sqlite" @@ -14,7 +16,9 @@ var DB *gorm.DB //Init is used to Initialize Database func Init() (*gorm.DB, error) { // github.com/mattn/go-sqlite3 - db, err := gorm.Open(sqlite.Open("podgrab.db"), &gorm.Config{}) + configPath := os.Getenv("CONFIG") + dbPath := path.Join(configPath, "podgrab.db") + db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{}) if err != nil { fmt.Println("db err: ", err) return nil, err diff --git a/db/dbfunctions.go b/db/dbfunctions.go index 4be04b2..9150b97 100644 --- a/db/dbfunctions.go +++ b/db/dbfunctions.go @@ -40,7 +40,7 @@ func GetAllPodcastItemsByPodcastId(podcastId string, podcasts *[]PodcastItem) er func GetAllPodcastItemsToBeDownloaded() (*[]PodcastItem, error) { var podcastItems []PodcastItem - result := DB.Preload(clause.Associations).Where(&PodcastItem{DownloadDate: time.Time{}}).Find(&podcastItems) + result := DB.Debug().Preload(clause.Associations).Where("download_date=?", time.Time{}).Find(&podcastItems) return &podcastItems, result.Error } @@ -65,6 +65,6 @@ func CreatePodcastItem(podcastItem *PodcastItem) error { return tx.Error } func UpdatePodcastItem(podcastItem *PodcastItem) error { - tx := DB.Save(&podcastItem) + tx := DB.Omit("Podcast").Save(&podcastItem) return tx.Error } diff --git a/go.mod b/go.mod index 83f916b..ed071e3 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/gin-gonic/gin v1.6.3 github.com/gobeam/stringy v0.0.0-20200717095810-8a3637503f62 github.com/jasonlvhit/gocron v0.0.1 + github.com/joho/godotenv v1.3.0 github.com/microcosm-cc/bluemonday v1.0.4 github.com/satori/go.uuid v1.2.0 golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a diff --git a/go.sum b/go.sum index 3605121..2e21335 100644 --- a/go.sum +++ b/go.sum @@ -44,6 +44,8 @@ github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkr github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jinzhu/now v1.1.1 h1:g39TucaRWyV3dwDO++eEc6qf8TVIQ/Da48WmqjZ3i7E= github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= +github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= diff --git a/main.go b/main.go index ad964f5..6e5d035 100644 --- a/main.go +++ b/main.go @@ -2,13 +2,17 @@ package main import ( "fmt" + "log" "net/http" + "os" + "strconv" "github.com/akhilrex/podgrab/controllers" "github.com/akhilrex/podgrab/db" "github.com/akhilrex/podgrab/service" "github.com/gin-gonic/gin" "github.com/jasonlvhit/gocron" + _ "github.com/joho/godotenv/autoload" ) func main() { @@ -23,7 +27,9 @@ func main() { db.Migrate() r := gin.Default() - r.Static("/assets", "./assets") + dataPath := os.Getenv("DATA") + //r.Static("/assets", "./assets") + r.Static("/assets", dataPath) r.LoadHTMLGlob("client/*") r.GET("/podcasts", controllers.AddPodcast) @@ -89,6 +95,7 @@ func main() { }) + go assetEnv() go intiCron() r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080") @@ -96,7 +103,18 @@ func main() { } func intiCron() { - //gocron.Every(5).Minutes().Do(service.DownloadMissingEpisodes) - gocron.Every(5).Minutes().Do(service.RefreshEpisodes) + checkFrequency, err := strconv.Atoi(os.Getenv("CHECK_FREQUENCY")) + if err != nil { + checkFrequency = 10 + log.Print(err) + } + gocron.Every(uint64(checkFrequency)).Hours().Do(service.DownloadMissingEpisodes) + gocron.Every(uint64(checkFrequency)).Hours().Do(service.RefreshEpisodes) <-gocron.Start() } + +func assetEnv() { + log.Println("Config Dir: ", os.Getenv("CONFIG")) + log.Println("Assets Dir: ", os.Getenv("DATA")) + log.Println("Check Frequency (hrs): ", os.Getenv("CHECK_FREQUENCY")) +} diff --git a/service/fileService.go b/service/fileService.go index ade79f3..b592c7e 100644 --- a/service/fileService.go +++ b/service/fileService.go @@ -49,7 +49,8 @@ func httpClient() *http.Client { func createIfFoldeDoesntExist(folder string) string { str := stringy.New(folder) folder = str.RemoveSpecialCharacter() - folderPath := path.Join("./assets/", folder) + dataPath := os.Getenv("DATA") + folderPath := path.Join(dataPath, folder) if _, err := os.Stat(folderPath); os.IsNotExist(err) { os.MkdirAll(folderPath, 0755) } diff --git a/service/podcastService.go b/service/podcastService.go index 96c7ac7..53c86bd 100644 --- a/service/podcastService.go +++ b/service/podcastService.go @@ -101,13 +101,15 @@ func SetPodcastItemAsDownloaded(id string, location string) { func DownloadMissingEpisodes() error { data, err := db.GetAllPodcastItemsToBeDownloaded() - fmt.Println("Processing episodes: %d", len(*data)) + fmt.Println("Processing episodes: ", strconv.Itoa(len(*data))) if err != nil { return err } for _, item := range *data { + url, _ := Download(item.FileURL, item.Title, item.Podcast.Title) SetPodcastItemAsDownloaded(item.ID, url) + return nil } return nil }