mirror of https://github.com/writeas/writefreely
commit
cf4f08b264
@ -0,0 +1,61 @@ |
||||
/* |
||||
* Copyright © 2020 A Bunch Tell LLC. |
||||
* |
||||
* This file is part of WriteFreely. |
||||
* |
||||
* WriteFreely is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License, included |
||||
* in the LICENSE file in this source code package. |
||||
*/ |
||||
|
||||
package main |
||||
|
||||
import ( |
||||
"github.com/writeas/writefreely" |
||||
|
||||
"github.com/urfave/cli/v2" |
||||
) |
||||
|
||||
var ( |
||||
cmdConfig cli.Command = cli.Command{ |
||||
Name: "config", |
||||
Usage: "config management tools", |
||||
Subcommands: []*cli.Command{ |
||||
&cmdConfigGenerate, |
||||
&cmdConfigInteractive, |
||||
}, |
||||
} |
||||
|
||||
cmdConfigGenerate cli.Command = cli.Command{ |
||||
Name: "generate", |
||||
Aliases: []string{"gen"}, |
||||
Usage: "Generate a basic configuration", |
||||
Action: genConfigAction, |
||||
} |
||||
|
||||
cmdConfigInteractive cli.Command = cli.Command{ |
||||
Name: "start", |
||||
Usage: "Interactive configuration process", |
||||
Action: interactiveConfigAction, |
||||
Flags: []cli.Flag{ |
||||
&cli.StringFlag{ |
||||
Name: "sections", |
||||
Value: "server db app", |
||||
Usage: "Which sections of the configuration to go through\n" + |
||||
"valid values of sections flag are any combination of 'server', 'db' and 'app' \n" + |
||||
"example: writefreely config start --sections \"db app\"", |
||||
}, |
||||
}, |
||||
} |
||||
) |
||||
|
||||
func genConfigAction(c *cli.Context) error { |
||||
app := writefreely.NewApp(c.String("c")) |
||||
return writefreely.CreateConfig(app) |
||||
} |
||||
|
||||
func interactiveConfigAction(c *cli.Context) error { |
||||
app := writefreely.NewApp(c.String("c")) |
||||
writefreely.DoConfig(app, c.String("sections")) |
||||
return nil |
||||
} |
@ -0,0 +1,50 @@ |
||||
/* |
||||
* Copyright © 2020 A Bunch Tell LLC. |
||||
* |
||||
* This file is part of WriteFreely. |
||||
* |
||||
* WriteFreely is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License, included |
||||
* in the LICENSE file in this source code package. |
||||
*/ |
||||
|
||||
package main |
||||
|
||||
import ( |
||||
"github.com/writeas/writefreely" |
||||
|
||||
"github.com/urfave/cli/v2" |
||||
) |
||||
|
||||
var ( |
||||
cmdDB cli.Command = cli.Command{ |
||||
Name: "db", |
||||
Usage: "db management tools", |
||||
Subcommands: []*cli.Command{ |
||||
&cmdDBInit, |
||||
&cmdDBMigrate, |
||||
}, |
||||
} |
||||
|
||||
cmdDBInit cli.Command = cli.Command{ |
||||
Name: "init", |
||||
Usage: "Initialize Database", |
||||
Action: initDBAction, |
||||
} |
||||
|
||||
cmdDBMigrate cli.Command = cli.Command{ |
||||
Name: "migrate", |
||||
Usage: "Migrate Database", |
||||
Action: migrateDBAction, |
||||
} |
||||
) |
||||
|
||||
func initDBAction(c *cli.Context) error { |
||||
app := writefreely.NewApp(c.String("c")) |
||||
return writefreely.CreateSchema(app) |
||||
} |
||||
|
||||
func migrateDBAction(c *cli.Context) error { |
||||
app := writefreely.NewApp(c.String("c")) |
||||
return writefreely.Migrate(app) |
||||
} |
@ -0,0 +1,39 @@ |
||||
/* |
||||
* Copyright © 2020 A Bunch Tell LLC. |
||||
* |
||||
* This file is part of WriteFreely. |
||||
* |
||||
* WriteFreely is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License, included |
||||
* in the LICENSE file in this source code package. |
||||
*/ |
||||
|
||||
package main |
||||
|
||||
import ( |
||||
"github.com/writeas/writefreely" |
||||
|
||||
"github.com/urfave/cli/v2" |
||||
) |
||||
|
||||
var ( |
||||
cmdKeys cli.Command = cli.Command{ |
||||
Name: "keys", |
||||
Usage: "key management tools", |
||||
Subcommands: []*cli.Command{ |
||||
&cmdGenerateKeys, |
||||
}, |
||||
} |
||||
|
||||
cmdGenerateKeys cli.Command = cli.Command{ |
||||
Name: "generate", |
||||
Aliases: []string{"gen"}, |
||||
Usage: "Generate encryption and authentication keys", |
||||
Action: genKeysAction, |
||||
} |
||||
) |
||||
|
||||
func genKeysAction(c *cli.Context) error { |
||||
app := writefreely.NewApp(c.String("c")) |
||||
return writefreely.GenerateKeyFiles(app) |
||||
} |
@ -0,0 +1,97 @@ |
||||
/* |
||||
* Copyright © 2020 A Bunch Tell LLC. |
||||
* |
||||
* This file is part of WriteFreely. |
||||
* |
||||
* WriteFreely is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License, included |
||||
* in the LICENSE file in this source code package. |
||||
*/ |
||||
|
||||
package main |
||||
|
||||
import ( |
||||
"fmt" |
||||
|
||||
"github.com/writeas/writefreely" |
||||
|
||||
"github.com/urfave/cli/v2" |
||||
) |
||||
|
||||
var ( |
||||
cmdUser cli.Command = cli.Command{ |
||||
Name: "user", |
||||
Usage: "user management tools", |
||||
Subcommands: []*cli.Command{ |
||||
&cmdAddUser, |
||||
&cmdDelUser, |
||||
&cmdResetPass, |
||||
// TODO: possibly add a user list command
|
||||
}, |
||||
} |
||||
|
||||
cmdAddUser cli.Command = cli.Command{ |
||||
Name: "create", |
||||
Usage: "Add new user", |
||||
Aliases: []string{"a", "add"}, |
||||
Flags: []cli.Flag{ |
||||
&cli.BoolFlag{ |
||||
Name: "admin", |
||||
Value: false, |
||||
Usage: "Create admin user", |
||||
}, |
||||
}, |
||||
Action: addUserAction, |
||||
} |
||||
|
||||
cmdDelUser cli.Command = cli.Command{ |
||||
Name: "delete", |
||||
Usage: "Delete user", |
||||
Aliases: []string{"del", "d"}, |
||||
Action: delUserAction, |
||||
} |
||||
|
||||
cmdResetPass cli.Command = cli.Command{ |
||||
Name: "reset-pass", |
||||
Usage: "Reset user's password", |
||||
Aliases: []string{"resetpass", "reset"}, |
||||
Action: resetPassAction, |
||||
} |
||||
) |
||||
|
||||
func addUserAction(c *cli.Context) error { |
||||
credentials := "" |
||||
if c.NArg() > 0 { |
||||
credentials = c.Args().Get(0) |
||||
} else { |
||||
return fmt.Errorf("No user passed. Example: writefreely user add [USER]:[PASSWORD]") |
||||
} |
||||
username, password, err := parseCredentials(credentials) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
app := writefreely.NewApp(c.String("c")) |
||||
return writefreely.CreateUser(app, username, password, c.Bool("admin")) |
||||
} |
||||
|
||||
func delUserAction(c *cli.Context) error { |
||||
username := "" |
||||
if c.NArg() > 0 { |
||||
username = c.Args().Get(0) |
||||
} else { |
||||
return fmt.Errorf("No user passed. Example: writefreely user delete [USER]") |
||||
} |
||||
app := writefreely.NewApp(c.String("c")) |
||||
return writefreely.DoDeleteAccount(app, username) |
||||
} |
||||
|
||||
func resetPassAction(c *cli.Context) error { |
||||
username := "" |
||||
if c.NArg() > 0 { |
||||
username = c.Args().Get(0) |
||||
} else { |
||||
return fmt.Errorf("No user passed. Example: writefreely user reset-pass [USER]") |
||||
} |
||||
app := writefreely.NewApp(c.String("c")) |
||||
return writefreely.ResetPassword(app, username) |
||||
} |
@ -0,0 +1,49 @@ |
||||
/* |
||||
* Copyright © 2020 A Bunch Tell LLC. |
||||
* |
||||
* This file is part of WriteFreely. |
||||
* |
||||
* WriteFreely is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License, included |
||||
* in the LICENSE file in this source code package. |
||||
*/ |
||||
|
||||
package main |
||||
|
||||
import ( |
||||
"github.com/writeas/web-core/log" |
||||
"github.com/writeas/writefreely" |
||||
|
||||
"github.com/gorilla/mux" |
||||
"github.com/urfave/cli/v2" |
||||
) |
||||
|
||||
var ( |
||||
cmdServe cli.Command = cli.Command{ |
||||
Name: "serve", |
||||
Aliases: []string{"web"}, |
||||
Usage: "Run web application", |
||||
Action: serveAction, |
||||
} |
||||
) |
||||
|
||||
func serveAction(c *cli.Context) error { |
||||
// Initialize the application
|
||||
app := writefreely.NewApp(c.String("c")) |
||||
var err error |
||||
log.Info("Starting %s...", writefreely.FormatVersion()) |
||||
app, err = writefreely.Initialize(app, c.Bool("debug")) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
// Set app routes
|
||||
r := mux.NewRouter() |
||||
writefreely.InitRoutes(app, r) |
||||
app.InitStaticRoutes(r) |
||||
|
||||
// Serve the application
|
||||
writefreely.Serve(app, r) |
||||
|
||||
return nil |
||||
} |
@ -0,0 +1,115 @@ |
||||
package writefreely |
||||
|
||||
import ( |
||||
"context" |
||||
"errors" |
||||
"net/http" |
||||
"net/url" |
||||
"strings" |
||||
) |
||||
|
||||
type gitlabOauthClient struct { |
||||
ClientID string |
||||
ClientSecret string |
||||
AuthLocation string |
||||
ExchangeLocation string |
||||
InspectLocation string |
||||
CallbackLocation string |
||||
HttpClient HttpClient |
||||
} |
||||
|
||||
var _ oauthClient = gitlabOauthClient{} |
||||
|
||||
const ( |
||||
gitlabHost = "https://gitlab.com" |
||||
gitlabDisplayName = "GitLab" |
||||
) |
||||
|
||||
func (c gitlabOauthClient) GetProvider() string { |
||||
return "gitlab" |
||||
} |
||||
|
||||
func (c gitlabOauthClient) GetClientID() string { |
||||
return c.ClientID |
||||
} |
||||
|
||||
func (c gitlabOauthClient) GetCallbackLocation() string { |
||||
return c.CallbackLocation |
||||
} |
||||
|
||||
func (c gitlabOauthClient) buildLoginURL(state string) (string, error) { |
||||
u, err := url.Parse(c.AuthLocation) |
||||
if err != nil { |
||||
return "", err |
||||
} |
||||
q := u.Query() |
||||
q.Set("client_id", c.ClientID) |
||||
q.Set("redirect_uri", c.CallbackLocation) |
||||
q.Set("response_type", "code") |
||||
q.Set("state", state) |
||||
q.Set("scope", "read_user") |
||||
u.RawQuery = q.Encode() |
||||
return u.String(), nil |
||||
} |
||||
|
||||
func (c gitlabOauthClient) exchangeOauthCode(ctx context.Context, code string) (*TokenResponse, error) { |
||||
form := url.Values{} |
||||
form.Add("grant_type", "authorization_code") |
||||
form.Add("redirect_uri", c.CallbackLocation) |
||||
form.Add("scope", "read_user") |
||||
form.Add("code", code) |
||||
req, err := http.NewRequest("POST", c.ExchangeLocation, strings.NewReader(form.Encode())) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
req.WithContext(ctx) |
||||
req.Header.Set("User-Agent", "writefreely") |
||||
req.Header.Set("Accept", "application/json") |
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
||||
req.SetBasicAuth(c.ClientID, c.ClientSecret) |
||||
|
||||
resp, err := c.HttpClient.Do(req) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
if resp.StatusCode != http.StatusOK { |
||||
return nil, errors.New("unable to exchange code for access token") |
||||
} |
||||
|
||||
var tokenResponse TokenResponse |
||||
if err := limitedJsonUnmarshal(resp.Body, tokenRequestMaxLen, &tokenResponse); err != nil { |
||||
return nil, err |
||||
} |
||||
if tokenResponse.Error != "" { |
||||
return nil, errors.New(tokenResponse.Error) |
||||
} |
||||
return &tokenResponse, nil |
||||
} |
||||
|
||||
func (c gitlabOauthClient) inspectOauthAccessToken(ctx context.Context, accessToken string) (*InspectResponse, error) { |
||||
req, err := http.NewRequest("GET", c.InspectLocation, nil) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
req.WithContext(ctx) |
||||
req.Header.Set("User-Agent", "writefreely") |
||||
req.Header.Set("Accept", "application/json") |
||||
req.Header.Set("Authorization", "Bearer "+accessToken) |
||||
|
||||
resp, err := c.HttpClient.Do(req) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
if resp.StatusCode != http.StatusOK { |
||||
return nil, errors.New("unable to inspect access token") |
||||
} |
||||
|
||||
var inspectResponse InspectResponse |
||||
if err := limitedJsonUnmarshal(resp.Body, infoRequestMaxLen, &inspectResponse); err != nil { |
||||
return nil, err |
||||
} |
||||
if inspectResponse.Error != "" { |
||||
return nil, errors.New(inspectResponse.Error) |
||||
} |
||||
return &inspectResponse, nil |
||||
} |
@ -0,0 +1,315 @@ |
||||
// Copyright 2018 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package semver implements comparison of semantic version strings.
|
||||
// In this package, semantic version strings must begin with a leading "v",
|
||||
// as in "v1.0.0".
|
||||
//
|
||||
// The general form of a semantic version string accepted by this package is
|
||||
//
|
||||
// vMAJOR[.MINOR[.PATCH[-PRERELEASE][+BUILD]]]
|
||||
//
|
||||
// where square brackets indicate optional parts of the syntax;
|
||||
// MAJOR, MINOR, and PATCH are decimal integers without extra leading zeros;
|
||||
// PRERELEASE and BUILD are each a series of non-empty dot-separated identifiers
|
||||
// using only alphanumeric characters and hyphens; and
|
||||
// all-numeric PRERELEASE identifiers must not have leading zeros.
|
||||
//
|
||||
// This package follows Semantic Versioning 2.0.0 (see semver.org)
|
||||
// with two exceptions. First, it requires the "v" prefix. Second, it recognizes
|
||||
// vMAJOR and vMAJOR.MINOR (with no prerelease or build suffixes)
|
||||
// as shorthands for vMAJOR.0.0 and vMAJOR.MINOR.0.
|
||||
|
||||
// Package writefreely
|
||||
// copied from
|
||||
// https://github.com/golang/tools/blob/master/internal/semver/semver.go
|
||||
// slight modifications made
|
||||
package writefreely |
||||
|
||||
// parsed returns the parsed form of a semantic version string.
|
||||
type parsed struct { |
||||
major string |
||||
minor string |
||||
patch string |
||||
short string |
||||
prerelease string |
||||
build string |
||||
err string |
||||
} |
||||
|
||||
// IsValid reports whether v is a valid semantic version string.
|
||||
func IsValid(v string) bool { |
||||
_, ok := semParse(v) |
||||
return ok |
||||
} |
||||
|
||||
// CompareSemver returns an integer comparing two versions according to
|
||||
// according to semantic version precedence.
|
||||
// The result will be 0 if v == w, -1 if v < w, or +1 if v > w.
|
||||
//
|
||||
// An invalid semantic version string is considered less than a valid one.
|
||||
// All invalid semantic version strings compare equal to each other.
|
||||
func CompareSemver(v, w string) int { |
||||
pv, ok1 := semParse(v) |
||||
pw, ok2 := semParse(w) |
||||
if !ok1 && !ok2 { |
||||
return 0 |
||||
} |
||||
if !ok1 { |
||||
return -1 |
||||
} |
||||
if !ok2 { |
||||
return +1 |
||||
} |
||||
if c := compareInt(pv.major, pw.major); c != 0 { |
||||
return c |
||||
} |
||||
if c := compareInt(pv.minor, pw.minor); c != 0 { |
||||
return c |
||||
} |
||||
if c := compareInt(pv.patch, pw.patch); c != 0 { |
||||
return c |
||||
} |
||||
return comparePrerelease(pv.prerelease, pw.prerelease) |
||||
} |
||||
|
||||
func semParse(v string) (p parsed, ok bool) { |
||||
if v == "" || v[0] != 'v' { |
||||
p.err = "missing v prefix" |
||||
return |
||||
} |
||||
p.major, v, ok = parseInt(v[1:]) |
||||
if !ok { |
||||
p.err = "bad major version" |
||||
return |
||||
} |
||||
if v == "" { |
||||
p.minor = "0" |
||||
p.patch = "0" |
||||
p.short = ".0.0" |
||||
return |
||||
} |
||||
if v[0] != '.' { |
||||
p.err = "bad minor prefix" |
||||
ok = false |
||||
return |
||||
} |
||||
p.minor, v, ok = parseInt(v[1:]) |
||||
if !ok { |
||||
p.err = "bad minor version" |
||||
return |
||||
} |
||||
if v == "" { |
||||
p.patch = "0" |
||||
p.short = ".0" |
||||
return |
||||
} |
||||
if v[0] != '.' { |
||||
p.err = "bad patch prefix" |
||||
ok = false |
||||
return |
||||
} |
||||
p.patch, v, ok = parseInt(v[1:]) |
||||
if !ok { |
||||
p.err = "bad patch version" |
||||
return |
||||
} |
||||
if len(v) > 0 && v[0] == '-' { |
||||
p.prerelease, v, ok = parsePrerelease(v) |
||||
if !ok { |
||||
p.err = "bad prerelease" |
||||
return |
||||
} |
||||
} |
||||
if len(v) > 0 && v[0] == '+' { |
||||
p.build, v, ok = parseBuild(v) |
||||
if !ok { |
||||
p.err = "bad build" |
||||
return |
||||
} |
||||
} |
||||
if v != "" { |
||||
p.err = "junk on end" |
||||
ok = false |
||||
return |
||||
} |
||||
ok = true |
||||
return |
||||
} |
||||
|
||||
func parseInt(v string) (t, rest string, ok bool) { |
||||
if v == "" { |
||||
return |
||||
} |
||||
if v[0] < '0' || '9' < v[0] { |
||||
return |
||||
} |
||||
i := 1 |
||||
for i < len(v) && '0' <= v[i] && v[i] <= '9' { |
||||
i++ |
||||
} |
||||
if v[0] == '0' && i != 1 { |
||||
return |
||||
} |
||||
return v[:i], v[i:], true |
||||
} |
||||
|
||||
func parsePrerelease(v string) (t, rest string, ok bool) { |
||||
// "A pre-release version MAY be denoted by appending a hyphen and
|
||||
// a series of dot separated identifiers immediately following the patch version.
|
||||
// Identifiers MUST comprise only ASCII alphanumerics and hyphen [0-9A-Za-z-].
|
||||
// Identifiers MUST NOT be empty. Numeric identifiers MUST NOT include leading zeroes."
|
||||
if v == "" || v[0] != '-' { |
||||
return |
||||
} |
||||
i := 1 |
||||
start := 1 |
||||
for i < len(v) && v[i] != '+' { |
||||
if !isIdentChar(v[i]) && v[i] != '.' { |
||||
return |
||||
} |
||||
if v[i] == '.' { |
||||
if start == i || isBadNum(v[start:i]) { |
||||
return |
||||
} |
||||
start = i + 1 |
||||
} |
||||
i++ |
||||
} |
||||
if start == i || isBadNum(v[start:i]) { |
||||
return |
||||
} |
||||
return v[:i], v[i:], true |
||||
} |
||||
|
||||
func parseBuild(v string) (t, rest string, ok bool) { |
||||
if v == "" || v[0] != '+' { |
||||
return |
||||
} |
||||
i := 1 |
||||
start := 1 |
||||
for i < len(v) { |
||||
if !isIdentChar(v[i]) { |
||||
return |
||||
} |
||||
if v[i] == '.' { |
||||
if start == i { |
||||
return |
||||
} |
||||
start = i + 1 |
||||
} |
||||
i++ |
||||
} |
||||
if start == i { |
||||
return |
||||
} |
||||
return v[:i], v[i:], true |
||||
} |
||||
|
||||
func isIdentChar(c byte) bool { |
||||
return 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || '0' <= c && c <= '9' || c == '-' |
||||
} |
||||
|
||||
func isBadNum(v string) bool { |
||||
i := 0 |
||||
for i < len(v) && '0' <= v[i] && v[i] <= '9' { |
||||
i++ |
||||
} |
||||
return i == len(v) && i > 1 && v[0] == '0' |
||||
} |
||||
|
||||
func isNum(v string) bool { |
||||
i := 0 |
||||
for i < len(v) && '0' <= v[i] && v[i] <= '9' { |
||||
i++ |
||||
} |
||||
return i == len(v) |
||||
} |
||||
|
||||
func compareInt(x, y string) int { |
||||
if x == y { |
||||
return 0 |
||||
} |
||||
if len(x) < len(y) { |
||||
return -1 |
||||
} |
||||
if len(x) > len(y) { |
||||
return +1 |
||||
} |
||||
if x < y { |
||||
return -1 |
||||
} else { |
||||
return +1 |
||||
} |
||||
} |
||||
|
||||
func comparePrerelease(x, y string) int { |
||||
// "When major, minor, and patch are equal, a pre-release version has
|
||||
// lower precedence than a normal version.
|
||||
// Example: 1.0.0-alpha < 1.0.0.
|
||||
// Precedence for two pre-release versions with the same major, minor,
|
||||
// and patch version MUST be determined by comparing each dot separated
|
||||
// identifier from left to right until a difference is found as follows:
|
||||
// identifiers consisting of only digits are compared numerically and
|
||||
// identifiers with letters or hyphens are compared lexically in ASCII
|
||||
// sort order. Numeric identifiers always have lower precedence than
|
||||
// non-numeric identifiers. A larger set of pre-release fields has a
|
||||
// higher precedence than a smaller set, if all of the preceding
|
||||
// identifiers are equal.
|
||||
// Example: 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta <
|
||||
// 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0."
|
||||
if x == y { |
||||
return 0 |
||||
} |
||||
if x == "" { |
||||
return +1 |
||||
} |
||||
if y == "" { |
||||
return -1 |
||||
} |
||||
for x != "" && y != "" { |
||||
x = x[1:] // skip - or .
|
||||
y = y[1:] // skip - or .
|
||||
var dx, dy string |
||||
dx, x = nextIdent(x) |
||||
dy, y = nextIdent(y) |
||||
if dx != dy { |
||||
ix := isNum(dx) |
||||
iy := isNum(dy) |
||||
if ix != iy { |
||||
if ix { |
||||
return -1 |
||||
} else { |
||||
return +1 |
||||
} |
||||
} |
||||
if ix { |
||||
if len(dx) < len(dy) { |
||||
return -1 |
||||
} |
||||
if len(dx) > len(dy) { |
||||
return +1 |
||||
} |
||||
} |
||||
if dx < dy { |
||||
return -1 |
||||
} else { |
||||
return +1 |
||||
} |
||||
} |
||||
} |
||||
if x == "" { |
||||
return -1 |
||||
} else { |
||||
return +1 |
||||
} |
||||
} |
||||
|
||||
func nextIdent(x string) (dx, rest string) { |
||||
i := 0 |
||||
for i < len(x) && x[i] != '.' { |
||||
i++ |
||||
} |
||||
return x[:i], x[i:] |
||||
} |
@ -0,0 +1,154 @@ |
||||
{{define "app-settings"}} |
||||
{{template "header" .}} |
||||
|
||||
<style type="text/css"> |
||||
h2 {font-weight: normal;} |
||||
form { |
||||
margin: 0 0 2em; |
||||
} |
||||
form dt { |
||||
line-height: inherit; |
||||
} |
||||
.invisible { |
||||
display: none; |
||||
} |
||||
p.docs { |
||||
font-size: 0.86em; |
||||
} |
||||
</style> |
||||
|
||||
<div class="content-container snug"> |
||||
{{template "admin-header" .}} |
||||
|
||||
{{if .Message}}<p><a name="config"></a>{{.Message}}</p>{{end}} |
||||
|
||||
{{if .ConfigMessage}}<p class="success" style="text-align: center">{{.ConfigMessage}}</p>{{end}} |
||||
|
||||
<form action="/admin/update/config" method="post"> |
||||
<div class="features row"> |
||||
<div{{if .Config.SingleUser}} class="invisible"{{end}}> |
||||
Site Title |
||||
<p>Your public site name.</p> |
||||
</div> |
||||
<div{{if .Config.SingleUser}} class="invisible"{{end}}><input type="text" name="site_name" id="site_name" class="inline" value="{{.Config.SiteName}}" style="width: 14em;"/></div> |
||||
</div> |
||||
<div class="features row"> |
||||
<div{{if .Config.SingleUser}} class="invisible"{{end}}> |
||||
Site Description |
||||
<p>Describe your site — this shows in your site's metadata.</p> |
||||
</div> |
||||
<div{{if .Config.SingleUser}} class="invisible"{{end}}><input type="text" name="site_desc" id="site_desc" class="inline" value="{{.Config.SiteDesc}}" style="width: 14em;"/></div> |
||||
</div> |
||||
<div class="features row"> |
||||
<div> |
||||
Host |
||||
<p>The address where your site lives.</p> |
||||
</div> |
||||
<div>{{.Config.Host}}</div> |
||||
</div> |
||||
<div class="features row"> |
||||
<div> |
||||
Community Mode |
||||
<p>Whether your site is made for one person or many.</p> |
||||
</div> |
||||
<div>{{if .Config.SingleUser}}Single user{{else}}Multiple users{{end}}</div> |
||||
</div> |
||||
<div class="features row"> |
||||
<div{{if .Config.SingleUser}} class="invisible"{{end}}> |
||||
Landing Page |
||||
<p>The page that logged-out visitors will see first. This should be a path, e.g. <code>/read</code></p> |
||||
</div> |
||||
<div{{if .Config.SingleUser}} class="invisible"{{end}}><input type="text" name="landing" id="landing" class="inline" value="{{.Config.Landing}}" style="width: 14em;"/></div> |
||||
</div> |
||||
<div class="features row"> |
||||
<div{{if .Config.SingleUser}} class="invisible"{{end}}><label for="open_registration"> |
||||
Open Registrations |
||||
<p>Whether or not registration is open to anyone who visits the site.</p> |
||||
</label></div> |
||||
<div{{if .Config.SingleUser}} class="invisible"{{end}}><input type="checkbox" name="open_registration" id="open_registration" {{if .Config.OpenRegistration}}checked="checked"{{end}} /> |
||||
</div> |
||||
</div> |
||||
<div class="features row"> |
||||
<div><label for="min_username_len"> |
||||
Minimum Username Length |
||||
<p>The minimum number of characters allowed in a username. (Recommended: 2 or more.)</p> |
||||
</label></div> |
||||
<div><input type="number" name="min_username_len" id="min_username_len" class="inline" min="1" max="100" value="{{.Config.MinUsernameLen}}"/></div> |
||||
</div> |
||||
<div class="features row"> |
||||
<div{{if .Config.SingleUser}} class="invisible"{{end}}><label for="max_blogs"> |
||||
Maximum Blogs per User |
||||
<p>Keep things simple by setting this to <strong>1</strong>, unlimited by setting to <strong>0</strong>, or pick another amount.</p> |
||||
</label></div> |
||||
<div{{if .Config.SingleUser}} class="invisible"{{end}}><input type="number" name="max_blogs" id="max_blogs" class="inline" min="0" value="{{.Config.MaxBlogs}}"/></div> |
||||
</div> |
||||
<div class="features row"> |
||||
<div><label for="federation"> |
||||
Federation |
||||
<p>Enable accounts on this site to propagate their posts via the ActivityPub protocol.</p> |
||||
</label></div> |
||||
<div><input type="checkbox" name="federation" id="federation" {{if .Config.Federation}}checked="checked"{{end}} /></div> |
||||
</div> |
||||
<div class="features row"> |
||||
<div><label for="public_stats"> |
||||
Public Stats |
||||
<p>Publicly display the number of users and posts on your <strong>About</strong> page.</p> |
||||
</label></div> |
||||
<div><input type="checkbox" name="public_stats" id="public_stats" {{if .Config.PublicStats}}checked="checked"{{end}} /></div> |
||||
</div> |
||||
<div class="features row"> |
||||
<div><label for="private"> |
||||
Private Instance |
||||
<p>Make this instance accessible only to those with an account.</p> |
||||
</label></div> |
||||
<div><input type="checkbox" name="private" id="private" {{if .Config.Private}}checked="checked"{{end}} /></div> |
||||
</div> |
||||
<div class="features row"> |
||||
<div{{if .Config.SingleUser}} class="invisible"{{end}}><label for="local_timeline"> |
||||
Reader |
||||
<p>Show a feed of user posts for anyone who chooses to share there.</p> |
||||
</label></div> |
||||
<div{{if .Config.SingleUser}} class="invisible"{{end}}><input type="checkbox" name="local_timeline" id="local_timeline" {{if .Config.LocalTimeline}}checked="checked"{{end}} /></div> |
||||
</div> |
||||
<div class="features row"> |
||||
<div{{if .Config.SingleUser}} class="invisible"{{end}}><label for="user_invites"> |
||||
Allow invitations from... |
||||
<p>Choose who on this instance can invite new people.</p> |
||||
</label></div> |
||||
<div{{if .Config.SingleUser}} class="invisible"{{end}}> |
||||
<select name="user_invites" id="user_invites"> |
||||
<option value="none" {{if eq .Config.UserInvites ""}}selected="selected"{{end}}>No one</option> |
||||
<option value="admin" {{if eq .Config.UserInvites "admin"}}selected="selected"{{end}}>Only Admins</option> |
||||
<option value="user" {{if eq .Config.UserInvites "user"}}selected="selected"{{end}}>All Users</option> |
||||
</select> |
||||
</div> |
||||
</div> |
||||
<div class="features row"> |
||||
<div{{if .Config.SingleUser}} class="invisible"{{end}}><label for="default_visibility"> |
||||
Default blog visibility |
||||
<p>The default setting for new accounts and blogs.</p> |
||||
</label></div> |
||||
<div{{if .Config.SingleUser}} class="invisible"{{end}}> |
||||
<select name="default_visibility" id="default_visibility"> |
||||
<option value="unlisted" {{if eq .Config.DefaultVisibility "unlisted"}}selected="selected"{{end}}>Unlisted</option> |
||||
<option value="public" {{if eq .Config.DefaultVisibility "public"}}selected="selected"{{end}}>Public</option> |
||||
<option value="private" {{if eq .Config.DefaultVisibility "private"}}selected="selected"{{end}}>Private</option> |
||||
</select> |
||||
</div> |
||||
</div> |
||||
<div class="features row"> |
||||
<input type="submit" value="Save Settings" /> |
||||
</div> |
||||
</form> |
||||
|
||||
<p class="docs">Still have questions? Read more details in the <a href="https://writefreely.org/docs/{{.OfficialVersion}}/admin/config">configuration docs</a>.</p> |
||||
</div> |
||||
|
||||
<script> |
||||
history.replaceState(null, "", "/admin/settings"+window.location.hash); |
||||
</script> |
||||
|
||||
{{template "footer" .}} |
||||
|
||||
{{template "body-end" .}} |
||||
{{end}} |
@ -0,0 +1,48 @@ |
||||
{{define "app-updates"}} |
||||
{{template "header" .}} |
||||
|
||||
<style type="text/css"> |
||||
p.intro { |
||||
text-align: left; |
||||
} |
||||
p.disabled { |
||||
font-style: italic; |
||||
color: #999; |
||||
} |
||||
</style> |
||||
|
||||
<div class="content-container snug"> |
||||
{{template "admin-header" .}} |
||||
|
||||
{{ if .UpdateChecks }} |
||||
{{if .CheckFailed}} |
||||
<p class="intro"><span class="ex failure">×</span> Automated update check failed.</p> |
||||
<p>Installed version: <strong>{{.Version}}</strong> (<a href="{{.CurReleaseNotesURL}}" target="changelog-wf">release notes</a>).</p> |
||||
<p>Learn about latest releases on the <a href="https://blog.writefreely.org/tag:release" target="changelog-wf">WriteFreely blog</a> or <a href="https://discuss.write.as/c/writefreely/updates" target="forum-wf">forum</a>.</p> |
||||
{{else if not .UpdateAvailable}} |
||||
<p class="intro"><span class="check">✓</span> WriteFreely is <strong>up to date</strong>.</p> |
||||
<p>Installed version: <strong>{{.Version}}</strong> (<a href="{{.LatestReleaseNotesURL}}" target="changelog-wf">release notes</a>).</p> |
||||
{{else}} |
||||
<p class="intro">A new version of WriteFreely is available! <a href="{{.LatestReleaseURL}}" target="download-wf" style="font-weight: bold;">Get {{.LatestVersion}}</a></p> |
||||
<p class="changelog"> |
||||
<a href="{{.LatestReleaseNotesURL}}" target="changelog-wf">Read the release notes</a> for details on features, bug fixes, and notes on upgrading from your current version, <strong>{{.Version}}</strong>. |
||||
</p> |
||||
{{end}} |
||||
<p style="font-size: 0.86em;"><em>Last checked</em>: <time class="dt-published" datetime="{{.LastChecked8601}}">{{.LastChecked}}</time>. <a href="/admin/updates?check=now">Check now</a>.</p> |
||||
|
||||
<script> |
||||
// Code modified from /js/localdate.js |
||||
var displayEl = document.querySelector("time"); |
||||
var d = new Date(displayEl.getAttribute("datetime")); |
||||
displayEl.textContent = d.toLocaleDateString(navigator.language || "en-US", { dateStyle: 'long', timeStyle: 'short' }); |
||||
</script> |
||||
{{ else }} |
||||
<p class="intro disabled">Automated update checks are disabled.</p> |
||||
<p>Installed version: <strong>{{.Version}}</strong> (<a href="{{.CurReleaseNotesURL}}" target="changelog-wf">release notes</a>).</p> |
||||
<p>Learn about latest releases on the <a href="https://blog.writefreely.org/tag:release" target="changelog-wf">WriteFreely blog</a> or <a href="https://discuss.write.as/c/writefreely/updates" target="forum-wf">forum</a>.</p> |
||||
{{ end }} |
||||
|
||||
{{template "footer" .}} |
||||
|
||||
{{template "body-end" .}} |
||||
{{end}} |
@ -0,0 +1,105 @@ |
||||
{{define "monitor"}} |
||||
{{template "header" .}} |
||||
|
||||
<style type="text/css"> |
||||
h2 {font-weight: normal;} |
||||
.ui.divider:not(.vertical):not(.horizontal) { |
||||
border-top: 1px solid rgba(34,36,38,.15); |
||||
border-bottom: 1px solid rgba(255,255,255,.1); |
||||
} |
||||
.ui.divider { |
||||
margin: 1rem 0; |
||||
line-height: 1; |
||||
height: 0; |
||||
font-weight: 700; |
||||
text-transform: uppercase; |
||||
letter-spacing: .05em; |
||||
color: rgba(0,0,0,.85); |
||||
-webkit-user-select: none; |
||||
-moz-user-select: none; |
||||
-ms-user-select: none; |
||||
user-select: none; |
||||
-webkit-tap-highlight-color: transparent; |
||||
font-size: 1rem; |
||||
} |
||||
</style> |
||||
|
||||
<div class="content-container snug"> |
||||
{{template "admin-header" .}} |
||||
|
||||
{{if .Message}}<p>{{.Message}}</p>{{end}} |
||||
|
||||
<h2><a name="monitor"></a>Application Monitor</h2> |
||||
|
||||
<div class="ui attached table segment"> |
||||
<dl class="dl-horizontal admin-dl-horizontal"> |
||||
<dt>WriteFreely</dt> |
||||
<dd>{{.Version}}</dd> |
||||
<dt>Server Uptime</dt> |
||||
<dd>{{.SysStatus.Uptime}}</dd> |
||||
<dt>Current Goroutines</dt> |
||||
<dd>{{.SysStatus.NumGoroutine}}</dd> |
||||
<div class="ui divider"></div> |
||||
<dt>Current memory usage</dt> |
||||
<dd>{{.SysStatus.MemAllocated}}</dd> |
||||
<dt>Total mem allocated</dt> |
||||
<dd>{{.SysStatus.MemTotal}}</dd> |
||||
<dt>Memory obtained</dt> |
||||
<dd>{{.SysStatus.MemSys}}</dd> |
||||
<dt>Pointer lookup times</dt> |
||||
<dd>{{.SysStatus.Lookups}}</dd> |
||||
<dt>Memory allocate times</dt> |
||||
<dd>{{.SysStatus.MemMallocs}}</dd> |
||||
<dt>Memory free times</dt> |
||||
<dd>{{.SysStatus.MemFrees}}</dd> |
||||
<div class="ui divider"></div> |
||||
<dt>Current heap usage</dt> |
||||
<dd>{{.SysStatus.HeapAlloc}}</dd> |
||||
<dt>Heap memory obtained</dt> |
||||
<dd>{{.SysStatus.HeapSys}}</dd> |
||||
<dt>Heap memory idle</dt> |
||||
<dd>{{.SysStatus.HeapIdle}}</dd> |
||||
<dt>Heap memory in use</dt> |
||||
<dd>{{.SysStatus.HeapInuse}}</dd> |
||||
<dt>Heap memory released</dt> |
||||
<dd>{{.SysStatus.HeapReleased}}</dd> |
||||
<dt>Heap objects</dt> |
||||
<dd>{{.SysStatus.HeapObjects}}</dd> |
||||
<div class="ui divider"></div> |
||||
<dt>Bootstrap stack usage</dt> |
||||
<dd>{{.SysStatus.StackInuse}}</dd> |
||||
<dt>Stack memory obtained</dt> |
||||
<dd>{{.SysStatus.StackSys}}</dd> |
||||
<dt>MSpan structures in use</dt> |
||||
<dd>{{.SysStatus.MSpanInuse}}</dd> |
||||
<dt>MSpan structures obtained</dt> |
||||
<dd>{{.SysStatus.HeapSys}}</dd> |
||||
<dt>MCache structures in use</dt> |
||||
<dd>{{.SysStatus.MCacheInuse}}</dd> |
||||
<dt>MCache structures obtained</dt> |
||||
<dd>{{.SysStatus.MCacheSys}}</dd> |
||||
<dt>Profiling bucket hash table obtained</dt> |
||||
<dd>{{.SysStatus.BuckHashSys}}</dd> |
||||
<dt>GC metadata obtained</dt> |
||||
<dd>{{.SysStatus.GCSys}}</dd> |
||||
<dt>Other system allocation obtained</dt> |
||||
<dd>{{.SysStatus.OtherSys}}</dd> |
||||
<div class="ui divider"></div> |
||||
<dt>Next GC recycle</dt> |
||||
<dd>{{.SysStatus.NextGC}}</dd> |
||||
<dt>Since last GC</dt> |
||||
<dd>{{.SysStatus.LastGC}}</dd> |
||||
<dt>Total GC pause</dt> |
||||
<dd>{{.SysStatus.PauseTotalNs}}</dd> |
||||
<dt>Last GC pause</dt> |
||||
<dd>{{.SysStatus.PauseNs}}</dd> |
||||
<dt>GC times</dt> |
||||
<dd>{{.SysStatus.NumGC}}</dd> |
||||
</dl> |
||||
</div> |
||||
</div> |
||||
|
||||
{{template "footer" .}} |
||||
|
||||
{{template "body-end" .}} |
||||
{{end}} |
@ -0,0 +1,131 @@ |
||||
/* |
||||
* Copyright © 2019-2020 A Bunch Tell LLC. |
||||
* |
||||
* This file is part of WriteFreely. |
||||
* |
||||
* WriteFreely is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License, included |
||||
* in the LICENSE file in this source code package. |
||||
*/ |
||||
|
||||
package writefreely |
||||
|
||||
import ( |
||||
"github.com/writeas/web-core/log" |
||||
"io/ioutil" |
||||
"net/http" |
||||
"strings" |
||||
"sync" |
||||
"time" |
||||
) |
||||
|
||||
// updatesCacheTime is the default interval between cache updates for new
|
||||
// software versions
|
||||
const defaultUpdatesCacheTime = 12 * time.Hour |
||||
|
||||
// updatesCache holds data about current and new releases of the writefreely
|
||||
// software
|
||||
type updatesCache struct { |
||||
mu sync.Mutex |
||||
frequency time.Duration |
||||
lastCheck time.Time |
||||
latestVersion string |
||||
currentVersion string |
||||
checkError error |
||||
} |
||||
|
||||
// CheckNow asks for the latest released version of writefreely and updates
|
||||
// the cache last checked time. If the version postdates the current 'latest'
|
||||
// the version value is replaced.
|
||||
func (uc *updatesCache) CheckNow() error { |
||||
if debugging { |
||||
log.Info("[update check] Checking for update now.") |
||||
} |
||||
uc.mu.Lock() |
||||
defer uc.mu.Unlock() |
||||
uc.lastCheck = time.Now() |
||||
latestRemote, err := newVersionCheck() |
||||
if err != nil { |
||||
log.Error("[update check] Failed: %v", err) |
||||
uc.checkError = err |
||||
return err |
||||
} |
||||
if CompareSemver(latestRemote, uc.latestVersion) == 1 { |
||||
uc.latestVersion = latestRemote |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// AreAvailable updates the cache if the frequency duration has passed
|
||||
// then returns if the latest release is newer than the current running version.
|
||||
func (uc updatesCache) AreAvailable() bool { |
||||
if time.Since(uc.lastCheck) > uc.frequency { |
||||
uc.CheckNow() |
||||
} |
||||
return CompareSemver(uc.latestVersion, uc.currentVersion) == 1 |
||||
} |
||||
|
||||
// AreAvailableNoCheck returns if the latest release is newer than the current
|
||||
// running version.
|
||||
func (uc updatesCache) AreAvailableNoCheck() bool { |
||||
return CompareSemver(uc.latestVersion, uc.currentVersion) == 1 |
||||
} |
||||
|
||||
// LatestVersion returns the latest stored version available.
|
||||
func (uc updatesCache) LatestVersion() string { |
||||
return uc.latestVersion |
||||
} |
||||
|
||||
func (uc updatesCache) ReleaseURL() string { |
||||
return "https://writefreely.org/releases/" + uc.latestVersion |
||||
} |
||||
|
||||
// ReleaseNotesURL returns the full URL to the blog.writefreely.org release notes
|
||||
// for the latest version as stored in the cache.
|
||||
func (uc updatesCache) ReleaseNotesURL() string { |
||||
return wfReleaseNotesURL(uc.latestVersion) |
||||
} |
||||
|
||||
func wfReleaseNotesURL(v string) string { |
||||
ver := strings.TrimPrefix(v, "v") |
||||
ver = strings.TrimSuffix(ver, ".0") |
||||
// hack until go 1.12 in build/travis
|
||||
seg := strings.Split(ver, ".") |
||||
return "https://blog.writefreely.org/version-" + strings.Join(seg, "-") |
||||
} |
||||
|
||||
// newUpdatesCache returns an initialized updates cache
|
||||
func newUpdatesCache(expiry time.Duration) *updatesCache { |
||||
cache := updatesCache{ |
||||
frequency: expiry, |
||||
currentVersion: "v" + softwareVer, |
||||
} |
||||
go cache.CheckNow() |
||||
return &cache |
||||
} |
||||
|
||||
// InitUpdates initializes the updates cache, if the config value is set
|
||||
// It uses the defaultUpdatesCacheTime for the cache expiry
|
||||
func (app *App) InitUpdates() { |
||||
if app.cfg.App.UpdateChecks { |
||||
app.updates = newUpdatesCache(defaultUpdatesCacheTime) |
||||
} |
||||
} |
||||
|
||||
func newVersionCheck() (string, error) { |
||||
res, err := http.Get("https://version.writefreely.org") |
||||
if debugging { |
||||
log.Info("[update check] GET https://version.writefreely.org") |
||||
} |
||||
// TODO: return error if statusCode != OK
|
||||
if err == nil && res.StatusCode == http.StatusOK { |
||||
defer res.Body.Close() |
||||
|
||||
body, err := ioutil.ReadAll(res.Body) |
||||
if err != nil { |
||||
return "", err |
||||
} |
||||
return string(body), nil |
||||
} |
||||
return "", err |
||||
} |
@ -0,0 +1,82 @@ |
||||
package writefreely |
||||
|
||||
import ( |
||||
"regexp" |
||||
"testing" |
||||
"time" |
||||
) |
||||
|
||||
func TestUpdatesRoundTrip(t *testing.T) { |
||||
cache := newUpdatesCache(defaultUpdatesCacheTime) |
||||
t.Run("New Updates Cache", func(t *testing.T) { |
||||
|
||||
if cache == nil { |
||||
t.Fatal("Returned nil cache") |
||||
} |
||||
|
||||
if cache.frequency != defaultUpdatesCacheTime { |
||||
t.Fatalf("Got cache expiry frequency: %s but expected: %s", cache.frequency, defaultUpdatesCacheTime) |
||||
} |
||||
|
||||
if cache.currentVersion != "v"+softwareVer { |
||||
t.Fatalf("Got current version: %s but expected: %s", cache.currentVersion, "v"+softwareVer) |
||||
} |
||||
}) |
||||
|
||||
t.Run("Release URL", func(t *testing.T) { |
||||
url := cache.ReleaseNotesURL() |
||||
|
||||
reg, err := regexp.Compile(`^https:\/\/blog.writefreely.org\/version(-\d+){1,}$`) |
||||
if err != nil { |
||||
t.Fatalf("Test Case Error: Failed to compile regex: %v", err) |
||||
} |
||||
match := reg.MatchString(url) |
||||
|
||||
if !match { |
||||
t.Fatalf("Malformed Release URL: %s", url) |
||||
} |
||||
}) |
||||
|
||||
t.Run("Check Now", func(t *testing.T) { |
||||
// ensure time between init and next check
|
||||
time.Sleep(1 * time.Second) |
||||
|
||||
prevLastCheck := cache.lastCheck |
||||
|
||||
// force to known older version for latest and current
|
||||
prevLatestVer := "v0.8.1" |
||||
cache.latestVersion = prevLatestVer |
||||
cache.currentVersion = "v0.8.0" |
||||
|
||||
err := cache.CheckNow() |
||||
if err != nil { |
||||
t.Fatalf("Error should be nil, got: %v", err) |
||||
} |
||||
|
||||
if prevLastCheck == cache.lastCheck { |
||||
t.Fatal("Expected lastCheck to update") |
||||
} |
||||
|
||||
if cache.lastCheck.Before(prevLastCheck) { |
||||
t.Fatal("Last check should be newer than previous") |
||||
} |
||||
|
||||
if prevLatestVer == cache.latestVersion { |
||||
t.Fatal("expected latestVersion to update") |
||||
} |
||||
|
||||
}) |
||||
|
||||
t.Run("Are Available", func(t *testing.T) { |
||||
if !cache.AreAvailable() { |
||||
t.Fatalf("Cache reports not updates but Current is %s and Latest is %s", cache.currentVersion, cache.latestVersion) |
||||
} |
||||
}) |
||||
|
||||
t.Run("Latest Version", func(t *testing.T) { |
||||
gotLatest := cache.LatestVersion() |
||||
if gotLatest != cache.latestVersion { |
||||
t.Fatalf("Malformed latest version. Expected: %s but got: %s", cache.latestVersion, gotLatest) |
||||
} |
||||
}) |
||||
} |
Loading…
Reference in new issue