new styling for frontpage, update login and authorize templates (#46)

* new styling for frontpage, update login and authorize templates

* run go fmt

* add AssetBaseDir to command flag parsing

* untested: move landing page to it's own router

* go fmt, fix typo

* fix package, adapt to proper Route structure
This commit is contained in:
f0x52
2021-06-21 19:46:10 +02:00
committed by GitHub
parent a5fd6f427b
commit f9bc305aca
22 changed files with 963 additions and 62 deletions

View File

@ -44,6 +44,7 @@ import (
timelineprocessing "github.com/superseriousbusiness/gotosocial/internal/timeline"
"github.com/superseriousbusiness/gotosocial/internal/transport"
"github.com/superseriousbusiness/gotosocial/internal/typeutils"
"github.com/superseriousbusiness/gotosocial/internal/web"
)
var models []interface{} = []interface{}{
@ -123,6 +124,7 @@ var Start cliactions.GTSAction = func(ctx context.Context, c *config.Config, log
appsModule := app.New(c, processor, log)
followRequestsModule := followrequest.New(c, processor, log)
webfingerModule := webfinger.New(c, processor, log)
webBaseModule := web.New(c, processor, log)
usersModule := user.New(c, processor, log)
timelineModule := timeline.New(c, processor, log)
notificationModule := notification.New(c, processor, log)
@ -143,6 +145,7 @@ var Start cliactions.GTSAction = func(ctx context.Context, c *config.Config, log
authModule,
// now everything else
webBaseModule,
accountModule,
instanceModule,
appsModule,

View File

@ -170,6 +170,11 @@ func (c *Config) ParseCLIFlags(f KeyedFlags) error {
c.TemplateConfig.BaseDir = f.String(fn.TemplateBaseDir)
}
// template flags
if c.TemplateConfig.AssetBaseDir == "" || f.IsSet(fn.AssetBaseDir) {
c.TemplateConfig.AssetBaseDir = f.String(fn.AssetBaseDir)
}
// accounts flags
if f.IsSet(fn.AccountsOpenRegistration) {
c.AccountsConfig.OpenRegistration = f.Bool(fn.AccountsOpenRegistration)
@ -283,6 +288,7 @@ type Flags struct {
DbDatabase string
TemplateBaseDir string
AssetBaseDir string
AccountsOpenRegistration string
AccountsApprovalRequired string
@ -326,6 +332,7 @@ type Defaults struct {
DbDatabase string
TemplateBaseDir string
AssetBaseDir string
AccountsOpenRegistration bool
AccountsRequireApproval bool
@ -371,6 +378,7 @@ func GetFlagNames() Flags {
DbDatabase: "db-database",
TemplateBaseDir: "template-basedir",
AssetBaseDir: "asset-basedir",
AccountsOpenRegistration: "accounts-open-registration",
AccountsApprovalRequired: "accounts-approval-required",
@ -417,6 +425,7 @@ func GetEnvNames() Flags {
DbDatabase: "GTS_DB_DATABASE",
TemplateBaseDir: "GTS_TEMPLATE_BASEDIR",
AssetBaseDir: "GTS_ASSET_BASEDIR",
AccountsOpenRegistration: "GTS_ACCOUNTS_OPEN_REGISTRATION",
AccountsApprovalRequired: "GTS_ACCOUNTS_APPROVAL_REQUIRED",

View File

@ -18,7 +18,8 @@ func TestDefault() *Config {
ApplicationName: defaults.ApplicationName,
},
TemplateConfig: &TemplateConfig{
BaseDir: defaults.TemplateBaseDir,
BaseDir: defaults.TemplateBaseDir,
AssetBaseDir: defaults.AssetBaseDir,
},
AccountsConfig: &AccountsConfig{
OpenRegistration: defaults.AccountsOpenRegistration,
@ -71,7 +72,8 @@ func Default() *Config {
ApplicationName: defaults.ApplicationName,
},
TemplateConfig: &TemplateConfig{
BaseDir: defaults.TemplateBaseDir,
BaseDir: defaults.TemplateBaseDir,
AssetBaseDir: defaults.AssetBaseDir,
},
AccountsConfig: &AccountsConfig{
OpenRegistration: defaults.AccountsOpenRegistration,
@ -124,6 +126,7 @@ func GetDefaults() Defaults {
DbDatabase: "postgres",
TemplateBaseDir: "./web/template/",
AssetBaseDir: "./web/assets/",
AccountsOpenRegistration: true,
AccountsRequireApproval: true,

View File

@ -22,4 +22,6 @@ package config
type TemplateConfig struct {
// Directory from which gotosocial will attempt to load html templates (.tmpl files).
BaseDir string `yaml:"baseDir"`
// Directory from which static files are served
AssetBaseDir string `yaml:"assetDir"`
}

85
internal/web/base.go Normal file
View File

@ -0,0 +1,85 @@
/*
GoToSocial
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package web
import (
"fmt"
"net/http"
"os"
"path/filepath"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/api"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/processing"
"github.com/superseriousbusiness/gotosocial/internal/router"
)
type Module struct {
config *config.Config
processor processing.Processor
log *logrus.Logger
}
func New(config *config.Config, processor processing.Processor, log *logrus.Logger) api.ClientModule {
return &Module{
config: config,
log: log,
processor: processor,
}
}
func (m *Module) baseHandler(c *gin.Context) {
l := m.log.WithField("func", "BaseGETHandler")
l.Trace("serving index html")
instance, err := m.processor.InstanceGet(m.config.Host)
if err != nil {
l.Debugf("error getting instance from processor: %s", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "internal server error"})
return
}
// FIXME: fill in more variables?
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"instance": instance,
"countUsers": 3,
"countStatuses": 42069,
"version": "1.0.0",
"adminUsername": "@admin",
})
}
// Route satisfies the RESTAPIModule interface
func (m *Module) Route(s router.Router) error {
// serve static files from /assets
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("error getting current working directory: %s", err)
}
assetPath := filepath.Join(cwd, m.config.TemplateConfig.AssetBaseDir)
s.AttachMiddleware(static.Serve("/assets", static.LocalFile(assetPath, false)))
// serve front-page
s.AttachHandler(http.MethodGet, "/", m.baseHandler)
return nil
}