Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM golang:1.9-alpine as build
RUN apk add --no-cache git
RUN apk add --no-cache git gcc musl-dev
WORKDIR /go/src/github.com/streamlist/streamlist
ARG STREAMLIST_VERSION=unknown
ENV GODEBUG="netdns=go http2server=0"
Expand All @@ -14,15 +14,18 @@ RUN go get \
github.com/eduncan911/podcast \
github.com/rylio/ytdl \
go.uber.org/zap \
golang.org/x/crypto/acme/autocert
golang.org/x/crypto/acme/autocert \
github.com/jinzhu/gorm \
github.com/jinzhu/gorm/dialects/sqlite \
github.com/go-sql-driver/mysql
COPY *.go ./
COPY internal ./internal
COPY static ./static
COPY templates ./templates
RUN go fmt && \
go vet --all && \
go-bindata --pkg main static/... templates/...
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 \
go build -v --compiler gc --ldflags "-extldflags -static -s -w -X main.version=${STREAMLIST_VERSION}" -o /usr/bin/streamlist-linux-amd64
#RUN CGO_ENABLED=0 GOOS=linux GOARCH=arm GOARM=7 \
# go build -v --compiler gc --ldflags "-extldflags -static -s -w -X main.version=${STREAMLIST_VERSION}" -o /usr/bin/streamlist-linux-armv7
Expand Down
42 changes: 42 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"os"
// _ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/sqlite"
// _ "github.com/jinzhu/gorm/dialects/mssql"
//"fmt"
)

// User ...
/*type User struct {
ID uint
Username string
Password string
}*/

var db *gorm.DB

func dbInit() {
var err error
// Switch database
if os.Getenv("MYSQL_DB") != "" {
db, err = gorm.Open("mysql", os.Getenv("MYSQL_USER")+":"+os.Getenv("MYSQL_PASSWORD")+"@tcp("+os.Getenv("MYSQL_HOST")+":"+os.Getenv("MYSQL_PORT")+")/"+os.Getenv("MYSQL_DB")+"?charset=utf8&parseTime=True&loc=Local")
} else {
db, err = gorm.Open("sqlite3", "./streamlist.db")
}
if err != nil {
panic("failed to connect database")
}
//defer db.Close()
db.SingularTable(true)
//db.AutoMigrate(&User{})
db.AutoMigrate(&Media{})
db.AutoMigrate(&List{})
db.AutoMigrate(&User{})

// Add / verify foreign keys
db.Exec("ALTER TABLE `list_media` ADD CONSTRAINT `fk_list_id` FOREIGN KEY (`list_id`) REFERENCES `list` (`id`) ON UPDATE CASCADE ON DELETE CASCADE, ADD CONSTRAINT `fk_media_id` FOREIGN KEY (`media_id`) REFERENCES `media` (`id`) ON UPDATE CASCADE ON DELETE CASCADE;")
}
15 changes: 14 additions & 1 deletion handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ func index(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
redirect(w, r, "/")
}

/*func createUser(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Create a user
u1 := User{Username: "admin", Password: "admin"}
db.Create(&u1)
fmt.Fprintln(w, "user created")
}*/

func home(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
lists, err := listLists()
if err != nil {
Expand Down Expand Up @@ -142,7 +149,7 @@ func importHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
for _, v := range youtubes {
// Already exists in library, so filter it out.
if m, err := loadMedia(v.ID); err == nil {
if m.hasAudio() || archive.InProgress(m.ID) {
if m.HasAudio() || archive.InProgress(m.ID) {
continue
}
}
Expand Down Expand Up @@ -432,6 +439,9 @@ func playList(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
res := newResponse(r, ps)
res.Section = "play"
res.List = list
var medias []*Media
db.Model(&list).Related(&medias, "Medias")
res.Medias = medias
html(w, "play.html", res)
}

Expand Down Expand Up @@ -517,6 +527,9 @@ func editList(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
res := newResponse(r, ps)
res.Section = "edit"
res.List = list
var medias []*Media
db.Model(&list).Related(&medias, "Medias")
res.Medias = medias
html(w, "edit.html", res)
}

Expand Down
8 changes: 4 additions & 4 deletions internal/youtube/cmd/youtube.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package main

import (
"os"
"fmt"
"github.com/streamlist/streamlist/internal/youtube"
"os"
)

func main() {
youtube.SetDebug()
youtube.SetDebug()

videos, err := youtube.Search(os.Args[1])
if err != nil {
fmt.Println(err)
os.Exit(1)
fmt.Println(err)
os.Exit(1)
}

for _, v := range videos {
Expand Down
19 changes: 17 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
"crypto/rand"
"crypto/sha512"
"crypto/tls"
"encoding/hex"
"flag"
"fmt"
"net"
Expand Down Expand Up @@ -34,7 +36,6 @@ var (
httpAdmins arrayFlags
httpAdminUsers []string
httpReadOnlys arrayFlags
httpReadOnlyUsers []string
httpHost string
httpPrefix string
letsencrypt bool
Expand All @@ -60,6 +61,7 @@ var (
)

func init() {
dbInit()
cli.StringVar(&backlink, "backlink", "", "backlink (optional)")
cli.StringVar(&datadir, "data-dir", "/data", "data directory")
cli.BoolVar(&debug, "debug", false, "debug mode")
Expand All @@ -78,15 +80,25 @@ func main() {

cli.Parse(os.Args[1:])

// Create users in db if not exists, or set password and role if needed
for _, httpUser := range httpAdmins {
split := strings.Split(httpUser, ":")
httpUsername := split[0]
httpUserPassword := split[1]
hasher := sha512.New()
hasher.Write([]byte(httpUserPassword))
httpAdminUsers = append(httpAdminUsers, httpUsername)
var user User
db.Where(User{Username: httpUsername}).Assign(User{Password: hex.EncodeToString(hasher.Sum(nil)), Role: "admin"}).FirstOrCreate(&user)
}
for _, httpUser := range httpReadOnlys {
split := strings.Split(httpUser, ":")
httpUsername := split[0]
httpReadOnlyUsers = append(httpReadOnlyUsers, httpUsername)
httpUserPassword := split[1]
hasher := sha512.New()
hasher.Write([]byte(httpUserPassword))
var user User
db.Where(User{Username: httpUsername}).Assign(User{Password: hex.EncodeToString(hasher.Sum(nil)), Role: "readonly"}).FirstOrCreate(&user)
}

// logtailer
Expand Down Expand Up @@ -178,6 +190,9 @@ func main() {
r.GET(prefix("/logs"), log(auth(logs, "admin")))
r.GET(prefix("/"), log(auth(home, "readonly")))

// User
//r.GET(prefix("/user/create"), log(auth(createUser, "none")))

// Library
r.GET(prefix("/library"), log(auth(library, "readonly")))

Expand Down
Loading