Update header template and add option for custom css

This commit is contained in:
r 2019-12-25 16:38:47 +00:00
parent bde2c03495
commit 656ff3931c
16 changed files with 90 additions and 47 deletions

View File

@ -16,6 +16,7 @@ type config struct {
StaticDirectory string StaticDirectory string
TemplatesGlobPattern string TemplatesGlobPattern string
DatabasePath string DatabasePath string
CustomCSS string
Logfile string Logfile string
} }
@ -41,6 +42,7 @@ func getDefaultConfig() *config {
StaticDirectory: "static", StaticDirectory: "static",
TemplatesGlobPattern: "templates/*", TemplatesGlobPattern: "templates/*",
DatabasePath: "database.db", DatabasePath: "database.db",
CustomCSS: "",
Logfile: "", Logfile: "",
} }
} }
@ -83,6 +85,8 @@ func Parse(r io.Reader) (c *config, err error) {
c.TemplatesGlobPattern = val c.TemplatesGlobPattern = val
case "database_path": case "database_path":
c.DatabasePath = val c.DatabasePath = val
case "custom_css":
c.CustomCSS = val
case "logfile": case "logfile":
c.Logfile = val c.Logfile = val
default: default:

View File

@ -4,4 +4,5 @@ client_scope=read write follow
client_website=http://localhost:8080 client_website=http://localhost:8080
static_directory=static static_directory=static
templates_glob_pattern=templates/* templates_glob_pattern=templates/*
#custom_css=custom.css
database_path=database database_path=database

View File

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"time" "time"
"web/config" "web/config"
@ -52,6 +53,12 @@ func main() {
sessionRepo := repository.NewSessionRepository(sessionDB) sessionRepo := repository.NewSessionRepository(sessionDB)
appRepo := repository.NewAppRepository(appDB) appRepo := repository.NewAppRepository(appDB)
customCSS := config.CustomCSS
if !strings.HasPrefix(customCSS, "http://") &&
!strings.HasPrefix(customCSS, "https://") {
customCSS = "/static/" + customCSS
}
var logger *log.Logger var logger *log.Logger
if len(config.Logfile) < 1 { if len(config.Logfile) < 1 {
logger = log.New(os.Stdout, "", log.LstdFlags) logger = log.New(os.Stdout, "", log.LstdFlags)
@ -64,7 +71,7 @@ func main() {
logger = log.New(lf, "", log.LstdFlags) logger = log.New(lf, "", log.LstdFlags)
} }
s := service.NewService(config.ClientName, config.ClientScope, config.ClientWebsite, renderer, sessionRepo, appRepo) s := service.NewService(config.ClientName, config.ClientScope, config.ClientWebsite, customCSS, renderer, sessionRepo, appRepo)
s = service.NewAuthService(sessionRepo, appRepo, s) s = service.NewAuthService(sessionRepo, appRepo, s)
s = service.NewLoggingService(logger, s) s = service.NewLoggingService(logger, s)
handler := service.NewHandler(s, config.StaticDirectory) handler := service.NewHandler(s, config.StaticDirectory)

View File

@ -5,12 +5,24 @@ import (
"web/model" "web/model"
) )
type HeaderData struct {
Title string
NotificationCount int
CustomCSS string
}
type NavbarData struct { type NavbarData struct {
User *mastodon.Account User *mastodon.Account
NotificationCount int NotificationCount int
} }
type CommonData struct {
HeaderData *HeaderData
NavbarData *NavbarData
}
type TimelineData struct { type TimelineData struct {
*CommonData
Title string Title string
Statuses []*mastodon.Status Statuses []*mastodon.Status
HasNext bool HasNext bool
@ -18,36 +30,35 @@ type TimelineData struct {
HasPrev bool HasPrev bool
PrevLink string PrevLink string
PostContext model.PostContext PostContext model.PostContext
NavbarData *NavbarData
} }
type ThreadData struct { type ThreadData struct {
*CommonData
Statuses []*mastodon.Status Statuses []*mastodon.Status
PostContext model.PostContext PostContext model.PostContext
ReplyMap map[string][]mastodon.ReplyInfo ReplyMap map[string][]mastodon.ReplyInfo
NavbarData *NavbarData
} }
type NotificationData struct { type NotificationData struct {
*CommonData
Notifications []*mastodon.Notification Notifications []*mastodon.Notification
HasNext bool HasNext bool
NextLink string NextLink string
NavbarData *NavbarData
} }
type UserData struct { type UserData struct {
*CommonData
User *mastodon.Account User *mastodon.Account
Statuses []*mastodon.Status Statuses []*mastodon.Status
HasNext bool HasNext bool
NextLink string NextLink string
NavbarData *NavbarData
} }
type AboutData struct { type AboutData struct {
NavbarData *NavbarData *CommonData
} }
type EmojiData struct { type EmojiData struct {
Emojis []*mastodon.Emoji Emojis []*mastodon.Emoji
NavbarData *NavbarData CommonData *CommonData
} }

View File

@ -50,18 +50,20 @@ type service struct {
clientName string clientName string
clientScope string clientScope string
clientWebsite string clientWebsite string
customCSS string
renderer renderer.Renderer renderer renderer.Renderer
sessionRepo model.SessionRepository sessionRepo model.SessionRepository
appRepo model.AppRepository appRepo model.AppRepository
} }
func NewService(clientName string, clientScope string, clientWebsite string, func NewService(clientName string, clientScope string, clientWebsite string,
renderer renderer.Renderer, sessionRepo model.SessionRepository, customCSS string, renderer renderer.Renderer, sessionRepo model.SessionRepository,
appRepo model.AppRepository) Service { appRepo model.AppRepository) Service {
return &service{ return &service{
clientName: clientName, clientName: clientName,
clientScope: clientScope, clientScope: clientScope,
clientWebsite: clientWebsite, clientWebsite: clientWebsite,
customCSS: customCSS,
renderer: renderer, renderer: renderer,
sessionRepo: sessionRepo, sessionRepo: sessionRepo,
appRepo: appRepo, appRepo: appRepo,
@ -272,7 +274,7 @@ func (svc *service) ServeTimelinePage(ctx context.Context, client io.Writer,
DefaultVisibility: c.Session.Settings.DefaultVisibility, DefaultVisibility: c.Session.Settings.DefaultVisibility,
} }
navbarData, err := svc.getNavbarTemplateData(ctx, client, c) commonData, err := svc.getCommonData(ctx, client, c)
if err != nil { if err != nil {
return return
} }
@ -285,7 +287,7 @@ func (svc *service) ServeTimelinePage(ctx context.Context, client io.Writer,
HasPrev: hasPrev, HasPrev: hasPrev,
PrevLink: prevLink, PrevLink: prevLink,
PostContext: postContext, PostContext: postContext,
NavbarData: navbarData, CommonData: commonData,
} }
err = svc.renderer.RenderTimelinePage(ctx, client, data) err = svc.renderer.RenderTimelinePage(ctx, client, data)
@ -349,7 +351,7 @@ func (svc *service) ServeThreadPage(ctx context.Context, client io.Writer, c *mo
addToReplyMap(replyMap, statuses[i].InReplyToID, statuses[i].ID, i+1) addToReplyMap(replyMap, statuses[i].InReplyToID, statuses[i].ID, i+1)
} }
navbarData, err := svc.getNavbarTemplateData(ctx, client, c) commonData, err := svc.getCommonData(ctx, client, c)
if err != nil { if err != nil {
return return
} }
@ -358,7 +360,7 @@ func (svc *service) ServeThreadPage(ctx context.Context, client io.Writer, c *mo
Statuses: statuses, Statuses: statuses,
PostContext: postContext, PostContext: postContext,
ReplyMap: replyMap, ReplyMap: replyMap,
NavbarData: navbarData, CommonData: commonData,
} }
err = svc.renderer.RenderThreadPage(ctx, client, data) err = svc.renderer.RenderThreadPage(ctx, client, data)
@ -409,7 +411,7 @@ func (svc *service) ServeNotificationPage(ctx context.Context, client io.Writer,
nextLink = "/notifications?max_id=" + pg.MaxID nextLink = "/notifications?max_id=" + pg.MaxID
} }
navbarData, err := svc.getNavbarTemplateData(ctx, client, c) commonData, err := svc.getCommonData(ctx, client, c)
if err != nil { if err != nil {
return return
} }
@ -418,7 +420,7 @@ func (svc *service) ServeNotificationPage(ctx context.Context, client io.Writer,
Notifications: notifications, Notifications: notifications,
HasNext: hasNext, HasNext: hasNext,
NextLink: nextLink, NextLink: nextLink,
NavbarData: navbarData, CommonData: commonData,
} }
err = svc.renderer.RenderNotificationPage(ctx, client, data) err = svc.renderer.RenderNotificationPage(ctx, client, data)
if err != nil { if err != nil {
@ -453,7 +455,7 @@ func (svc *service) ServeUserPage(ctx context.Context, client io.Writer, c *mode
nextLink = "/user/" + id + "?max_id=" + pg.MaxID nextLink = "/user/" + id + "?max_id=" + pg.MaxID
} }
navbarData, err := svc.getNavbarTemplateData(ctx, client, c) commonData, err := svc.getCommonData(ctx, client, c)
if err != nil { if err != nil {
return return
} }
@ -463,7 +465,7 @@ func (svc *service) ServeUserPage(ctx context.Context, client io.Writer, c *mode
Statuses: statuses, Statuses: statuses,
HasNext: hasNext, HasNext: hasNext,
NextLink: nextLink, NextLink: nextLink,
NavbarData: navbarData, CommonData: commonData,
} }
err = svc.renderer.RenderUserPage(ctx, client, data) err = svc.renderer.RenderUserPage(ctx, client, data)
@ -475,13 +477,13 @@ func (svc *service) ServeUserPage(ctx context.Context, client io.Writer, c *mode
} }
func (svc *service) ServeAboutPage(ctx context.Context, client io.Writer, c *model.Client) (err error) { func (svc *service) ServeAboutPage(ctx context.Context, client io.Writer, c *model.Client) (err error) {
navbarData, err := svc.getNavbarTemplateData(ctx, client, c) commonData, err := svc.getCommonData(ctx, client, c)
if err != nil { if err != nil {
return return
} }
data := &renderer.AboutData{ data := &renderer.AboutData{
NavbarData: navbarData, CommonData: commonData,
} }
err = svc.renderer.RenderAboutPage(ctx, client, data) err = svc.renderer.RenderAboutPage(ctx, client, data)
if err != nil { if err != nil {
@ -492,7 +494,7 @@ func (svc *service) ServeAboutPage(ctx context.Context, client io.Writer, c *mod
} }
func (svc *service) ServeEmojiPage(ctx context.Context, client io.Writer, c *model.Client) (err error) { func (svc *service) ServeEmojiPage(ctx context.Context, client io.Writer, c *model.Client) (err error) {
navbarData, err := svc.getNavbarTemplateData(ctx, client, c) commonData, err := svc.getCommonData(ctx, client, c)
if err != nil { if err != nil {
return return
} }
@ -504,7 +506,7 @@ func (svc *service) ServeEmojiPage(ctx context.Context, client io.Writer, c *mod
data := &renderer.EmojiData{ data := &renderer.EmojiData{
Emojis: emojis, Emojis: emojis,
NavbarData: navbarData, CommonData: commonData,
} }
err = svc.renderer.RenderEmojiPage(ctx, client, data) err = svc.renderer.RenderEmojiPage(ctx, client, data)
@ -515,27 +517,39 @@ func (svc *service) ServeEmojiPage(ctx context.Context, client io.Writer, c *mod
return return
} }
func (svc *service) getNavbarTemplateData(ctx context.Context, client io.Writer, c *model.Client) (data *renderer.NavbarData, err error) { func (svc *service) getCommonData(ctx context.Context, client io.Writer, c *model.Client) (data *renderer.CommonData, err error) {
notifications, err := c.GetNotifications(ctx, nil) data = new(renderer.CommonData)
if err != nil {
return data.HeaderData = &renderer.HeaderData{
Title: "Web",
NotificationCount: 0,
CustomCSS: svc.customCSS,
} }
var notificationCount int if c != nil && c.Session.IsLoggedIn() {
for i := range notifications { notifications, err := c.GetNotifications(ctx, nil)
if notifications[i].Pleroma != nil && !notifications[i].Pleroma.IsSeen { if err != nil {
notificationCount++ return nil, err
} }
}
u, err := c.GetAccountCurrentUser(ctx) var notificationCount int
if err != nil { for i := range notifications {
return if notifications[i].Pleroma != nil && !notifications[i].Pleroma.IsSeen {
} notificationCount++
}
}
data = &renderer.NavbarData{ u, err := c.GetAccountCurrentUser(ctx)
User: u, if err != nil {
NotificationCount: notificationCount, return nil, err
}
data.NavbarData = &renderer.NavbarData{
User: u,
NotificationCount: notificationCount,
}
data.HeaderData.NotificationCount = notificationCount
} }
return return

3
static/custom.css Normal file
View File

@ -0,0 +1,3 @@
html {
background: #000000;
}

View File

@ -1,4 +1,4 @@
{{template "header.tmpl"}} {{template "header.tmpl" .HeaderData}}
{{template "navigation.tmpl" .NavbarData}} {{template "navigation.tmpl" .NavbarData}}
<div class="page-title"> About </div> <div class="page-title"> About </div>

View File

@ -1,4 +1,4 @@
{{template "header.tmpl"}} {{template "header.tmpl" .HeaderData}}
{{template "navigation.tmpl" .NavbarData}} {{template "navigation.tmpl" .NavbarData}}
<div class="page-title"> Emojis </div> <div class="page-title"> Emojis </div>

View File

@ -1,4 +1,4 @@
{{template "header.tmpl"}} {{template "header.tmpl" .HeaderData}}
<div class="page-title"> Error </div> <div class="page-title"> Error </div>
<div class="error-text"> {{.}} </div> <div class="error-text"> {{.}} </div>
<div> <div>

View File

@ -3,8 +3,11 @@
<head> <head>
<meta charset='utf-8'> <meta charset='utf-8'>
<meta content='width=device-width, initial-scale=1' name='viewport'> <meta content='width=device-width, initial-scale=1' name='viewport'>
<title> Web </title> <title>{{if gt .NotificationCount 0}}({{.NotificationCount}}) {{end}}{{.Title}}</title>
<link rel="stylesheet" href="/static/main.css" /> <link rel="stylesheet" href="/static/main.css">
{{if .CustomCSS}}
<link rel="stylesheet" href="{{.CustomCSS}}">
{{end}}
<link rel="stylesheet" href="/static/fonts/fork-awesome.css"> <link rel="stylesheet" href="/static/fonts/fork-awesome.css">
</head> </head>
<body> <body>

View File

@ -1,4 +1,4 @@
{{template "header.tmpl"}} {{template "header.tmpl" .HeaderData}}
<div class="page-title"> Home </div> <div class="page-title"> Home </div>
<a href="/signin"> Signin </a> <a href="/signin"> Signin </a>
{{template "footer.tmpl"}} {{template "footer.tmpl"}}

View File

@ -1,4 +1,4 @@
{{template "header.tmpl"}} {{template "header.tmpl" .HeaderData}}
{{template "navigation.tmpl" .NavbarData}} {{template "navigation.tmpl" .NavbarData}}
<div class="page-title"> Notifications </div> <div class="page-title"> Notifications </div>

View File

@ -1,4 +1,4 @@
{{template "header.tmpl"}} {{template "header.tmpl" .HeaderData}}
<div class="page-title"> Signin </div> <div class="page-title"> Signin </div>
<form class="signin-form" action="/signin" method="post"> <form class="signin-form" action="/signin" method="post">

View File

@ -1,4 +1,4 @@
{{template "header.tmpl"}} {{template "header.tmpl" .HeaderData}}
{{template "navigation.tmpl" .NavbarData}} {{template "navigation.tmpl" .NavbarData}}
<div class="page-title"> Thread </div> <div class="page-title"> Thread </div>

View File

@ -1,4 +1,4 @@
{{template "header.tmpl"}} {{template "header.tmpl" .HeaderData}}
{{template "navigation.tmpl" .NavbarData}} {{template "navigation.tmpl" .NavbarData}}
<div class="page-title"> {{.Title}} </div> <div class="page-title"> {{.Title}} </div>

View File

@ -1,4 +1,4 @@
{{template "header.tmpl"}} {{template "header.tmpl" .HeaderData}}
{{template "navigation.tmpl" .NavbarData}} {{template "navigation.tmpl" .NavbarData}}
<div class="page-title"> User </div> <div class="page-title"> User </div>