Compare commits
29 Commits
Author | SHA1 | Date | |
---|---|---|---|
e50cb819f3 | |||
365d5a68a1 | |||
b451da7e8e | |||
6a4a592714 | |||
806cfb01bd | |||
c32e38ca10 | |||
c2cadd1542 | |||
4cb2831e9a | |||
ee31a9f8e2 | |||
beec20cdda | |||
7ef957c2d7 | |||
d7c8c7a43a | |||
d56cdc78eb | |||
151d89d9f0 | |||
8d2944d00c | |||
9ddd8198ee | |||
ff6c7a66d7 | |||
129c85929c | |||
b1a6359473 | |||
b5d979dbf8 | |||
1cd3edc90c | |||
77e8edbe16 | |||
0e768c9f61 | |||
1fcd775cc5 | |||
203a1ed147 | |||
ef1914ee5c | |||
084b101e31 | |||
b2db128aa9 | |||
081e880fb6 |
@ -14,6 +14,7 @@ type Config struct {
|
|||||||
DBName string
|
DBName string
|
||||||
Description string
|
Description string
|
||||||
Domain string
|
Domain string
|
||||||
|
FirebaseKey string
|
||||||
KeyFile string
|
KeyFile string
|
||||||
Link string
|
Link string
|
||||||
LogFile string
|
LogFile string
|
||||||
@ -28,6 +29,7 @@ type Config struct {
|
|||||||
func newConfig() *Config {
|
func newConfig() *Config {
|
||||||
return &Config{
|
return &Config{
|
||||||
DBName: "cpolis",
|
DBName: "cpolis",
|
||||||
|
FirebaseKey: "/var/www/cpolis/serviceAccountKey.json",
|
||||||
KeyFile: "/var/www/cpolis/cpolis.key",
|
KeyFile: "/var/www/cpolis/cpolis.key",
|
||||||
LogFile: "/var/log/cpolis.log",
|
LogFile: "/var/log/cpolis.log",
|
||||||
PDFDir: "/var/www/cpolis/pdfs",
|
PDFDir: "/var/www/cpolis/pdfs",
|
||||||
@ -76,19 +78,25 @@ func (c *Config) handleCliArgs() error {
|
|||||||
port := 8080
|
port := 8080
|
||||||
|
|
||||||
flag.StringVar(&c.DBName, "db", c.DBName, "DB name")
|
flag.StringVar(&c.DBName, "db", c.DBName, "DB name")
|
||||||
flag.StringVar(&c.Description, "desc", c.Description, "Channel description")
|
flag.StringVar(&c.Description, "desc", c.Description, "channel description")
|
||||||
flag.StringVar(&c.Domain, "domain", c.Domain, "domain name")
|
flag.StringVar(&c.Domain, "domain", c.Domain, "domain name")
|
||||||
|
flag.StringVar(&c.FirebaseKey, "firebase", c.FirebaseKey, "Firebase service account key file")
|
||||||
flag.StringVar(&c.KeyFile, "key", c.KeyFile, "key file")
|
flag.StringVar(&c.KeyFile, "key", c.KeyFile, "key file")
|
||||||
flag.StringVar(&c.Link, "link", c.Link, "Channel Link")
|
flag.StringVar(&c.Link, "link", c.Link, "channel Link")
|
||||||
flag.StringVar(&c.LogFile, "log", c.LogFile, "log file")
|
flag.StringVar(&c.LogFile, "log", c.LogFile, "log file")
|
||||||
flag.StringVar(&c.PDFDir, "pdfs", c.PDFDir, "pdf directory")
|
flag.StringVar(&c.PDFDir, "pdfs", c.PDFDir, "pdf directory")
|
||||||
flag.StringVar(&c.PicsDir, "pics", c.PicsDir, "pictures directory")
|
flag.StringVar(&c.PicsDir, "pics", c.PicsDir, "pictures directory")
|
||||||
flag.StringVar(&c.RSSFile, "rss", c.RSSFile, "RSS file")
|
flag.StringVar(&c.RSSFile, "rss", c.RSSFile, "RSS file")
|
||||||
flag.StringVar(&c.Title, "title", c.Title, "Channel title")
|
flag.StringVar(&c.Title, "title", c.Title, "channel title")
|
||||||
flag.StringVar(&c.WebDir, "web", c.WebDir, "web directory")
|
flag.StringVar(&c.WebDir, "web", c.WebDir, "web directory")
|
||||||
flag.IntVar(&port, "port", port, "port")
|
flag.IntVar(&port, "port", port, "port")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
c.FirebaseKey, err = filepath.Abs(c.FirebaseKey)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error finding absolute path for Firebase service account key file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
c.KeyFile, err = filepath.Abs(c.KeyFile)
|
c.KeyFile, err = filepath.Abs(c.KeyFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error finding absolute path for key file: %v", err)
|
return fmt.Errorf("error finding absolute path for key file: %v", err)
|
||||||
|
@ -12,12 +12,12 @@ type Client struct {
|
|||||||
*auth.Client
|
*auth.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient() (*Client, error) {
|
func NewClient(c *Config) (*Client, error) {
|
||||||
var err error
|
var err error
|
||||||
client := new(Client)
|
client := new(Client)
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
opt := option.WithCredentialsFile("path/to/serviceAccountKey.json")
|
opt := option.WithCredentialsFile(c.FirebaseKey)
|
||||||
|
|
||||||
app, err := firebase.NewApp(ctx, nil, opt)
|
app, err := firebase.NewApp(ctx, nil, opt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
func ServePDFList(c *b.Config) http.HandlerFunc {
|
func ServePDFList(c *b.Config) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if tokenIsVerified(w, r) {
|
if tokenIsVerified(w, r, c) {
|
||||||
files, err := os.ReadDir(c.PDFDir)
|
files, err := os.ReadDir(c.PDFDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
@ -37,7 +37,7 @@ func ServePDFList(c *b.Config) http.HandlerFunc {
|
|||||||
|
|
||||||
func ServePDF(c *b.Config) http.HandlerFunc {
|
func ServePDF(c *b.Config) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if tokenIsVerified(w, r) {
|
if tokenIsVerified(w, r, c) {
|
||||||
http.ServeFile(w, r, fmt.Sprint(c.PDFDir, "/", r.PathValue("id")))
|
http.ServeFile(w, r, fmt.Sprint(c.PDFDir, "/", r.PathValue("id")))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
func ServeRSS(c *b.Config) http.HandlerFunc {
|
func ServeRSS(c *b.Config) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if tokenIsVerified(w, r) {
|
if tokenIsVerified(w, r, c) {
|
||||||
http.ServeFile(w, r, c.RSSFile)
|
http.ServeFile(w, r, c.RSSFile)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// tokenIsVerified verifies that a request is authorized. It returns a bool.
|
// tokenIsVerified verifies that a request is authorized. It returns a bool.
|
||||||
func tokenIsVerified(w http.ResponseWriter, r *http.Request) bool {
|
func tokenIsVerified(w http.ResponseWriter, r *http.Request, c *b.Config) bool {
|
||||||
idToken := r.Header.Get("Authorization")
|
idToken := r.Header.Get("Authorization")
|
||||||
if idToken == "" {
|
if idToken == "" {
|
||||||
log.Println("Authorization header missing")
|
log.Println("Authorization header missing")
|
||||||
@ -16,7 +16,7 @@ func tokenIsVerified(w http.ResponseWriter, r *http.Request) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := b.NewClient()
|
client, err := b.NewClient(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
@ -23,25 +23,6 @@ const (
|
|||||||
PreviewMode
|
PreviewMode
|
||||||
)
|
)
|
||||||
|
|
||||||
func ShowHub(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
session, err := getSession(w, r, c, s)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
session.Values["article"] = nil
|
|
||||||
if err = session.Save(r, w); err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", session.Values["role"].(int))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func WriteArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
func WriteArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
session, err := getSession(w, r, c, s)
|
session, err := getSession(w, r, c, s)
|
||||||
|
@ -107,3 +107,22 @@ func Logout(c *b.Config, s *b.CookieStore) http.HandlerFunc {
|
|||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ShowHub(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
session, err := getSession(w, r, c, s)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
session.Values["article"] = nil
|
||||||
|
if err = session.Save(r, w); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||||
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", session.Values["role"].(int))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
9
cpolis.service
Normal file
9
cpolis.service
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=cpolis
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
ExecStart=/usr/local/bin/cpolis
|
||||||
|
Restart=on-failure
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=default.target
|
56
update.sh
Executable file
56
update.sh
Executable file
@ -0,0 +1,56 @@
|
|||||||
|
#! /bin/sh -
|
||||||
|
|
||||||
|
CPOLIS_REPO_URL="https://git.streifling.com/api/v1/repos/jason/cpolis/releases"
|
||||||
|
EXTRACTION_DIR=$HOME
|
||||||
|
CPOLIS_DIR=$EXTRACTION_DIR/cpolis
|
||||||
|
TAILWINDCSS_REPO_URL=https://api.github.com/repos/tailwindlabs/tailwindcss/releases/latest
|
||||||
|
TMP_DIR=/tmp
|
||||||
|
BIN_DIR=/usr/local/bin
|
||||||
|
SYSTEMD_DIR=/etc/systemd/system
|
||||||
|
|
||||||
|
check_dependency() {
|
||||||
|
if ! which $1 >/dev/null 2>&1; then
|
||||||
|
echo "$1 needs to be installed" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
if ! groups | grep -E 'root|wheel|sudo' >/dev/null; then
|
||||||
|
echo "You need administrative privileges for this script" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
check_dependency curl
|
||||||
|
check_dependency go
|
||||||
|
check_dependency jq
|
||||||
|
check_dependency tar
|
||||||
|
check_dependency xargs
|
||||||
|
|
||||||
|
echo '\nDownloading cpolis...' >&2
|
||||||
|
rm -fr $CPOLIS_DIR/*
|
||||||
|
latest_release=$(curl -s $CPOLIS_REPO_URL | jq -r '.[0].tag_name')
|
||||||
|
curl -Lo $TMP_DIR/cpolis.tar.gz https://git.streifling.com/jason/cpolis/archive/$latest_release.tar.gz
|
||||||
|
tar -xzf $TMP_DIR/cpolis.tar.gz -C $EXTRACTION_DIR
|
||||||
|
rm $TMP_DIR/cpolis.tar.gz
|
||||||
|
|
||||||
|
echo '\nDownloading TailwindCSS...' >&2
|
||||||
|
curl -s $TAILWINDCSS_REPO_URL |
|
||||||
|
grep -F browser_download_url |
|
||||||
|
grep -F linux-x64 |
|
||||||
|
cut -d'"' -f4 |
|
||||||
|
xargs -r curl -Lo $CPOLIS_DIR/tailwindcss
|
||||||
|
chmod +x $CPOLIS_DIR/tailwindcss
|
||||||
|
$CPOLIS_DIR/tailwindcss -i $CPOLIS_DIR/web/static/css/input.css -o $CPOLIS_DIR/web/static/css/style.css
|
||||||
|
|
||||||
|
echo '\nBuilding cpolis...' >&2
|
||||||
|
go build -o $TMP_DIR/cpolis $CPOLIS_DIR/cmd/main.go
|
||||||
|
|
||||||
|
echo '\nSetting system files up...' >&2
|
||||||
|
sudo mv $TMP_DIR/cpolis $BIN_DIR/cpolis
|
||||||
|
sudo chown root:root $BIN_DIR/cpolis
|
||||||
|
chmod +x $BIN_DIR/cpolis
|
||||||
|
|
||||||
|
sudo mv $CPOLIS_DIR/cpolis.service $SYSTEMD_DIR
|
||||||
|
sudo chown root:root $SYSTEMD_DIR/cpolis.service
|
||||||
|
sudo systemctl daemon-reload
|
||||||
|
sudo systemctl is-active --quiet cpolis.service && sudo systemctl restart cpolis.service
|
Reference in New Issue
Block a user