Compare commits
15 Commits
new_rss
...
article_db
Author | SHA1 | Date | |
---|---|---|---|
42596756de | |||
8530c76f2d | |||
c6b2a17220 | |||
88e0d5086c | |||
fa5f189cda | |||
4d65be195b | |||
582f25bec7 | |||
052d36b01b | |||
ea45da66b7 | |||
3822a3f30e | |||
f1abb9d353 | |||
6baaec5b33 | |||
4aa4fff5e8 | |||
a9c61c5a11 | |||
dd50c4f385 |
@ -1,4 +1,4 @@
|
|||||||
package data
|
package control
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
50
cmd/control/rss.go
Normal file
50
cmd/control/rss.go
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
package control
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.streifling.com/jason/rss"
|
||||||
|
"streifling.com/jason/cpolis/cmd/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetChannel(db *model.DB, title, link, desc string) (*rss.Channel, error) {
|
||||||
|
channel := &rss.Channel{
|
||||||
|
Title: title,
|
||||||
|
Link: link,
|
||||||
|
Description: desc,
|
||||||
|
Items: make([]*rss.Item, 0),
|
||||||
|
}
|
||||||
|
|
||||||
|
articles, err := db.GetCertainArticles(true)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error fetching published articles: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, article := range articles {
|
||||||
|
tags, err := db.GetArticleTags(article.ID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error fetching tags for article %v: %v", article.Title, err)
|
||||||
|
}
|
||||||
|
tagNames := make([]string, 0)
|
||||||
|
for _, tag := range tags {
|
||||||
|
tagNames = append(tagNames, tag.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
user, err := db.GetUser(article.AuthorID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error finding user %v: %v", article.AuthorID, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
channel.Items = append(channel.Items, &rss.Item{
|
||||||
|
Title: article.Title,
|
||||||
|
Author: user.FirstName + user.LastName,
|
||||||
|
PubDate: article.Created.Format(time.RFC1123Z),
|
||||||
|
Description: article.Desc,
|
||||||
|
Content: &rss.Content{Value: article.Content},
|
||||||
|
Categories: tagNames,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return channel, nil
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package data
|
package control
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
@ -17,8 +17,7 @@ type CookieStore struct {
|
|||||||
func NewKey() ([]byte, error) {
|
func NewKey() ([]byte, error) {
|
||||||
key := make([]byte, 32)
|
key := make([]byte, 32)
|
||||||
|
|
||||||
_, err := io.ReadFull(rand.Reader, key)
|
if _, err := io.ReadFull(rand.Reader, key); err != nil {
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error generating key: %v", err)
|
return nil, fmt.Errorf("error generating key: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,9 +32,7 @@ func SaveKey(key []byte, filename string) error {
|
|||||||
defer file.Close()
|
defer file.Close()
|
||||||
file.Chmod(0600)
|
file.Chmod(0600)
|
||||||
|
|
||||||
encoder := gob.NewEncoder(file)
|
if err = gob.NewEncoder(file).Encode(key); err != nil {
|
||||||
err = encoder.Encode(key)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error ecoding key: %v", err)
|
return fmt.Errorf("error ecoding key: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,9 +46,7 @@ func LoadKey(filename string) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
key := make([]byte, 32)
|
key := make([]byte, 32)
|
||||||
decoder := gob.NewDecoder(file)
|
if err = gob.NewDecoder(file).Decode(&key); err != nil {
|
||||||
err = decoder.Decode(&key)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error decoding key: %v", err)
|
return nil, fmt.Errorf("error decoding key: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -1,202 +0,0 @@
|
|||||||
package data
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/gob"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Article struct {
|
|
||||||
Title string
|
|
||||||
Author string
|
|
||||||
Created time.Time
|
|
||||||
Desc string
|
|
||||||
Content string
|
|
||||||
Tags []string
|
|
||||||
UUID uuid.UUID
|
|
||||||
AuthorID int64
|
|
||||||
}
|
|
||||||
|
|
||||||
type ArticleList struct {
|
|
||||||
addCh chan *Article
|
|
||||||
delCh chan uuid.UUID
|
|
||||||
retCh chan *Article
|
|
||||||
getCh chan []Article
|
|
||||||
articles []*Article
|
|
||||||
wg sync.WaitGroup
|
|
||||||
}
|
|
||||||
|
|
||||||
type TagList struct {
|
|
||||||
addCh chan string
|
|
||||||
getCh chan []string
|
|
||||||
tags []string
|
|
||||||
wg sync.WaitGroup
|
|
||||||
}
|
|
||||||
|
|
||||||
func minArticleList() *ArticleList {
|
|
||||||
return &ArticleList{
|
|
||||||
addCh: make(chan *Article),
|
|
||||||
delCh: make(chan uuid.UUID),
|
|
||||||
retCh: make(chan *Article),
|
|
||||||
getCh: make(chan []Article),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func minTagList() *TagList {
|
|
||||||
return &TagList{
|
|
||||||
addCh: make(chan string),
|
|
||||||
getCh: make(chan []string),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (al *ArticleList) start() {
|
|
||||||
al.wg.Done()
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case article := <-al.addCh:
|
|
||||||
al.articles = append(al.articles, article)
|
|
||||||
case uuid := <-al.delCh:
|
|
||||||
for i, article := range al.articles {
|
|
||||||
if article.UUID == uuid {
|
|
||||||
al.articles = append(al.articles[:i], al.articles[i+1:]...)
|
|
||||||
al.retCh <- article
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case al.getCh <- func() []Article {
|
|
||||||
var list []Article
|
|
||||||
for _, article := range al.articles {
|
|
||||||
list = append(list, *article)
|
|
||||||
}
|
|
||||||
return list
|
|
||||||
}():
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tl *TagList) start() {
|
|
||||||
tl.wg.Done()
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case tag := <-tl.addCh:
|
|
||||||
tl.tags = append(tl.tags, tag)
|
|
||||||
case tl.getCh <- tl.tags:
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewArticleList() *ArticleList {
|
|
||||||
list := minArticleList()
|
|
||||||
list.articles = []*Article{}
|
|
||||||
|
|
||||||
list.wg.Add(1)
|
|
||||||
go list.start()
|
|
||||||
list.wg.Wait()
|
|
||||||
|
|
||||||
return list
|
|
||||||
}
|
|
||||||
|
|
||||||
func (al *ArticleList) Add(a *Article) {
|
|
||||||
al.addCh <- a
|
|
||||||
}
|
|
||||||
|
|
||||||
func (al *ArticleList) Release(uuid uuid.UUID) (*Article, bool) {
|
|
||||||
al.delCh <- uuid
|
|
||||||
article := <-al.retCh
|
|
||||||
if article == nil {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
return article, true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (al *ArticleList) Get() []Article {
|
|
||||||
return <-al.getCh
|
|
||||||
}
|
|
||||||
|
|
||||||
func (al *ArticleList) Save(filename string) error {
|
|
||||||
file, err := os.Create(filename)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error creating key file: %v", err)
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
encoder := gob.NewEncoder(file)
|
|
||||||
articles := al.Get()
|
|
||||||
err = encoder.Encode(articles)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error ecoding key: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func LoadArticleList(filename string) (*ArticleList, error) {
|
|
||||||
file, err := os.Open(filename)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error opening key file: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
decoder := gob.NewDecoder(file)
|
|
||||||
articleList := NewArticleList()
|
|
||||||
err = decoder.Decode(&articleList.articles)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error decoding key: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return articleList, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewTagList() *TagList {
|
|
||||||
list := minTagList()
|
|
||||||
list.tags = []string{}
|
|
||||||
|
|
||||||
list.wg.Add(1)
|
|
||||||
go list.start()
|
|
||||||
list.wg.Wait()
|
|
||||||
|
|
||||||
return list
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tl *TagList) Add(tag string) {
|
|
||||||
tl.addCh <- tag
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tl *TagList) Get() []string {
|
|
||||||
return <-tl.getCh
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tl *TagList) Save(filename string) error {
|
|
||||||
file, err := os.Create(filename)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error creating key file: %v", err)
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
encoder := gob.NewEncoder(file)
|
|
||||||
tags := tl.Get()
|
|
||||||
err = encoder.Encode(tags)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error ecoding key: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func LoadTagList(filename string) (*TagList, error) {
|
|
||||||
file, err := os.Open(filename)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error opening key file: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
decoder := gob.NewDecoder(file)
|
|
||||||
tagList := NewTagList()
|
|
||||||
err = decoder.Decode(&tagList.tags)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error decoding key: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return tagList, nil
|
|
||||||
}
|
|
143
cmd/data/db.go
143
cmd/data/db.go
@ -1,143 +0,0 @@
|
|||||||
package data
|
|
||||||
|
|
||||||
import (
|
|
||||||
"database/sql"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/go-sql-driver/mysql"
|
|
||||||
"golang.org/x/crypto/bcrypt"
|
|
||||||
)
|
|
||||||
|
|
||||||
type DB struct {
|
|
||||||
*sql.DB
|
|
||||||
}
|
|
||||||
|
|
||||||
func OpenDB(dbName string) (*DB, error) {
|
|
||||||
var err error
|
|
||||||
db := DB{DB: &sql.DB{}}
|
|
||||||
|
|
||||||
cfg := mysql.NewConfig()
|
|
||||||
cfg.DBName = dbName
|
|
||||||
cfg.User, cfg.Passwd, err = getCredentials()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error reading user credentials for DB: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
db.DB, err = sql.Open("mysql", cfg.FormatDSN())
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error opening DB: %v", err)
|
|
||||||
}
|
|
||||||
if err = db.Ping(); err != nil {
|
|
||||||
return nil, fmt.Errorf("error pinging DB: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &db, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *DB) AddUser(user *User, pass string) error {
|
|
||||||
hashedPass, err := bcrypt.GenerateFromPassword([]byte(pass), bcrypt.DefaultCost)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error creating password hash: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
query := `
|
|
||||||
INSERT INTO users
|
|
||||||
(username, password, first_name, last_name, role)
|
|
||||||
VALUES (?, ?, ?, ?, ?)
|
|
||||||
`
|
|
||||||
_, err = db.Exec(query, user.UserName, string(hashedPass), user.FirstName, user.LastName, user.Role)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error inserting user into DB: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *DB) GetID(userName string) (int64, error) {
|
|
||||||
var id int64
|
|
||||||
|
|
||||||
query := `
|
|
||||||
SELECT id
|
|
||||||
FROM users
|
|
||||||
WHERE username = ?
|
|
||||||
`
|
|
||||||
row := db.QueryRow(query, userName)
|
|
||||||
if err := row.Scan(&id); err != nil {
|
|
||||||
return 0, fmt.Errorf("user not in DB: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return id, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *DB) CheckPassword(id int64, pass string) error {
|
|
||||||
var queriedPass string
|
|
||||||
|
|
||||||
query := `
|
|
||||||
SELECT password
|
|
||||||
FROM users
|
|
||||||
WHERE id = ?
|
|
||||||
`
|
|
||||||
row := db.QueryRow(query, id)
|
|
||||||
if err := row.Scan(&queriedPass); err != nil {
|
|
||||||
return fmt.Errorf("error reading password from DB: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := bcrypt.CompareHashAndPassword([]byte(queriedPass), []byte(pass)); err != nil {
|
|
||||||
return fmt.Errorf("incorrect password: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *DB) ChangePassword(id int64, oldPass, newPass string) error {
|
|
||||||
if err := db.CheckPassword(id, oldPass); err != nil {
|
|
||||||
return fmt.Errorf("error checking password: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
newHashedPass, err := bcrypt.GenerateFromPassword([]byte(newPass), bcrypt.DefaultCost)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error creating password hash: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
query := `
|
|
||||||
UPDATE users
|
|
||||||
SET password = ?
|
|
||||||
WHERE id = ?
|
|
||||||
`
|
|
||||||
_, err = db.Exec(query, string(newHashedPass), id)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error updating password in DB: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *DB) CountEntries() (int64, error) {
|
|
||||||
var count int64
|
|
||||||
|
|
||||||
query := `SELECT COUNT(*) FROM users`
|
|
||||||
row := db.QueryRow(query)
|
|
||||||
if err := row.Scan(&count); err != nil {
|
|
||||||
return 0, fmt.Errorf("error counting rows in user DB: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return count, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: No need for ID field in general
|
|
||||||
func (db *DB) GetUser(id int64) (*User, error) {
|
|
||||||
user := new(User)
|
|
||||||
query := `
|
|
||||||
SELECT id, username, first_name, last_name, role
|
|
||||||
FROM users
|
|
||||||
WHERE id = ?
|
|
||||||
`
|
|
||||||
|
|
||||||
row := db.QueryRow(query, id)
|
|
||||||
if err := row.Scan(&user.ID, &user.UserName, &user.FirstName,
|
|
||||||
&user.LastName, &user.Role); err != nil {
|
|
||||||
return nil, fmt.Errorf("error reading user information: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return user, nil
|
|
||||||
}
|
|
106
cmd/data/rss.go
106
cmd/data/rss.go
@ -1,106 +0,0 @@
|
|||||||
package data
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/gob"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/gorilla/feeds"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Feed struct {
|
|
||||||
addCh chan *feeds.Item
|
|
||||||
setCh chan feeds.Feed
|
|
||||||
getCh chan feeds.Feed
|
|
||||||
feed feeds.Feed
|
|
||||||
wg sync.WaitGroup
|
|
||||||
}
|
|
||||||
|
|
||||||
func minFeed() *Feed {
|
|
||||||
return &Feed{
|
|
||||||
addCh: make(chan *feeds.Item),
|
|
||||||
setCh: make(chan feeds.Feed),
|
|
||||||
getCh: make(chan feeds.Feed),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Feed) start() {
|
|
||||||
f.wg.Done()
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case item := <-f.addCh:
|
|
||||||
f.feed.Items = append(f.feed.Items, item)
|
|
||||||
case f.getCh <- f.feed:
|
|
||||||
case f.feed = <-f.setCh:
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewFeed(title, link, desc string) *Feed {
|
|
||||||
feed := minFeed()
|
|
||||||
feed.feed = feeds.Feed{
|
|
||||||
Title: title,
|
|
||||||
Link: &feeds.Link{Href: link},
|
|
||||||
Description: desc,
|
|
||||||
}
|
|
||||||
|
|
||||||
feed.wg.Add(1)
|
|
||||||
go feed.start()
|
|
||||||
feed.wg.Wait()
|
|
||||||
|
|
||||||
return feed
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Feed) Get() feeds.Feed {
|
|
||||||
return <-f.getCh
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Feed) Set(feed feeds.Feed) {
|
|
||||||
f.setCh <- feed
|
|
||||||
}
|
|
||||||
|
|
||||||
func OpenFeed(filename string) (*Feed, error) {
|
|
||||||
file, err := os.Open(filename)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error opening file %v: %v", filename, err)
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
feed := minFeed()
|
|
||||||
feed.wg.Add(1)
|
|
||||||
go feed.start()
|
|
||||||
feed.wg.Wait()
|
|
||||||
|
|
||||||
decoder := gob.NewDecoder(file)
|
|
||||||
tmpFeed := new(feeds.Feed)
|
|
||||||
err = decoder.Decode(tmpFeed)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("error decoding file %v: %v", filename, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
feed.Set(*tmpFeed)
|
|
||||||
|
|
||||||
return feed, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Feed) Save(filename string) error {
|
|
||||||
file, err := os.Create(filename)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error creating file %v: %v", filename, err)
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
encoder := gob.NewEncoder(file)
|
|
||||||
feed := f.Get()
|
|
||||||
err = encoder.Encode(feed)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error encoding file %v: %v", filename, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Feed) Add(i *feeds.Item) {
|
|
||||||
f.addCh <- i
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
package data
|
|
||||||
|
|
||||||
const (
|
|
||||||
Admin = iota
|
|
||||||
Editor
|
|
||||||
Writer
|
|
||||||
)
|
|
||||||
|
|
||||||
type User struct {
|
|
||||||
UserName string
|
|
||||||
FirstName string
|
|
||||||
LastName string
|
|
||||||
RejectedArticles []*Article
|
|
||||||
ID int64
|
|
||||||
Role int
|
|
||||||
}
|
|
68
cmd/main.go
Normal file
68
cmd/main.go
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/gob"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"streifling.com/jason/cpolis/cmd/control"
|
||||||
|
"streifling.com/jason/cpolis/cmd/model"
|
||||||
|
"streifling.com/jason/cpolis/cmd/view"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
gob.Register(model.User{})
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
logFile, err := os.OpenFile("tmp/cpolis.log",
|
||||||
|
os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
defer logFile.Close()
|
||||||
|
// log.SetOutput(logFile)
|
||||||
|
|
||||||
|
db, err := model.OpenDB("cpolis")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
key, err := control.LoadKey("tmp/key.gob")
|
||||||
|
if err != nil {
|
||||||
|
key, err = control.NewKey()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
control.SaveKey(key, "tmp/key.gob")
|
||||||
|
}
|
||||||
|
store := control.NewCookieStore(key)
|
||||||
|
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.Handle("/web/static/", http.StripPrefix("/web/static/",
|
||||||
|
http.FileServer(http.Dir("web/static/"))))
|
||||||
|
mux.HandleFunc("/", view.HomePage(db, store))
|
||||||
|
|
||||||
|
mux.HandleFunc("GET /create-tag/", view.CreateTag)
|
||||||
|
mux.HandleFunc("GET /create-user/", view.CreateUser)
|
||||||
|
mux.HandleFunc("GET /hub/", view.ShowHub(store))
|
||||||
|
mux.HandleFunc("GET /rss/", view.ShowRSS(
|
||||||
|
db,
|
||||||
|
"Freimaurer Distrikt Niedersachsen und Sachsen-Anhalt",
|
||||||
|
"https://distrikt-ni-st.de",
|
||||||
|
"Freiheit, Gleichheit, Brüderlichkeit, Toleranz und Humanität",
|
||||||
|
))
|
||||||
|
mux.HandleFunc("GET /unpublished-articles/", view.ShowUnpublishedArticles(db))
|
||||||
|
mux.HandleFunc("GET /write-article/", view.WriteArticle(db))
|
||||||
|
|
||||||
|
mux.HandleFunc("POST /add-tag/", view.AddTag(db, store))
|
||||||
|
mux.HandleFunc("POST /add-user/", view.AddUser(db, store))
|
||||||
|
mux.HandleFunc("POST /finish-article/", view.FinishArticle(db, store))
|
||||||
|
mux.HandleFunc("POST /login/", view.Login(db, store))
|
||||||
|
mux.HandleFunc("POST /review-article/", view.ReviewArticle(db, store))
|
||||||
|
mux.HandleFunc("POST /publish-article/", view.PublishArticle(db, store))
|
||||||
|
|
||||||
|
log.Fatalln(http.ListenAndServe(":8080", mux))
|
||||||
|
}
|
342
cmd/model/db.go
Normal file
342
cmd/model/db.go
Normal file
@ -0,0 +1,342 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-sql-driver/mysql"
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DB struct {
|
||||||
|
*sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func OpenDB(dbName string) (*DB, error) {
|
||||||
|
var err error
|
||||||
|
db := DB{DB: &sql.DB{}}
|
||||||
|
|
||||||
|
cfg := mysql.NewConfig()
|
||||||
|
cfg.DBName = dbName
|
||||||
|
cfg.User, cfg.Passwd, err = getCredentials()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error reading user credentials for DB: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
db.DB, err = sql.Open("mysql", cfg.FormatDSN())
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error opening DB: %v", err)
|
||||||
|
}
|
||||||
|
if err = db.Ping(); err != nil {
|
||||||
|
return nil, fmt.Errorf("error pinging DB: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &db, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) UpdateAttribute(table string, id int64, attribute string, val interface{}) error {
|
||||||
|
query := fmt.Sprintf(`
|
||||||
|
UPDATE %s
|
||||||
|
SET %s = ?
|
||||||
|
WHERE id = ?
|
||||||
|
`, table, attribute)
|
||||||
|
if _, err := db.Exec(query, val, id); err != nil {
|
||||||
|
return fmt.Errorf("error updating article in DB: %v", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) AddUser(user *User, pass string) error {
|
||||||
|
hashedPass, err := bcrypt.GenerateFromPassword([]byte(pass), bcrypt.DefaultCost)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error creating password hash: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
query := `
|
||||||
|
INSERT INTO users
|
||||||
|
(username, password, first_name, last_name, role)
|
||||||
|
VALUES (?, ?, ?, ?, ?)
|
||||||
|
`
|
||||||
|
if _, err = db.Exec(query, user.UserName, string(hashedPass), user.FirstName, user.LastName, user.Role); err != nil {
|
||||||
|
return fmt.Errorf("error inserting user into DB: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) GetID(userName string) (int64, error) {
|
||||||
|
var id int64
|
||||||
|
|
||||||
|
query := `
|
||||||
|
SELECT id
|
||||||
|
FROM users
|
||||||
|
WHERE username = ?
|
||||||
|
`
|
||||||
|
row := db.QueryRow(query, userName)
|
||||||
|
if err := row.Scan(&id); err != nil {
|
||||||
|
return 0, fmt.Errorf("user not in DB: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return id, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) CheckPassword(id int64, pass string) error {
|
||||||
|
var queriedPass string
|
||||||
|
|
||||||
|
query := `
|
||||||
|
SELECT password
|
||||||
|
FROM users
|
||||||
|
WHERE id = ?
|
||||||
|
`
|
||||||
|
row := db.QueryRow(query, id)
|
||||||
|
if err := row.Scan(&queriedPass); err != nil {
|
||||||
|
return fmt.Errorf("error reading password from DB: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := bcrypt.CompareHashAndPassword([]byte(queriedPass), []byte(pass)); err != nil {
|
||||||
|
return fmt.Errorf("incorrect password: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) ChangePassword(id int64, oldPass, newPass string) error {
|
||||||
|
tx, err := db.Begin()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error starting transaction: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var queriedPass string
|
||||||
|
getQuery := `
|
||||||
|
SELECT password
|
||||||
|
FROM users
|
||||||
|
WHERE id = ?
|
||||||
|
`
|
||||||
|
row := tx.QueryRow(getQuery, id)
|
||||||
|
if err := row.Scan(&queriedPass); err != nil {
|
||||||
|
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
||||||
|
log.Fatalf("error: transaction error: %v, rollback error: %v", err, rollbackErr)
|
||||||
|
}
|
||||||
|
return fmt.Errorf("error reading password from DB: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := bcrypt.CompareHashAndPassword([]byte(queriedPass), []byte(oldPass)); err != nil {
|
||||||
|
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
||||||
|
log.Fatalf("error: transaction error: %v, rollback error: %v", err, rollbackErr)
|
||||||
|
}
|
||||||
|
return fmt.Errorf("incorrect password: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
newHashedPass, err := bcrypt.GenerateFromPassword([]byte(newPass), bcrypt.DefaultCost)
|
||||||
|
if err != nil {
|
||||||
|
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
||||||
|
log.Fatalf("error: transaction error: %v, rollback error: %v", err, rollbackErr)
|
||||||
|
}
|
||||||
|
return fmt.Errorf("error creating password hash: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
setQuery := `
|
||||||
|
UPDATE users
|
||||||
|
SET password = ?
|
||||||
|
WHERE id = ?
|
||||||
|
`
|
||||||
|
if _, err = tx.Exec(setQuery, string(newHashedPass), id); err != nil {
|
||||||
|
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
||||||
|
log.Fatalf("error: transaction error: %v, rollback error: %v", err, rollbackErr)
|
||||||
|
}
|
||||||
|
return fmt.Errorf("error updating password in DB: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = tx.Commit(); err != nil {
|
||||||
|
return fmt.Errorf("error committing transaction: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) CountEntries() (int64, error) {
|
||||||
|
var count int64
|
||||||
|
|
||||||
|
query := `SELECT COUNT(*) FROM users`
|
||||||
|
row := db.QueryRow(query)
|
||||||
|
if err := row.Scan(&count); err != nil {
|
||||||
|
return 0, fmt.Errorf("error counting rows in user DB: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return count, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: No need for ID field in general
|
||||||
|
func (db *DB) GetUser(id int64) (*User, error) {
|
||||||
|
user := new(User)
|
||||||
|
query := `
|
||||||
|
SELECT id, username, first_name, last_name, role
|
||||||
|
FROM users
|
||||||
|
WHERE id = ?
|
||||||
|
`
|
||||||
|
|
||||||
|
row := db.QueryRow(query, id)
|
||||||
|
if err := row.Scan(&user.ID, &user.UserName, &user.FirstName, &user.LastName, &user.Role); err != nil {
|
||||||
|
return nil, fmt.Errorf("error reading user information: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return user, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) AddTag(tagName string) error {
|
||||||
|
query := "INSERT INTO tags (name) VALUES (?)"
|
||||||
|
if _, err := db.Exec(query, tagName); err != nil {
|
||||||
|
return fmt.Errorf("error inserting tag into DB: %v", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) GetTagList() ([]*Tag, error) {
|
||||||
|
query := "SELECT id, name FROM tags"
|
||||||
|
rows, err := db.Query(query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error querying tags: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tagList := make([]*Tag, 0)
|
||||||
|
for rows.Next() {
|
||||||
|
tag := new(Tag)
|
||||||
|
if err = rows.Scan(&tag.ID, &tag.Name); err != nil {
|
||||||
|
return nil, fmt.Errorf("error scanning tag row: %v", err)
|
||||||
|
}
|
||||||
|
tagList = append(tagList, tag)
|
||||||
|
}
|
||||||
|
|
||||||
|
return tagList, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) AddArticle(a *Article) (int64, error) {
|
||||||
|
query := `
|
||||||
|
INSERT INTO articles
|
||||||
|
(title, description, content, published, author_id)
|
||||||
|
VALUES
|
||||||
|
(?, ?, ?, ?, ?)
|
||||||
|
`
|
||||||
|
|
||||||
|
result, err := db.Exec(query, a.Title, a.Desc, a.Content, a.Published, a.AuthorID)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("error inserting article into DB: %v", err)
|
||||||
|
}
|
||||||
|
id, err := result.LastInsertId()
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("error retrieving last ID: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return id, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) GetArticle(id int64) (*Article, error) {
|
||||||
|
query := `
|
||||||
|
SELECT title, created, description, content, published, author_id
|
||||||
|
FROM articles
|
||||||
|
WHERE id = ?
|
||||||
|
`
|
||||||
|
row := db.QueryRow(query, id)
|
||||||
|
|
||||||
|
article := new(Article)
|
||||||
|
var created []byte
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if err := row.Scan(&article.Title, &created, &article.Desc,
|
||||||
|
&article.Content, &article.Published, &article.AuthorID); err != nil {
|
||||||
|
return nil, fmt.Errorf("error scanning article row: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
article.ID = id
|
||||||
|
article.Created, err = time.Parse("2006-01-02 15:04:05", string(created))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error parsing created: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return article, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) GetCertainArticles(published bool) ([]*Article, error) {
|
||||||
|
query := `
|
||||||
|
SELECT id, title, created, description, content, author_id
|
||||||
|
FROM articles
|
||||||
|
WHERE published = ?
|
||||||
|
`
|
||||||
|
rows, err := db.Query(query, published)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error querying articles: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
articleList := make([]*Article, 0)
|
||||||
|
for rows.Next() {
|
||||||
|
article := new(Article)
|
||||||
|
var created []byte
|
||||||
|
|
||||||
|
if err = rows.Scan(&article.ID, &article.Title, &created, &article.Desc,
|
||||||
|
&article.Content, &article.AuthorID); err != nil {
|
||||||
|
return nil, fmt.Errorf("error scanning article row: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
article.Published = false
|
||||||
|
article.Created, err = time.Parse("2006-01-02 15:04:05", string(created))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error parsing created: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
articleList = append(articleList, article)
|
||||||
|
}
|
||||||
|
|
||||||
|
return articleList, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) WriteArticleTags(articleID int64, tagIDs []int64) error {
|
||||||
|
tx, err := db.Begin()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error starting transaction: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tagID := range tagIDs {
|
||||||
|
query := `
|
||||||
|
INSERT INTO articles_tags (article_id, tag_id)
|
||||||
|
VALUES (?, ?)
|
||||||
|
`
|
||||||
|
if _, err := tx.Exec(query, articleID, tagID); err != nil {
|
||||||
|
if rollbackErr := tx.Rollback(); rollbackErr != nil {
|
||||||
|
log.Fatalf("error: transaction error: %v, rollback error: %v", err, rollbackErr)
|
||||||
|
}
|
||||||
|
return fmt.Errorf("error inserting into articles_tags: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = tx.Commit(); err != nil {
|
||||||
|
return fmt.Errorf("error committing transaction: %v", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *DB) GetArticleTags(articleID int64) ([]*Tag, error) {
|
||||||
|
query := `
|
||||||
|
SELECT t.id, t.name
|
||||||
|
FROM articles a
|
||||||
|
INNER JOIN articles_tags at ON a.id = at.article_id
|
||||||
|
INNER JOIN tags t ON at.tag_id = t.id
|
||||||
|
WHERE a.id = ?
|
||||||
|
`
|
||||||
|
rows, err := db.Query(query, articleID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error querying articles_tags: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tags := make([]*Tag, 0)
|
||||||
|
for rows.Next() {
|
||||||
|
tag := new(Tag)
|
||||||
|
if err = rows.Scan(&tag.ID, &tag.Name); err != nil {
|
||||||
|
return nil, fmt.Errorf("error scanning rows: %v", err)
|
||||||
|
}
|
||||||
|
tags = append(tags, tag)
|
||||||
|
}
|
||||||
|
|
||||||
|
return tags, nil
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package data
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
35
cmd/model/structs.go
Normal file
35
cmd/model/structs.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
Admin = iota
|
||||||
|
Editor
|
||||||
|
Writer
|
||||||
|
)
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
UserName string
|
||||||
|
FirstName string
|
||||||
|
LastName string
|
||||||
|
RejectedArticles []*Article
|
||||||
|
ID int64
|
||||||
|
Role int
|
||||||
|
}
|
||||||
|
|
||||||
|
type Tag struct {
|
||||||
|
Name string
|
||||||
|
ID int64
|
||||||
|
}
|
||||||
|
|
||||||
|
type Article struct {
|
||||||
|
Title string
|
||||||
|
Created time.Time
|
||||||
|
Desc string
|
||||||
|
Content string
|
||||||
|
Published bool
|
||||||
|
ID int64
|
||||||
|
AuthorID int64
|
||||||
|
}
|
@ -1,157 +0,0 @@
|
|||||||
package ui
|
|
||||||
|
|
||||||
import (
|
|
||||||
"html/template"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/gorilla/feeds"
|
|
||||||
"streifling.com/jason/cpolis/cmd/data"
|
|
||||||
)
|
|
||||||
|
|
||||||
func ShowHub(s *data.CookieStore) http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
session, err := s.Get(r, "cookie")
|
|
||||||
if err != nil {
|
|
||||||
tmpl, err := template.ParseFiles("web/templates/login.html")
|
|
||||||
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
tmpl, err := template.ParseFiles("web/templates/hub.html")
|
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", session.Values["role"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func WriteArticle(tl *data.TagList) http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
tmpl, err := template.ParseFiles("web/templates/editor.html")
|
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", tl.Get())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func FinishArticle(al *data.ArticleList, s *data.CookieStore) http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
article := new(data.Article)
|
|
||||||
var err error
|
|
||||||
|
|
||||||
article.Title, err = data.ConvertToPlain(r.PostFormValue("editor-title"))
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
article.Desc, err = data.ConvertToPlain(r.PostFormValue("editor-desc"))
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
article.Content, err = data.ConvertToHTML(r.PostFormValue("editor-text"))
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
session, err := s.Get(r, "cookie")
|
|
||||||
if err != nil {
|
|
||||||
tmpl, err := template.ParseFiles("web/templates/login.html")
|
|
||||||
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
article.UUID = uuid.New()
|
|
||||||
article.Author = session.Values["name"].(string)
|
|
||||||
article.Created = time.Now()
|
|
||||||
article.AuthorID = session.Values["id"].(int64)
|
|
||||||
|
|
||||||
al.Add(article)
|
|
||||||
al.Save("tmp/articles.gob")
|
|
||||||
|
|
||||||
tmpl, err := template.ParseFiles("web/templates/hub.html")
|
|
||||||
tmpl = template.Must(tmpl, err)
|
|
||||||
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ShowUnpublishedArticles(al *data.ArticleList) http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
tmpl, err := template.ParseFiles("web/templates/unpublished-articles.html")
|
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", al.Get())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ReviewArticle(al *data.ArticleList, s *data.CookieStore) http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
uuid, err := uuid.Parse(r.PostFormValue("uuid"))
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, article := range al.Get() {
|
|
||||||
if article.UUID == uuid {
|
|
||||||
tmpl, err := template.ParseFiles("web/templates/to-be-published.html")
|
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", article)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
session, err := s.Get(r, "cookie")
|
|
||||||
if err != nil {
|
|
||||||
tmpl, err := template.ParseFiles("web/templates/login.html")
|
|
||||||
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
tmpl, err := template.ParseFiles("web/templates/hub.html")
|
|
||||||
tmpl = template.Must(tmpl, err)
|
|
||||||
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func PublishArticle(f *data.Feed, al *data.ArticleList, s *data.CookieStore) http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
uuid, err := uuid.Parse(r.PostFormValue("uuid"))
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
article, ok := al.Release(uuid)
|
|
||||||
if !ok {
|
|
||||||
// TODO: Warnung anzeigen
|
|
||||||
// msg = "Alle Felder müssen ausgefüllt werden."
|
|
||||||
// tmpl, err := template.ParseFiles("web/templates/add-user.html")
|
|
||||||
// template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
session, err := s.Get(r, "cookie")
|
|
||||||
if err != nil {
|
|
||||||
tmpl, err := template.ParseFiles("web/templates/login.html")
|
|
||||||
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
f.Add(&feeds.Item{
|
|
||||||
Title: article.Title,
|
|
||||||
Author: &feeds.Author{Name: session.Values["name"].(string)},
|
|
||||||
Created: article.Created,
|
|
||||||
Description: article.Desc,
|
|
||||||
Content: article.Content,
|
|
||||||
})
|
|
||||||
f.Save("tmp/rss.gob")
|
|
||||||
|
|
||||||
tmpl, err := template.ParseFiles("web/templates/hub.html")
|
|
||||||
tmpl = template.Must(tmpl, err)
|
|
||||||
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
package ui
|
|
||||||
|
|
||||||
import (
|
|
||||||
"html/template"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"streifling.com/jason/cpolis/cmd/data"
|
|
||||||
)
|
|
||||||
|
|
||||||
func ShowRSS(f *data.Feed) http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
feed := f.Get()
|
|
||||||
rss, err := feed.ToRss()
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
files := []string{"web/templates/index.html", "web/templates/feed.rss"}
|
|
||||||
tmpl, err := template.ParseFiles(files...)
|
|
||||||
template.Must(tmpl, err).Execute(w, rss)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,4 +1,4 @@
|
|||||||
package ui
|
package view
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -7,15 +7,16 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"streifling.com/jason/cpolis/cmd/data"
|
"streifling.com/jason/cpolis/cmd/control"
|
||||||
|
"streifling.com/jason/cpolis/cmd/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AddUserData struct {
|
type AddUserData struct {
|
||||||
*data.User
|
*model.User
|
||||||
Msg string
|
Msg string
|
||||||
}
|
}
|
||||||
|
|
||||||
func inputsEmpty(user *data.User, pass, pass2 string) bool {
|
func inputsEmpty(user *model.User, pass, pass2 string) bool {
|
||||||
return len(user.UserName) == 0 ||
|
return len(user.UserName) == 0 ||
|
||||||
len(user.FirstName) == 0 ||
|
len(user.FirstName) == 0 ||
|
||||||
len(user.LastName) == 0 ||
|
len(user.LastName) == 0 ||
|
||||||
@ -23,7 +24,7 @@ func inputsEmpty(user *data.User, pass, pass2 string) bool {
|
|||||||
len(pass2) == 0
|
len(pass2) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkUserStrings(user *data.User) (string, int, bool) {
|
func checkUserStrings(user *model.User) (string, int, bool) {
|
||||||
userLen := 15
|
userLen := 15
|
||||||
nameLen := 50
|
nameLen := 50
|
||||||
|
|
||||||
@ -43,7 +44,7 @@ func CreateUser(w http.ResponseWriter, r *http.Request) {
|
|||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddUser(db *data.DB, s *data.CookieStore) http.HandlerFunc {
|
func AddUser(db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
role, err := strconv.Atoi(r.PostFormValue("role"))
|
role, err := strconv.Atoi(r.PostFormValue("role"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -53,7 +54,7 @@ func AddUser(db *data.DB, s *data.CookieStore) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
htmlData := AddUserData{
|
htmlData := AddUserData{
|
||||||
User: &data.User{
|
User: &model.User{
|
||||||
UserName: r.PostFormValue("username"),
|
UserName: r.PostFormValue("username"),
|
||||||
FirstName: r.PostFormValue("first-name"),
|
FirstName: r.PostFormValue("first-name"),
|
||||||
LastName: r.PostFormValue("last-name"),
|
LastName: r.PostFormValue("last-name"),
|
||||||
@ -99,9 +100,9 @@ func AddUser(db *data.DB, s *data.CookieStore) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if num == 0 {
|
if num == 0 {
|
||||||
if htmlData.Role != data.Admin {
|
if htmlData.Role != model.Admin {
|
||||||
htmlData.Msg = "Der erste Benutzer muss ein Administrator sein."
|
htmlData.Msg = "Der erste Benutzer muss ein Administrator sein."
|
||||||
htmlData.Role = data.Admin
|
htmlData.Role = model.Admin
|
||||||
tmpl, err := template.ParseFiles("web/templates/add-user.html")
|
tmpl, err := template.ParseFiles("web/templates/add-user.html")
|
||||||
tmpl = template.Must(tmpl, err)
|
tmpl = template.Must(tmpl, err)
|
||||||
tmpl.ExecuteTemplate(w, "page-content", htmlData)
|
tmpl.ExecuteTemplate(w, "page-content", htmlData)
|
169
cmd/view/articles.go
Normal file
169
cmd/view/articles.go
Normal file
@ -0,0 +1,169 @@
|
|||||||
|
package view
|
||||||
|
|
||||||
|
import (
|
||||||
|
"html/template"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"streifling.com/jason/cpolis/cmd/control"
|
||||||
|
"streifling.com/jason/cpolis/cmd/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ShowHub(s *control.CookieStore) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
session, err := s.Get(r, "cookie")
|
||||||
|
if err != nil {
|
||||||
|
tmpl, err := template.ParseFiles("web/templates/login.html")
|
||||||
|
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
||||||
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpl, err := template.ParseFiles("web/templates/hub.html")
|
||||||
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", session.Values["role"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WriteArticle(db *model.DB) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
tags, err := db.GetTagList()
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpl, err := template.ParseFiles("web/templates/editor.html")
|
||||||
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", tags)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func FinishArticle(db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
article := new(model.Article)
|
||||||
|
var err error
|
||||||
|
|
||||||
|
article.Title, err = control.ConvertToPlain(r.PostFormValue("editor-title"))
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
article.Desc, err = control.ConvertToPlain(r.PostFormValue("editor-desc"))
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
article.Content, err = control.ConvertToHTML(r.PostFormValue("editor-text"))
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
session, err := s.Get(r, "cookie")
|
||||||
|
if err != nil {
|
||||||
|
tmpl, err := template.ParseFiles("web/templates/login.html")
|
||||||
|
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
||||||
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
article.AuthorID = session.Values["id"].(int64)
|
||||||
|
article.ID, err = db.AddArticle(article)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
r.ParseForm()
|
||||||
|
tags := make([]int64, 0)
|
||||||
|
for _, tag := range r.Form["tags"] {
|
||||||
|
tagID, err := strconv.ParseInt(tag, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
tags = append(tags, tagID)
|
||||||
|
}
|
||||||
|
if err = db.WriteArticleTags(article.ID, tags); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpl, err := template.ParseFiles("web/templates/hub.html")
|
||||||
|
tmpl = template.Must(tmpl, err)
|
||||||
|
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ShowUnpublishedArticles(db *model.DB) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
articles, err := db.GetCertainArticles(false)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
tmpl, err := template.ParseFiles("web/templates/unpublished-articles.html")
|
||||||
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", articles)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReviewArticle(db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
id, err := strconv.ParseInt(r.PostFormValue("id"), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
article, err := db.GetArticle(id)
|
||||||
|
if err != nil {
|
||||||
|
tmpl, err := template.ParseFiles("web/templates/to-be-published.html")
|
||||||
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", article)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// session, err := s.Get(r, "cookie")
|
||||||
|
// if err != nil {
|
||||||
|
// tmpl, err := template.ParseFiles("web/templates/login.html")
|
||||||
|
// msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
||||||
|
// template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
||||||
|
// }
|
||||||
|
|
||||||
|
tmpl, err := template.ParseFiles("web/templates/to-be-published.html")
|
||||||
|
tmpl = template.Must(tmpl, err)
|
||||||
|
tmpl.ExecuteTemplate(w, "page-content", article)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func PublishArticle(db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
id, err := strconv.ParseInt(r.PostFormValue("id"), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
session, err := s.Get(r, "cookie")
|
||||||
|
if err != nil {
|
||||||
|
tmpl, err := template.ParseFiles("web/templates/login.html")
|
||||||
|
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
||||||
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
db.UpdateAttribute("articles", id, "published", true)
|
||||||
|
|
||||||
|
tmpl, err := template.ParseFiles("web/templates/hub.html")
|
||||||
|
tmpl = template.Must(tmpl, err)
|
||||||
|
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +1,11 @@
|
|||||||
package ui
|
package view
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"streifling.com/jason/cpolis/cmd/data"
|
"streifling.com/jason/cpolis/cmd/control"
|
||||||
|
"streifling.com/jason/cpolis/cmd/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateTag(w http.ResponseWriter, r *http.Request) {
|
func CreateTag(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -12,10 +13,9 @@ func CreateTag(w http.ResponseWriter, r *http.Request) {
|
|||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddTag(tl *data.TagList, s *data.CookieStore) http.HandlerFunc {
|
func AddTag(db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
tl.Add(r.PostFormValue("tag"))
|
db.AddTag(r.PostFormValue("tag"))
|
||||||
tl.Save("tmp/tags.gob")
|
|
||||||
|
|
||||||
session, err := s.Get(r, "cookie")
|
session, err := s.Get(r, "cookie")
|
||||||
if err != nil {
|
if err != nil {
|
71
cmd/view/rss.go
Normal file
71
cmd/view/rss.go
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
package view
|
||||||
|
|
||||||
|
import (
|
||||||
|
"html/template"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.streifling.com/jason/rss"
|
||||||
|
"streifling.com/jason/cpolis/cmd/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ShowRSS(db *model.DB, title, link, desc string) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
channel := &rss.Channel{
|
||||||
|
Title: title,
|
||||||
|
Link: link,
|
||||||
|
Description: desc,
|
||||||
|
Items: make([]*rss.Item, 0),
|
||||||
|
}
|
||||||
|
|
||||||
|
articles, err := db.GetCertainArticles(true)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, article := range articles {
|
||||||
|
tags, err := db.GetArticleTags(article.ID)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
tagNames := make([]string, 0)
|
||||||
|
for _, tag := range tags {
|
||||||
|
tagNames = append(tagNames, tag.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
user, err := db.GetUser(article.AuthorID)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
channel.Items = append(channel.Items, &rss.Item{
|
||||||
|
Title: article.Title,
|
||||||
|
Author: user.FirstName + user.LastName,
|
||||||
|
PubDate: article.Created.Format(time.RFC1123Z),
|
||||||
|
Description: article.Desc,
|
||||||
|
Content: &rss.Content{Value: article.Content},
|
||||||
|
Categories: tagNames,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
feed := rss.NewFeed()
|
||||||
|
feed.Channels = append(feed.Channels, channel)
|
||||||
|
rss, err := feed.ToXML()
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
files := []string{"web/templates/index.html", "web/templates/feed.rss"}
|
||||||
|
tmpl, err := template.ParseFiles(files...)
|
||||||
|
template.Must(tmpl, err).Execute(w, rss)
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package ui
|
package view
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -6,10 +6,11 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"streifling.com/jason/cpolis/cmd/data"
|
"streifling.com/jason/cpolis/cmd/control"
|
||||||
|
"streifling.com/jason/cpolis/cmd/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func saveSession(w http.ResponseWriter, r *http.Request, s *data.CookieStore, u *data.User) error {
|
func saveSession(w http.ResponseWriter, r *http.Request, s *control.CookieStore, u *model.User) error {
|
||||||
session, err := s.Get(r, "cookie")
|
session, err := s.Get(r, "cookie")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error getting session: %v", err)
|
return fmt.Errorf("error getting session: %v", err)
|
||||||
@ -26,7 +27,7 @@ func saveSession(w http.ResponseWriter, r *http.Request, s *data.CookieStore, u
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func HomePage(db *data.DB, s *data.CookieStore) http.HandlerFunc {
|
func HomePage(db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
numRows, err := db.CountEntries()
|
numRows, err := db.CountEntries()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -53,7 +54,7 @@ func HomePage(db *data.DB, s *data.CookieStore) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Login(db *data.DB, s *data.CookieStore) http.HandlerFunc {
|
func Login(db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
userName := r.PostFormValue("username")
|
userName := r.PostFormValue("username")
|
||||||
password := r.PostFormValue("password")
|
password := r.PostFormValue("password")
|
3
go.mod
3
go.mod
@ -3,9 +3,8 @@ module streifling.com/jason/cpolis
|
|||||||
go 1.22.0
|
go 1.22.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
git.streifling.com/jason/rss v0.0.0-20240305164907-524bf9676188
|
||||||
github.com/go-sql-driver/mysql v1.7.1
|
github.com/go-sql-driver/mysql v1.7.1
|
||||||
github.com/google/uuid v1.6.0
|
|
||||||
github.com/gorilla/feeds v1.1.2
|
|
||||||
github.com/gorilla/sessions v1.2.2
|
github.com/gorilla/sessions v1.2.2
|
||||||
github.com/microcosm-cc/bluemonday v1.0.26
|
github.com/microcosm-cc/bluemonday v1.0.26
|
||||||
github.com/yuin/goldmark v1.7.0
|
github.com/yuin/goldmark v1.7.0
|
||||||
|
12
go.sum
12
go.sum
@ -1,27 +1,19 @@
|
|||||||
|
git.streifling.com/jason/rss v0.0.0-20240305164907-524bf9676188 h1:C8M/j3f+cl5Y7YfGpU/ynb/SC/4tTYMDsyGFt3rswM8=
|
||||||
|
git.streifling.com/jason/rss v0.0.0-20240305164907-524bf9676188/go.mod h1:gpZF0nZbQSstMpyHD9DTAvlQEG7v4pjO5c7aIMWM4Jg=
|
||||||
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
|
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
|
||||||
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
|
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
|
||||||
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
|
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
|
||||||
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
|
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
|
||||||
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
|
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
|
||||||
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
|
||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
|
||||||
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
|
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
|
||||||
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
|
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
|
||||||
github.com/gorilla/feeds v1.1.2 h1:pxzZ5PD3RJdhFH2FsJJ4x6PqMqbgFk1+Vez4XWBW8Iw=
|
|
||||||
github.com/gorilla/feeds v1.1.2/go.mod h1:WMib8uJP3BbY+X8Szd1rA5Pzhdfh+HCCAYT2z7Fza6Y=
|
|
||||||
github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kXD8ePA=
|
github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kXD8ePA=
|
||||||
github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo=
|
github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo=
|
||||||
github.com/gorilla/sessions v1.2.2 h1:lqzMYz6bOfvn2WriPUjNByzeXIlVzURcPmgMczkmTjY=
|
github.com/gorilla/sessions v1.2.2 h1:lqzMYz6bOfvn2WriPUjNByzeXIlVzURcPmgMczkmTjY=
|
||||||
github.com/gorilla/sessions v1.2.2/go.mod h1:ePLdVu+jbEgHH+KWw8I1z2wqd0BAdAQh/8LRvBeoNcQ=
|
github.com/gorilla/sessions v1.2.2/go.mod h1:ePLdVu+jbEgHH+KWw8I1z2wqd0BAdAQh/8LRvBeoNcQ=
|
||||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
|
||||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
|
||||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
|
||||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
|
||||||
github.com/microcosm-cc/bluemonday v1.0.26 h1:xbqSvqzQMeEHCqMi64VAs4d8uy6Mequs3rQ0k/Khz58=
|
github.com/microcosm-cc/bluemonday v1.0.26 h1:xbqSvqzQMeEHCqMi64VAs4d8uy6Mequs3rQ0k/Khz58=
|
||||||
github.com/microcosm-cc/bluemonday v1.0.26/go.mod h1:JyzOCs9gkyQyjs+6h10UEVSe02CGwkhd72Xdqh78TWs=
|
github.com/microcosm-cc/bluemonday v1.0.26/go.mod h1:JyzOCs9gkyQyjs+6h10UEVSe02CGwkhd72Xdqh78TWs=
|
||||||
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
|
|
||||||
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
|
||||||
github.com/yuin/goldmark v1.7.0 h1:EfOIvIMZIzHdB/R/zVrikYLPPwJlfMcNczJFMs1m6sA=
|
github.com/yuin/goldmark v1.7.0 h1:EfOIvIMZIzHdB/R/zVrikYLPPwJlfMcNczJFMs1m6sA=
|
||||||
github.com/yuin/goldmark v1.7.0/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
|
github.com/yuin/goldmark v1.7.0/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
|
||||||
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
|
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
|
||||||
|
79
main.go
79
main.go
@ -1,79 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/gob"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"streifling.com/jason/cpolis/cmd/data"
|
|
||||||
"streifling.com/jason/cpolis/cmd/ui"
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
gob.Register(data.User{})
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
logFile, err := os.OpenFile("tmp/cpolis.log",
|
|
||||||
os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
defer logFile.Close()
|
|
||||||
log.SetOutput(logFile)
|
|
||||||
|
|
||||||
db, err := data.OpenDB("cpolis")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
feed, err := data.OpenFeed("tmp/rss.gob")
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
feed = data.NewFeed("Freimaurer Distrikt Niedersachsen und Sachsen-Anhalt",
|
|
||||||
"https://distrikt-ni-st.de",
|
|
||||||
"Freiheit, Gleichheit, Brüderlichkeit, Toleranz und Humanität")
|
|
||||||
}
|
|
||||||
|
|
||||||
key, err := data.LoadKey("tmp/key.gob")
|
|
||||||
if err != nil {
|
|
||||||
key, err = data.NewKey()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
data.SaveKey(key, "tmp/key.gob")
|
|
||||||
}
|
|
||||||
store := data.NewCookieStore(key)
|
|
||||||
|
|
||||||
articleList, err := data.LoadArticleList("tmp/articles.gob")
|
|
||||||
if err != nil {
|
|
||||||
articleList = data.NewArticleList()
|
|
||||||
}
|
|
||||||
|
|
||||||
tagList, err := data.LoadTagList("tmp/tags.gob")
|
|
||||||
if err != nil {
|
|
||||||
tagList = data.NewTagList()
|
|
||||||
}
|
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
|
||||||
mux.Handle("/web/static/", http.StripPrefix("/web/static/", http.FileServer(http.Dir("web/static/"))))
|
|
||||||
mux.HandleFunc("/", ui.HomePage(db, store))
|
|
||||||
|
|
||||||
mux.HandleFunc("GET /create-tag/", ui.CreateTag)
|
|
||||||
mux.HandleFunc("GET /create-user/", ui.CreateUser)
|
|
||||||
mux.HandleFunc("GET /hub/", ui.ShowHub(store))
|
|
||||||
mux.HandleFunc("GET /rss/", ui.ShowRSS(feed))
|
|
||||||
mux.HandleFunc("GET /unpublished-articles/", ui.ShowUnpublishedArticles(articleList))
|
|
||||||
mux.HandleFunc("GET /write-article/", ui.WriteArticle(tagList))
|
|
||||||
|
|
||||||
mux.HandleFunc("POST /add-tag/", ui.AddTag(tagList, store))
|
|
||||||
mux.HandleFunc("POST /add-user/", ui.AddUser(db, store))
|
|
||||||
mux.HandleFunc("POST /finish-article/", ui.FinishArticle(articleList, store))
|
|
||||||
mux.HandleFunc("POST /login/", ui.Login(db, store))
|
|
||||||
mux.HandleFunc("POST /review-article/", ui.ReviewArticle(articleList, store))
|
|
||||||
mux.HandleFunc("POST /publish-article/", ui.PublishArticle(feed, articleList, store))
|
|
||||||
|
|
||||||
log.Fatalln(http.ListenAndServe(":8080", mux))
|
|
||||||
}
|
|
@ -5,8 +5,8 @@
|
|||||||
<textarea name="editor-desc" placeholder="Beschreibung"></textarea>
|
<textarea name="editor-desc" placeholder="Beschreibung"></textarea>
|
||||||
<textarea name="editor-text" placeholder="Artikel"></textarea>
|
<textarea name="editor-text" placeholder="Artikel"></textarea>
|
||||||
{{range .}}
|
{{range .}}
|
||||||
<input id="{{.}}" name="tags" type="checkbox" value="{{.}}" />
|
<input id="{{.Name}}" name="tags" type="checkbox" value="{{.ID}}" />
|
||||||
<label for="{{.}}">{{.}}</label>
|
<label for="{{.Name}}">{{.Name}}</label>
|
||||||
{{end}}
|
{{end}}
|
||||||
<input type="submit" value="Senden" hx-post="/finish-article/" hx-target="#page-content" />
|
<input type="submit" value="Senden" hx-post="/finish-article/" hx-target="#page-content" />
|
||||||
</form>
|
</form>
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
{{define "page-content"}}
|
{{define "page-content"}}
|
||||||
<form>
|
<form>
|
||||||
<input name="editor-title" type="text" value="{{.Title}}" />
|
<h2>{{.Title}}</h2>
|
||||||
<textarea name="editor-desc">{{.Desc}}</textarea>
|
<p>{{.Desc}}</p>
|
||||||
<textarea name="editor-text">{{.Content}}</textarea>
|
<span>{{.Content}}</span>
|
||||||
<input name="uuid" type="hidden" value="{{.UUID}}" />
|
<input name="id" type="hidden" value="{{.ID}}" />
|
||||||
<input type="submit" value="Veröffentlichen" hx-post="/publish-article/" hx-target="#page-content" />
|
<input type="submit" value="Veröffentlichen" hx-post="/publish-article/" hx-target="#page-content" />
|
||||||
<input type="submit" value="Ablehnen" hx-post="/reject-article/" hx-target="#page-content" />
|
<input type="submit" value="Ablehnen" hx-post="/reject-article/" hx-target="#page-content" />
|
||||||
</form>
|
</form>
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
{{define "page-content"}}
|
{{define "page-content"}}
|
||||||
<form>
|
<form>
|
||||||
{{range .}}
|
{{range .}}
|
||||||
<input required id="{{.UUID}}" name="uuid" type="radio" value="{{.UUID}}" />
|
<input required id="{{.ID}}" name="id" type="radio" value="{{.ID}}" />
|
||||||
<label for="{{.UUID}}">{{.Title}}</label>
|
<label for="{{.ID}}">{{.Title}}</label>
|
||||||
{{end}}
|
{{end}}
|
||||||
<input type="submit" value="Auswählen" hx-post="/review-article/" hx-target="#page-content" />
|
<input type="submit" value="Auswählen" hx-post="/review-article/" hx-target="#page-content" />
|
||||||
</form>
|
</form>
|
||||||
|
Reference in New Issue
Block a user