Changed everything to MVC
This commit is contained in:
35
cmd/control/markdown.go
Normal file
35
cmd/control/markdown.go
Normal file
@ -0,0 +1,35 @@
|
||||
package control
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"github.com/microcosm-cc/bluemonday"
|
||||
"github.com/yuin/goldmark"
|
||||
)
|
||||
|
||||
func ConvertToHTML(md string) (string, error) {
|
||||
var buf bytes.Buffer
|
||||
|
||||
if err := goldmark.Convert([]byte(md), &buf); err != nil {
|
||||
return "", fmt.Errorf("error converting markdown to html: %v", err)
|
||||
}
|
||||
|
||||
p := bluemonday.UGCPolicy()
|
||||
html := p.Sanitize(buf.String())
|
||||
|
||||
return html, nil
|
||||
}
|
||||
|
||||
func ConvertToPlain(md string) (string, error) {
|
||||
var buf bytes.Buffer
|
||||
|
||||
if err := goldmark.Convert([]byte(md), &buf); err != nil {
|
||||
return "", fmt.Errorf("error converting markdown to html: %v", err)
|
||||
}
|
||||
|
||||
p := bluemonday.StrictPolicy()
|
||||
plain := p.Sanitize(buf.String())
|
||||
|
||||
return plain, nil
|
||||
}
|
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/data"
|
||||
)
|
||||
|
||||
func GetChannel(db *data.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
|
||||
}
|
61
cmd/control/sessions.go
Normal file
61
cmd/control/sessions.go
Normal file
@ -0,0 +1,61 @@
|
||||
package control
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/gorilla/sessions"
|
||||
)
|
||||
|
||||
type CookieStore struct {
|
||||
sessions.CookieStore
|
||||
}
|
||||
|
||||
func NewKey() ([]byte, error) {
|
||||
key := make([]byte, 32)
|
||||
|
||||
if _, err := io.ReadFull(rand.Reader, key); err != nil {
|
||||
return nil, fmt.Errorf("error generating key: %v", err)
|
||||
}
|
||||
|
||||
return key, nil
|
||||
}
|
||||
|
||||
func SaveKey(key []byte, filename string) error {
|
||||
file, err := os.Create(filename)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating key file: %v", err)
|
||||
}
|
||||
defer file.Close()
|
||||
file.Chmod(0600)
|
||||
|
||||
if err = gob.NewEncoder(file).Encode(key); err != nil {
|
||||
return fmt.Errorf("error ecoding key: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func LoadKey(filename string) ([]byte, error) {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error opening key file: %v", err)
|
||||
}
|
||||
|
||||
key := make([]byte, 32)
|
||||
if err = gob.NewDecoder(file).Decode(&key); err != nil {
|
||||
return nil, fmt.Errorf("error decoding key: %v", err)
|
||||
}
|
||||
|
||||
return key, nil
|
||||
}
|
||||
|
||||
func NewCookieStore(key []byte) *CookieStore {
|
||||
store := sessions.NewCookieStore(key)
|
||||
store.Options.Secure = true
|
||||
store.Options.HttpOnly = true
|
||||
return &CookieStore{*store}
|
||||
}
|
Reference in New Issue
Block a user