pull/24/head
Akhil Gupta 4 years ago
parent 84b98b3524
commit fe034a249c

@ -0,0 +1,3 @@
CONFIG=.
DATA=./assets
CHECK_FREQUENCY = 10

@ -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"]

@ -2,6 +2,8 @@ package db
import ( import (
"fmt" "fmt"
"os"
"path"
"gorm.io/driver/sqlite" "gorm.io/driver/sqlite"
@ -14,7 +16,9 @@ var DB *gorm.DB
//Init is used to Initialize Database //Init is used to Initialize Database
func Init() (*gorm.DB, error) { func Init() (*gorm.DB, error) {
// github.com/mattn/go-sqlite3 // 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 { if err != nil {
fmt.Println("db err: ", err) fmt.Println("db err: ", err)
return nil, err return nil, err

@ -40,7 +40,7 @@ func GetAllPodcastItemsByPodcastId(podcastId string, podcasts *[]PodcastItem) er
func GetAllPodcastItemsToBeDownloaded() (*[]PodcastItem, error) { func GetAllPodcastItemsToBeDownloaded() (*[]PodcastItem, error) {
var podcastItems []PodcastItem 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 return &podcastItems, result.Error
} }
@ -65,6 +65,6 @@ func CreatePodcastItem(podcastItem *PodcastItem) error {
return tx.Error return tx.Error
} }
func UpdatePodcastItem(podcastItem *PodcastItem) error { func UpdatePodcastItem(podcastItem *PodcastItem) error {
tx := DB.Save(&podcastItem) tx := DB.Omit("Podcast").Save(&podcastItem)
return tx.Error return tx.Error
} }

@ -7,6 +7,7 @@ require (
github.com/gin-gonic/gin v1.6.3 github.com/gin-gonic/gin v1.6.3
github.com/gobeam/stringy v0.0.0-20200717095810-8a3637503f62 github.com/gobeam/stringy v0.0.0-20200717095810-8a3637503f62
github.com/jasonlvhit/gocron v0.0.1 github.com/jasonlvhit/gocron v0.0.1
github.com/joho/godotenv v1.3.0
github.com/microcosm-cc/bluemonday v1.0.4 github.com/microcosm-cc/bluemonday v1.0.4
github.com/satori/go.uuid v1.2.0 github.com/satori/go.uuid v1.2.0
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a

@ -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.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 h1:g39TucaRWyV3dwDO++eEc6qf8TVIQ/Da48WmqjZ3i7E=
github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= 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 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= 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= github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=

@ -2,13 +2,17 @@ package main
import ( import (
"fmt" "fmt"
"log"
"net/http" "net/http"
"os"
"strconv"
"github.com/akhilrex/podgrab/controllers" "github.com/akhilrex/podgrab/controllers"
"github.com/akhilrex/podgrab/db" "github.com/akhilrex/podgrab/db"
"github.com/akhilrex/podgrab/service" "github.com/akhilrex/podgrab/service"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/jasonlvhit/gocron" "github.com/jasonlvhit/gocron"
_ "github.com/joho/godotenv/autoload"
) )
func main() { func main() {
@ -23,7 +27,9 @@ func main() {
db.Migrate() db.Migrate()
r := gin.Default() r := gin.Default()
r.Static("/assets", "./assets") dataPath := os.Getenv("DATA")
//r.Static("/assets", "./assets")
r.Static("/assets", dataPath)
r.LoadHTMLGlob("client/*") r.LoadHTMLGlob("client/*")
r.GET("/podcasts", controllers.AddPodcast) r.GET("/podcasts", controllers.AddPodcast)
@ -89,6 +95,7 @@ func main() {
}) })
go assetEnv()
go intiCron() go intiCron()
r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080") r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
@ -96,7 +103,18 @@ func main() {
} }
func intiCron() { func intiCron() {
//gocron.Every(5).Minutes().Do(service.DownloadMissingEpisodes) checkFrequency, err := strconv.Atoi(os.Getenv("CHECK_FREQUENCY"))
gocron.Every(5).Minutes().Do(service.RefreshEpisodes) 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() <-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"))
}

@ -49,7 +49,8 @@ func httpClient() *http.Client {
func createIfFoldeDoesntExist(folder string) string { func createIfFoldeDoesntExist(folder string) string {
str := stringy.New(folder) str := stringy.New(folder)
folder = str.RemoveSpecialCharacter() 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) { if _, err := os.Stat(folderPath); os.IsNotExist(err) {
os.MkdirAll(folderPath, 0755) os.MkdirAll(folderPath, 0755)
} }

@ -101,13 +101,15 @@ func SetPodcastItemAsDownloaded(id string, location string) {
func DownloadMissingEpisodes() error { func DownloadMissingEpisodes() error {
data, err := db.GetAllPodcastItemsToBeDownloaded() data, err := db.GetAllPodcastItemsToBeDownloaded()
fmt.Println("Processing episodes: %d", len(*data)) fmt.Println("Processing episodes: ", strconv.Itoa(len(*data)))
if err != nil { if err != nil {
return err return err
} }
for _, item := range *data { for _, item := range *data {
url, _ := Download(item.FileURL, item.Title, item.Podcast.Title) url, _ := Download(item.FileURL, item.Title, item.Podcast.Title)
SetPodcastItemAsDownloaded(item.ID, url) SetPodcastItemAsDownloaded(item.ID, url)
return nil
} }
return nil return nil
} }

Loading…
Cancel
Save