From 877d8128de488cf3401f3c5c95d613ee3e0e91a3 Mon Sep 17 00:00:00 2001 From: Thomas Krzero Date: Mon, 20 Nov 2017 00:48:14 +0100 Subject: [PATCH 01/11] Init work with gorm --- Dockerfile | 8 +++++--- db.go | 38 ++++++++++++++++++++++++++++++++++++++ handlers.go | 7 +++++++ main.go | 4 ++++ 4 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 db.go diff --git a/Dockerfile b/Dockerfile index da070a9..70b4425 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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" @@ -14,7 +14,9 @@ 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 COPY *.go ./ COPY internal ./internal COPY static ./static @@ -22,7 +24,7 @@ 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 diff --git a/db.go b/db.go new file mode 100644 index 0000000..474993a --- /dev/null +++ b/db.go @@ -0,0 +1,38 @@ +package main + +import ( +// "fmt" + "github.com/jinzhu/gorm" + _ "github.com/jinzhu/gorm/dialects/sqlite" +) + +type User struct { + ID uint + Username string + Password string +} + +var db *gorm.DB + +func dbInit() { + var err error + db, err = gorm.Open("sqlite3", "./gorm.db") + if err != nil { + panic("failed to connect database") + } + //defer db.Close() + + // User + db.AutoMigrate(&User{}) + //var count int + //db.Model(&User{}).Count(&count) + //fmt.Println(count) + //u1 := User{Username: "admin", Password: "admin"} + //u2 := User{Username: "ro", Password: "ro"} + //db.Create(&u1) + //var u3 User // identify a Person type for us to store the results in + //db.First(&u3) // Find the first record in the Database and store it in p3 + //fmt.Println(u1.Username) + //fmt.Println(u2.Username) + //fmt.Println(u3.Username) // print out our record from the database +} diff --git a/handlers.go b/handlers.go index 2cfffec..fd15829 100644 --- a/handlers.go +++ b/handlers.go @@ -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 { diff --git a/main.go b/main.go index 2192008..a063663 100644 --- a/main.go +++ b/main.go @@ -60,6 +60,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") @@ -178,6 +179,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"))) From c3121ffe3b35768cc92c82062eed7044d5be44bb Mon Sep 17 00:00:00 2001 From: Thomas Krzero Date: Mon, 20 Nov 2017 23:00:33 +0100 Subject: [PATCH 02/11] Store Media in DB --- db.go | 14 +------------- streamlist.go | 25 ++++++------------------- 2 files changed, 7 insertions(+), 32 deletions(-) diff --git a/db.go b/db.go index 474993a..1012697 100644 --- a/db.go +++ b/db.go @@ -1,7 +1,6 @@ package main import ( -// "fmt" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/sqlite" ) @@ -22,17 +21,6 @@ func dbInit() { } //defer db.Close() - // User db.AutoMigrate(&User{}) - //var count int - //db.Model(&User{}).Count(&count) - //fmt.Println(count) - //u1 := User{Username: "admin", Password: "admin"} - //u2 := User{Username: "ro", Password: "ro"} - //db.Create(&u1) - //var u3 User // identify a Person type for us to store the results in - //db.First(&u3) // Find the first record in the Database and store it in p3 - //fmt.Println(u1.Username) - //fmt.Println(u2.Username) - //fmt.Println(u3.Username) // print out our record from the database + db.AutoMigrate(&Media{}) } diff --git a/streamlist.go b/streamlist.go index fc88847..51eb985 100644 --- a/streamlist.go +++ b/streamlist.go @@ -199,12 +199,9 @@ func FindMedia(id string) (*Media, error) { // LoadMedia reads media file func loadMedia(id string) (*Media, error) { - b, err := ioutil.ReadFile(mediaFile(id)) - if err != nil { - return nil, err - } var media Media - return &media, json.Unmarshal(b, &media) + db.First(&media, "ID = ?", id) + return &media, db.Error } // ListMedias list medias in library @@ -217,16 +214,10 @@ func ListMedias() ([]*Media, error) { return files[j].ModTime().Before(files[i].ModTime()) }) + var mediasBDD []*Media var medias []*Media - for _, f := range files { - if !strings.HasSuffix(f.Name(), ".media") { - continue - } - // media must exist. - m, err := loadMedia(strings.TrimSuffix(f.Name(), ".media")) - if err != nil { - return nil, err - } + db.Find(&mediasBDD) + for _, m := range mediasBDD { // must have an image file. if !m.hasImage() { continue @@ -241,11 +232,7 @@ func ListMedias() ([]*Media, error) { } func (m Media) save() error { - b, err := json.MarshalIndent(m, "", " ") - if err != nil { - return err - } - return overwrite(m.file(), b, 0644) + return db.Create(&m).Error } func (m Media) file() string { From 86ad8efee20bc7b157e23e984d01807bddc1d8cf Mon Sep 17 00:00:00 2001 From: Thomas Krzero Date: Mon, 20 Nov 2017 23:02:32 +0100 Subject: [PATCH 03/11] gofmt --- db.go | 26 +++++++++++++------------- handlers.go | 4 ++-- internal/youtube/cmd/youtube.go | 8 ++++---- streamlist.go | 2 +- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/db.go b/db.go index 1012697..926875f 100644 --- a/db.go +++ b/db.go @@ -1,26 +1,26 @@ package main import ( - "github.com/jinzhu/gorm" - _ "github.com/jinzhu/gorm/dialects/sqlite" + "github.com/jinzhu/gorm" + _ "github.com/jinzhu/gorm/dialects/sqlite" ) type User struct { - ID uint - Username string - Password string + ID uint + Username string + Password string } var db *gorm.DB func dbInit() { - var err error - db, err = gorm.Open("sqlite3", "./gorm.db") - if err != nil { - panic("failed to connect database") - } - //defer db.Close() + var err error + db, err = gorm.Open("sqlite3", "./gorm.db") + if err != nil { + panic("failed to connect database") + } + //defer db.Close() - db.AutoMigrate(&User{}) - db.AutoMigrate(&Media{}) + db.AutoMigrate(&User{}) + db.AutoMigrate(&Media{}) } diff --git a/handlers.go b/handlers.go index fd15829..9b1dff4 100644 --- a/handlers.go +++ b/handlers.go @@ -98,8 +98,8 @@ func index(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { func createUser(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { // Create a user - u1 := User{Username: "admin", Password: "admin"} - db.Create(&u1) + u1 := User{Username: "admin", Password: "admin"} + db.Create(&u1) fmt.Fprintln(w, "user created") } diff --git a/internal/youtube/cmd/youtube.go b/internal/youtube/cmd/youtube.go index f9facef..c8da65f 100644 --- a/internal/youtube/cmd/youtube.go +++ b/internal/youtube/cmd/youtube.go @@ -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 { diff --git a/streamlist.go b/streamlist.go index 51eb985..7c911f0 100644 --- a/streamlist.go +++ b/streamlist.go @@ -217,7 +217,7 @@ func ListMedias() ([]*Media, error) { var mediasBDD []*Media var medias []*Media db.Find(&mediasBDD) - for _, m := range mediasBDD { + for _, m := range mediasBDD { // must have an image file. if !m.hasImage() { continue From 7a784472aee8530d2047aa3fca3d50085d8c06a9 Mon Sep 17 00:00:00 2001 From: Thomas Krzero Date: Mon, 20 Nov 2017 23:27:49 +0100 Subject: [PATCH 04/11] Init Playlist migration --- db.go | 1 + streamlist.go | 16 ++++++++-------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/db.go b/db.go index 926875f..850012f 100644 --- a/db.go +++ b/db.go @@ -23,4 +23,5 @@ func dbInit() { db.AutoMigrate(&User{}) db.AutoMigrate(&Media{}) + db.AutoMigrate(&List{}) } diff --git a/streamlist.go b/streamlist.go index 7c911f0..ca26fa2 100644 --- a/streamlist.go +++ b/streamlist.go @@ -9,7 +9,7 @@ import ( "os" "path/filepath" "sort" - "strings" + //"strings" "sync" "time" ) @@ -303,12 +303,9 @@ func (l *List) file() string { } func (l *List) save() error { - b, err := json.MarshalIndent(l, "", " ") - if err != nil { - return err - } l.Modified = time.Now() - return overwrite(l.file(), b, 0644) + db.Create(&l) + return db.Error } // HasMedia ... @@ -369,7 +366,7 @@ func findList(id string) (*List, error) { } func listLists() ([]*List, error) { - files, err := ioutil.ReadDir(datadir) + /*files, err := ioutil.ReadDir(datadir) if err != nil { return nil, err } @@ -391,5 +388,8 @@ func listLists() ([]*List, error) { sort.Slice(lists, func(i, j int) bool { return lists[j].Created.Before(lists[i].Created) }) - return lists, nil + return lists, nil*/ + var lists []*List + db.Find(&lists) + return lists, db.Error } From bcfc3603025ce8f261291e2c306df32ea22d2d40 Mon Sep 17 00:00:00 2001 From: Thomas Krzero Date: Tue, 21 Nov 2017 01:08:17 +0100 Subject: [PATCH 05/11] WIP: Store playlist in DB --- db.go | 2 +- streamlist.go | 44 ++++++++++++++++++++++---------------------- templates/home.html | 2 +- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/db.go b/db.go index 850012f..37c4f33 100644 --- a/db.go +++ b/db.go @@ -20,7 +20,7 @@ func dbInit() { panic("failed to connect database") } //defer db.Close() - + db.SingularTable(true) db.AutoMigrate(&User{}) db.AutoMigrate(&Media{}) db.AutoMigrate(&List{}) diff --git a/streamlist.go b/streamlist.go index ca26fa2..7c1f217 100644 --- a/streamlist.go +++ b/streamlist.go @@ -271,7 +271,7 @@ type List struct { ID string `json:"id"` Title string `json:"title"` - Medias []*Media `json:"medias"` + Medias []*Media `json:"medias" gorm:"many2many:list_media"` Modified time.Time `json:"modified"` Created time.Time `json:"created"` @@ -304,27 +304,37 @@ func (l *List) file() string { func (l *List) save() error { l.Modified = time.Now() - db.Create(&l) + db.Where(List{ID: l.ID}).Assign(&l).FirstOrCreate(&l) return db.Error } // HasMedia ... func (l *List) HasMedia(media *Media) bool { - for _, m := range l.Medias { + var medias []Media + db.Model(&l).Related(&medias, "Medias") + for _, m := range medias { if m.ID == media.ID { - return true - } - } - return false + return true + } + } + return false } // TotalLength ... func (l *List) TotalLength() (total int64) { - for _, m := range l.Medias { + var medias []Media + db.Model(&l).Related(&medias, "Medias") + for _, m := range medias { total += m.Length } return total } +// MediasCount ... +func (l *List) MediasCount() int { + var medias []Media + db.Model(&l).Related(&medias, "Medias") + return len(medias) +} func (l *List) shuffleMedia() error { r := rand.New(rand.NewSource(time.Now().Unix())) @@ -345,24 +355,14 @@ func (l *List) removeMedia(media *Media) error { if !l.HasMedia(media) { return nil } - var medias []*Media - for _, m := range l.Medias { - if m.ID == media.ID { - continue - } - medias = append(medias, m) - } - l.Medias = medias - return l.save() + db.Model(&l).Association("Medias").Delete(media) + return db.Error } func findList(id string) (*List, error) { - b, err := ioutil.ReadFile(listFile(id)) - if err != nil { - return nil, err - } var list List - return &list, json.Unmarshal(b, &list) + db.First(&list, "ID = ?", id) + return &list, db.Error } func listLists() ([]*List, error) { diff --git a/templates/home.html b/templates/home.html index 65703eb..a7c5734 100644 --- a/templates/home.html +++ b/templates/home.html @@ -18,7 +18,7 @@

Playlists

- {{len $list.Medias}} + {{$list.MediasCount}} {{with $tl := $list.TotalLength}} From d79ee3342ed1542e5f25235125d55b25c5d318cf Mon Sep 17 00:00:00 2001 From: Thomas Krzero Date: Tue, 21 Nov 2017 01:10:01 +0100 Subject: [PATCH 06/11] Codeclimate --- db.go | 1 + streamlist.go | 17 +++++++++-------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/db.go b/db.go index 37c4f33..0bae253 100644 --- a/db.go +++ b/db.go @@ -5,6 +5,7 @@ import ( _ "github.com/jinzhu/gorm/dialects/sqlite" ) +// User ... type User struct { ID uint Username string diff --git a/streamlist.go b/streamlist.go index 7c1f217..a58a983 100644 --- a/streamlist.go +++ b/streamlist.go @@ -314,26 +314,27 @@ func (l *List) HasMedia(media *Media) bool { db.Model(&l).Related(&medias, "Medias") for _, m := range medias { if m.ID == media.ID { - return true - } - } - return false + return true + } + } + return false } // TotalLength ... func (l *List) TotalLength() (total int64) { var medias []Media - db.Model(&l).Related(&medias, "Medias") + db.Model(&l).Related(&medias, "Medias") for _, m := range medias { total += m.Length } return total } + // MediasCount ... func (l *List) MediasCount() int { - var medias []Media - db.Model(&l).Related(&medias, "Medias") - return len(medias) + var medias []Media + db.Model(&l).Related(&medias, "Medias") + return len(medias) } func (l *List) shuffleMedia() error { From 144d6b01840845130db8e12be84ea345e0592997 Mon Sep 17 00:00:00 2001 From: WTFKr0 Date: Tue, 21 Nov 2017 13:05:42 +0100 Subject: [PATCH 07/11] WIP --- Dockerfile | 3 ++- db.go | 15 ++++++++++++++- handlers.go | 12 +++++++++--- main.go | 2 +- streamlist.go | 17 ++++++++++------- templates/edit.html | 4 ++-- templates/play.html | 4 ++-- templates/view.html | 2 +- 8 files changed, 41 insertions(+), 18 deletions(-) diff --git a/Dockerfile b/Dockerfile index 70b4425..e27dc1a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,7 +16,8 @@ RUN go get \ go.uber.org/zap \ golang.org/x/crypto/acme/autocert \ github.com/jinzhu/gorm \ - github.com/jinzhu/gorm/dialects/sqlite + github.com/jinzhu/gorm/dialects/sqlite \ + github.com/go-sql-driver/mysql COPY *.go ./ COPY internal ./internal COPY static ./static diff --git a/db.go b/db.go index 0bae253..2262b94 100644 --- a/db.go +++ b/db.go @@ -2,7 +2,12 @@ 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 ... @@ -16,7 +21,12 @@ var db *gorm.DB func dbInit() { var err error - db, err = gorm.Open("sqlite3", "./gorm.db") + // 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") } @@ -25,4 +35,7 @@ func dbInit() { db.AutoMigrate(&User{}) db.AutoMigrate(&Media{}) db.AutoMigrate(&List{}) + + // 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;") } diff --git a/handlers.go b/handlers.go index 9b1dff4..8d2cc9e 100644 --- a/handlers.go +++ b/handlers.go @@ -96,12 +96,12 @@ 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) { +/*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() @@ -149,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 } } @@ -439,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) } @@ -524,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) } diff --git a/main.go b/main.go index a063663..3467c24 100644 --- a/main.go +++ b/main.go @@ -180,7 +180,7 @@ func main() { r.GET(prefix("/"), log(auth(home, "readonly"))) // User - r.GET(prefix("/user/create"), log(auth(createUser, "none"))) + //r.GET(prefix("/user/create"), log(auth(createUser, "none"))) // Library r.GET(prefix("/library"), log(auth(library, "readonly"))) diff --git a/streamlist.go b/streamlist.go index a58a983..3c31ec1 100644 --- a/streamlist.go +++ b/streamlist.go @@ -8,7 +8,7 @@ import ( "math/rand" "os" "path/filepath" - "sort" + //"sort" //"strings" "sync" "time" @@ -180,7 +180,9 @@ func DeleteList(id string) error { if err != nil { return err } - return os.Remove(list.file()) + db.Delete(&list) + return db.Error + //return os.Remove(list.file()) } // FindMedia search media in library @@ -206,13 +208,13 @@ func loadMedia(id string) (*Media, error) { // ListMedias list medias in library func ListMedias() ([]*Media, error) { - files, err := ioutil.ReadDir(datadir) + /*files, err := ioutil.ReadDir(datadir) if err != nil { return nil, err } sort.Slice(files, func(i, j int) bool { return files[j].ModTime().Before(files[i].ModTime()) - }) + })*/ var mediasBDD []*Media var medias []*Media @@ -223,7 +225,7 @@ func ListMedias() ([]*Media, error) { continue } // must have an audio file (otherwise it's not finished transcoding) - if !m.hasAudio() { + if !m.HasAudio() { continue } medias = append(medias, m) @@ -261,7 +263,8 @@ func (m Media) hasVideo() bool { return err == nil } -func (m Media) hasAudio() bool { +// HasAudio ... +func (m Media) HasAudio() bool { _, err := os.Stat(m.audioFile()) return err == nil } @@ -271,7 +274,7 @@ type List struct { ID string `json:"id"` Title string `json:"title"` - Medias []*Media `json:"medias" gorm:"many2many:list_media"` + Medias []*Media `json:"medias" gorm:"many2many:list_media;AssociationForeignKey:ID;ForeignKey:ID"` Modified time.Time `json:"modified"` Created time.Time `json:"created"` diff --git a/templates/edit.html b/templates/edit.html index ffe71df..51b5a0c 100644 --- a/templates/edit.html +++ b/templates/edit.html @@ -5,10 +5,10 @@

Edit {{$.List.Title}}

- {{if $.List.Medias}} + {{if $.Medias}} - {{range $media := $.List.Medias}} + {{range $media := $.Medias}}
diff --git a/templates/play.html b/templates/play.html index 6cd6ff9..22947a1 100644 --- a/templates/play.html +++ b/templates/play.html @@ -28,7 +28,7 @@

- {{range $n, $media := $.List.Medias}} + {{range $n, $media := $.Medias}}
@@ -71,7 +71,7 @@

// create playlist var playlist = []; - {{range $media := $.List.Medias}} + {{range $media := $.Medias}} playlist.push('/streamlist/stream/{{$.List.ID}}/{{$media.ID}}.m4a'); {{end}} diff --git a/templates/view.html b/templates/view.html index bf9e4a5..647a08d 100644 --- a/templates/view.html +++ b/templates/view.html @@ -16,7 +16,7 @@

{{$.Media.Title}}

- {{if $.Media.hasAudio}} + {{if $.Media.HasAudio}} Audio {{end}} From 1c35e36c306fad99bd07b0e8dfa305f0d9c6cf68 Mon Sep 17 00:00:00 2001 From: WTFKr0 Date: Tue, 21 Nov 2017 13:11:37 +0100 Subject: [PATCH 08/11] WIP --- Dockerfile | 2 +- db.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index e27dc1a..a7ce563 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,7 +17,7 @@ RUN go get \ golang.org/x/crypto/acme/autocert \ github.com/jinzhu/gorm \ github.com/jinzhu/gorm/dialects/sqlite \ - github.com/go-sql-driver/mysql + github.com/go-sql-driver/mysql COPY *.go ./ COPY internal ./internal COPY static ./static diff --git a/db.go b/db.go index 2262b94..78cb895 100644 --- a/db.go +++ b/db.go @@ -11,11 +11,11 @@ import ( ) // User ... -type User struct { +/*type User struct { ID uint Username string Password string -} +}*/ var db *gorm.DB @@ -32,7 +32,7 @@ func dbInit() { } //defer db.Close() db.SingularTable(true) - db.AutoMigrate(&User{}) + //db.AutoMigrate(&User{}) db.AutoMigrate(&Media{}) db.AutoMigrate(&List{}) From 1cbf8e7a99d3581e8b178daf109068da3967e80b Mon Sep 17 00:00:00 2001 From: WTFKr0 Date: Tue, 21 Nov 2017 16:29:55 +0100 Subject: [PATCH 09/11] Medias order in library --- streamlist.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/streamlist.go b/streamlist.go index 3c31ec1..d5df597 100644 --- a/streamlist.go +++ b/streamlist.go @@ -218,7 +218,7 @@ func ListMedias() ([]*Media, error) { var mediasBDD []*Media var medias []*Media - db.Find(&mediasBDD) + db.Order("modified desc").Find(&mediasBDD) for _, m := range mediasBDD { // must have an image file. if !m.hasImage() { From 622fddc315b296a29a953bb02ba9740f648ebab1 Mon Sep 17 00:00:00 2001 From: Thomas Krzero Date: Thu, 23 Nov 2017 00:06:22 +0100 Subject: [PATCH 10/11] Add users in BDD and hash password --- db.go | 1 + main.go | 15 +++++++++++++-- streamlist.go | 8 ++++++++ web.go | 33 +++++++++++++++------------------ 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/db.go b/db.go index 78cb895..e73052e 100644 --- a/db.go +++ b/db.go @@ -35,6 +35,7 @@ func dbInit() { //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;") diff --git a/main.go b/main.go index 3467c24..ae16da4 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,9 @@ package main import ( "crypto/rand" + "crypto/sha512" "crypto/tls" + "encoding/hex" "flag" "fmt" "net" @@ -34,7 +36,6 @@ var ( httpAdmins arrayFlags httpAdminUsers []string httpReadOnlys arrayFlags - httpReadOnlyUsers []string httpHost string httpPrefix string letsencrypt bool @@ -79,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 diff --git a/streamlist.go b/streamlist.go index d5df597..9c8b7cf 100644 --- a/streamlist.go +++ b/streamlist.go @@ -77,6 +77,14 @@ func (c *Config) Save() error { return overwrite(c.filename, b, 0644) } +// User ... +type User struct { + ID uint + Username string + Password string + Role string +} + // Media represent a media in the library type Media struct { ID string `json:"id"` diff --git a/web.go b/web.go index 7f34d68..146a9d9 100644 --- a/web.go +++ b/web.go @@ -1,6 +1,8 @@ package main import ( + "crypto/sha512" + "encoding/hex" "encoding/json" "encoding/xml" "fmt" @@ -129,31 +131,26 @@ func auth(h httprouter.Handle, role string) httprouter.Handle { // Method: Basic Auth (if we're not behind a reverse proxy, use basic auth) if httpAdmins != nil { - var userList []string - // Admin are always OK - userList = append(userList, httpAdmins...) - // If role readonly, we add readonly users - if role == "readonly" { - userList = append(userList, httpReadOnlys...) - } - for _, httpUser := range userList { - split := strings.Split(httpUser, ":") - httpUsername := split[0] - httpPassword := split[1] - user, password, _ := r.BasicAuth() - if user == httpUsername && password == httpPassword { - ps = append(ps, httprouter.Param{Key: "user", Value: user}) - h(w, r, ps) - return - } + user, password, _ := r.BasicAuth() + // Get user in BDD + var userBDD User + var count int + db.Where("username = ?", user).First(&userBDD).Count(&count) + // Get Hash password + hasher := sha512.New() + hasher.Write([]byte(password)) + if count == 1 && userBDD.Password == hex.EncodeToString(hasher.Sum(nil)) && (userBDD.Role == "admin" || userBDD.Role == role) { + ps = append(ps, httprouter.Param{Key: "user", Value: user}) + h(w, r, ps) + return } + // Else query for auth w.Header().Set("WWW-Authenticate", `Basic realm="Sign-in Required"`) http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) return } // Method: Reverse Proxy (if we're behind a reverse proxy, trust it.) - clientIP, _, err := net.SplitHostPort(r.RemoteAddr) if err != nil { http.NotFound(w, r) From 9f2bf8e5f1e7be99acfcc316dca232407d9c3816 Mon Sep 17 00:00:00 2001 From: Thomas Krzero Date: Thu, 23 Nov 2017 00:09:19 +0100 Subject: [PATCH 11/11] gofmt --- main.go | 2 +- streamlist.go | 6 +++--- web.go | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index ae16da4..9e91cd7 100644 --- a/main.go +++ b/main.go @@ -96,7 +96,7 @@ func main() { httpUsername := split[0] httpUserPassword := split[1] hasher := sha512.New() - hasher.Write([]byte(httpUserPassword)) + hasher.Write([]byte(httpUserPassword)) var user User db.Where(User{Username: httpUsername}).Assign(User{Password: hex.EncodeToString(hasher.Sum(nil)), Role: "readonly"}).FirstOrCreate(&user) } diff --git a/streamlist.go b/streamlist.go index 9c8b7cf..58d67fb 100644 --- a/streamlist.go +++ b/streamlist.go @@ -79,9 +79,9 @@ func (c *Config) Save() error { // User ... type User struct { - ID uint - Username string - Password string + ID uint + Username string + Password string Role string } diff --git a/web.go b/web.go index 146a9d9..acf557a 100644 --- a/web.go +++ b/web.go @@ -137,11 +137,11 @@ func auth(h httprouter.Handle, role string) httprouter.Handle { var count int db.Where("username = ?", user).First(&userBDD).Count(&count) // Get Hash password - hasher := sha512.New() + hasher := sha512.New() hasher.Write([]byte(password)) if count == 1 && userBDD.Password == hex.EncodeToString(hasher.Sum(nil)) && (userBDD.Role == "admin" || userBDD.Role == role) { ps = append(ps, httprouter.Param{Key: "user", Value: user}) - h(w, r, ps) + h(w, r, ps) return } // Else query for auth