108 lines
4.9 KiB
Go
108 lines
4.9 KiB
Go
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"html/template"
|
|
"net/http"
|
|
"net/http/pprof"
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
"github.com/microcosm-cc/bluemonday"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
const (
|
|
msgTemplateError = "Template Error"
|
|
msgDataError = "Data Error"
|
|
msgDBError = "DB Error"
|
|
)
|
|
|
|
type WebData struct {
|
|
router *httprouter.Router
|
|
ugcPolicy *bluemonday.Policy
|
|
tmplHome *template.Template
|
|
tmplStatus *template.Template
|
|
tmplStatuses *template.Template
|
|
tmplUser *template.Template
|
|
tmplUsers *template.Template
|
|
tmplDomain *template.Template
|
|
tmplDomains *template.Template
|
|
stmtGetStatusByID *sql.Stmt
|
|
stmtGetStatuses *sql.Stmt
|
|
stmtGetStatusesByUser *sql.Stmt
|
|
stmtGetStatusesByMaxID *sql.Stmt
|
|
stmtGetStatusesByContent *sql.Stmt
|
|
stmtGetUsers *sql.Stmt
|
|
stmtGetUsersByDomain *sql.Stmt
|
|
}
|
|
|
|
func (w *WebData) setup(debug bool) {
|
|
/* Setup XSS Remover (bluemonday) */
|
|
w.ugcPolicy = bluemonday.UGCPolicy()
|
|
|
|
/* Setup Templates */
|
|
w.tmplHome = template.Must(template.ParseFiles("tmpl/home.html", "tmpl/header.html", "tmpl/footer.html"))
|
|
w.tmplStatus = template.Must(template.ParseFiles("tmpl/status.html", "tmpl/header.html", "tmpl/footer.html", "tmpl/status_card.html"))
|
|
w.tmplStatuses = template.Must(template.ParseFiles("tmpl/statuses.html", "tmpl/header.html", "tmpl/footer.html", "tmpl/status_card.html"))
|
|
w.tmplDomain = template.Must(template.ParseFiles("tmpl/domain.html", "tmpl/header.html", "tmpl/footer.html"))
|
|
w.tmplDomains = template.Must(template.ParseFiles("tmpl/domains.html", "tmpl/header.html", "tmpl/footer.html"))
|
|
w.tmplUser = template.Must(template.ParseFiles("tmpl/user.html", "tmpl/header.html", "tmpl/footer.html", "tmpl/status_card.html"))
|
|
w.tmplUsers = template.Must(template.ParseFiles("tmpl/users.html", "tmpl/header.html", "tmpl/footer.html"))
|
|
|
|
/* Setup Template-specific DB Queries */
|
|
var err error
|
|
w.stmtGetStatusByID, err = db.Prepare("SELECT id,user_id,domain,status,archiveis,archiveorg,archiveis_json,archiveorg_json FROM statuses WHERE id=$1")
|
|
if err != nil {
|
|
log.Panic().Err(err).Msg(msgPrepareSQLFailed)
|
|
}
|
|
w.stmtGetStatuses, err = db.Prepare("SELECT id,user_id,domain,status,archiveis,archiveorg,archiveis_json,archiveorg_json FROM statuses WHERE id LIKE $1 ORDER BY id DESC LIMIT 1000")
|
|
if err != nil {
|
|
log.Panic().Err(err).Msg(msgPrepareSQLFailed)
|
|
}
|
|
w.stmtGetStatusesByUser, err = db.Prepare("SELECT id,user_id,domain,status,archiveis,archiveorg,archiveis_json,archiveorg_json FROM statuses WHERE user_id=$1 AND domain=$2 ORDER BY id DESC LIMIT 1000")
|
|
if err != nil {
|
|
log.Panic().Err(err).Msg(msgPrepareSQLFailed)
|
|
}
|
|
w.stmtGetStatusesByMaxID, err = db.Prepare("SELECT id,user_id,domain,status,archiveis,archiveorg,archiveis_json,archiveorg_json FROM statuses WHERE id < $1 ORDER BY id DESC LIMIT 1000")
|
|
if err != nil {
|
|
log.Panic().Err(err).Msg(msgPrepareSQLFailed)
|
|
}
|
|
w.stmtGetStatusesByContent, err = db.Prepare("SELECT id,user_id,domain,status,archiveis,archiveorg,archiveis_json,archiveorg_json FROM statuses WHERE status->>'content' LIKE $1 ORDER BY status->'created_at' DESC LIMIT 1000")
|
|
if err != nil {
|
|
log.Panic().Err(err).Msg(msgPrepareSQLFailed)
|
|
}
|
|
w.stmtGetUsers, err = db.Prepare("SELECT id,user_id,username,domain,last_status FROM users")
|
|
if err != nil {
|
|
log.Panic().Err(err).Msg(msgPrepareSQLFailed)
|
|
}
|
|
w.stmtGetUsersByDomain, err = db.Prepare("SELECT id,user_id,username,domain,last_status FROM users WHERE domain=$1")
|
|
if err != nil {
|
|
log.Panic().Err(err).Msg(msgPrepareSQLFailed)
|
|
}
|
|
|
|
/* Setup Router */
|
|
w.router = httprouter.New()
|
|
if debug {
|
|
w.router.HandlerFunc(http.MethodGet, "/debug/pprof/", pprof.Index)
|
|
w.router.HandlerFunc(http.MethodGet, "/debug/pprof/cmdline", pprof.Cmdline)
|
|
w.router.HandlerFunc(http.MethodGet, "/debug/pprof/profile", pprof.Profile)
|
|
w.router.HandlerFunc(http.MethodGet, "/debug/pprof/symbol", pprof.Symbol)
|
|
w.router.HandlerFunc(http.MethodGet, "/debug/pprof/trace", pprof.Trace)
|
|
w.router.Handler(http.MethodGet, "/debug/pprof/allocs", pprof.Handler("allocs"))
|
|
w.router.Handler(http.MethodGet, "/debug/pprof/block", pprof.Handler("block"))
|
|
w.router.Handler(http.MethodGet, "/debug/pprof/goroutine", pprof.Handler("goroutine"))
|
|
w.router.Handler(http.MethodGet, "/debug/pprof/heap", pprof.Handler("heap"))
|
|
w.router.Handler(http.MethodGet, "/debug/pprof/mutex", pprof.Handler("mutex"))
|
|
w.router.Handler(http.MethodGet, "/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
|
|
}
|
|
w.router.GET("/", w.httpHome)
|
|
w.router.GET("/statuses/:id", w.httpStatus)
|
|
w.router.GET("/statuses", w.httpStatuses)
|
|
w.router.GET("/users", w.httpUsers)
|
|
w.router.GET("/domains", w.httpDomains)
|
|
w.router.GET("/domains/:domain", w.httpDomain)
|
|
w.router.GET("/domains/:domain/:user", w.httpUser)
|
|
w.router.GET("/search/statuses", w.httpSearchStatuses)
|
|
w.router.ServeFiles("/static/*filepath", http.Dir("static"))
|
|
}
|