Compare commits
24
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
26fc9ca3e7 | ||
|
|
8c7c7c9f91 | ||
|
|
0d576e4b55 | ||
|
|
d239f39f9a | ||
|
|
4078d62efc | ||
|
|
b20a2a4f6c | ||
|
|
aedf58b7df | ||
|
|
a72130fc33 | ||
|
|
ac24cda0b9 | ||
|
|
b6632b2d09 | ||
|
|
630d129bb5 | ||
|
|
ec50f048a1 | ||
|
|
9db7d4604f | ||
|
|
9cf826075f | ||
|
|
15b4db78ee | ||
|
|
a2c2a1f750 | ||
|
|
08a10a55dd | ||
|
|
64161721bf | ||
|
|
d3e89b886e | ||
|
|
e962c4d0c5 | ||
|
|
53ee2281bc | ||
|
|
d309b90953 | ||
|
|
e05b34a866 | ||
|
|
6526c74ed2 |
@@ -20,48 +20,14 @@ jobs:
|
||||
GOOS=windows GOARCH=amd64 go build -o amsterdam-windows-amd64.exe
|
||||
GOOS=darwin GOARCH=arm64 go build -o amsterdam-macos-arm64
|
||||
GOOS=darwin GOARCH=amd64 go build -o amsterdam-macos-amd64
|
||||
- name: Upload Linux AMD64 Binary
|
||||
uses: actions/upload-release-asset@v1
|
||||
- name: Upload Assets to Release
|
||||
uses: softprops/action-gh-release@v2
|
||||
with:
|
||||
files: |
|
||||
amsterdam-linux-amd64
|
||||
amsterdam-linux-arm64
|
||||
amsterdam-windows-amd64.exe
|
||||
amsterdam-macos-arm64
|
||||
amsterdam-macos-amd64
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
upload_url: ${{ github.event.release.upload_url }}
|
||||
asset_path: ./amsterdam-linux-amd64
|
||||
asset_name: amsterdam-linux-amd64
|
||||
asset_content_type: application/octet-stream
|
||||
- name: Upload Linux ARM64 Binary
|
||||
uses: actions/upload-release-asset@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
upload_url: ${{ github.event.release.upload_url }}
|
||||
asset_path: ./amsterdam-linux-arm64
|
||||
asset_name: amsterdam-linux-arm64
|
||||
asset_content_type: application/octet-stream
|
||||
- name: Upload Windows Binary
|
||||
uses: actions/upload-release-asset@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
upload_url: ${{ github.event.release.upload_url }}
|
||||
asset_path: ./amsterdam-windows-amd64.exe
|
||||
asset_name: amsterdam-windows-amd64.exe
|
||||
asset_content_type: application/vnd.microsoft.portable-executable
|
||||
- name: Upload Mac ARM64 Binary
|
||||
uses: actions/upload-release-asset@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
upload_url: ${{ github.event.release.upload_url }}
|
||||
asset_path: ./amsterdam-macos-arm64
|
||||
asset_name: amsterdam-macos-arm64
|
||||
asset_content_type: application/octet-stream
|
||||
- name: Upload Mac AMD64 Binary
|
||||
uses: actions/upload-release-asset@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
upload_url: ${{ github.event.release.upload_url }}
|
||||
asset_path: ./amsterdam-macos-amd64
|
||||
asset_name: amsterdam-macos-amd64
|
||||
asset_content_type: application/octet-stream
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,644 @@
|
||||
/*
|
||||
* Amsterdam Web Communities System
|
||||
* Copyright (c) 2025-2026 Erbosoft Metaverse Design Solutions, All Rights Reserved
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*/
|
||||
/* This file contains code adapted from Mintlify agent-traffic, https://github.com/mintlify/agent-traffic
|
||||
* ---
|
||||
* Copyright (c) 2026 Mintlify, Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
// Package agentclass contains support for agent request classification.
|
||||
package agentclass
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"encoding/json/v2"
|
||||
"fmt"
|
||||
"maps"
|
||||
"math"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Constants reflecting confidence levels.
|
||||
const (
|
||||
ConfidenceHigh = iota // high confidence
|
||||
ConfidenceMedium // medium confidence
|
||||
ConfidenceLow // low confidence
|
||||
)
|
||||
|
||||
// Classification is the return value from AmClassify, showing what a particular HTTP request is classified as.
|
||||
type Classification struct {
|
||||
Category string // category of requester: agent, ai-crawler, crawler, bot, human, unknown
|
||||
Kind string // what kind of requester is it?
|
||||
Id string // actual identifier
|
||||
Name string // name of requester
|
||||
Operator string // what organization operates this requester?
|
||||
Version string // version of requester, where applicable
|
||||
Verified bool // true if all this is verified, false if not
|
||||
Confidence int // confidence level (above)
|
||||
Reasons []string // why did we classify the request this way?
|
||||
}
|
||||
|
||||
// RawAgentDef is the agent definition data loaded from the agents.json file.
|
||||
type RawAgentDef struct {
|
||||
Id string `json:"id"` // agent ID
|
||||
Name string `json:"name"` // agent name
|
||||
Operator string `json:"operator"` // agent operating organization
|
||||
Category string `json:"category"` // category of agent
|
||||
Kind string `json:"kind"` // kind (sub-category) of agent
|
||||
RunsOn string `json:"runsOn"` // does it run on a user machine or an operator machine?
|
||||
UserAgents []string `json:"userAgents"` // regexps to recognize its User-Agent
|
||||
Accept []string `json:"accept"` // Accept: string(s) the agent sends
|
||||
SignatureAgent []string `json:"signatureAgent"` // what agent(s) we can find in a request signature
|
||||
Verify struct {
|
||||
IpRanges []string `json:"ipRanges"` // where can we find what IP ranges this agent uses?
|
||||
ReverseDNS []string `json:"reverseDns"` // what should the agent's IP address resolve to?
|
||||
WebBotAuth string `json:"webBotAuth"` // where can I find the authentication?
|
||||
} `json:"verify"`
|
||||
Docs string `json:"docs"` // URL to documentation
|
||||
Examples []string `json:"examples"` // examples of strings that match
|
||||
Notes string `json:"notes"` // notes
|
||||
LastVerified string `json:"lastVerified"` // when was this information last verified?
|
||||
}
|
||||
|
||||
// agentDef is the definition for a compiled agent.
|
||||
type agentDef struct {
|
||||
r *RawAgentDef // all basic agent information
|
||||
patterns []*regexp.Regexp // compiled user agent expressions
|
||||
}
|
||||
|
||||
// RawClientDef is the client definition data loaded from the http-clients.json file.
|
||||
type RawClientDef struct {
|
||||
Name string `json:"name"`
|
||||
Kind string `json:"kind"`
|
||||
UserAgent string `json:"userAgent"`
|
||||
}
|
||||
|
||||
// clientdef is the definition for a compiled client.
|
||||
type clientDef struct {
|
||||
r *RawClientDef
|
||||
pattern *regexp.Regexp
|
||||
}
|
||||
|
||||
//go:embed agents.json
|
||||
var initAgents []byte
|
||||
|
||||
// compiledAgents is the compiled list of agents.
|
||||
var compiledAgents []agentDef
|
||||
|
||||
//go:embed http-clients.json
|
||||
var initClients []byte
|
||||
|
||||
// compiledClients is the compiled list of clients.
|
||||
var compiledClients []clientDef
|
||||
|
||||
// AmInitAgentClass is the initialization function.
|
||||
func AmInitAgentClass(ctxname any) func() {
|
||||
// load the raw agent list
|
||||
var rawAgents []RawAgentDef
|
||||
err := json.Unmarshal(initAgents, &rawAgents)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("agent definition load failure: %v", err))
|
||||
}
|
||||
|
||||
// build the compiled agent list
|
||||
compiledAgents = make([]agentDef, len(rawAgents))
|
||||
for i, v := range rawAgents {
|
||||
compiledAgents[i].r = &(rawAgents[i])
|
||||
if v.UserAgents != nil {
|
||||
compiledAgents[i].patterns = make([]*regexp.Regexp, len(v.UserAgents))
|
||||
for j, p := range v.UserAgents {
|
||||
compiledAgents[i].patterns[j] = regexp.MustCompile("(?i)" + p)
|
||||
}
|
||||
} else {
|
||||
compiledAgents[i].patterns = make([]*regexp.Regexp, 0)
|
||||
}
|
||||
}
|
||||
|
||||
// load the raw clients list
|
||||
var rawClients []RawClientDef
|
||||
err = json.Unmarshal(initClients, &rawClients)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("client definition load failure: %v", err))
|
||||
}
|
||||
|
||||
// build the compiled client list
|
||||
compiledClients = make([]clientDef, len(rawClients))
|
||||
for i, v := range rawClients {
|
||||
compiledClients[i].r = &(rawClients[i])
|
||||
compiledClients[i].pattern = regexp.MustCompile("(?i)" + v.UserAgent)
|
||||
}
|
||||
|
||||
// get the list of IP range files
|
||||
urls := make(map[string]struct{})
|
||||
for _, ra := range rawAgents {
|
||||
if ra.Verify.IpRanges != nil {
|
||||
for _, url := range ra.Verify.IpRanges {
|
||||
urls[url] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
return loadIPRanges(slices.Collect(maps.Keys(urls)), ctxname)
|
||||
}
|
||||
|
||||
type ClassifyOptions struct {
|
||||
MaxAge int // maximum age of IP range files
|
||||
Now *time.Time // "now" timestamp (for testing)
|
||||
HeadersComplete bool // look at complete headers?
|
||||
}
|
||||
|
||||
func AmClassifyOptions() ClassifyOptions {
|
||||
return ClassifyOptions{
|
||||
MaxAge: 180,
|
||||
Now: nil,
|
||||
HeadersComplete: false,
|
||||
}
|
||||
}
|
||||
|
||||
func (o *ClassifyOptions) WithMaxAge(maxAge int) *ClassifyOptions {
|
||||
o.MaxAge = maxAge
|
||||
return o
|
||||
}
|
||||
|
||||
func (o *ClassifyOptions) WithNow(now *time.Time) *ClassifyOptions {
|
||||
o.Now = now
|
||||
return o
|
||||
}
|
||||
|
||||
func (o *ClassifyOptions) WithHeadersComplete(flag bool) *ClassifyOptions {
|
||||
o.HeadersComplete = flag
|
||||
return o
|
||||
}
|
||||
|
||||
// AgentMatch is the return from findAgent.
|
||||
type AgentMatch struct {
|
||||
Agent *RawAgentDef // pointer to raw agent definition
|
||||
Version string // version of agent, where applicable
|
||||
Reason string // reason we matched this agent
|
||||
Weak bool // is this a weak match?
|
||||
}
|
||||
|
||||
var textBrowserExpr = regexp.MustCompile(`^(Lynx|ELinks|Links|w3m)\/`)
|
||||
var browserExpr = regexp.MustCompile(`^Mozilla\/[4-6]\.0 \(|^Opera\/\d`)
|
||||
var browserEngineExpr = regexp.MustCompile(`\(KHTML,? ?like Gecko|KHTML\/[\d.]+ \(like Gecko|Gecko\/\d|\) like Gecko|Trident\/\d|MSIE \d|Presto\/\d|Chromium\/\d`)
|
||||
var botWordExpr = regexp.MustCompile(`(?:[a-z0-9-]|\b)[Bb]ot(?:\b|\/)`)
|
||||
var emailExpr = regexp.MustCompile(`(?i)[\w.+-]+@[\w-]+(\.[\w-]+)*\.[a-z]{2,}`)
|
||||
var automationExpr = regexp.MustCompile(`(?i)crawl|spider|scrap|headless|https?:\/\/|\+http|(?:^|[^a-z])(?:archive|observ|monitor|check(?:er)?\b|preview|verif|render|insight|console|transcod|scanner|synthetic)|lighthouse|selenium|cypress|webdriver|\bsplash\b|gomezagent|rigor|speedcurve|\bptst\b|dareboost|dejaclick|favicon|siteimprove|onetrust|datanyze|feeder\.co|rss\b|weblight|duplexweb|ips-agent`)
|
||||
var compatibleExpr = regexp.MustCompile(`compatible;`)
|
||||
var enginesExpr = regexp.MustCompile(`MSIE|Trident|Konqueror`)
|
||||
|
||||
// isBrowser retruns true if the user agent indicates a browser.
|
||||
func isBrowser(ua string) bool {
|
||||
if textBrowserExpr.MatchString(ua) {
|
||||
return true
|
||||
}
|
||||
if !browserExpr.MatchString(ua) || !browserEngineExpr.MatchString(ua) {
|
||||
return false
|
||||
}
|
||||
if botWordExpr.MatchString(ua) || emailExpr.MatchString(ua) || automationExpr.MatchString(ua) {
|
||||
return false
|
||||
}
|
||||
return !compatibleExpr.MatchString(ua) || enginesExpr.MatchString(ua)
|
||||
}
|
||||
|
||||
// normalizeHeaders joins header values and smashes case on header names.
|
||||
func normalizeHeaders(headers http.Header) map[string]string {
|
||||
rc := make(map[string]string)
|
||||
if headers == nil {
|
||||
return rc
|
||||
}
|
||||
for k, v := range headers {
|
||||
rc[strings.ToLower(k)] = strings.Join(v, ", ")
|
||||
}
|
||||
return rc
|
||||
}
|
||||
|
||||
// rankedAcceptData is the type of data returned from rankAccept.
|
||||
type rankedAcceptData struct {
|
||||
mtype string // MIME type
|
||||
q1000 int // quality value multiplied by 1000
|
||||
ndx int // original index in Accept string
|
||||
}
|
||||
|
||||
// rankAccept turns an Accept string into a list of ranked accept members.
|
||||
func rankAccept(accept string) []rankedAcceptData {
|
||||
components := strings.Split(accept, ",")
|
||||
ranks := make([]rankedAcceptData, len(components))
|
||||
for i, com := range components {
|
||||
subparts := strings.Split(com, ";")
|
||||
ranks[i].mtype = subparts[0]
|
||||
ranks[i].q1000 = 1000
|
||||
ranks[i].ndx = i
|
||||
if len(subparts) > 1 {
|
||||
for _, part := range subparts[1:] {
|
||||
part = strings.TrimSpace(part)
|
||||
if strings.HasPrefix(part, "q=") {
|
||||
parsed, err := strconv.ParseFloat(part[2:], 64)
|
||||
if err != nil {
|
||||
ranks[i].q1000 = int(math.Floor(1000.0 * parsed))
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ranks2 := make([]rankedAcceptData, 0, len(ranks))
|
||||
for i := range ranks {
|
||||
if ranks[i].q1000 > 0 {
|
||||
ranks2 = append(ranks2, ranks[i])
|
||||
}
|
||||
}
|
||||
slices.SortFunc(ranks2, func(a, b rankedAcceptData) int {
|
||||
t := b.q1000 - a.q1000
|
||||
if t == 0 {
|
||||
t = b.ndx - a.ndx
|
||||
}
|
||||
return t
|
||||
})
|
||||
return ranks2
|
||||
}
|
||||
|
||||
// ranksAboveHtml returns true if any of the given MIME types ranks above HTML in the given Accept: string.
|
||||
func ranksAboveHtml(accept string, types []string) bool {
|
||||
ranked := rankAccept(accept)
|
||||
md := slices.IndexFunc(ranked, func(d rankedAcceptData) bool {
|
||||
return slices.Contains(types, d.mtype)
|
||||
})
|
||||
if md < 0 {
|
||||
return false
|
||||
}
|
||||
html := slices.IndexFunc(ranked, func(d rankedAcceptData) bool {
|
||||
return d.mtype == "text/html"
|
||||
})
|
||||
if html < 0 {
|
||||
return true
|
||||
}
|
||||
return md < html
|
||||
}
|
||||
|
||||
// markdownTypes are the MIME types associated with Markdown.
|
||||
var markdownTypes = []string{"text/markdown", "text/x-markdown"}
|
||||
|
||||
// prefersMarkdown returns true if the given Accept: string prefers Markdown over HTML.
|
||||
func prefersMarkdown(accept string) bool {
|
||||
return ranksAboveHtml(accept, markdownTypes)
|
||||
}
|
||||
|
||||
// normalizeAcceptExpr is how we close the gaps around delimiters in an Accept: expression.
|
||||
var normalizeAcceptExpr = regexp.MustCompile(`\s*([,;=])\s*`)
|
||||
|
||||
// normalizeAccept squeezes the whitespace out of an Accept: expression.
|
||||
func normalizeAccept(accept string) string {
|
||||
return strings.TrimSpace(normalizeAcceptExpr.ReplaceAllString(strings.ToLower(accept), "$1"))
|
||||
}
|
||||
|
||||
// signatureAgentHostExpr recognizes the "host" in a URL.
|
||||
var signatureAgentHostExpr = regexp.MustCompile(`(?i)https?:\/\/([^/"\s]+)`)
|
||||
|
||||
// signatureAgentHostBare1 is used to trim up a bare host name.
|
||||
var signatureAgentHostBare1 = regexp.MustCompile(`^[\w-]+=`)
|
||||
|
||||
// signatureAgentHostBare2 is used to trim up a bare host name.
|
||||
var signatureAgentHostBare2 = regexp.MustCompile(`"`)
|
||||
|
||||
// signatureAgentHost locates the host associated with a request signature.
|
||||
func signatureAgentHost(value string) *string {
|
||||
m := signatureAgentHostExpr.FindStringSubmatch(value)
|
||||
if m != nil {
|
||||
return new(strings.ToLower(m[1]))
|
||||
}
|
||||
bare := strings.TrimSpace(signatureAgentHostBare2.ReplaceAllString(signatureAgentHostBare1.ReplaceAllString(value, ""), ""))
|
||||
if bare != "" {
|
||||
return new(strings.ToLower(bare))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// findAgent matches the request against the list of compiled agents to locate a match. Returns nil on failure.
|
||||
func findAgent(ua string, h map[string]string) *AgentMatch {
|
||||
for _, ca := range compiledAgents {
|
||||
for _, p := range ca.patterns {
|
||||
m := p.FindStringSubmatch(ua)
|
||||
if m != nil {
|
||||
ndx := p.SubexpIndex("version")
|
||||
vers := ""
|
||||
if ndx > 0 {
|
||||
vers = m[ndx]
|
||||
}
|
||||
return new(AgentMatch{Agent: ca.r, Version: vers, Reason: fmt.Sprintf("user agent matches %s", ca.r.Name), Weak: false})
|
||||
}
|
||||
}
|
||||
}
|
||||
accept, ok := h["accept"]
|
||||
if ok {
|
||||
accept = normalizeAccept(accept)
|
||||
p := slices.IndexFunc(compiledAgents[:], func(ad agentDef) bool {
|
||||
if ad.r.Accept == nil {
|
||||
return false
|
||||
}
|
||||
for _, t := range ad.r.Accept {
|
||||
if normalizeAccept(t) == accept {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
if p >= 0 {
|
||||
rc := compiledAgents[p]
|
||||
return new(AgentMatch{Agent: rc.r, Version: "", Reason: fmt.Sprintf("Accept header matches %s's fingerprint", rc.r.Name), Weak: false})
|
||||
}
|
||||
}
|
||||
_, isSigned := h["signed"]
|
||||
if isSigned {
|
||||
_, isSigned = h["signature-input"]
|
||||
}
|
||||
if isSigned {
|
||||
sig, ok := h["signature-agent"]
|
||||
if ok {
|
||||
psig := signatureAgentHost(sig)
|
||||
if psig != nil {
|
||||
p := slices.IndexFunc(compiledAgents[:], func(ad agentDef) bool {
|
||||
if ad.r.SignatureAgent == nil {
|
||||
return false
|
||||
}
|
||||
return slices.Index(ad.r.SignatureAgent[:], *psig) >= 0
|
||||
})
|
||||
if p >= 0 {
|
||||
rc := compiledAgents[p]
|
||||
return new(AgentMatch{Agent: rc.r, Version: "",
|
||||
Reason: fmt.Sprintf("signed request names %s in Signature-Agent; the signature itself is not verified", *psig),
|
||||
Weak: true})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// fromAgent turns an AgentMatch into a Classification.
|
||||
func fromAgent(match *AgentMatch, rawIP string, opts ClassifyOptions) *Classification {
|
||||
agent := match.Agent
|
||||
base := new(Classification{
|
||||
Category: agent.Category,
|
||||
Kind: agent.Kind,
|
||||
Name: agent.Name,
|
||||
Operator: agent.Operator,
|
||||
Version: match.Version,
|
||||
Verified: true,
|
||||
Confidence: ConfidenceMedium,
|
||||
Reasons: make([]string, 1),
|
||||
})
|
||||
base.Reasons[0] = match.Reason
|
||||
if agent.RunsOn == "user" {
|
||||
base.Reasons = append(base.Reasons, fmt.Sprintf("%s runs on the user's machine, so IP verification does not apply", agent.Name))
|
||||
return base
|
||||
}
|
||||
ip := normalizeIP(rawIP)
|
||||
if ip == nil {
|
||||
base.Reasons = append(base.Reasons, fmt.Sprintf("%s is not a single valid address, so it was not checked", rawIP))
|
||||
return base
|
||||
}
|
||||
urls := make([]string, 0)
|
||||
if agent.Verify.IpRanges != nil {
|
||||
urls = append(urls, agent.Verify.IpRanges...)
|
||||
}
|
||||
now := opts.Now
|
||||
if now == nil {
|
||||
tmp := time.Now()
|
||||
now = &tmp
|
||||
}
|
||||
maxDuration := time.Hour * time.Duration(24*opts.MaxAge)
|
||||
impersonation := false
|
||||
validRangeFiles := false
|
||||
for _, url := range urls {
|
||||
cidrs, creation, err := getIPRangeData(url)
|
||||
if err != nil {
|
||||
base.Reasons = append(base.Reasons, fmt.Sprintf("unable to get range file %s: %v", url, err))
|
||||
continue
|
||||
}
|
||||
validRangeFiles = true
|
||||
if inRanges(*ip, cidrs) {
|
||||
base.Verified = true
|
||||
base.Confidence = ConfidenceHigh
|
||||
base.Reasons = append(base.Reasons, "%s is in %s's published ranges", *ip, agent.Operator)
|
||||
return base
|
||||
}
|
||||
if now.Sub(*creation) <= maxDuration {
|
||||
impersonation = true
|
||||
}
|
||||
}
|
||||
if !validRangeFiles {
|
||||
return base
|
||||
}
|
||||
if impersonation {
|
||||
base.Category = "bot"
|
||||
base.Kind = "impersonator"
|
||||
base.Verified = false
|
||||
base.Confidence = ConfidenceHigh
|
||||
base.Reasons = append(base.Reasons, fmt.Sprintf("claims to be %s but %s is outside %s's published ranges", agent.Name, *ip, agent.Operator))
|
||||
return base
|
||||
}
|
||||
base.Reasons = append(base.Reasons, fmt.Sprintf("%s's range file is older than %d days, so a mismatch is not counted as spoofing", agent.Operator, opts.MaxAge))
|
||||
return base
|
||||
}
|
||||
|
||||
// fake returns a Classification identifying the request as a fake browser.
|
||||
func fake(reason string, confidence int) *Classification {
|
||||
rc := new(Classification{
|
||||
Category: "bot",
|
||||
Kind: "fake-browser",
|
||||
Verified: false,
|
||||
Confidence: confidence,
|
||||
Reasons: make([]string, 1),
|
||||
})
|
||||
rc.Reasons[0] = reason
|
||||
return rc
|
||||
}
|
||||
|
||||
// majorVersion extracts a browser's major version from a regular expression.
|
||||
func majorVersion(ua string, re *regexp.Regexp) int {
|
||||
m := re.FindStringSubmatch(ua)
|
||||
if m != nil {
|
||||
v, err := strconv.Atoi(m[1])
|
||||
if err == nil {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
var firefoxBrowserExpr = regexp.MustCompile(`Firefox\/`)
|
||||
var firefoxVersionExpr = regexp.MustCompile(`Firefox\/(\d+)`)
|
||||
var chromeBrowserExpr = regexp.MustCompile(`Chrome\/`)
|
||||
var chromeVersionExpr = regexp.MustCompile(`Chrome\/(\d+)`)
|
||||
var safariBrowserExpr = regexp.MustCompile(`Version\/[\d.]+.*Safari\/`)
|
||||
var safariVersionExpr = regexp.MustCompile(`Version\/(\d+)`)
|
||||
|
||||
func sendsFetchMetadata(ua string) bool {
|
||||
if firefoxBrowserExpr.MatchString(ua) {
|
||||
return majorVersion(ua, firefoxVersionExpr) >= 90
|
||||
}
|
||||
if chromeBrowserExpr.MatchString(ua) {
|
||||
return majorVersion(ua, chromeVersionExpr) >= 80
|
||||
}
|
||||
if safariBrowserExpr.MatchString(ua) {
|
||||
return majorVersion(ua, safariVersionExpr) >= 17
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
var plainTextType = []string{"text/plain"}
|
||||
|
||||
func fromBrowser(ua string, h map[string]string, opts ClassifyOptions) *Classification {
|
||||
accept, ok := h["accept"]
|
||||
if ok {
|
||||
if ranksAboveHtml(accept, plainTextType) {
|
||||
return fake("Accept header ranks text/plain above HTML, which browsers never do", ConfidenceLow)
|
||||
}
|
||||
if strings.Contains(accept, "application/metalink") {
|
||||
return fake("Accept header asks for metalink, which download tools send and browsers do not", ConfidenceMedium)
|
||||
}
|
||||
}
|
||||
if !opts.HeadersComplete {
|
||||
rc := new(Classification{
|
||||
Category: "human",
|
||||
Verified: false,
|
||||
Confidence: ConfidenceLow,
|
||||
Reasons: make([]string, 1),
|
||||
})
|
||||
rc.Reasons[0] = "browser user agent; headers not checked"
|
||||
return rc
|
||||
}
|
||||
_, ok = h["sec-ch-ua"]
|
||||
if ok && !chromeBrowserExpr.MatchString(ua) {
|
||||
return fake("sec-ch-ua sent with a non-Chromium user agent", ConfidenceHigh)
|
||||
}
|
||||
_, ok = h["sec-fetch-mode"]
|
||||
if !ok && sendsFetchMetadata(ua) {
|
||||
return fake("browser user agent without sec-fetch-* headers", ConfidenceMedium)
|
||||
}
|
||||
_, ok = h["accept-language"]
|
||||
if !ok {
|
||||
return fake("browser user agent without Accept-Language", ConfidenceLow)
|
||||
}
|
||||
rc := new(Classification{
|
||||
Category: "human",
|
||||
Verified: false,
|
||||
Confidence: ConfidenceMedium,
|
||||
Reasons: make([]string, 1),
|
||||
})
|
||||
rc.Reasons[0] = "browser user agent with consistent browser headers"
|
||||
return rc
|
||||
}
|
||||
|
||||
func AmClassify(headers http.Header, ipAddr string, userAgent string, opts ...ClassifyOptions) *Classification {
|
||||
myopts := AmClassifyOptions()
|
||||
if len(opts) > 0 {
|
||||
myopts = opts[0]
|
||||
}
|
||||
h := normalizeHeaders(headers)
|
||||
ua := userAgent
|
||||
if ua == "" {
|
||||
t, ok := h["user-agent"]
|
||||
if ok {
|
||||
ua = t
|
||||
}
|
||||
}
|
||||
|
||||
if ua == "" {
|
||||
rc := new(Classification{
|
||||
Verified: false,
|
||||
Confidence: ConfidenceLow,
|
||||
Reasons: make([]string, 1),
|
||||
})
|
||||
if len(h) == 0 {
|
||||
rc.Category = "unknown"
|
||||
rc.Reasons[0] = "no user agent or headers to classify"
|
||||
} else {
|
||||
rc.Category = "bot"
|
||||
rc.Kind = "unidentified"
|
||||
rc.Reasons[0] = "request has no user agent, which browsers always send"
|
||||
}
|
||||
return rc
|
||||
}
|
||||
|
||||
// Look for a matching agent.
|
||||
match := findAgent(ua, h)
|
||||
if match != nil {
|
||||
return fromAgent(match, ipAddr, myopts)
|
||||
}
|
||||
|
||||
// Check the Accept: line; if the request prefers Markdown to HTML, we're some kind of agent.
|
||||
acc, ok := h["accept"]
|
||||
if ok {
|
||||
if prefersMarkdown(acc) {
|
||||
rc := new(Classification{
|
||||
Category: "agent",
|
||||
Kind: "unidentified",
|
||||
Verified: false,
|
||||
Confidence: ConfidenceMedium,
|
||||
Reasons: make([]string, 1),
|
||||
})
|
||||
rc.Reasons[0] = "Accept header prefers markdown over HTML, which browsers never do"
|
||||
return rc
|
||||
}
|
||||
}
|
||||
|
||||
// Look for an HTTP client.
|
||||
ndx := slices.IndexFunc(compiledClients, func(d clientDef) bool {
|
||||
return d.pattern.MatchString(ua)
|
||||
})
|
||||
if ndx >= 0 {
|
||||
rc := new(Classification{
|
||||
Category: "bot",
|
||||
Kind: compiledClients[ndx].r.Kind,
|
||||
Name: compiledClients[ndx].r.Name,
|
||||
Verified: false,
|
||||
Confidence: ConfidenceMedium,
|
||||
Reasons: make([]string, 1),
|
||||
})
|
||||
rc.Reasons[0] = fmt.Sprintf("user agent is %s", compiledClients[ndx].r.Name)
|
||||
return rc
|
||||
}
|
||||
if isBrowser(ua) {
|
||||
return fromBrowser(ua, h, myopts)
|
||||
}
|
||||
rc := new(Classification{
|
||||
Category: "bot",
|
||||
Kind: "unidentified",
|
||||
Verified: false,
|
||||
Confidence: ConfidenceLow,
|
||||
Reasons: make([]string, 1),
|
||||
})
|
||||
rc.Reasons[0] = "user agent is not a browser and matches no known agent, crawler or client"
|
||||
return rc
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
[
|
||||
{ "name": "axios", "kind": "http-client", "userAgent": "^axios/" },
|
||||
{ "name": "curl", "kind": "http-client", "userAgent": "^curl/" },
|
||||
{ "name": "Wget", "kind": "http-client", "userAgent": "^Wget/" },
|
||||
{ "name": "Node.js", "kind": "http-client", "userAgent": "^(node|undici|node-fetch)(/|$)" },
|
||||
{ "name": "Bun", "kind": "http-client", "userAgent": "^Bun/" },
|
||||
{ "name": "Deno", "kind": "http-client", "userAgent": "^Deno/" },
|
||||
{ "name": "Python requests", "kind": "http-client", "userAgent": "^python-requests/" },
|
||||
{ "name": "Python httpx", "kind": "http-client", "userAgent": "^python-httpx/" },
|
||||
{ "name": "Python urllib", "kind": "http-client", "userAgent": "^Python-urllib/" },
|
||||
{ "name": "Python aiohttp", "kind": "http-client", "userAgent": "^Python/[\\d.]+ aiohttp/" },
|
||||
{ "name": "Go net/http", "kind": "http-client", "userAgent": "^Go-http-client/" },
|
||||
{ "name": "OkHttp", "kind": "http-client", "userAgent": "^okhttp/" },
|
||||
{ "name": "Java", "kind": "http-client", "userAgent": "^(Java/|Apache-HttpClient/)" },
|
||||
{ "name": "Guzzle", "kind": "http-client", "userAgent": "^GuzzleHttp/" },
|
||||
{ "name": "Rust reqwest", "kind": "http-client", "userAgent": "^reqwest/" },
|
||||
{ "name": "lua-resty-http", "kind": "http-client", "userAgent": "^lua-resty-http/" },
|
||||
{ "name": "Colly", "kind": "http-client", "userAgent": "^colly\\b" },
|
||||
{ "name": "Scrapy", "kind": "http-client", "userAgent": "\\bScrapy/" },
|
||||
{ "name": "n8n", "kind": "http-client", "userAgent": "^n8n$" },
|
||||
{ "name": "Postman", "kind": "http-client", "userAgent": "^PostmanRuntime/" },
|
||||
{ "name": "Headless Chrome", "kind": "headless", "userAgent": "\\bHeadlessChrome/" },
|
||||
{ "name": "PhantomJS", "kind": "headless", "userAgent": "\\bPhantomJS/" },
|
||||
{ "name": "Playwright", "kind": "headless", "userAgent": "\\bPlaywright/" },
|
||||
{ "name": "PowerShell", "kind": "http-client", "userAgent": "PowerShell/" },
|
||||
{ "name": "Ruby", "kind": "http-client", "userAgent": "^Ruby$" },
|
||||
{ "name": "got", "kind": "http-client", "userAgent": "^got\\b" },
|
||||
{ "name": "libwww-perl", "kind": "http-client", "userAgent": "\\blibwww-perl/" },
|
||||
{ "name": "aria2", "kind": "http-client", "userAgent": "^aria2/" },
|
||||
{ "name": "HTTrack", "kind": "http-client", "userAgent": "\\bHTTrack\\b" },
|
||||
{ "name": "Crawl4AI", "kind": "http-client", "userAgent": "^crawl4ai" },
|
||||
{ "name": "Apple CFNetwork (native app)", "kind": "http-client", "userAgent": "\\bCFNetwork/" },
|
||||
{ "name": "Android Dalvik (native app)", "kind": "http-client", "userAgent": "\\bDalvik/" },
|
||||
{ "name": "Amazon CloudFront", "kind": "http-client", "userAgent": "^Amazon CloudFront$" },
|
||||
{ "name": "Vercel Edge Functions", "kind": "http-client", "userAgent": "^Vercel Edge Functions$" },
|
||||
{ "name": "Google Cloud Scheduler and Tasks", "kind": "http-client", "userAgent": "^Google-Cloud-(Scheduler|Tasks)$" }
|
||||
]
|
||||
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Amsterdam Web Communities System
|
||||
* Copyright (c) 2025-2026 Erbosoft Metaverse Design Solutions, All Rights Reserved
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*/
|
||||
/* This file contains code adapted from Mintlify agent-traffic, https://github.com/mintlify/agent-traffic
|
||||
* ---
|
||||
* Copyright (c) 2026 Mintlify, Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
// Package agentclass contains support for agent request classification.
|
||||
package agentclass
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// parseIP turns a string IP address into a ParsedIP. Returns nil if address is not parseable.
|
||||
func parseIP(ip string) *netip.Addr {
|
||||
s := strings.TrimSpace(ip)
|
||||
v6 := strings.Contains(s, ":")
|
||||
addy, err := netip.ParseAddr(s)
|
||||
if err != nil || addy.Is6() != v6 {
|
||||
return nil
|
||||
}
|
||||
return &addy
|
||||
}
|
||||
|
||||
// bracketedExpr recognizes an IP address with brackets.
|
||||
var bracketedExpr = regexp.MustCompile(`^\[([^\]]+)\](?::\d+)?$`)
|
||||
|
||||
// v4WithPortExpr recognizes an IPv4 address with a port.
|
||||
var v4WithPortExpr = regexp.MustCompile(`^(\d{1,3}(?:\.\d{1,3}){3}):\d+$`)
|
||||
|
||||
// normalizeIP filters a supplied IP address. Returns nil if the address is not parseable.
|
||||
func normalizeIP(ip string) *string {
|
||||
s := strings.TrimSpace(ip)
|
||||
if s == "" || strings.Contains(s, ",") {
|
||||
return nil
|
||||
}
|
||||
bracketed := bracketedExpr.FindStringSubmatch(s)
|
||||
v4WithPort := v4WithPortExpr.FindStringSubmatch(s)
|
||||
candidate := s
|
||||
if bracketed != nil {
|
||||
candidate = bracketed[1]
|
||||
} else if v4WithPort != nil {
|
||||
candidate = v4WithPort[1]
|
||||
}
|
||||
parsed := parseIP(candidate)
|
||||
if parsed == nil {
|
||||
return nil
|
||||
}
|
||||
if parsed.Is4In6() {
|
||||
return new(netip.AddrFrom4(parsed.As4()).String())
|
||||
}
|
||||
return new(candidate)
|
||||
}
|
||||
|
||||
func inRanges(ip string, cidrs []netip.Prefix) bool {
|
||||
normalized := normalizeIP(ip)
|
||||
if normalized == nil {
|
||||
return false
|
||||
}
|
||||
addy := parseIP(*normalized)
|
||||
return slices.IndexFunc(cidrs, func(c netip.Prefix) bool {
|
||||
return c.Contains(*addy)
|
||||
}) >= 0
|
||||
}
|
||||
@@ -0,0 +1,262 @@
|
||||
/*
|
||||
* Amsterdam Web Communities System
|
||||
* Copyright (c) 2025-2026 Erbosoft Metaverse Design Solutions, All Rights Reserved
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*/
|
||||
/* This file contains code adapted from Mintlify agent-traffic, https://github.com/mintlify/agent-traffic
|
||||
* ---
|
||||
* Copyright (c) 2026 Mintlify, Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
// Package agentclass contains support for agent request classification.
|
||||
package agentclass
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json/jsontext"
|
||||
"encoding/json/v2"
|
||||
"fmt"
|
||||
"maps"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.erbosoft.com/amy/amsterdam/config"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Format for IP range data inside the RangeDataFormat.
|
||||
type RangeDataPrefix struct {
|
||||
IPv4Prefix string `json:"ipv4Prefix"` // prefix, in IPv4 format
|
||||
IPv6Prefix string `json:"ipv6Prefix"` // prefix, in IPv6 format
|
||||
}
|
||||
|
||||
// Format of IP range data files exported by bot providers.
|
||||
type RangeDataFormat struct {
|
||||
CreationTime *time.Time `json:"creationTime"` // creation date and time
|
||||
Prefixes []RangeDataPrefix `json:"prefixes"` // the prefixes
|
||||
}
|
||||
|
||||
// Stored IP range data for a single URL.
|
||||
type ipRangeData struct {
|
||||
m sync.Mutex // mutex on the range data
|
||||
c *sync.Cond // signals when load complete
|
||||
loaded bool // set when load operation completed
|
||||
loadError error // error during load, or nil
|
||||
lastMod string // Last-Modified header from last fetch
|
||||
creation *time.Time // creation date and time
|
||||
cidrs []netip.Prefix // the prefixes from this file
|
||||
}
|
||||
|
||||
// Contains all the data for loaded IP range files.
|
||||
var ipRangeStore map[string]*ipRangeData
|
||||
|
||||
// loadIPRangeData is the goroutine that fetches range data from a URL and parses it.
|
||||
func loadIPRangeData(ctx context.Context, url string, rd *ipRangeData) {
|
||||
rd.m.Lock()
|
||||
defer rd.m.Unlock()
|
||||
LOADLOOP:
|
||||
for counter := config.GlobalConfig.Tuning.IPRangeLoadAttempts; counter > 0; counter-- {
|
||||
select {
|
||||
case <-ctx.Done(): // cancellation check on every loop
|
||||
rd.loadError = ctx.Err()
|
||||
break LOADLOOP
|
||||
default:
|
||||
// build the HTTP request
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
rd.loadError = err
|
||||
break LOADLOOP
|
||||
}
|
||||
if rd.lastMod != "" {
|
||||
req.Header.Set("If-Modified-Since", rd.lastMod)
|
||||
}
|
||||
|
||||
// build a client and use it to load the file
|
||||
client := &http.Client{Timeout: time.Duration(config.GlobalConfig.Tuning.Timeouts.IPRangeLoad) * time.Second}
|
||||
resp, err := client.Do(req)
|
||||
select {
|
||||
case <-ctx.Done(): // we might have been canceled while waiting for the GET
|
||||
resp.Body.Close()
|
||||
rd.loadError = ctx.Err()
|
||||
break LOADLOOP
|
||||
default:
|
||||
// interpret error and status codes
|
||||
if err != nil {
|
||||
rd.loadError = err
|
||||
continue LOADLOOP
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode == http.StatusNotModified {
|
||||
rd.loadError = nil
|
||||
break LOADLOOP
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
rd.loadError = fmt.Errorf("failed to get file with status %d", resp.StatusCode)
|
||||
continue LOADLOOP
|
||||
}
|
||||
|
||||
// load Last-Modified date
|
||||
lastmod := ""
|
||||
if lm, ok := resp.Header["Last-Modified"]; ok {
|
||||
lastmod = lm[0]
|
||||
}
|
||||
|
||||
// parse the JSON data retrieved
|
||||
decoder := jsontext.NewDecoder(resp.Body)
|
||||
var parsed RangeDataFormat
|
||||
err = json.UnmarshalDecode(decoder, &parsed)
|
||||
if err != nil {
|
||||
rd.loadError = err
|
||||
break LOADLOOP
|
||||
}
|
||||
|
||||
// turn the parsed data into our internal data
|
||||
lx := 0
|
||||
if parsed.Prefixes != nil {
|
||||
lx = len(parsed.Prefixes)
|
||||
}
|
||||
rd.cidrs = make([]netip.Prefix, lx)
|
||||
if lx > 0 {
|
||||
for i, p := range parsed.Prefixes {
|
||||
if p.IPv4Prefix == "" {
|
||||
rd.cidrs[i], err = netip.ParsePrefix(p.IPv6Prefix)
|
||||
} else {
|
||||
rd.cidrs[i], err = netip.ParsePrefix(p.IPv4Prefix)
|
||||
}
|
||||
if err != nil {
|
||||
rd.loadError = err
|
||||
break LOADLOOP
|
||||
}
|
||||
}
|
||||
}
|
||||
rd.loadError = nil
|
||||
rd.lastMod = lastmod
|
||||
rd.creation = parsed.CreationTime
|
||||
break LOADLOOP
|
||||
}
|
||||
}
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done(): // one more cancel check before ending
|
||||
if rd.loadError == nil {
|
||||
rd.loadError = ctx.Err()
|
||||
}
|
||||
default:
|
||||
// fall through
|
||||
}
|
||||
rd.loaded = true // finished loading, let another routine go
|
||||
rd.c.Signal()
|
||||
log.Debugf("loaded IP range document at %s", url)
|
||||
}
|
||||
|
||||
// prepareRefresh prepares some ipRangeData for being refreshed.
|
||||
func prepareRefresh(rd *ipRangeData) {
|
||||
rd.m.Lock()
|
||||
defer rd.m.Unlock()
|
||||
for !rd.loaded {
|
||||
rd.c.Wait()
|
||||
}
|
||||
rd.loaded = false
|
||||
}
|
||||
|
||||
// refreshRanges runs periodically to refresh the range data from the various sites.
|
||||
func refreshRanges(ctx context.Context, interval time.Duration) {
|
||||
tick := time.NewTicker(interval)
|
||||
REFRESHLOOP:
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
tick.Stop()
|
||||
break REFRESHLOOP
|
||||
case <-tick.C:
|
||||
mymap := make(map[string]*ipRangeData)
|
||||
maps.Copy(mymap, ipRangeStore)
|
||||
for url, rd := range mymap {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
tick.Stop()
|
||||
break REFRESHLOOP
|
||||
default:
|
||||
prepareRefresh(rd)
|
||||
go loadIPRangeData(ctx, url, rd)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// loadRangesCtx is the context used for running the range load and refresh data.
|
||||
var loadRangesCtx context.Context = nil
|
||||
|
||||
// loadIPRanges begins loading the initial IP ranges.
|
||||
func loadIPRanges(urls []string, ctxname any) func() {
|
||||
ipRangeStore = make(map[string]*ipRangeData)
|
||||
ctx, cancelfunc := context.WithCancel(context.Background())
|
||||
loadRangesCtx = context.WithValue(ctx, ctxname, "loadIPRanges")
|
||||
for _, url := range urls {
|
||||
rdat := new(ipRangeData{loaded: false, loadError: nil, lastMod: "", creation: nil})
|
||||
rdat.c = sync.NewCond(&(rdat.m))
|
||||
ipRangeStore[url] = rdat
|
||||
go loadIPRangeData(loadRangesCtx, url, rdat)
|
||||
}
|
||||
log.Infof("loadIPRanges: started loading %d IP range files", len(urls))
|
||||
dur, err := time.ParseDuration(config.GlobalConfig.Tuning.IPRangeRefresh)
|
||||
if err != nil {
|
||||
log.Errorf("invalid IP range refresh duration \"%s\", using default", config.GlobalConfig.Tuning.IPRangeRefresh)
|
||||
dur = 24 * time.Hour
|
||||
}
|
||||
go refreshRanges(loadRangesCtx, dur)
|
||||
return func() {
|
||||
cancelfunc()
|
||||
for _, v := range ipRangeStore {
|
||||
v.m.Lock()
|
||||
if !v.loaded {
|
||||
v.loadError = context.Canceled
|
||||
v.c.Signal()
|
||||
}
|
||||
v.m.Unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// getIPRangeData gets the IP range data for a specified URL, which may have been cached already.
|
||||
func getIPRangeData(url string) ([]netip.Prefix, *time.Time, error) {
|
||||
rdat, ok := ipRangeStore[url]
|
||||
if !ok {
|
||||
rdat := new(ipRangeData{loaded: false, loadError: nil, lastMod: "", creation: nil})
|
||||
rdat.c = sync.NewCond(&(rdat.m))
|
||||
ipRangeStore[url] = rdat
|
||||
go loadIPRangeData(loadRangesCtx, url, rdat)
|
||||
}
|
||||
rdat.m.Lock()
|
||||
defer rdat.m.Unlock()
|
||||
for !rdat.loaded {
|
||||
rdat.c.Wait()
|
||||
}
|
||||
return rdat.cidrs, rdat.creation, rdat.loadError
|
||||
}
|
||||
+3
-2
@@ -22,7 +22,7 @@ import (
|
||||
"git.erbosoft.com/amy/amsterdam/ui"
|
||||
"git.erbosoft.com/amy/amsterdam/util"
|
||||
"github.com/biter777/countries"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v5"
|
||||
)
|
||||
|
||||
// ENOJOIN is an error for not being permitted to join a community.
|
||||
@@ -117,7 +117,8 @@ func ShowCommunity(ctxt ui.AmContext) (string, any) {
|
||||
b.WriteString(*ci.Region)
|
||||
}
|
||||
if ci.PostalCode != nil {
|
||||
b.WriteString(" " + *ci.PostalCode)
|
||||
b.WriteString(" ")
|
||||
b.WriteString(*ci.PostalCode)
|
||||
}
|
||||
ctxt.VarMap().Set("addrLast", b.String())
|
||||
if ci.Country != nil && *ci.Country != "" {
|
||||
|
||||
+1
-1
@@ -28,7 +28,7 @@ import (
|
||||
"git.erbosoft.com/amy/amsterdam/exports"
|
||||
"git.erbosoft.com/amy/amsterdam/ui"
|
||||
"git.erbosoft.com/amy/amsterdam/util"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v5"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
|
||||
+27
-24
@@ -26,7 +26,7 @@ import (
|
||||
"git.erbosoft.com/amy/amsterdam/htmlcheck"
|
||||
"git.erbosoft.com/amy/amsterdam/ui"
|
||||
"github.com/CloudyKit/jet/v6"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v5"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -357,16 +357,13 @@ func breakRange(topic *database.Topic, into []int32, param string, sep string) e
|
||||
|
||||
// templateExtractUserName extracts the user name from the post.
|
||||
func templateExtractUserName(args jet.Arguments) reflect.Value {
|
||||
rc := "<<ERROR>>"
|
||||
post := args.Get(0).Convert(reflect.TypeFor[*database.PostHeader]()).Interface().(*database.PostHeader)
|
||||
ctxt := args.Get(1).Convert(reflect.TypeFor[ui.AmContext]()).Interface().(ui.AmContext)
|
||||
user, err := database.AmGetUser(ctxt.Ctx(), post.CreatorUid)
|
||||
if err == nil {
|
||||
rc = user.Username
|
||||
} else {
|
||||
log.Errorf("templateExtractUserName failed to get user #%d: %v", post.CreatorUid, err)
|
||||
if err != nil {
|
||||
args.Panicf("templateExtractUserName failed to get user #%d: %v", post.CreatorUid, err)
|
||||
}
|
||||
return reflect.ValueOf(rc)
|
||||
return reflect.ValueOf(user.Username)
|
||||
}
|
||||
|
||||
// templatePostText gets the text of a post.
|
||||
@@ -375,8 +372,7 @@ func templatePostText(args jet.Arguments) reflect.Value {
|
||||
ctxt := args.Get(1).Convert(reflect.TypeFor[ui.AmContext]()).Interface().(ui.AmContext)
|
||||
rc, err := post.Text(ctxt.Ctx())
|
||||
if err != nil {
|
||||
log.Errorf("templatePostText could not get post text from post #%d: %v", post.PostId, err)
|
||||
rc = ""
|
||||
args.Panicf("templatePostText could not get post text from post #%d: %v", post.PostId, err)
|
||||
}
|
||||
return reflect.ValueOf(rc)
|
||||
}
|
||||
@@ -397,11 +393,10 @@ func templateOverrideLine(args jet.Arguments) reflect.Value {
|
||||
scr_date = p.Localizer().Strftime("%b %e, %Y %r", *post.ScribbleDate)
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
rc = fmt.Sprintf("(Scribbled by %s on %s)", scr_user.Username, scr_date)
|
||||
} else {
|
||||
rc = fmt.Sprintf("<<<%v>>>", err)
|
||||
if err != nil {
|
||||
args.Panicf("failed to get prefs for current user in templateOverrideLine - %v", err)
|
||||
}
|
||||
rc = fmt.Sprintf("(Scribbled by %s on %s)", scr_user.Username, scr_date)
|
||||
} else if post.Hidden && !advanced {
|
||||
rc = fmt.Sprintf("(Hidden Message: %d Lines)", *post.LineCount)
|
||||
}
|
||||
@@ -424,7 +419,10 @@ func templateOverrideLink(args jet.Arguments) reflect.Value {
|
||||
func templateAttachmentInfo(args jet.Arguments) reflect.Value {
|
||||
post := args.Get(0).Convert(reflect.TypeFor[*database.PostHeader]()).Interface().(*database.PostHeader)
|
||||
ctxt := args.Get(1).Convert(reflect.TypeFor[ui.AmContext]()).Interface().(ui.AmContext)
|
||||
rc, _ := post.AttachmentInfo(ctxt.Ctx())
|
||||
rc, err := post.AttachmentInfo(ctxt.Ctx())
|
||||
if err != nil {
|
||||
args.Panicf("templateAttachmentInfo unable to get attachment info for post #%d: %v", post.PostId, err)
|
||||
}
|
||||
if rc == nil {
|
||||
rc = &database.PostAttachInfo{}
|
||||
}
|
||||
@@ -436,7 +434,10 @@ func templateBozo(args jet.Arguments) reflect.Value {
|
||||
post := args.Get(0).Convert(reflect.TypeFor[*database.PostHeader]()).Interface().(*database.PostHeader)
|
||||
topic := args.Get(1).Convert(reflect.TypeFor[*database.Topic]()).Interface().(*database.Topic)
|
||||
ctxt := args.Get(2).Convert(reflect.TypeFor[ui.AmContext]()).Interface().(ui.AmContext)
|
||||
rc, _ := topic.IsBozo(ctxt.Ctx(), ctxt.CurrentUser(), post.CreatorUid)
|
||||
rc, err := topic.IsBozo(ctxt.Ctx(), ctxt.CurrentUser(), post.CreatorUid)
|
||||
if err != nil {
|
||||
args.Panicf("templateBozo unable to get status for user #%d in topic #%d: %v", post.CreatorUid, topic.TopicId, err)
|
||||
}
|
||||
return reflect.ValueOf(rc)
|
||||
}
|
||||
|
||||
@@ -446,13 +447,15 @@ func templateProfileImage(args jet.Arguments) reflect.Value {
|
||||
ctxt := args.Get(1).Convert(reflect.TypeFor[ui.AmContext]()).Interface().(ui.AmContext)
|
||||
rc := config.GlobalConfig.Site.DefaultUserPhoto
|
||||
user, err := post.Creator(ctxt.Ctx())
|
||||
if err == nil {
|
||||
ci, err := user.ContactInfo(ctxt.Ctx())
|
||||
if err == nil {
|
||||
if ci.PhotoURL != nil {
|
||||
rc = *ci.PhotoURL
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
args.Panicf("templateProfileImage unable to get post creator for post #%d: %v", post.PostId, err)
|
||||
}
|
||||
ci, err := user.ContactInfo(ctxt.Ctx())
|
||||
if err != nil {
|
||||
args.Panicf("templateProfileImage unable to get contact info for user #%d: %v", user.Uid, err)
|
||||
}
|
||||
if ci.PhotoURL != nil {
|
||||
rc = *ci.PhotoURL
|
||||
}
|
||||
return reflect.ValueOf(rc)
|
||||
}
|
||||
@@ -513,11 +516,11 @@ func ReadPosts(ctxt ui.AmContext) (string, any) {
|
||||
resetLastRead := false
|
||||
if ctxt.HasParameter("r") {
|
||||
if err := breakRange(topic, postRange, ctxt.Parameter("r"), ","); err != nil {
|
||||
return "error", echo.NewHTTPError(http.StatusNotFound).SetInternal(err)
|
||||
return "error", echo.NewHTTPError(http.StatusNotFound, err.Error()).Wrap(err)
|
||||
}
|
||||
} else if ctxt.HasParameter("rgo") {
|
||||
if err := breakRange(topic, postRange, ctxt.Parameter("rgo"), "-"); err != nil {
|
||||
return "error", echo.NewHTTPError(http.StatusNotFound).SetInternal(err)
|
||||
return "error", echo.NewHTTPError(http.StatusNotFound, err.Error()).Wrap(err)
|
||||
}
|
||||
} else {
|
||||
postRange[0] = lastRead + 1
|
||||
|
||||
+1
-1
@@ -26,7 +26,7 @@ import (
|
||||
"git.erbosoft.com/amy/amsterdam/database"
|
||||
"git.erbosoft.com/amy/amsterdam/email"
|
||||
"git.erbosoft.com/amy/amsterdam/ui"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v5"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
|
||||
+82
-22
@@ -21,16 +21,18 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"slices"
|
||||
|
||||
"git.erbosoft.com/amy/amsterdam/util"
|
||||
argparse "github.com/alexflint/go-arg"
|
||||
"github.com/dustin/go-humanize"
|
||||
"github.com/labstack/echo/v5"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// AMSTERDAM_VERSION contains the version number of Amsterdam.
|
||||
const AMSTERDAM_VERSION = "0.2.0"
|
||||
const AMSTERDAM_VERSION = "0.2.1"
|
||||
|
||||
// AMSTERDAM_COPYRIGHT contains the copyright dates for Amsterdam.
|
||||
const AMSTERDAM_COPYRIGHT = "2025-2026"
|
||||
@@ -100,12 +102,16 @@ type AmConfig struct {
|
||||
SessionExpire string `yaml:"sessionExpire"`
|
||||
UserAgreementResource string `yaml:"userAgreementResource"`
|
||||
PolicyResource string `yaml:"policyResource"`
|
||||
FrameTemplate string `yaml:"frameTemplate"`
|
||||
FooterTemplate string `yaml:"footerTemplate"`
|
||||
TopMenuId string `yaml:"topMenuId"`
|
||||
FixedMenuId string `yaml:"fixedMenuId"`
|
||||
DefaultCommunityLogo string `yaml:"defaultCommunityLogo"`
|
||||
DefaultUserPhoto string `yaml:"defaultUserPhoto"`
|
||||
WelcomeTitle string `yaml:"welcomeTitle"`
|
||||
WelcomeMessage string `yaml:"welcomeMessage"`
|
||||
TopPostsTitle string `yaml:"topPostsTitle"`
|
||||
IPResolver string `yaml:"ipResolver"`
|
||||
RateLimit struct {
|
||||
Rate float64 `yaml:"rate"`
|
||||
Burst int `yaml:"burst"`
|
||||
@@ -118,6 +124,12 @@ type AmConfig struct {
|
||||
User string `yaml:"user"`
|
||||
Password string `yaml:"password"`
|
||||
DatabaseName string `yaml:"databaseName"`
|
||||
Pooling struct {
|
||||
MaxOpen int `yaml:"maxOpen"`
|
||||
MaxIdle int `yaml:"maxIdle"`
|
||||
MaxIdleTime int `yaml:"maxIdleTime"`
|
||||
MaxLifetime int `yaml:"maxLifetime"`
|
||||
} `yaml:"pooling"`
|
||||
} `yaml:"database"`
|
||||
Defaults struct {
|
||||
Language string `yaml:"language"`
|
||||
@@ -148,6 +160,9 @@ type AmConfig struct {
|
||||
Prioritize string `yaml:"prioritize"`
|
||||
} `yaml:"countryList"`
|
||||
VeniceCompatibleImageURLs bool `yaml:"veniceCompatibleImageURLs"`
|
||||
PanicRecovery struct {
|
||||
StackDataSize string `yaml:"stackDataSize"`
|
||||
} `yaml:"panicRecovery"`
|
||||
} `yaml:"rendering"`
|
||||
Resources struct {
|
||||
ViewTemplateDir string `yaml:"viewTemplateDir"`
|
||||
@@ -166,8 +181,18 @@ type AmConfig struct {
|
||||
} `yaml:"uploads"`
|
||||
} `yaml:"posting"`
|
||||
Tuning struct {
|
||||
WorkerTasks int `yaml:"workerTasks"`
|
||||
Queues struct {
|
||||
WorkerTasks int `yaml:"workerTasks"`
|
||||
IPRangeLoadAttempts int `yaml:"ipRangeLoadAttempts"`
|
||||
IPRangeRefresh string `yaml:"ipRangeRefresh"`
|
||||
Timeouts struct {
|
||||
HttpRead int `yaml:"httpRead"`
|
||||
HttpWrite int `yaml:"httpWrite"`
|
||||
HttpIdle int `yaml:"httpIdle"`
|
||||
PageExecute int `yaml:"pageExecute"`
|
||||
PageRender int `yaml:"pageRender"`
|
||||
IPRangeLoad int `yaml:"ipRangeLoad"`
|
||||
} `yaml:"timeouts"`
|
||||
Queues struct {
|
||||
AuditWrites int `yaml:"auditWrites"`
|
||||
ContextRecycle int `yaml:"contextRecycle"`
|
||||
EmailRecycle int `yaml:"emailRecycle"`
|
||||
@@ -200,24 +225,25 @@ func (c *AmConfig) ExPath(path string) string {
|
||||
return filepath.Join(c.baseDir, path)
|
||||
}
|
||||
|
||||
// AmConfigComputed is the configuration values which are "computed" based only on values in AmConfig.
|
||||
// AmConfigComputed is the configuration values which are "computed" based only on values in AmConfig and CommandLine.
|
||||
type AmConfigComputed struct {
|
||||
DebugMode bool // are we in debug mode?
|
||||
LogLevel string // the logging level
|
||||
Listen string // listen address
|
||||
DatabaseDriver string // name of database driver
|
||||
DatabaseHost string // hostname for database
|
||||
DatabaseUser string // user name for database
|
||||
DatabasePassword string // password for database
|
||||
DatabaseName string // database name
|
||||
MailHost string // SMTP host
|
||||
MailPort int // SMTP port
|
||||
MailTLS string // SMTP TLS setting
|
||||
MailAuthType string // SMTP auth type
|
||||
MailUser string // SMTP user name
|
||||
MailPassword string // SMTP password
|
||||
UploadMaxSize int32 // maximum upload size in bytes
|
||||
UploadNoCompress map[string]bool // which upload types are not compressed?
|
||||
DebugMode bool // are we in debug mode?
|
||||
LogLevel string // the logging level
|
||||
Listen string // listen address
|
||||
DatabaseDriver string // name of database driver
|
||||
DatabaseHost string // hostname for database
|
||||
DatabaseUser string // user name for database
|
||||
DatabasePassword string // password for database
|
||||
DatabaseName string // database name
|
||||
MailHost string // SMTP host
|
||||
MailPort int // SMTP port
|
||||
MailTLS string // SMTP TLS setting
|
||||
MailAuthType string // SMTP auth type
|
||||
MailUser string // SMTP user name
|
||||
MailPassword string // SMTP password
|
||||
PanicRecoveryStack int32 // stack size for panic recovery
|
||||
UploadMaxSize int32 // maximum upload size in bytes
|
||||
UploadNoCompress map[string]bool // which upload types are not compressed?
|
||||
}
|
||||
|
||||
//go:embed default.yaml
|
||||
@@ -321,7 +347,7 @@ func overlayStructValue(dest, loaded, defaults reflect.Value) {
|
||||
}
|
||||
} else {
|
||||
// if we see this message, this function needs more work
|
||||
log.Errorf("*** unable to deal with field %s of type %s", structField.Name, typ.Name())
|
||||
log.Fatalf("*** unable to deal with field %s of type %s", structField.Name, typ.Name())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -348,6 +374,22 @@ func AmOpenExternalContentPath() (fs.FS, error) {
|
||||
return root.FS(), nil
|
||||
}
|
||||
|
||||
// validIPResolvers contains the list of valid values for GlobalConfig.Site.IPResolver.
|
||||
var validIPResolvers = [3]string{"direct", "realIP", "forwardedFor"}
|
||||
|
||||
// AmGetConfiguredIPExtractor returns the configured IP Extractor function based on GlobalConfig.
|
||||
func AmGetConfiguredIPExtractor() echo.IPExtractor {
|
||||
switch GlobalConfig.Site.IPResolver {
|
||||
case "direct":
|
||||
return echo.ExtractIPDirect()
|
||||
case "realIP":
|
||||
return echo.ExtractIPFromRealIPHeader()
|
||||
case "forwardedFor":
|
||||
return echo.ExtractIPFromXFFHeader()
|
||||
}
|
||||
panic("improperly configured IP Extractor")
|
||||
}
|
||||
|
||||
// SetupConfig loads the command line arguments, loads the config file, and prepares GlobalConfig.
|
||||
func SetupConfig() {
|
||||
argparse.MustParse(&CommandLine)
|
||||
@@ -377,6 +419,19 @@ func SetupConfig() {
|
||||
GlobalConfig.baseDir = ""
|
||||
}
|
||||
|
||||
// Error-check values.
|
||||
if !slices.Contains(validIPResolvers[:], GlobalConfig.Site.IPResolver) {
|
||||
panic(fmt.Sprintf("invalid IP resolver name \"%s\"", GlobalConfig.Site.IPResolver))
|
||||
}
|
||||
if GlobalConfig.Database.Pooling.MaxOpen < 0 {
|
||||
panic("maximum database connections must not be less than zero")
|
||||
} else if GlobalConfig.Database.Pooling.MaxOpen == 0 {
|
||||
log.Warn("WARNING: database connections unlimited - server may crash under heavy load")
|
||||
}
|
||||
if GlobalConfig.Database.Pooling.MaxIdle < 0 {
|
||||
panic("maximum idle database connections must not be less than zero")
|
||||
}
|
||||
|
||||
// Compute additional values.
|
||||
if CommandLine.Debug {
|
||||
GlobalComputedConfig.DebugMode = true
|
||||
@@ -398,7 +453,12 @@ func SetupConfig() {
|
||||
GlobalComputedConfig.MailAuthType = util.IIF(CommandLine.MailAuthType != "", CommandLine.MailAuthType, GlobalConfig.Email.AuthType)
|
||||
GlobalComputedConfig.MailUser = util.IIF(CommandLine.MailUser != "", CommandLine.MailUser, GlobalConfig.Email.User)
|
||||
GlobalComputedConfig.MailPassword = util.IIF(CommandLine.MailPassword != "", CommandLine.MailPassword, GlobalConfig.Email.Password)
|
||||
tmp, err := humanize.ParseBytes(GlobalConfig.Posting.Uploads.MaxSize)
|
||||
tmp, err := humanize.ParseBytes(GlobalConfig.Rendering.PanicRecovery.StackDataSize)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
GlobalComputedConfig.PanicRecoveryStack = int32(tmp)
|
||||
tmp, err = humanize.ParseBytes(GlobalConfig.Posting.Uploads.MaxSize)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
|
||||
@@ -25,7 +25,10 @@ site:
|
||||
sessionExpire: "3h"
|
||||
userAgreementResource: "useragreement.html"
|
||||
policyResource: "policy.html"
|
||||
frameTemplate: "frame.jet"
|
||||
footerTemplate: "footer.jet"
|
||||
topMenuId: "top"
|
||||
fixedMenuId: "fixed"
|
||||
defaultCommunityLogo: "/img/builtin/default-community.jpg"
|
||||
defaultUserPhoto: "/img/builtin/no-user.png"
|
||||
welcomeTitle: "Welcome to Amsterdam"
|
||||
@@ -33,6 +36,7 @@ site:
|
||||
Welcome to the <strong>Amsterdam Web Communities System</strong>. To get the most out of this site, you should log in or create an account,
|
||||
using one of the links above.
|
||||
topPostsTitle: "Latest from the Conferences"
|
||||
ipResolver: direct
|
||||
rateLimit:
|
||||
rate: 20.0
|
||||
burst: 0
|
||||
@@ -43,6 +47,11 @@ database:
|
||||
user: "amsdb"
|
||||
password: "x00yes2k"
|
||||
databaseName: "amsterdam"
|
||||
pooling:
|
||||
maxOpen: 100
|
||||
maxIdle: 10
|
||||
maxIdleTime: -1
|
||||
maxLifetime: -1
|
||||
defaults:
|
||||
language: "en-US"
|
||||
timezone: "America/Denver"
|
||||
@@ -72,6 +81,8 @@ rendering:
|
||||
countryList:
|
||||
prioritize: US
|
||||
veniceCompatibleImageURLs: false
|
||||
panicRecovery:
|
||||
stackDataSize: "4 KiB"
|
||||
resources:
|
||||
viewTemplateDir: ""
|
||||
dialogTemplateDir: ""
|
||||
@@ -91,6 +102,15 @@ posting:
|
||||
- "image/png"
|
||||
tuning:
|
||||
workerTasks: 4
|
||||
ipRangeLoadAttempts: 3
|
||||
ipRangeRefresh: "24h"
|
||||
timeouts:
|
||||
httpRead: 30
|
||||
httpWrite: 30
|
||||
httpIdle: 120
|
||||
pageExecute: 15
|
||||
pageRender: 15
|
||||
ipRangeLoad: 10
|
||||
queues:
|
||||
auditWrites: 16
|
||||
contextRecycle: 16
|
||||
|
||||
+26
-8
@@ -20,6 +20,7 @@ import (
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.erbosoft.com/amy/amsterdam/config"
|
||||
"github.com/go-sql-driver/mysql"
|
||||
@@ -29,9 +30,9 @@ import (
|
||||
|
||||
// Error classifications
|
||||
const (
|
||||
classUnspecified = 0
|
||||
classNeedInstall = 1
|
||||
classNeedConvert = 2
|
||||
classUnspecified = iota // unspecified, barf
|
||||
classNeedInstall // need to install the database
|
||||
classNeedConvert // need to convert a Venice database
|
||||
)
|
||||
|
||||
// MySQL Errors
|
||||
@@ -210,20 +211,35 @@ func prepareDB() (string, error) {
|
||||
}
|
||||
|
||||
// SetupDb sets up the database and associated items.
|
||||
func SetupDb() (func(), error) {
|
||||
func SetupDb() (string, func(), error) {
|
||||
exitfns := make([]func(), 0, 2)
|
||||
version, err := prepareDB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return "X", nil, err
|
||||
}
|
||||
db, err := sqlx.Connect(config.GlobalComputedConfig.DatabaseDriver, buildMysqlDSN(false))
|
||||
if err == nil {
|
||||
// Configure the connection pool options.
|
||||
db.SetMaxOpenConns(config.GlobalConfig.Database.Pooling.MaxOpen)
|
||||
db.SetMaxIdleConns(config.GlobalConfig.Database.Pooling.MaxIdle)
|
||||
if config.GlobalConfig.Database.Pooling.MaxIdleTime > 0 {
|
||||
db.SetConnMaxIdleTime(time.Duration(config.GlobalConfig.Database.Pooling.MaxIdleTime) * time.Second)
|
||||
}
|
||||
if config.GlobalConfig.Database.Pooling.MaxLifetime > 0 {
|
||||
db.SetConnMaxLifetime(time.Duration(config.GlobalConfig.Database.Pooling.MaxLifetime) * time.Second)
|
||||
}
|
||||
|
||||
// Retrieve the database globals.
|
||||
amdb = db
|
||||
g, err := AmGlobals(context.Background())
|
||||
if err == nil {
|
||||
// Check database version against expected version.
|
||||
if g.Version != version {
|
||||
log.Warnf("!! database version %s does not match prepared version %s", g.Version, version)
|
||||
version = g.Version
|
||||
}
|
||||
|
||||
// Setup the database-dependent caches and background tasks.
|
||||
setupAdCache()
|
||||
setupUserCache()
|
||||
setupContactsCache()
|
||||
@@ -232,11 +248,11 @@ func SetupDb() (func(), error) {
|
||||
setupConferenceCache()
|
||||
exitfns = append(exitfns, setupAuditWriter())
|
||||
exitfns = append(exitfns, setupIPBanSweep())
|
||||
log.Infof("SetupDb(): database version %s", g.Version)
|
||||
log.Infof("SetupDb(): database version %s", version)
|
||||
}
|
||||
}
|
||||
slices.Reverse(exitfns)
|
||||
return func() {
|
||||
slices.Reverse(exitfns) // exit funcs run in reverse order
|
||||
return version, func() {
|
||||
for _, f := range exitfns {
|
||||
f()
|
||||
}
|
||||
@@ -262,6 +278,8 @@ func transaction(ctx context.Context) (*sqlx.Tx, func() error, func()) {
|
||||
err = tx.Commit()
|
||||
if err == nil {
|
||||
live = false
|
||||
} else {
|
||||
log.Errorf("***COMMIT ERROR*** %v", err)
|
||||
}
|
||||
}
|
||||
return err
|
||||
|
||||
@@ -243,6 +243,7 @@ func AmSearchCategories(ctx context.Context, oper int, term string, offset int,
|
||||
if err != nil {
|
||||
return nil, total, err
|
||||
}
|
||||
defer rs.Close()
|
||||
rc := make([]*Category, 0, min(max, 1000))
|
||||
for rs.Next() {
|
||||
var catid int32
|
||||
@@ -256,5 +257,5 @@ func AmSearchCategories(ctx context.Context, oper int, term string, offset int,
|
||||
log.Errorf("AmSearchCategoris scan error: %v", err)
|
||||
}
|
||||
}
|
||||
return rc, total, nil
|
||||
return rc, total, rs.Err()
|
||||
}
|
||||
|
||||
@@ -314,6 +314,7 @@ func (c *Community) ListMembers(ctx context.Context, field, oper int, term strin
|
||||
if err != nil {
|
||||
return nil, total, err
|
||||
}
|
||||
defer rs.Close()
|
||||
rc := make([]*User, 0, min(maxCount, 10000))
|
||||
for rs.Next() {
|
||||
var uid int32
|
||||
@@ -324,7 +325,7 @@ func (c *Community) ListMembers(ctx context.Context, field, oper int, term strin
|
||||
}
|
||||
}
|
||||
}
|
||||
return rc, total, nil
|
||||
return rc, total, rs.Err()
|
||||
}
|
||||
|
||||
/* SetMembership sets a user's membership status within the community.
|
||||
@@ -1054,6 +1055,7 @@ func AmGetCommunitiesForCategory(ctx context.Context, catid int32, offset, maxCo
|
||||
if err != nil {
|
||||
return nil, total, err
|
||||
}
|
||||
defer rs.Close()
|
||||
rc := make([]*Community, 0, min(maxCount, 10000))
|
||||
for rs.Next() {
|
||||
var commid int32
|
||||
@@ -1064,7 +1066,7 @@ func AmGetCommunitiesForCategory(ctx context.Context, catid int32, offset, maxCo
|
||||
}
|
||||
}
|
||||
}
|
||||
return rc, total, nil
|
||||
return rc, total, rs.Err()
|
||||
}
|
||||
|
||||
/* AmSearchCommunities searches for communities matching certain criteria.
|
||||
@@ -1131,6 +1133,7 @@ func AmSearchCommunities(ctx context.Context, field, oper int, term string, offs
|
||||
if err != nil {
|
||||
return nil, total, err
|
||||
}
|
||||
defer rs.Close()
|
||||
rc := make([]*Community, 0, min(maxCount, 10000))
|
||||
for rs.Next() {
|
||||
var commid int32
|
||||
@@ -1141,7 +1144,7 @@ func AmSearchCommunities(ctx context.Context, field, oper int, term string, offs
|
||||
}
|
||||
}
|
||||
}
|
||||
return rc, total, nil
|
||||
return rc, total, rs.Err()
|
||||
}
|
||||
|
||||
/* AmSearchCommunityMembers searches for members of a community matching certain criteria.
|
||||
@@ -1216,6 +1219,7 @@ func AmSearchCommunityMembers(ctx context.Context, c *Community, field, oper int
|
||||
if err != nil {
|
||||
return nil, total, err
|
||||
}
|
||||
defer rs.Close()
|
||||
rc := make([]*User, 0, min(maxCount, 10000))
|
||||
for rs.Next() {
|
||||
var uid int32
|
||||
@@ -1230,5 +1234,5 @@ func AmSearchCommunityMembers(ctx context.Context, c *Community, field, oper int
|
||||
log.Errorf("AmSearchCommunityMembers scan error: %v", err)
|
||||
}
|
||||
}
|
||||
return rc, total, nil
|
||||
return rc, total, rs.Err()
|
||||
}
|
||||
|
||||
+16
-4
@@ -158,6 +158,7 @@ func (c *Conference) Aliases(ctx context.Context, commid int32) ([]string, error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rs.Close()
|
||||
rc := make([]string, 0, 5)
|
||||
for rs.Next() {
|
||||
var a string
|
||||
@@ -167,7 +168,7 @@ func (c *Conference) Aliases(ctx context.Context, commid int32) ([]string, error
|
||||
log.Errorf("Aliases scan error: %v", err)
|
||||
}
|
||||
}
|
||||
return rc, nil
|
||||
return rc, rs.Err()
|
||||
}
|
||||
|
||||
// AddAlias adds an alias to the conference.
|
||||
@@ -290,6 +291,7 @@ func (c *Conference) ContainedBy(ctx context.Context) ([]*Community, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rs.Close()
|
||||
rc := make([]*Community, 0, 1)
|
||||
for rs.Next() {
|
||||
var cid int32
|
||||
@@ -303,7 +305,7 @@ func (c *Conference) ContainedBy(ctx context.Context) ([]*Community, error) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return rc, nil
|
||||
return rc, rs.Err()
|
||||
}
|
||||
|
||||
// Members returns all the members of this conference, with their granted user levels.
|
||||
@@ -614,12 +616,16 @@ func (c *Conference) Fixseen(ctx context.Context, u *User) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rs.Close()
|
||||
work := make([]fixseenData, 0, count)
|
||||
for rs.Next() {
|
||||
var d fixseenData
|
||||
err = rs.Scan(&(d.topicid), &(d.topmessage), &(d.insert))
|
||||
work = append(work, d)
|
||||
}
|
||||
if rs.Err() != nil {
|
||||
return rs.Err()
|
||||
}
|
||||
|
||||
// Adjust each topic in turn.
|
||||
for _, d := range work {
|
||||
@@ -727,6 +733,7 @@ func (c *Conference) GetActivity(ctx context.Context, reportType int) ([]Activit
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rs.Close()
|
||||
rc := make([]ActivityReport, 0)
|
||||
for rs.Next() {
|
||||
var cur ActivityReport
|
||||
@@ -736,7 +743,7 @@ func (c *Conference) GetActivity(ctx context.Context, reportType int) ([]Activit
|
||||
}
|
||||
rc = append(rc, cur)
|
||||
}
|
||||
return rc, nil
|
||||
return rc, rs.Err()
|
||||
}
|
||||
|
||||
// Active user selection types.
|
||||
@@ -777,6 +784,7 @@ func (c *Conference) GetActiveUserEMailAddrs(ctx context.Context, userSelect, da
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rs.Close()
|
||||
var stopPoint *time.Time = nil
|
||||
if dayLimit > 0 {
|
||||
mynow := time.Now().UTC()
|
||||
@@ -796,7 +804,7 @@ func (c *Conference) GetActiveUserEMailAddrs(ctx context.Context, userSelect, da
|
||||
}
|
||||
rc = append(rc, addy)
|
||||
}
|
||||
return rc, nil
|
||||
return rc, rs.Err()
|
||||
}
|
||||
|
||||
// Stats retrieves the number of topics and posts in this conference.
|
||||
@@ -1108,6 +1116,7 @@ func AmListConferences(ctx context.Context, cid int32, showHidden bool) ([]*Conf
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rs.Close()
|
||||
rc := make([]*ConferenceSummary, 0)
|
||||
for rs.Next() {
|
||||
cs := new(ConferenceSummary)
|
||||
@@ -1117,6 +1126,9 @@ func AmListConferences(ctx context.Context, cid int32, showHidden bool) ([]*Conf
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if rs.Err() != nil {
|
||||
return nil, rs.Err()
|
||||
}
|
||||
for i := range rc {
|
||||
err := amdb.GetContext(ctx, &(rc[i].Alias), "SELECT alias FROM confalias WHERE commid = ? AND confid = ?", cid, rc[i].ConfId)
|
||||
if err != nil {
|
||||
|
||||
+30
-26
@@ -254,10 +254,10 @@ func (p *PostHeader) Text(ctx context.Context) (string, error) {
|
||||
|
||||
// Link returns a link string to this post.
|
||||
func (p *PostHeader) Link(ctx context.Context, commid int32, scope string) (string, error) {
|
||||
if scope == "topic" {
|
||||
if scope == PLSCOPE_TOPIC {
|
||||
return fmt.Sprintf("%d", p.Num), nil
|
||||
}
|
||||
if scope == "conference" || scope == "community" || scope == "global" {
|
||||
if scope == PLSCOPE_CONFERENCE || scope == PLSCOPE_COMMUNITY || scope == PLSCOPE_GLOBAL {
|
||||
topic, err := AmGetTopic(ctx, p.TopicId)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -639,6 +639,7 @@ func AmGetPublishedPosts(ctx context.Context) ([]*PostHeader, []*Community, erro
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
defer rs.Close()
|
||||
// Extract post IDs to an array.
|
||||
cids := make([]int32, gv.FrontPagePosts)
|
||||
pids := make([]int64, gv.FrontPagePosts)
|
||||
@@ -649,7 +650,9 @@ func AmGetPublishedPosts(ctx context.Context) ([]*PostHeader, []*Community, erro
|
||||
}
|
||||
i++
|
||||
}
|
||||
if i == 0 { // no published posts, short-circuit response
|
||||
if rs.Err() != nil {
|
||||
return nil, nil, rs.Err()
|
||||
} else if i == 0 { // no published posts, short-circuit response
|
||||
return make([]*PostHeader, 0), make([]*Community, 0), nil
|
||||
}
|
||||
if i < int(gv.FrontPagePosts) {
|
||||
@@ -720,55 +723,55 @@ func decodeSearchScope(ctx context.Context, scopeValues []any) (string, *Communi
|
||||
}
|
||||
if thisComm, ok := scopeValues[i].(*Community); ok {
|
||||
if myComm != nil {
|
||||
return "error", nil, nil, nil, errors.New("cannot specify multiple communities")
|
||||
return PLSCOPE_ERROR, nil, nil, nil, errors.New("cannot specify multiple communities")
|
||||
}
|
||||
myComm = thisComm
|
||||
continue
|
||||
}
|
||||
if thisConf, ok := scopeValues[i].(*Conference); ok {
|
||||
if myConf != nil {
|
||||
return "error", nil, nil, nil, errors.New("cannot specify multiple conferences")
|
||||
return PLSCOPE_ERROR, nil, nil, nil, errors.New("cannot specify multiple conferences")
|
||||
}
|
||||
myConf = thisConf
|
||||
continue
|
||||
}
|
||||
if thisTopic, ok := scopeValues[i].(*Topic); ok {
|
||||
if myTopic != nil {
|
||||
return "error", nil, nil, nil, errors.New("cannot specify multiple topics")
|
||||
return PLSCOPE_ERROR, nil, nil, nil, errors.New("cannot specify multiple topics")
|
||||
}
|
||||
myTopic = thisTopic
|
||||
continue
|
||||
}
|
||||
return "error", nil, nil, nil, errors.New("invalid item specified in scope")
|
||||
return PLSCOPE_ERROR, nil, nil, nil, errors.New("invalid item specified in scope")
|
||||
}
|
||||
|
||||
// Based on which slots are full, determine the scope. Also error-check relations between the specified slots.
|
||||
if myComm == nil {
|
||||
if myConf != nil || myTopic != nil {
|
||||
return "error", nil, nil, nil, errors.New("conference/topic specified without community")
|
||||
return PLSCOPE_ERROR, nil, nil, nil, errors.New("conference/topic specified without community")
|
||||
}
|
||||
return "global", nil, nil, nil, nil
|
||||
return PLSCOPE_GLOBAL, nil, nil, nil, nil
|
||||
}
|
||||
if myConf == nil {
|
||||
if myTopic != nil {
|
||||
return "error", nil, nil, nil, errors.New("topic specified without conference")
|
||||
return PLSCOPE_ERROR, nil, nil, nil, errors.New("topic specified without conference")
|
||||
}
|
||||
return "community", myComm, nil, nil, nil
|
||||
return PLSCOPE_COMMUNITY, myComm, nil, nil, nil
|
||||
}
|
||||
f, err := myConf.InCommunity(ctx, myComm)
|
||||
if err != nil {
|
||||
return "error", nil, nil, nil, err
|
||||
return PLSCOPE_ERROR, nil, nil, nil, err
|
||||
}
|
||||
if !f {
|
||||
return "error", nil, nil, nil, errors.New("community does not contain conference")
|
||||
return PLSCOPE_ERROR, nil, nil, nil, errors.New("community does not contain conference")
|
||||
}
|
||||
if myTopic == nil {
|
||||
return "conference", myComm, myConf, nil, nil
|
||||
return PLSCOPE_CONFERENCE, myComm, myConf, nil, nil
|
||||
}
|
||||
if myTopic.ConfId != myConf.ConfId {
|
||||
return "error", nil, nil, nil, errors.New("conference does not contain topic")
|
||||
return PLSCOPE_ERROR, nil, nil, nil, errors.New("conference does not contain topic")
|
||||
}
|
||||
return "topic", myComm, myConf, myTopic, nil
|
||||
return PLSCOPE_TOPIC, myComm, myConf, myTopic, nil
|
||||
}
|
||||
|
||||
/* AmSearchPosts finds posts by using full text search on their contents.
|
||||
@@ -794,7 +797,7 @@ func AmSearchPosts(ctx context.Context, searchTerms string, u *User, offset, max
|
||||
}
|
||||
|
||||
// Get the proper service index to match against the community services.
|
||||
confService, err := AmGetServiceIndex("community", "Conference")
|
||||
confService, err := AmGetServiceIndex(AM_DOMAIN_COMMUNITY, AM_SVC_CONFERENCE)
|
||||
if err != nil {
|
||||
return nil, -1, err
|
||||
}
|
||||
@@ -802,7 +805,7 @@ func AmSearchPosts(ctx context.Context, searchTerms string, u *User, offset, max
|
||||
// Get the count of matching posts.
|
||||
var count int
|
||||
switch scope {
|
||||
case "global":
|
||||
case PLSCOPE_GLOBAL:
|
||||
err = amdb.GetContext(ctx, &count, `SELECT COUNT(*)
|
||||
FROM communities q JOIN commtoconf s ON s.commid = q.commid JOIN confs c ON c.confid = s.confid
|
||||
JOIN commmember m ON m.commid = q.commid JOIN users u ON u.uid = m.uid JOIN commftrs f ON f.commid = q.commid
|
||||
@@ -810,7 +813,7 @@ func AmSearchPosts(ctx context.Context, searchTerms string, u *User, offset, max
|
||||
LEFT JOIN confmember x ON (c.confid = x.confid AND u.uid = x.uid)
|
||||
WHERE u.uid = ? AND f.ftr_code = ? AND GREATEST(u.base_lvl,m.granted_lvl,s.granted_lvl,COALESCE(x.granted_lvl,0)) >= c.read_lvl
|
||||
AND p.scribble_uid IS NULL AND MATCH(d.data) AGAINST (?)`, u.Uid, confService, searchTerms)
|
||||
case "community":
|
||||
case PLSCOPE_COMMUNITY:
|
||||
err = amdb.GetContext(ctx, &count, `SELECT COUNT(*)
|
||||
FROM communities q JOIN commtoconf s ON s.commid = q.commid JOIN confs c ON c.confid = s.confid
|
||||
JOIN commmember m ON m.commid = q.commid JOIN users u ON u.uid = m.uid JOIN commftrs f ON f.commid = q.commid
|
||||
@@ -818,7 +821,7 @@ func AmSearchPosts(ctx context.Context, searchTerms string, u *User, offset, max
|
||||
LEFT JOIN confmember x ON (c.confid = x.confid AND u.uid = x.uid)
|
||||
WHERE u.uid = ? AND f.ftr_code = ? AND GREATEST(u.base_lvl,m.granted_lvl,s.granted_lvl,COALESCE(x.granted_lvl,0)) >= c.read_lvl
|
||||
AND q.commid = ? AND p.scribble_uid IS NULL AND MATCH(d.data) AGAINST (?)`, u.Uid, confService, comm.Id, searchTerms)
|
||||
case "conference":
|
||||
case PLSCOPE_CONFERENCE:
|
||||
err = amdb.GetContext(ctx, &count, `SELECT COUNT(*)
|
||||
FROM communities q JOIN commtoconf s ON s.commid = q.commid JOIN confs c ON c.confid = s.confid
|
||||
JOIN commmember m ON m.commid = q.commid JOIN users u ON u.uid = m.uid JOIN commftrs f ON f.commid = q.commid
|
||||
@@ -826,7 +829,7 @@ func AmSearchPosts(ctx context.Context, searchTerms string, u *User, offset, max
|
||||
LEFT JOIN confmember x ON (c.confid = x.confid AND u.uid = x.uid)
|
||||
WHERE u.uid = ? AND f.ftr_code = ? AND GREATEST(u.base_lvl,m.granted_lvl,s.granted_lvl,COALESCE(x.granted_lvl,0)) >= c.read_lvl
|
||||
AND q.commid = ? AND c.confid = ? AND p.scribble_uid IS NULL AND MATCH(d.data) AGAINST (?)`, u.Uid, confService, comm.Id, conf.ConfId, searchTerms)
|
||||
case "topic":
|
||||
case PLSCOPE_TOPIC:
|
||||
err = amdb.GetContext(ctx, &count, `SELECT COUNT(*)
|
||||
FROM communities q JOIN commtoconf s ON s.commid = q.commid JOIN confs c ON c.confid = s.confid
|
||||
JOIN commmember m ON m.commid = q.commid JOIN users u ON u.uid = m.uid JOIN commftrs f ON f.commid = q.commid
|
||||
@@ -844,7 +847,7 @@ func AmSearchPosts(ctx context.Context, searchTerms string, u *User, offset, max
|
||||
// Get the matching posts themselves.
|
||||
var rs *sql.Rows
|
||||
switch scope {
|
||||
case "global":
|
||||
case PLSCOPE_GLOBAL:
|
||||
rs, err = amdb.QueryContext(ctx, `SELECT q.commid, q.alias, c.confid, t.topicid, t.num, p.postid, p.num, u2.username, p.posted, p.linecount, d.data
|
||||
FROM communities q JOIN commtoconf s ON s.commid = q.commid JOIN confs c ON c.confid = s.confid
|
||||
JOIN commmember m ON m.commid = q.commid JOIN users u ON u.uid = m.uid JOIN commftrs f ON f.commid = q.commid
|
||||
@@ -853,7 +856,7 @@ func AmSearchPosts(ctx context.Context, searchTerms string, u *User, offset, max
|
||||
WHERE u.uid = ? AND f.ftr_code = ? AND GREATEST(u.base_lvl,m.granted_lvl,s.granted_lvl,COALESCE(x.granted_lvl,0)) >= c.read_lvl
|
||||
AND p.scribble_uid IS NULL AND MATCH(d.data) AGAINST (?) ORDER BY q.commname, c.name, t.num, p.num
|
||||
LIMIT ? OFFSET ?`, u.Uid, confService, searchTerms, max, offset)
|
||||
case "community":
|
||||
case PLSCOPE_COMMUNITY:
|
||||
rs, err = amdb.QueryContext(ctx, `SELECT q.commid, q.alias, c.confid, t.topicid, t.num, p.postid, p.num, u2.username, p.posted, p.linecount, d.data
|
||||
FROM communities q JOIN commtoconf s ON s.commid = q.commid JOIN confs c ON c.confid = s.confid
|
||||
JOIN commmember m ON m.commid = q.commid JOIN users u ON u.uid = m.uid JOIN commftrs f ON f.commid = q.commid
|
||||
@@ -862,7 +865,7 @@ func AmSearchPosts(ctx context.Context, searchTerms string, u *User, offset, max
|
||||
WHERE u.uid = ? AND f.ftr_code = ? AND GREATEST(u.base_lvl,m.granted_lvl,s.granted_lvl,COALESCE(x.granted_lvl,0)) >= c.read_lvl
|
||||
AND q.commid = ? AND p.scribble_uid IS NULL AND MATCH(d.data) AGAINST (?) ORDER BY q.commname, c.name, t.num, p.num
|
||||
LIMIT ? OFFSET ?`, u.Uid, confService, comm.Id, searchTerms, max, offset)
|
||||
case "conference":
|
||||
case PLSCOPE_CONFERENCE:
|
||||
rs, err = amdb.QueryContext(ctx, `SELECT q.commid, q.alias, c.confid, t.topicid, t.num, p.postid, p.num, u2.username, p.posted, p.linecount, d.data
|
||||
FROM communities q JOIN commtoconf s ON s.commid = q.commid JOIN confs c ON c.confid = s.confid
|
||||
JOIN commmember m ON m.commid = q.commid JOIN users u ON u.uid = m.uid JOIN commftrs f ON f.commid = q.commid
|
||||
@@ -871,7 +874,7 @@ func AmSearchPosts(ctx context.Context, searchTerms string, u *User, offset, max
|
||||
WHERE u.uid = ? AND f.ftr_code = ? AND GREATEST(u.base_lvl,m.granted_lvl,s.granted_lvl,COALESCE(x.granted_lvl,0)) >= c.read_lvl
|
||||
AND q.commid = ? AND c.confid = ? AND p.scribble_uid IS NULL AND MATCH(d.data) AGAINST (?) ORDER BY q.commname, c.name, t.num, p.num
|
||||
LIMIT ? OFFSET ?`, u.Uid, confService, comm.Id, conf.ConfId, searchTerms, max, offset)
|
||||
case "topic":
|
||||
case PLSCOPE_TOPIC:
|
||||
rs, err = amdb.QueryContext(ctx, `SELECT q.commid, q.alias, c.confid, t.topicid, t.num, p.postid, p.num, u2.username, p.posted, p.linecount, d.data
|
||||
FROM communities q JOIN commtoconf s ON s.commid = q.commid JOIN confs c ON c.confid = s.confid
|
||||
JOIN commmember m ON m.commid = q.commid JOIN users u ON u.uid = m.uid JOIN commftrs f ON f.commid = q.commid
|
||||
@@ -885,6 +888,7 @@ func AmSearchPosts(ctx context.Context, searchTerms string, u *User, offset, max
|
||||
log.Errorf("AmSearchPosts query 2 error %v", err)
|
||||
return nil, count, err
|
||||
}
|
||||
defer rs.Close()
|
||||
rc := make([]PostSearchResult, max)
|
||||
i := 0
|
||||
for rs.Next() {
|
||||
@@ -930,5 +934,5 @@ func AmSearchPosts(ctx context.Context, searchTerms string, u *User, offset, max
|
||||
if i < max {
|
||||
rc = rc[:i] // slice off any empty entries at the end
|
||||
}
|
||||
return rc, count, nil
|
||||
return rc, count, rs.Err()
|
||||
}
|
||||
|
||||
+63
-43
@@ -20,6 +20,25 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Post link scopes.
|
||||
const (
|
||||
PLSCOPE_GLOBAL = "global"
|
||||
PLSCOPE_COMMUNITY = "community"
|
||||
PLSCOPE_CONFERENCE = "conference"
|
||||
PLSCOPE_TOPIC = "topic"
|
||||
PLSCOPE_ERROR = "error"
|
||||
)
|
||||
|
||||
// Post link classifications.
|
||||
const (
|
||||
PLCLASS_COMMUNITY = "community"
|
||||
PLCLASS_CONFERENCE = "conference"
|
||||
PLCLASS_TOPIC = "topic"
|
||||
PLCLASS_POST = "post"
|
||||
PLCLASS_POSTRANGE = "postrange"
|
||||
PLCLASS_POSTOPENRANGE = "postopenrange"
|
||||
)
|
||||
|
||||
// PostLinkData is the structure holding the decoded parts of the post link.
|
||||
type PostLinkData struct {
|
||||
Community string
|
||||
@@ -121,53 +140,53 @@ func (d *PostLinkData) Classify() (string, string) {
|
||||
if d.FirstPost == -1 {
|
||||
return "", ""
|
||||
} else if d.LastPost == -1 {
|
||||
return "topic", "postopenrange"
|
||||
return PLSCOPE_TOPIC, PLCLASS_POSTOPENRANGE
|
||||
} else if d.LastPost == d.FirstPost {
|
||||
return "topic", "post"
|
||||
return PLSCOPE_TOPIC, PLCLASS_POST
|
||||
} else {
|
||||
return "topic", "postrange"
|
||||
return PLSCOPE_TOPIC, PLCLASS_POSTRANGE
|
||||
}
|
||||
} else {
|
||||
if d.FirstPost == -1 {
|
||||
return "conference", "topic"
|
||||
return PLSCOPE_CONFERENCE, PLCLASS_TOPIC
|
||||
} else if d.LastPost == -1 {
|
||||
return "conference", "postopenrange"
|
||||
return PLSCOPE_CONFERENCE, PLCLASS_POSTOPENRANGE
|
||||
} else if d.LastPost == d.FirstPost {
|
||||
return "conference", "post"
|
||||
return PLSCOPE_CONFERENCE, PLCLASS_POST
|
||||
} else {
|
||||
return "conference", "postrange"
|
||||
return PLSCOPE_CONFERENCE, PLCLASS_POSTRANGE
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if d.Topic == -1 {
|
||||
return "community", "conference"
|
||||
return PLSCOPE_COMMUNITY, PLCLASS_CONFERENCE
|
||||
} else {
|
||||
if d.FirstPost == -1 {
|
||||
return "community", "topic"
|
||||
return PLSCOPE_COMMUNITY, PLCLASS_TOPIC
|
||||
} else if d.LastPost == -1 {
|
||||
return "community", "postopenrange"
|
||||
return PLSCOPE_COMMUNITY, PLCLASS_POSTOPENRANGE
|
||||
} else if d.LastPost == d.FirstPost {
|
||||
return "community", "post"
|
||||
return PLSCOPE_COMMUNITY, PLCLASS_POST
|
||||
} else {
|
||||
return "community", "postrange"
|
||||
return PLSCOPE_COMMUNITY, PLCLASS_POSTRANGE
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if d.Conference == "" {
|
||||
return "global", "community"
|
||||
return PLSCOPE_GLOBAL, PLCLASS_COMMUNITY
|
||||
} else {
|
||||
if d.Topic == -1 {
|
||||
return "global", "conference"
|
||||
return PLSCOPE_GLOBAL, PLCLASS_CONFERENCE
|
||||
} else {
|
||||
if d.FirstPost == -1 {
|
||||
return "global", "topic"
|
||||
return PLSCOPE_GLOBAL, PLCLASS_TOPIC
|
||||
} else if d.LastPost == -1 {
|
||||
return "global", "postopenrange"
|
||||
return PLSCOPE_GLOBAL, PLCLASS_POSTOPENRANGE
|
||||
} else if d.LastPost == d.FirstPost {
|
||||
return "global", "post"
|
||||
return PLSCOPE_GLOBAL, PLCLASS_POST
|
||||
} else {
|
||||
return "global", "postrange"
|
||||
return PLSCOPE_GLOBAL, PLCLASS_POSTRANGE
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -276,25 +295,25 @@ func AmDecodePostLink(data string) (*PostLinkData, error) {
|
||||
if len(data) > maxLinkLength {
|
||||
return nil, errors.New("post link string too long")
|
||||
}
|
||||
rc := PostLinkData{
|
||||
rc := new(PostLinkData{
|
||||
Community: "",
|
||||
Conference: "",
|
||||
Topic: -1,
|
||||
FirstPost: -1,
|
||||
LastPost: -1,
|
||||
}
|
||||
})
|
||||
|
||||
work := data
|
||||
// First test: Bang
|
||||
pos := strings.IndexByte(work, '!')
|
||||
if pos > 0 {
|
||||
err := validateCommunity(work[:pos], &rc)
|
||||
err := validateCommunity(work[:pos], rc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
work = work[pos+1:]
|
||||
if len(work) == 0 {
|
||||
return &rc, nil // community link
|
||||
return rc, nil // community link
|
||||
}
|
||||
} else if pos == 0 {
|
||||
return nil, errors.New("cannot have ! at beginning")
|
||||
@@ -306,14 +325,14 @@ func AmDecodePostLink(data string) (*PostLinkData, error) {
|
||||
// no dots in here, must be either "postlink" or "community!conference"
|
||||
var err error
|
||||
if rc.Community == "" {
|
||||
err = decodePostRange(work, &rc)
|
||||
err = decodePostRange(work, rc)
|
||||
} else {
|
||||
err = validateConference(work, &rc)
|
||||
err = validateConference(work, rc)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &rc, nil
|
||||
return rc, nil
|
||||
}
|
||||
|
||||
// Peel off the initial substring before the dot.
|
||||
@@ -324,19 +343,19 @@ func AmDecodePostLink(data string) (*PostLinkData, error) {
|
||||
var err error
|
||||
if rc.Community == "" {
|
||||
// it's either "conference." or "topic." - try the latter first
|
||||
err = decodeTopicNumber(confOrTopic, &rc)
|
||||
err = decodeTopicNumber(confOrTopic, rc)
|
||||
if err != nil {
|
||||
// it's not a topic number, try it as a conference name
|
||||
err = validateConference(confOrTopic, &rc)
|
||||
err = validateConference(confOrTopic, rc)
|
||||
}
|
||||
} else {
|
||||
// it was "community!conference."
|
||||
err = validateConference(confOrTopic, &rc)
|
||||
err = validateConference(confOrTopic, rc)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &rc, nil
|
||||
return rc, nil
|
||||
}
|
||||
|
||||
// Third test: Dot #2
|
||||
@@ -347,38 +366,38 @@ func AmDecodePostLink(data string) (*PostLinkData, error) {
|
||||
if rc.Community == "" {
|
||||
// either "conference.topic" or "topic.posts"
|
||||
isTopic := false
|
||||
err = decodeTopicNumber(confOrTopic, &rc)
|
||||
err = decodeTopicNumber(confOrTopic, rc)
|
||||
if err != nil {
|
||||
// it's "conference.topic"
|
||||
err = validateConference(confOrTopic, &rc)
|
||||
err = validateConference(confOrTopic, rc)
|
||||
isTopic = true
|
||||
}
|
||||
if err == nil {
|
||||
if isTopic {
|
||||
err = decodeTopicNumber(work, &rc)
|
||||
err = decodeTopicNumber(work, rc)
|
||||
} else {
|
||||
err = decodePostRange(work, &rc)
|
||||
err = decodePostRange(work, rc)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// we have "community!conference.topic"
|
||||
err = validateConference(confOrTopic, &rc)
|
||||
err = validateConference(confOrTopic, rc)
|
||||
if err == nil {
|
||||
err = decodeTopicNumber(work, &rc)
|
||||
err = decodeTopicNumber(work, rc)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &rc, nil
|
||||
return rc, nil
|
||||
} else if pos == 0 {
|
||||
return nil, errors.New("cannot have . at beginning of string")
|
||||
}
|
||||
|
||||
// We definitely have "conference.topic.something" or "community!conference.topic.something"
|
||||
err := validateConference(confOrTopic, &rc)
|
||||
err := validateConference(confOrTopic, rc)
|
||||
if err == nil {
|
||||
err = decodeTopicNumber(work[:pos], &rc)
|
||||
err = decodeTopicNumber(work[:pos], rc)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -386,22 +405,23 @@ func AmDecodePostLink(data string) (*PostLinkData, error) {
|
||||
work = work[pos+1:]
|
||||
if len(work) == 0 {
|
||||
// we had "conference.topic." or "communtiy!conference.topic.", those are both valid
|
||||
return &rc, nil
|
||||
return rc, nil
|
||||
}
|
||||
err = decodePostRange(work, &rc) // the rest must be the post range
|
||||
err = decodePostRange(work, rc) // the rest must be the post range
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &rc, nil
|
||||
return rc, nil
|
||||
}
|
||||
|
||||
// AmCreatePostLinkContext creates a new empty post link context.
|
||||
func AmCreatePostLinkContext(community string, commid int32, conference string, topic int16) *PostLinkData {
|
||||
return &PostLinkData{
|
||||
return new(PostLinkData{
|
||||
Community: community,
|
||||
CommId: commid,
|
||||
Conference: conference,
|
||||
Topic: topic,
|
||||
FirstPost: -1,
|
||||
LastPost: -1,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
+37
-13
@@ -25,6 +25,20 @@ import (
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// The service domain names.
|
||||
const (
|
||||
AM_DOMAIN_COMMUNITY = "community"
|
||||
)
|
||||
|
||||
// The service names.
|
||||
const (
|
||||
AM_SVC_PROFILE = "Profile"
|
||||
AM_SVC_ADMIN = "Admin"
|
||||
AM_SVC_SYSADMIN = "SysAdmin"
|
||||
AM_SVC_CONFERENCE = "Conference"
|
||||
AM_SVC_MEMBERS = "Members"
|
||||
)
|
||||
|
||||
// ServiceVTable is a series of functions called for services on specific events.
|
||||
type ServiceVTable interface {
|
||||
OnNewCommunity(context.Context, *sqlx.Tx, *Community) error
|
||||
@@ -114,13 +128,13 @@ func init() {
|
||||
serviceRoot.Domains[i].seqOrder = sqo
|
||||
serviceRoot.byName[dom.DomainName] = &(serviceRoot.Domains[i])
|
||||
}
|
||||
dom := serviceRoot.byName["community"]
|
||||
dom := serviceRoot.byName[AM_DOMAIN_COMMUNITY]
|
||||
empty := emptyServiceVTable{}
|
||||
dom.byId["Profile"].vtable = &empty
|
||||
dom.byId["Admin"].vtable = &empty
|
||||
dom.byId["SysAdmin"].vtable = &empty
|
||||
dom.byId["Conference"].vtable = &(conferenceServiceVTable{})
|
||||
dom.byId["Members"].vtable = &empty
|
||||
dom.byId[AM_SVC_PROFILE].vtable = &empty
|
||||
dom.byId[AM_SVC_ADMIN].vtable = &empty
|
||||
dom.byId[AM_SVC_SYSADMIN].vtable = &empty
|
||||
dom.byId[AM_SVC_CONFERENCE].vtable = &(conferenceServiceVTable{})
|
||||
dom.byId[AM_SVC_MEMBERS].vtable = &empty
|
||||
}
|
||||
|
||||
// setupServicesCache sets up the services cache.
|
||||
@@ -166,7 +180,8 @@ func AmGetCommunityServices(ctx context.Context, cid int32) ([]*ServiceDef, erro
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dom := serviceRoot.byName["community"]
|
||||
defer rs.Close()
|
||||
dom := serviceRoot.byName[AM_DOMAIN_COMMUNITY]
|
||||
a := make([]*ServiceDef, 0, len(dom.Services))
|
||||
for rs.Next() {
|
||||
var ndx int16
|
||||
@@ -174,8 +189,12 @@ func AmGetCommunityServices(ctx context.Context, cid int32) ([]*ServiceDef, erro
|
||||
a = append(a, dom.byIndex[ndx])
|
||||
}
|
||||
}
|
||||
servicesCache.Add(cid, a)
|
||||
rc = a
|
||||
if rs.Err() == nil {
|
||||
servicesCache.Add(cid, a)
|
||||
rc = a
|
||||
} else {
|
||||
return nil, rs.Err()
|
||||
}
|
||||
}
|
||||
return rc.([]*ServiceDef), nil
|
||||
}
|
||||
@@ -198,7 +217,8 @@ func AmGetCommunityServicesTx(ctx context.Context, tx *sqlx.Tx, cid int32) ([]*S
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dom := serviceRoot.byName["community"]
|
||||
defer rs.Close()
|
||||
dom := serviceRoot.byName[AM_DOMAIN_COMMUNITY]
|
||||
a := make([]*ServiceDef, 0, len(dom.Services))
|
||||
for rs.Next() {
|
||||
var ndx int16
|
||||
@@ -206,8 +226,12 @@ func AmGetCommunityServicesTx(ctx context.Context, tx *sqlx.Tx, cid int32) ([]*S
|
||||
a = append(a, dom.byIndex[ndx])
|
||||
}
|
||||
}
|
||||
servicesCache.Add(cid, a)
|
||||
rc = a
|
||||
if rs.Err() == nil {
|
||||
servicesCache.Add(cid, a)
|
||||
rc = a
|
||||
} else {
|
||||
return nil, rs.Err()
|
||||
}
|
||||
}
|
||||
return rc.([]*ServiceDef), nil
|
||||
}
|
||||
@@ -222,7 +246,7 @@ func AmGetCommunityServicesTx(ctx context.Context, tx *sqlx.Tx, cid int32) ([]*S
|
||||
* Standard Go error status.
|
||||
*/
|
||||
func AmEstablishCommunityServices(ctx context.Context, tx *sqlx.Tx, c *Community) error {
|
||||
dom := serviceRoot.byName["community"]
|
||||
dom := serviceRoot.byName[AM_DOMAIN_COMMUNITY]
|
||||
a := make([]*ServiceDef, 0, len(dom.Services))
|
||||
for i, svc := range dom.Services {
|
||||
if svc.Default {
|
||||
|
||||
+14
-6
@@ -44,10 +44,10 @@ type Topic struct {
|
||||
|
||||
// Link returns a link string to this topic.
|
||||
func (t *Topic) Link(ctx context.Context, commid int32, scope string) (string, error) {
|
||||
if scope == "conference" {
|
||||
if scope == PLSCOPE_CONFERENCE {
|
||||
return fmt.Sprintf("%d.", t.Number), nil
|
||||
}
|
||||
if scope == "community" || scope == "global" {
|
||||
if scope == PLSCOPE_COMMUNITY || scope == PLSCOPE_GLOBAL {
|
||||
conf, err := AmGetConference(ctx, t.ConfId)
|
||||
if err == nil {
|
||||
var plink string
|
||||
@@ -211,6 +211,7 @@ func (t *Topic) GetBozos(ctx context.Context, u *User) ([]*TopicBozo, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rs.Close()
|
||||
rc := make([]*TopicBozo, 0)
|
||||
for rs.Next() {
|
||||
tb := new(TopicBozo)
|
||||
@@ -220,7 +221,7 @@ func (t *Topic) GetBozos(ctx context.Context, u *User) ([]*TopicBozo, error) {
|
||||
}
|
||||
rc = append(rc, tb)
|
||||
}
|
||||
return rc, nil
|
||||
return rc, rs.Err()
|
||||
}
|
||||
|
||||
// IsSubscribed returns true if the given user is subscribed to receive E-mails of topic posts.
|
||||
@@ -258,6 +259,7 @@ func (t *Topic) GetSubscribers(ctx context.Context) ([]int32, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rs.Close()
|
||||
rc := make([]int32, 0)
|
||||
for rs.Next() {
|
||||
var tmp int32
|
||||
@@ -268,6 +270,9 @@ func (t *Topic) GetSubscribers(ctx context.Context) ([]int32, error) {
|
||||
break
|
||||
}
|
||||
}
|
||||
if err == nil && rs.Err() != nil {
|
||||
err = rs.Err()
|
||||
}
|
||||
return rc, err
|
||||
}
|
||||
|
||||
@@ -297,6 +302,7 @@ func (t *Topic) GetActivity(ctx context.Context, reportType int) ([]*ActivityRep
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rs.Close()
|
||||
rc := make([]*ActivityReport, 0)
|
||||
for rs.Next() {
|
||||
cur := new(ActivityReport)
|
||||
@@ -306,7 +312,7 @@ func (t *Topic) GetActivity(ctx context.Context, reportType int) ([]*ActivityRep
|
||||
}
|
||||
rc = append(rc, cur)
|
||||
}
|
||||
return rc, nil
|
||||
return rc, rs.Err()
|
||||
}
|
||||
|
||||
/* GetActiveUserEMailAddrs gets the E-mail addresses of each user that's active in the topic, omitting those that have opted out of mass E-mails.
|
||||
@@ -341,6 +347,7 @@ func (t *Topic) GetActiveUserEMailAddrs(ctx context.Context, userSelect, dayLimi
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rs.Close()
|
||||
var stopPoint *time.Time = nil
|
||||
if dayLimit > 0 {
|
||||
mynow := time.Now().UTC()
|
||||
@@ -360,7 +367,7 @@ func (t *Topic) GetActiveUserEMailAddrs(ctx context.Context, userSelect, dayLimi
|
||||
}
|
||||
rc = append(rc, addy)
|
||||
}
|
||||
return rc, nil
|
||||
return rc, rs.Err()
|
||||
}
|
||||
|
||||
// backgroundPurgeTopic removes all posts from a topic that's been deleted.
|
||||
@@ -698,6 +705,7 @@ func AmListTopics(ctx context.Context, confid int32, uid int32, viewOption int,
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
defer rs.Close()
|
||||
rc := make([]*TopicSummary, 0)
|
||||
for rs.Next() {
|
||||
rec := new(TopicSummary)
|
||||
@@ -708,7 +716,7 @@ func AmListTopics(ctx context.Context, confid int32, uid int32, viewOption int,
|
||||
rc = append(rc, rec)
|
||||
}
|
||||
}
|
||||
return rc, nil
|
||||
return rc, rs.Err()
|
||||
}
|
||||
|
||||
/* AmNewTopic creates a new topic.
|
||||
|
||||
+2
-1
@@ -961,6 +961,7 @@ func AmSearchUsers(ctx context.Context, field, oper int, term string, offset, ma
|
||||
if err != nil {
|
||||
return nil, total, err
|
||||
}
|
||||
defer rs.Close()
|
||||
rc := make([]*User, 0, min(maxCount, 10000))
|
||||
for rs.Next() {
|
||||
var uid int32
|
||||
@@ -975,5 +976,5 @@ func AmSearchUsers(ctx context.Context, field, oper int, term string, offset, ma
|
||||
log.Errorf("AmSearchUsers scan error: %v", err)
|
||||
}
|
||||
}
|
||||
return rc, total, nil
|
||||
return rc, total, rs.Err()
|
||||
}
|
||||
|
||||
@@ -1,5 +1,22 @@
|
||||
# Changelog
|
||||
|
||||
## Release 0.2.1 - November 14, 2026
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* Fixed an issue in selecting log levels.
|
||||
* Template functions now panic on error for quicker error handling.
|
||||
|
||||
### Enhancements
|
||||
|
||||
* Added database connection pooling.
|
||||
* Added timeouts and rate limiting on HTTP requests.
|
||||
* Updated Echo to version 5, and added a selectable IP resolver which can be configured based on Amsterdam setup.
|
||||
|
||||
### Meta-Enhancements
|
||||
|
||||
* Release build action updated.
|
||||
|
||||
## Release 0.2.0 - April 29, 2026
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
@@ -234,6 +234,9 @@ func SetupMailSender() func() {
|
||||
emailRenderer.AddGlobal("AmsterdamVersion", config.AMSTERDAM_VERSION)
|
||||
emailRenderer.AddGlobal("AmsterdamCopyright", config.AMSTERDAM_COPYRIGHT)
|
||||
emailRenderer.AddGlobal("GlobalConfig", config.GlobalConfig)
|
||||
emailRenderer.AddGlobal("PLSCOPE_COMMUNITY", database.PLSCOPE_COMMUNITY)
|
||||
emailRenderer.AddGlobal("PLSCOPE_CONFERENCE", database.PLSCOPE_CONFERENCE)
|
||||
emailRenderer.AddGlobal("PLSCOPE_TOPIC", database.PLSCOPE_TOPIC)
|
||||
|
||||
// Start the recycler.
|
||||
messageRecycleBin = make(chan *amMessage, config.GlobalConfig.Tuning.Queues.EmailRecycle)
|
||||
|
||||
@@ -18,12 +18,12 @@ a Amsterdam account. Once you have completed the process, click the "Join Now"
|
||||
button. You will be prompted for the "password" for this community, which is
|
||||
"{{ comm.JoinKey }}". You will then be able to take part in the conferences that are
|
||||
going on in the community.
|
||||
{{ if mode == "conference" }}
|
||||
{{ if mode == INVMODE_CONFERENCE }}
|
||||
After you've joined the "{{ comm.Name }}" community, check out the
|
||||
"{{ conf.Name }}" conference. To find it, after joining the community,
|
||||
click "Conferences" on the left menu bar, then click on the
|
||||
"{{ conf.Name }}" conference name in the conference list.
|
||||
{{ else if mode == "topic" }}
|
||||
{{ else if mode == INVMODE_TOPIC }}
|
||||
After you've joined the "{{ comm.Name }}" community, check out the
|
||||
"{{ topic.Name | raw }}" topic in the "{{ conf.Name }}" conference. To find it,
|
||||
after joining the community, click "Conferences" on the left menu bar, then
|
||||
|
||||
@@ -17,12 +17,12 @@ link at the top of the page, or click the "Log In" link if you already have
|
||||
a Amsterdam account. Once you have completed the process, click the "Join Now"
|
||||
button. You will then be able to take part in the conferences that are
|
||||
going on in the community.
|
||||
{{ if mode == "conference" }}
|
||||
{{ if mode == INVMODE_CONFERENCE }}
|
||||
After you've joined the "{{ comm.Name }}" community, check out the
|
||||
"{{ conf.Name }}" conference. To find it, after joining the community,
|
||||
click "Conferences" on the left menu bar, then click on the
|
||||
"{{ conf.Name }}" conference name in the conference list.
|
||||
{{ else if mode == "topic" }}
|
||||
{{ else if mode == INVMODE_TOPIC }}
|
||||
After you've joined the "{{ comm.Name }}" community, check out the
|
||||
"{{ topic.Name | raw }}" topic in the "{{ conf.Name }}" conference. To find it,
|
||||
after joining the community, click "Conferences" on the left menu bar, then
|
||||
|
||||
@@ -16,13 +16,13 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
"uuid"
|
||||
|
||||
"git.erbosoft.com/amy/amsterdam/config"
|
||||
"git.erbosoft.com/amy/amsterdam/ui"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
"github.com/labstack/echo/v5"
|
||||
"github.com/labstack/echo/v5/middleware"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
// EBUTTON is the standard error for an unknown button.
|
||||
@@ -74,9 +74,9 @@ func AmNotFoundHandler(ctxt ui.AmContext) (string, any) {
|
||||
* err - The error to be handled.
|
||||
* c - The Echo context error is being handled on.
|
||||
*/
|
||||
func AmErrorHandler(err error, c echo.Context) {
|
||||
func AmErrorHandler(c *echo.Context, err error) {
|
||||
log.Infof("-> AmErrorHandler on path %s", c.Request().URL.Path)
|
||||
if c.Response().Committed {
|
||||
if c.Response().(*echo.Response).Committed {
|
||||
return
|
||||
}
|
||||
cerr := ui.AmWithTempContext(c, func(ctxt ui.AmContext) (string, any) {
|
||||
@@ -88,16 +88,17 @@ func AmErrorHandler(err error, c echo.Context) {
|
||||
}
|
||||
|
||||
// rateLimitErrorHandler is called if there's an error getting the identifier for a connection (unlikely).
|
||||
func rateLimitErrorHandler(c echo.Context, err error) error {
|
||||
func rateLimitErrorHandler(c *echo.Context, err error) error {
|
||||
return ui.AmWithTempContext(c, func(ctxt ui.AmContext) (string, any) {
|
||||
return "error", err
|
||||
})
|
||||
}
|
||||
|
||||
// rateLimitDenyHandler is called if the rate limit is exceeded by a connection.
|
||||
func rateLimitDenyHandler(c echo.Context, identifier string, err error) error {
|
||||
func rateLimitDenyHandler(c *echo.Context, identifier string, err error) error {
|
||||
log.Errorf("request %s tripped rate limiter", c.Request().Context().Value(CONTEXTVAL_REQUESTID).(uuid.UUID).String())
|
||||
return ui.AmWithTempContext(c, func(ctxt ui.AmContext) (string, any) {
|
||||
ctxt.VarMap().Set("identifier", identifier)
|
||||
ctxt.VarMap().Set("identifier", fmt.Sprintf("%s/%s", identifier, ctxt.RemoteIP()))
|
||||
return "ratelimit", err
|
||||
})
|
||||
}
|
||||
@@ -105,7 +106,7 @@ func rateLimitDenyHandler(c echo.Context, identifier string, err error) error {
|
||||
// AmSetupRateLimiter sets up the rate-limiting middleware.
|
||||
func AmSetupRateLimiter() echo.MiddlewareFunc {
|
||||
rcfg := middleware.RateLimiterMemoryStoreConfig{
|
||||
Rate: rate.Limit(config.GlobalConfig.Site.RateLimit.Rate),
|
||||
Rate: config.GlobalConfig.Site.RateLimit.Rate,
|
||||
Burst: config.GlobalConfig.Site.RateLimit.Burst,
|
||||
ExpiresIn: time.Duration(config.GlobalConfig.Site.RateLimit.ExpireMinutes) * time.Minute,
|
||||
}
|
||||
|
||||
@@ -330,7 +330,7 @@ func commonFindGetBackend(ctxt ui.AmContext) (string, any) {
|
||||
*/
|
||||
func FindPostsPageCommunity(ctxt ui.AmContext) (string, any) {
|
||||
comm := ctxt.CurrentCommunity()
|
||||
ctxt.VarMap().Set("scope", "community")
|
||||
ctxt.VarMap().Set("scope", database.PLSCOPE_COMMUNITY)
|
||||
ctxt.VarMap().Set("entityName", comm.Name)
|
||||
ctxt.VarMap().Set("backlink", fmt.Sprintf("/comm/%s/conf", comm.Alias))
|
||||
ctxt.VarMap().Set("postlink", fmt.Sprintf("/comm/%s/find", comm.Alias))
|
||||
@@ -346,7 +346,7 @@ func FindPostsPageCommunity(ctxt ui.AmContext) (string, any) {
|
||||
*/
|
||||
func FindPostsPageConference(ctxt ui.AmContext) (string, any) {
|
||||
conf := ctxt.GetScratch("currentConference").(*database.Conference)
|
||||
ctxt.VarMap().Set("scope", "conference")
|
||||
ctxt.VarMap().Set("scope", database.PLSCOPE_CONFERENCE)
|
||||
ctxt.VarMap().Set("entityName", conf.Name)
|
||||
ctxt.VarMap().Set("backlink", ctxt.GetScratch("ConferenceLink").(string))
|
||||
ctxt.VarMap().Set("postlink", fmt.Sprintf("%s/find", ctxt.GetScratch("ConferenceLink")))
|
||||
@@ -362,7 +362,7 @@ func FindPostsPageConference(ctxt ui.AmContext) (string, any) {
|
||||
*/
|
||||
func FindPostsPageTopic(ctxt ui.AmContext) (string, any) {
|
||||
topic := ctxt.GetScratch("currentTopic").(*database.Topic)
|
||||
ctxt.VarMap().Set("scope", "topic")
|
||||
ctxt.VarMap().Set("scope", database.PLSCOPE_TOPIC)
|
||||
ctxt.VarMap().Set("entityName", topic.Name)
|
||||
ctxt.VarMap().Set("backlink", fmt.Sprintf("%s/r/%d", ctxt.GetScratch("ConferenceLink"), topic.Number))
|
||||
ctxt.VarMap().Set("postlink", fmt.Sprintf("%s/op/%d/find", ctxt.GetScratch("ConferenceLink"), topic.Number))
|
||||
@@ -417,7 +417,7 @@ func commonFindPostBackend(ctxt ui.AmContext, comm *database.Community, conf *da
|
||||
*/
|
||||
func FindPostsCommunity(ctxt ui.AmContext) (string, any) {
|
||||
comm := ctxt.CurrentCommunity()
|
||||
ctxt.VarMap().Set("scope", "community")
|
||||
ctxt.VarMap().Set("scope", database.PLSCOPE_COMMUNITY)
|
||||
ctxt.VarMap().Set("entityName", comm.Name)
|
||||
ctxt.VarMap().Set("backlink", fmt.Sprintf("/comm/%s/conf", comm.Alias))
|
||||
ctxt.VarMap().Set("postlink", fmt.Sprintf("/comm/%s/find", comm.Alias))
|
||||
@@ -434,7 +434,7 @@ func FindPostsCommunity(ctxt ui.AmContext) (string, any) {
|
||||
func FindPostsConference(ctxt ui.AmContext) (string, any) {
|
||||
comm := ctxt.CurrentCommunity()
|
||||
conf := ctxt.GetScratch("currentConference").(*database.Conference)
|
||||
ctxt.VarMap().Set("scope", "conference")
|
||||
ctxt.VarMap().Set("scope", database.PLSCOPE_CONFERENCE)
|
||||
ctxt.VarMap().Set("entityName", conf.Name)
|
||||
ctxt.VarMap().Set("backlink", ctxt.GetScratch("ConferenceLink").(string))
|
||||
ctxt.VarMap().Set("postlink", fmt.Sprintf("%s/find", ctxt.GetScratch("ConferenceLink")))
|
||||
@@ -452,7 +452,7 @@ func FindPostsTopic(ctxt ui.AmContext) (string, any) {
|
||||
comm := ctxt.CurrentCommunity()
|
||||
conf := ctxt.GetScratch("currentConference").(*database.Conference)
|
||||
topic := ctxt.GetScratch("currentTopic").(*database.Topic)
|
||||
ctxt.VarMap().Set("scope", "topic")
|
||||
ctxt.VarMap().Set("scope", database.PLSCOPE_TOPIC)
|
||||
ctxt.VarMap().Set("entityName", topic.Name)
|
||||
ctxt.VarMap().Set("backlink", fmt.Sprintf("%s/r/%d", ctxt.GetScratch("ConferenceLink"), topic.Number))
|
||||
ctxt.VarMap().Set("postlink", fmt.Sprintf("%s/op/%d/find", ctxt.GetScratch("ConferenceLink"), topic.Number))
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
module git.erbosoft.com/amy/amsterdam
|
||||
|
||||
go 1.26
|
||||
go 1.27
|
||||
|
||||
require (
|
||||
github.com/CloudyKit/jet/v6 v6.3.2
|
||||
@@ -14,13 +14,12 @@ require (
|
||||
github.com/hashicorp/golang-lru v1.0.2
|
||||
github.com/jmoiron/sqlx v1.4.0
|
||||
github.com/klauspost/lctime v0.1.0
|
||||
github.com/labstack/echo/v4 v4.15.1
|
||||
github.com/labstack/gommon v0.4.2
|
||||
github.com/labstack/echo/v5 v5.4.4
|
||||
github.com/labstack/gommon v0.5.0
|
||||
github.com/sirupsen/logrus v1.9.4
|
||||
github.com/tkuchiki/go-timezone v0.2.3
|
||||
golang.org/x/net v0.52.0
|
||||
golang.org/x/text v0.35.0
|
||||
golang.org/x/time v0.15.0
|
||||
golang.org/x/net v0.62.0
|
||||
golang.org/x/text v0.43.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
@@ -29,10 +28,10 @@ require (
|
||||
github.com/CloudyKit/fastprinter v0.0.0-20251202014920-1725d2651bd4 // indirect
|
||||
github.com/alexflint/go-scalar v1.2.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/mattn/go-isatty v0.0.22 // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
github.com/valyala/fasttemplate v1.2.2 // indirect
|
||||
golang.org/x/crypto v0.49.0 // indirect
|
||||
golang.org/x/image v0.37.0 // indirect
|
||||
golang.org/x/sys v0.42.0 // indirect
|
||||
golang.org/x/image v0.45.0 // indirect
|
||||
golang.org/x/sys v0.49.0 // indirect
|
||||
golang.org/x/time v0.15.0 // indirect
|
||||
)
|
||||
|
||||
@@ -31,16 +31,16 @@ github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o=
|
||||
github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY=
|
||||
github.com/klauspost/lctime v0.1.0 h1:nINsuFc860M9cyYhT6vfg6U1USh7kiVBj/s/2b04U70=
|
||||
github.com/klauspost/lctime v0.1.0/go.mod h1:OwdMhr8tbQvusAsnilqkkgDQqivWlqyg0w5cfXkLiDk=
|
||||
github.com/labstack/echo/v4 v4.15.1 h1:S9keusg26gZpjMmPqB5hOEvNKnmd1lNmcHrbbH2lnFs=
|
||||
github.com/labstack/echo/v4 v4.15.1/go.mod h1:xmw1clThob0BSVRX1CRQkGQ/vjwcpOMjQZSZa9fKA/c=
|
||||
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
|
||||
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
|
||||
github.com/labstack/echo/v5 v5.4.4 h1:Nag9Mee0M9sOcKjProYuDNiw60QisysgfbIgz5US5Cw=
|
||||
github.com/labstack/echo/v5 v5.4.4/go.mod h1:4iEGNQiPPZnkfYpNR/L6fINd3NLiGWUD5+eBotFALas=
|
||||
github.com/labstack/gommon v0.5.0 h1:6VSQ2NOzsnEJ5W6+84E0RbcaDDmgB6NIAzWCczTEe6c=
|
||||
github.com/labstack/gommon v0.5.0/go.mod h1:Rzlg7HHy1maLfzBYGg9NZcVuz1sA68HHhLjhcEllYE0=
|
||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
|
||||
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-isatty v0.0.22 h1:j8l17JJ9i6VGPUFUYoTUKPSgKe/83EYU2zBC7YNKMw4=
|
||||
github.com/mattn/go-isatty v0.0.22/go.mod h1:ZXfXG4SQHsB/w3ZeOYbR0PrPwLy+n6xiMrJlRFqopa4=
|
||||
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
|
||||
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
@@ -56,19 +56,16 @@ github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6Kllzaw
|
||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
|
||||
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
|
||||
golang.org/x/crypto v0.49.0 h1:+Ng2ULVvLHnJ/ZFEq4KdcDd/cfjrrjjNSXNzxg0Y4U4=
|
||||
golang.org/x/crypto v0.49.0/go.mod h1:ErX4dUh2UM+CFYiXZRTcMpEcN8b/1gxEuv3nODoYtCA=
|
||||
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||
golang.org/x/image v0.37.0 h1:ZiRjArKI8GwxZOoEtUfhrBtaCN+4b/7709dlT6SSnQA=
|
||||
golang.org/x/image v0.37.0/go.mod h1:/3f6vaXC+6CEanU4KJxbcUZyEePbyKbaLoDOe4ehFYY=
|
||||
golang.org/x/net v0.52.0 h1:He/TN1l0e4mmR3QqHMT2Xab3Aj3L9qjbhRm78/6jrW0=
|
||||
golang.org/x/net v0.52.0/go.mod h1:R1MAz7uMZxVMualyPXb+VaqGSa3LIaUqk0eEt3w36Sw=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo=
|
||||
golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||
golang.org/x/image v0.45.0 h1:FMb1nTbH5H9vF55SriQHgFw5GnNL9Jg6L25BwXKzhB0=
|
||||
golang.org/x/image v0.45.0/go.mod h1:n62x/7RqlwXDvGsSU4u6IUTUf6KghUZ9Bt7cG/T9Fx4=
|
||||
golang.org/x/net v0.62.0 h1:18ELalfM5+XNRwzM2nYqXsZQ9ByS1uhj2O/U7+5jqRY=
|
||||
golang.org/x/net v0.62.0/go.mod h1:WgDCOTH8iwtB66T4/GfI94AHo/hhdb7Hah9NDseQfiI=
|
||||
golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA=
|
||||
golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.35.0 h1:JOVx6vVDFokkpaq1AEptVzLTpDe9KGpj5tR4/X+ybL8=
|
||||
golang.org/x/text v0.35.0/go.mod h1:khi/HExzZJ2pGnjenulevKNX1W67CUy0AsXcNubPGCA=
|
||||
golang.org/x/text v0.43.0 h1:1QivrlhwAsnMOcqgOdl9my7s9OyZl8O5/UGUTJVXoWc=
|
||||
golang.org/x/text v0.43.0/go.mod h1:hbSIYA/amXcRXTFSZqh8tWHEoqRvvbKPJ2uz/KU+a+s=
|
||||
golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=
|
||||
golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
|
||||
+6
-10
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Amsterdam Web Communities System
|
||||
* Copyright (c) 2025 Erbosoft Metaverse Design Solutions, All Rights Reserved
|
||||
* Copyright (c) 2025-2026 Erbosoft Metaverse Design Solutions, All Rights Reserved
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
@@ -44,17 +44,17 @@ func (d *TrieDictionary) Size() int {
|
||||
// CheckWord returns true if a word is in the dictionary, false if not.
|
||||
func (d *TrieDictionary) CheckWord(word string) bool {
|
||||
d.mutex.Lock()
|
||||
defer d.mutex.Unlock()
|
||||
_, rc := d.trie.Find(strings.ToLower(word))
|
||||
d.mutex.Unlock()
|
||||
return rc
|
||||
}
|
||||
|
||||
// AddWord adds a new word to the dictionary.
|
||||
func (d *TrieDictionary) AddWord(word string) {
|
||||
d.mutex.Lock()
|
||||
defer d.mutex.Unlock()
|
||||
d.trie.Add(strings.ToLower(word), true)
|
||||
d.count++
|
||||
d.mutex.Unlock()
|
||||
}
|
||||
|
||||
// DelWord deletes a word from the dictionary.
|
||||
@@ -89,12 +89,8 @@ func loadDict(d *TrieDictionary, words []byte) {
|
||||
|
||||
// LoadTrieDict creates a TrieDictionary from a byte array that represents a word list (one word per line).
|
||||
func LoadTrieDict(words []byte) *TrieDictionary {
|
||||
rc := TrieDictionary{
|
||||
loaded: atomic.Bool{},
|
||||
trie: trie.New(),
|
||||
count: 0,
|
||||
}
|
||||
rc := new(TrieDictionary{loaded: atomic.Bool{}, trie: trie.New(), count: 0})
|
||||
rc.loaded.Store(false)
|
||||
go loadDict(&rc, words)
|
||||
return &rc
|
||||
go loadDict(rc, words)
|
||||
return rc
|
||||
}
|
||||
|
||||
@@ -55,10 +55,8 @@ func SetupDicts() {
|
||||
log.Errorf("failed to load external dictionary %s: %v", config.GlobalConfig.Posting.ExternalDictionary, err)
|
||||
}
|
||||
}
|
||||
rw := spellingRewriter{
|
||||
dict: NewCompositeDict(dicts),
|
||||
}
|
||||
rewriterRegistry[rw.Name()] = &rw
|
||||
rw := new(spellingRewriter{dict: NewCompositeDict(dicts)})
|
||||
rewriterRegistry[rw.Name()] = rw
|
||||
}
|
||||
|
||||
// spellingRewriter is a rewriter that flags spelling errors.
|
||||
@@ -89,10 +87,10 @@ func (rw *spellingRewriter) Rewrite(ctx context.Context, data string, svc rewrit
|
||||
if rw.dict.CheckWord(data) {
|
||||
return nil
|
||||
}
|
||||
return &markupData{
|
||||
return new(markupData{
|
||||
beginMarkup: defaultBeginError,
|
||||
text: data,
|
||||
endMarkup: defaultEndError,
|
||||
rescan: false,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
+13
-3
@@ -21,6 +21,13 @@ import (
|
||||
"git.erbosoft.com/amy/amsterdam/ui"
|
||||
)
|
||||
|
||||
// Invitation modes.
|
||||
const (
|
||||
INVMODE_COMMUNITY = "community"
|
||||
INVMODE_CONFERENCE = "conference"
|
||||
INVMODE_TOPIC = "topic"
|
||||
)
|
||||
|
||||
/* InviteToCommunity displays the community invitation form.
|
||||
* Parameters:
|
||||
* ctxt - The AmContext for the request.
|
||||
@@ -116,7 +123,7 @@ func InviteSend(ctxt ui.AmContext) (string, any) {
|
||||
} else {
|
||||
return "error", EPARAM
|
||||
}
|
||||
mode := "community"
|
||||
mode := INVMODE_COMMUNITY
|
||||
var conf *database.Conference = nil
|
||||
var topic *database.Topic = nil
|
||||
if ctxt.FormFieldIsSet("confid") {
|
||||
@@ -145,9 +152,9 @@ func InviteSend(ctxt ui.AmContext) (string, any) {
|
||||
if err != nil {
|
||||
return "errors", err
|
||||
}
|
||||
mode = "topic"
|
||||
mode = INVMODE_TOPIC
|
||||
} else {
|
||||
mode = "conference"
|
||||
mode = INVMODE_CONFERENCE
|
||||
}
|
||||
}
|
||||
addr := ctxt.FormField("addr")
|
||||
@@ -168,6 +175,9 @@ func InviteSend(ctxt ui.AmContext) (string, any) {
|
||||
mailMessage.SetTemplate("invite_private.jet")
|
||||
}
|
||||
mailMessage.AddTo(addr, "")
|
||||
mailMessage.AddVariable("INVMODE_COMMUNITY", INVMODE_COMMUNITY)
|
||||
mailMessage.AddVariable("INVMODE_CONFERENCE", INVMODE_CONFERENCE)
|
||||
mailMessage.AddVariable("INVMODE_TOPIC", INVMODE_TOPIC)
|
||||
mailMessage.AddVariable("comm", comm)
|
||||
mailMessage.AddVariable("conf", conf)
|
||||
mailMessage.AddVariable("topic", topic)
|
||||
|
||||
+110
-100
@@ -13,143 +13,148 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"maps"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
"uuid"
|
||||
|
||||
"git.erbosoft.com/amy/amsterdam/config"
|
||||
"github.com/dustin/go-humanize"
|
||||
"github.com/labstack/echo/v4"
|
||||
glog "github.com/labstack/gommon/log"
|
||||
"github.com/labstack/echo/v5"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// DEFAULT_MAXLOG is the default maximum log file size (16 megabytes).
|
||||
const DEFAULT_MAXLOG = 16 * 1024 * 1024
|
||||
|
||||
// LOG_ROTATE_INTERVAL is the interval, in seconds, at which we try to rotate the logfile.
|
||||
const LOG_ROTATE_INTERVAL = 10
|
||||
|
||||
// ContextValKey is the type Amsterdam uses for context keys.
|
||||
type ContextValKey struct{}
|
||||
|
||||
// CONTEXTVAL_NAME is the context key used to store the context name.
|
||||
var CONTEXTVAL_NAME = ContextValKey{}
|
||||
|
||||
// CONTEXTVAL_REQUESTID is the context key used to store the request id
|
||||
var CONTEXTVAL_REQUESTID = ContextValKey{}
|
||||
|
||||
/*----------------------------------------------------------------------------
|
||||
* Gommon-log to logrus adapter
|
||||
* slog handler that outputs to Logrus
|
||||
*----------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/* toglog converts a Logrus logging level to a glog one.
|
||||
* Parameters:
|
||||
* l - The Logrus log level to be converted.
|
||||
* Returns:
|
||||
* The equivalent glog log level.
|
||||
*/
|
||||
func toglog(l log.Level) glog.Lvl {
|
||||
switch l {
|
||||
case log.DebugLevel:
|
||||
return glog.DEBUG
|
||||
case log.InfoLevel:
|
||||
return glog.INFO
|
||||
case log.WarnLevel:
|
||||
return glog.WARN
|
||||
case log.ErrorLevel:
|
||||
return glog.ERROR
|
||||
default:
|
||||
return glog.OFF
|
||||
}
|
||||
// slog2logrus converts slog levels to Logrus levels.
|
||||
var slog2logrus = map[slog.Level]log.Level{
|
||||
slog.LevelDebug: log.DebugLevel,
|
||||
slog.LevelInfo: log.InfoLevel,
|
||||
slog.LevelWarn: log.WarnLevel,
|
||||
slog.LevelError: log.ErrorLevel,
|
||||
}
|
||||
|
||||
/* fromglog converts a glog logging level to a Logrus one.
|
||||
* Parameters:
|
||||
* l - The glog log level to be converted.
|
||||
* Returns:
|
||||
* The equivalent Logrus log level.
|
||||
*/
|
||||
func fromglog(l glog.Lvl) log.Level {
|
||||
switch l {
|
||||
case glog.DEBUG:
|
||||
return log.DebugLevel
|
||||
case glog.INFO:
|
||||
return log.InfoLevel
|
||||
case glog.WARN:
|
||||
return log.WarnLevel
|
||||
case glog.ERROR:
|
||||
return log.ErrorLevel
|
||||
default:
|
||||
return log.PanicLevel
|
||||
}
|
||||
// SlogLogrusHandler implements slog.Handler and routes to Logrus.
|
||||
type SlogLogrusHandler struct {
|
||||
fields log.Fields // fields defined in this handler
|
||||
groupPrefix string // group prefix
|
||||
}
|
||||
|
||||
// EchoLogrusAdapter implements echo.Logger using logrus.
|
||||
type EchoLogrusAdapter struct{}
|
||||
// NewSlogLogrusHandler creates a SlogLogrusHandler with base information.
|
||||
func NewSlogLogrusHandler() *SlogLogrusHandler {
|
||||
return new(SlogLogrusHandler{fields: make(log.Fields), groupPrefix: ""})
|
||||
}
|
||||
|
||||
func (l *EchoLogrusAdapter) Output() io.Writer { return log.StandardLogger().Out }
|
||||
func (l *EchoLogrusAdapter) SetOutput(w io.Writer) { log.SetOutput(w) }
|
||||
func (l *EchoLogrusAdapter) Prefix() string { return "" }
|
||||
func (l *EchoLogrusAdapter) SetPrefix(p string) {}
|
||||
func (l *EchoLogrusAdapter) Level() glog.Lvl { return toglog(log.GetLevel()) }
|
||||
func (l *EchoLogrusAdapter) SetLevel(lvl glog.Lvl) { log.SetLevel(fromglog(lvl)) }
|
||||
func (l *EchoLogrusAdapter) Print(i ...any) { log.Print(i...) }
|
||||
func (l *EchoLogrusAdapter) Printf(format string, args ...any) { log.Printf(format, args...) }
|
||||
func (l *EchoLogrusAdapter) Printj(j glog.JSON) { log.WithFields(log.Fields(j)).Print() }
|
||||
func (l *EchoLogrusAdapter) Debug(i ...any) { log.Debug(i...) }
|
||||
func (l *EchoLogrusAdapter) Debugf(format string, args ...any) { log.Debugf(format, args...) }
|
||||
func (l *EchoLogrusAdapter) Debugj(j glog.JSON) { log.WithFields(log.Fields(j)).Debug() }
|
||||
func (l *EchoLogrusAdapter) Info(i ...any) { log.Info(i...) }
|
||||
func (l *EchoLogrusAdapter) Infof(format string, args ...any) { log.Infof(format, args...) }
|
||||
func (l *EchoLogrusAdapter) Infoj(j glog.JSON) { log.WithFields(log.Fields(j)).Info() }
|
||||
func (l *EchoLogrusAdapter) Warn(i ...any) { log.Warn(i...) }
|
||||
func (l *EchoLogrusAdapter) Warnf(format string, args ...any) { log.Warnf(format, args...) }
|
||||
func (l *EchoLogrusAdapter) Warnj(j glog.JSON) { log.WithFields(log.Fields(j)).Warn() }
|
||||
func (l *EchoLogrusAdapter) Error(i ...any) { log.Error(i...) }
|
||||
func (l *EchoLogrusAdapter) Errorf(format string, args ...any) { log.Errorf(format, args...) }
|
||||
func (l *EchoLogrusAdapter) Errorj(j glog.JSON) { log.WithFields(log.Fields(j)).Error() }
|
||||
func (l *EchoLogrusAdapter) Fatal(i ...any) { log.Fatal(i...) }
|
||||
func (l *EchoLogrusAdapter) Fatalf(format string, args ...any) { log.Fatalf(format, args...) }
|
||||
func (l *EchoLogrusAdapter) Fatalj(j glog.JSON) { log.WithFields(log.Fields(j)).Fatal() }
|
||||
func (l *EchoLogrusAdapter) Panic(i ...any) { log.Panic(i...) }
|
||||
func (l *EchoLogrusAdapter) Panicf(format string, args ...any) { log.Panicf(format, args...) }
|
||||
func (l *EchoLogrusAdapter) Panicj(j glog.JSON) { log.WithFields(log.Fields(j)).Panic() }
|
||||
func (l *EchoLogrusAdapter) SetHeader(h string) {}
|
||||
// Enabled returns true if the specified log level is handled.
|
||||
func (h *SlogLogrusHandler) Enabled(ctx context.Context, lvl slog.Level) bool {
|
||||
return log.IsLevelEnabled(slog2logrus[lvl])
|
||||
}
|
||||
|
||||
// Handle sends a slog.Record to the log output.
|
||||
func (h *SlogLogrusHandler) Handle(ctx context.Context, r slog.Record) error {
|
||||
flds := make(log.Fields)
|
||||
for k, v := range h.fields {
|
||||
flds[h.groupPrefix+k] = v
|
||||
}
|
||||
r.Attrs(func(a slog.Attr) bool {
|
||||
flds[h.groupPrefix+a.Key] = a.Value.Any()
|
||||
return true
|
||||
})
|
||||
ntry := log.NewEntry(log.StandardLogger()).WithTime(r.Time).WithFields(flds)
|
||||
ntry.Log(slog2logrus[r.Level], r.Message)
|
||||
return nil
|
||||
}
|
||||
|
||||
// WithAttrs creates a new Handler from this one, with extra attributes.
|
||||
func (h *SlogLogrusHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
||||
newh := new(SlogLogrusHandler{fields: make(log.Fields), groupPrefix: h.groupPrefix})
|
||||
maps.Copy(newh.fields, h.fields)
|
||||
for _, a := range attrs {
|
||||
newh.fields[a.Key] = a.Value.Any()
|
||||
}
|
||||
return newh
|
||||
}
|
||||
|
||||
// WithGroup creates a new Handler from this one, with an extra group prefix.
|
||||
func (h *SlogLogrusHandler) WithGroup(name string) slog.Handler {
|
||||
newh := new(SlogLogrusHandler{fields: make(log.Fields), groupPrefix: h.groupPrefix + name + "."})
|
||||
maps.Copy(newh.fields, h.fields)
|
||||
return newh
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------
|
||||
* Echo middleware adapters
|
||||
*----------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
// LogrusMiddleware installs Logrus logging into the Echo middleware chain.
|
||||
func LogrusMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
// LoggingMiddleware installs Logrus logging into the Echo middleware chain.
|
||||
func LoggingMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c *echo.Context) error {
|
||||
req := c.Request()
|
||||
orgReq := req
|
||||
|
||||
// Set up the request ID and the context.
|
||||
requestId := uuid.New()
|
||||
ctx := context.WithValue(req.Context(), CONTEXTVAL_REQUESTID, requestId)
|
||||
ctx = context.WithValue(ctx, CONTEXTVAL_NAME, fmt.Sprintf("rq:%s", requestId.String()))
|
||||
req = req.WithContext(ctx)
|
||||
c.SetRequest(req)
|
||||
defer c.SetRequest(orgReq)
|
||||
|
||||
// Log the start-request line.
|
||||
log.WithFields(log.Fields{
|
||||
"id": requestId.String(),
|
||||
"remote_ip": c.RealIP(),
|
||||
"host": req.Host,
|
||||
"method": req.Method,
|
||||
"uri": req.RequestURI,
|
||||
}).Info("start request")
|
||||
|
||||
// Execute the actual request.
|
||||
start := time.Now()
|
||||
err := next(c)
|
||||
stop := time.Now()
|
||||
|
||||
req := c.Request()
|
||||
res := c.Response()
|
||||
// Set the response and the request-ID header.
|
||||
res := c.Response().(*echo.Response)
|
||||
res.Header().Set(echo.HeaderXRequestID, requestId.String())
|
||||
|
||||
// Log the end-request line.
|
||||
log.WithFields(log.Fields{
|
||||
"remote_ip": c.RealIP(),
|
||||
"host": req.Host,
|
||||
"method": req.Method,
|
||||
"uri": req.RequestURI,
|
||||
"id": requestId.String(),
|
||||
"status": res.Status,
|
||||
"latency_ms": stop.Sub(start).Milliseconds(),
|
||||
}).Info("handled request")
|
||||
}).Info("end request")
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// LogrusPanicLogging is a log function hooked into the recovery middleware.
|
||||
func LogrusPanicLogging(c echo.Context, err error, stack []byte) error {
|
||||
log.Errorf("[PANIC RECOVERY] %v", err)
|
||||
scanner := bufio.NewScanner(bytes.NewReader(stack))
|
||||
for scanner.Scan() {
|
||||
line := strings.ReplaceAll(scanner.Text(), "\t", " ")
|
||||
log.Error(line)
|
||||
}
|
||||
return scanner.Err()
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------
|
||||
* Log output file implementation
|
||||
*----------------------------------------------------------------------------
|
||||
@@ -185,6 +190,7 @@ func (lf *amLogFile) Close() error {
|
||||
}
|
||||
|
||||
// rotate closes the log file and moves it to a new name, shuffling the previously stored log files by the same amount.
|
||||
// N.B.: We must be holding lf.mutex.
|
||||
func (lf *amLogFile) rotate() error {
|
||||
if lf.keep == 0 && lf.keepCompressed == 0 {
|
||||
return nil // degenerate case, keep the log file the same
|
||||
@@ -287,7 +293,9 @@ func (lf *amLogFile) tryRotate() {
|
||||
if lf.curSize >= lf.maxSize {
|
||||
err := lf.rotate()
|
||||
if err != nil {
|
||||
//log.Error("log rotation failed")
|
||||
log.SetOutput(os.Stderr)
|
||||
log.Errorf("log rotation failed: %v", err)
|
||||
log.SetOutput(lf)
|
||||
}
|
||||
}
|
||||
lf.mutex.Unlock()
|
||||
@@ -327,8 +335,7 @@ func (lf *amLogFile) open(path string) error {
|
||||
|
||||
// logScanner is a goroutine that monitors the log file to see when it needs rotating.
|
||||
func logScanner(ctx context.Context, lf *amLogFile, done chan bool) {
|
||||
d, _ := time.ParseDuration("10s")
|
||||
t := time.NewTicker(d)
|
||||
t := time.NewTicker(LOG_ROTATE_INTERVAL * time.Second)
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
@@ -345,6 +352,7 @@ func logScanner(ctx context.Context, lf *amLogFile, done chan bool) {
|
||||
func SetupLogging() func() {
|
||||
loglevel, err := log.ParseLevel(config.GlobalComputedConfig.LogLevel)
|
||||
if err != nil {
|
||||
log.Errorf("default log level not valid: %s (%v)", config.GlobalComputedConfig.LogLevel, err)
|
||||
loglevel = log.ErrorLevel
|
||||
}
|
||||
if config.GlobalComputedConfig.DebugMode && loglevel != log.TraceLevel {
|
||||
@@ -358,7 +366,8 @@ func SetupLogging() func() {
|
||||
amlog := new(amLogFile)
|
||||
maxlog, err := humanize.ParseBytes(config.GlobalConfig.Logging.MaxLogSize)
|
||||
if err != nil {
|
||||
maxlog = 16 * 1024 * 1024 // default to 16 megabytes
|
||||
log.Errorf("invalid value for max log size: %s (%v)", config.GlobalConfig.Logging.MaxLogSize, err)
|
||||
maxlog = DEFAULT_MAXLOG
|
||||
}
|
||||
amlog.maxSize = int64(maxlog)
|
||||
amlog.keep = config.GlobalConfig.Logging.KeepLogFiles
|
||||
@@ -369,13 +378,14 @@ func SetupLogging() func() {
|
||||
ctx, cancelfunc = context.WithCancel(context.Background())
|
||||
done = make(chan bool)
|
||||
go logScanner(ctx, amlog, done)
|
||||
} else {
|
||||
log.Errorf("**** failed to open amlog: %v - logs will go to stdout", err)
|
||||
}
|
||||
}
|
||||
if logfile == nil {
|
||||
log.SetOutput(os.Stdout)
|
||||
} else {
|
||||
log.SetOutput(logfile)
|
||||
|
||||
}
|
||||
log.SetLevel(loglevel)
|
||||
|
||||
|
||||
@@ -17,42 +17,74 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"git.erbosoft.com/amy/amsterdam/agentclass"
|
||||
"git.erbosoft.com/amy/amsterdam/config"
|
||||
"git.erbosoft.com/amy/amsterdam/database"
|
||||
"git.erbosoft.com/amy/amsterdam/email"
|
||||
"git.erbosoft.com/amy/amsterdam/htmlcheck"
|
||||
"git.erbosoft.com/amy/amsterdam/ui"
|
||||
"git.erbosoft.com/amy/amsterdam/util"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
"github.com/labstack/echo/v5"
|
||||
"github.com/labstack/echo/v5/middleware"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// READ_HEADER_TIMEOUT is the timeout value for reading headers in seconds. (Deliberately NOT configurable because this is a security issue)
|
||||
const READ_HEADER_TIMEOUT = 2
|
||||
|
||||
// GRACEFUL_SHUTDOWN_TIMEOUT is the timeout value for a graceful shutdown.
|
||||
const GRACEFUL_SHUTDOWN_TIMEOUT = 10 * time.Second
|
||||
|
||||
// GetAndPost is used to have functions that respond to both GET and POST on a URI.
|
||||
var GetAndPost = []string{http.MethodGet, http.MethodPost}
|
||||
|
||||
// myIPAddress returns the IP address of this computer.
|
||||
func myIPAddress() net.IP {
|
||||
conn, err := net.Dial("udp", "8.8.8.8:80")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
localAddr := conn.LocalAddr().(*net.UDPAddr)
|
||||
return localAddr.IP
|
||||
}
|
||||
|
||||
// ClassifyInjector injects AmClassify onto the request.
|
||||
func ClassifyInjector(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
opts := agentclass.AmClassifyOptions()
|
||||
opts.HeadersComplete = true
|
||||
return func(c *echo.Context) error {
|
||||
classif := agentclass.AmClassify(c.Request().Header, c.RealIP(), "", opts)
|
||||
c.Set("am.classification", classif)
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
|
||||
// setupEcho creates, configures, and returns a new Echo instance.
|
||||
func setupEcho() *echo.Echo {
|
||||
e := echo.New()
|
||||
e.HideBanner = true
|
||||
e.Logger = &EchoLogrusAdapter{}
|
||||
e.Logger = slog.New(NewSlogLogrusHandler())
|
||||
e.Renderer = &ui.TemplateRenderer{}
|
||||
e.HTTPErrorHandler = AmErrorHandler
|
||||
e.IPExtractor = config.AmGetConfiguredIPExtractor()
|
||||
if !config.CommandLine.DebugPanic {
|
||||
e.Use(middleware.RecoverWithConfig(middleware.RecoverConfig{
|
||||
LogErrorFunc: LogrusPanicLogging,
|
||||
StackSize: int(config.GlobalComputedConfig.PanicRecoveryStack),
|
||||
}))
|
||||
} else {
|
||||
log.Warn("WARNING: --debug-panic in effect - DO NOT use this in production!")
|
||||
}
|
||||
e.Use(LogrusMiddleware)
|
||||
e.Use(LoggingMiddleware, ClassifyInjector)
|
||||
|
||||
// set up the rate limiter from the configuration
|
||||
rateLimiter := AmSetupRateLimiter()
|
||||
@@ -215,17 +247,21 @@ func setupEcho() *echo.Echo {
|
||||
// ampool is the worker pool for one-shot background tasks.
|
||||
var ampool *util.WorkerPool
|
||||
|
||||
// SystemStartTime records the time since the system was started.
|
||||
// SystemStartTime records the time the system was started.
|
||||
var SystemStartTime time.Time
|
||||
|
||||
// main is Ye Olde Main Function.
|
||||
func main() {
|
||||
SystemStartTime = time.Now()
|
||||
|
||||
// Determine my IP address.
|
||||
myIP := myIPAddress()
|
||||
|
||||
// Configure the system.
|
||||
config.SetupConfig()
|
||||
closer := SetupLogging()
|
||||
defer closer()
|
||||
closer, err := database.SetupDb()
|
||||
dbVersion, closer, err := database.SetupDb()
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("Database open failure: %v", err))
|
||||
}
|
||||
@@ -233,21 +269,19 @@ func main() {
|
||||
closer = email.SetupMailSender()
|
||||
defer closer()
|
||||
htmlcheck.SetupDicts()
|
||||
closer = agentclass.AmInitAgentClass(CONTEXTVAL_NAME)
|
||||
defer closer()
|
||||
closer = ui.SetupUILayer()
|
||||
defer closer()
|
||||
|
||||
// Determine my IP address and the admin user.
|
||||
myIP, err := util.MyIPAddress()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Set up to trap SIGINT/SIGTERM and shut down gracefully
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
ctx = context.WithValue(ctx, CONTEXTVAL_NAME, "main")
|
||||
|
||||
// Set up ampool.
|
||||
ampool = util.AmNewPool(ctx, config.GlobalConfig.Tuning.WorkerTasks, config.GlobalConfig.Tuning.Queues.WorkerTasks)
|
||||
ampool = util.AmNewPool(context.WithValue(ctx, CONTEXTVAL_NAME, "ampool"), config.GlobalConfig.Tuning.WorkerTasks,
|
||||
config.GlobalConfig.Tuning.Queues.WorkerTasks)
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
ampool.Shutdown()
|
||||
@@ -258,27 +292,41 @@ func main() {
|
||||
|
||||
// Audit the startup
|
||||
database.AmStoreAudit(database.AmNewAudit(database.AuditStartup, 0, myIP.String(),
|
||||
fmt.Sprintf("version=%s", config.AMSTERDAM_VERSION)))
|
||||
fmt.Sprintf("version=%s", config.AMSTERDAM_VERSION), fmt.Sprintf("database=%s", dbVersion)))
|
||||
defer func() {
|
||||
// Audit the shutdown
|
||||
database.AmStoreAudit(database.AmNewAudit(database.AuditShutdown, 0, myIP.String()))
|
||||
}()
|
||||
|
||||
// Set up the start configuration.
|
||||
sconf := echo.StartConfig{
|
||||
Address: config.GlobalComputedConfig.Listen,
|
||||
HideBanner: true,
|
||||
HidePort: true,
|
||||
GracefulTimeout: GRACEFUL_SHUTDOWN_TIMEOUT,
|
||||
OnShutdownError: func(err error) {
|
||||
log.Fatalf("error in shutting down the server: %v", err)
|
||||
},
|
||||
BeforeServeFunc: func(s *http.Server) error {
|
||||
s.ReadTimeout = time.Duration(config.GlobalConfig.Tuning.Timeouts.HttpRead) * time.Second
|
||||
s.WriteTimeout = time.Duration(config.GlobalConfig.Tuning.Timeouts.HttpWrite) * time.Second
|
||||
s.IdleTimeout = time.Duration(config.GlobalConfig.Tuning.Timeouts.HttpIdle) * time.Second
|
||||
s.ReadHeaderTimeout = READ_HEADER_TIMEOUT * time.Second
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
stime := time.Since(SystemStartTime)
|
||||
log.Infof("Amsterdam %s startup sequence completed in %v", config.AMSTERDAM_VERSION, stime)
|
||||
|
||||
// Start server
|
||||
go func() {
|
||||
if err := e.Start(config.GlobalComputedConfig.Listen); err != nil && err != http.ErrServerClosed {
|
||||
e.Logger.Fatalf("shutting down the server: %v", err)
|
||||
if err := sconf.Start(ctx, e); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
log.Fatalf("shutting down the server: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
// Wait for the interrupt signal and then gracefully shut the server down.
|
||||
// Wait for the context to be done, when the server is shut down.
|
||||
<-ctx.Done()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := e.Shutdown(ctx); err != nil {
|
||||
e.Logger.Fatal(err)
|
||||
}
|
||||
log.Infof("Amsterdam shut down")
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ import (
|
||||
"git.erbosoft.com/amy/amsterdam/database"
|
||||
"git.erbosoft.com/amy/amsterdam/ui"
|
||||
"github.com/CloudyKit/jet/v6"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v5"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -170,7 +170,10 @@ func (rx *sbRender) SetVar(name string, value any) {
|
||||
func templateGetTopic(args jet.Arguments) reflect.Value {
|
||||
post := args.Get(0).Convert(reflect.TypeFor[*database.PostHeader]()).Interface().(*database.PostHeader)
|
||||
ctxt := args.Get(1).Convert(reflect.TypeFor[ui.AmContext]()).Interface().(ui.AmContext)
|
||||
topic, _ := database.AmGetTopic(ctxt.Ctx(), post.TopicId)
|
||||
topic, err := database.AmGetTopic(ctxt.Ctx(), post.TopicId)
|
||||
if err != nil {
|
||||
args.Panicf("templateGetTopic unable to get topic for post #%d: %v", post.PostId, err)
|
||||
}
|
||||
return reflect.ValueOf(topic)
|
||||
}
|
||||
|
||||
@@ -179,7 +182,10 @@ func templateTopicLink(args jet.Arguments) reflect.Value {
|
||||
comm := args.Get(0).Convert(reflect.TypeFor[*database.Community]()).Interface().(*database.Community)
|
||||
topic := args.Get(1).Convert(reflect.TypeFor[*database.Topic]()).Interface().(*database.Topic)
|
||||
ctxt := args.Get(2).Convert(reflect.TypeFor[ui.AmContext]()).Interface().(ui.AmContext)
|
||||
link, _ := topic.Link(ctxt.Ctx(), comm.Id, "global")
|
||||
link, err := topic.Link(ctxt.Ctx(), comm.Id, "global")
|
||||
if err != nil {
|
||||
args.Panicf("templateTopicLink unable to get link for topic %d: %v", topic.TopicId, err)
|
||||
}
|
||||
return reflect.ValueOf(link)
|
||||
}
|
||||
|
||||
@@ -285,24 +291,24 @@ func PolicyPage(ctxt ui.AmContext) (string, any) {
|
||||
func JumpToShortcut(ctxt ui.AmContext) (string, any) {
|
||||
link, err := database.AmDecodePostLink(ctxt.URLParam("postlink"))
|
||||
if err != nil {
|
||||
return "error", echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("not found: %s", ctxt.URLParam("postlink"))).SetInternal(err)
|
||||
return "error", echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("not found: %s", ctxt.URLParam("postlink"))).Wrap(err)
|
||||
}
|
||||
scope, target := link.Classify()
|
||||
if scope != "global" {
|
||||
if scope != database.PLSCOPE_GLOBAL {
|
||||
return "error", echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("not found: %s", ctxt.URLParam("postlink")))
|
||||
}
|
||||
if err = link.VerifyNames(ctxt.Ctx()); err != nil {
|
||||
return "error", echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("not found: %s", ctxt.URLParam("postlink"))).SetInternal(err)
|
||||
return "error", echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("not found: %s", ctxt.URLParam("postlink"))).Wrap(err)
|
||||
}
|
||||
targetURL := ""
|
||||
switch target {
|
||||
case "community":
|
||||
case database.PLCLASS_COMMUNITY:
|
||||
targetURL = fmt.Sprintf("/comm/%s", link.Community)
|
||||
case "conference":
|
||||
case database.PLCLASS_CONFERENCE:
|
||||
targetURL = fmt.Sprintf("/comm/%s/conf/%s", link.Community, link.Conference)
|
||||
case "topic":
|
||||
case database.PLCLASS_TOPIC:
|
||||
targetURL = fmt.Sprintf("/comm/%s/conf/%s/r/%d", link.Community, link.Conference, link.Topic)
|
||||
case "post", "postrange", "postopenrange":
|
||||
case database.PLCLASS_POST, database.PLCLASS_POSTRANGE, database.PLCLASS_POSTOPENRANGE:
|
||||
targetURL = fmt.Sprintf("/comm/%s/conf/%s/r/%d?r=%d,%d", link.Community, link.Conference, link.Topic, link.FirstPost, link.LastPost)
|
||||
default:
|
||||
return "error", fmt.Sprintf("invalid target '%s' for link: %s", target, ctxt.URLParam("postlink"))
|
||||
|
||||
+6
-6
@@ -27,7 +27,7 @@ import (
|
||||
"git.erbosoft.com/amy/amsterdam/database"
|
||||
"git.erbosoft.com/amy/amsterdam/util"
|
||||
"github.com/CloudyKit/jet/v6"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v5"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -98,7 +98,7 @@ type AmContext interface {
|
||||
|
||||
// amContext is the internal structure that implements AmContext.
|
||||
type amContext struct {
|
||||
echoContext echo.Context
|
||||
echoContext *echo.Context
|
||||
rendervars jet.VarMap
|
||||
frameTitle string
|
||||
frameMeta map[int]map[string]string
|
||||
@@ -243,7 +243,7 @@ func (c *amContext) FormFieldIsSet(name string) bool {
|
||||
|
||||
// FormFieldValues returns all values for a specified parameter name.
|
||||
func (c *amContext) FormFieldValues(name string) ([]string, error) {
|
||||
vals, err := c.echoContext.FormParams()
|
||||
vals, err := c.echoContext.FormValues()
|
||||
if err != nil {
|
||||
return make([]string, 0), err
|
||||
}
|
||||
@@ -525,7 +525,7 @@ var amContextRecycleBin chan *amContext
|
||||
* Internal Amsterdam context structure pointer, or nil.
|
||||
* Standard Go error status.
|
||||
*/
|
||||
func newContext(ctxt echo.Context) (*amContext, error) {
|
||||
func newContext(ctxt *echo.Context) (*amContext, error) {
|
||||
var rc *amContext
|
||||
tmp := freeContext.Get()
|
||||
if tmp == nil {
|
||||
@@ -593,7 +593,7 @@ func newContext(ctxt echo.Context) (*amContext, error) {
|
||||
* Returns:
|
||||
* The associated AmContext.
|
||||
*/
|
||||
func AmContextFromEchoContext(ctxt echo.Context) AmContext {
|
||||
func AmContextFromEchoContext(ctxt *echo.Context) AmContext {
|
||||
myctxt := ctxt.Get("__amsterdam_context")
|
||||
if myctxt != nil {
|
||||
rc, ok := myctxt.(*amContext)
|
||||
@@ -641,7 +641,7 @@ func setupContext() func() {
|
||||
|
||||
// ContextCreator is middleware that creates and recycles the AmContext.
|
||||
func ContextCreator(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
return func(c *echo.Context) error {
|
||||
myctxt, err := newContext(c)
|
||||
if err == nil {
|
||||
err = next(c)
|
||||
|
||||
+26
-29
@@ -18,12 +18,11 @@ import (
|
||||
"net/http"
|
||||
"slices"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"git.erbosoft.com/amy/amsterdam/config"
|
||||
"git.erbosoft.com/amy/amsterdam/database"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v5"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -33,6 +32,12 @@ import (
|
||||
be timed out as well as used to show the logged-in users. This is similar to the session support provided in J2EE servlets.
|
||||
*/
|
||||
|
||||
// DEFAULT_SESSION_EXPIRE is the default time in which sessions will expire.
|
||||
const DEFAULT_SESSION_EXPIRE = 1 * time.Hour
|
||||
|
||||
// The interval at which all sessions will be swept.
|
||||
const SESSION_STORE_SWEEP_INTERVAL = 2 * time.Minute
|
||||
|
||||
// AmSessionOptions gives the options for the session.
|
||||
type AmSessionOptions struct {
|
||||
Path string
|
||||
@@ -244,11 +249,10 @@ func (sess *amSession) Hit() {
|
||||
|
||||
// amSessionStore is the implementation structure for AmSessionStore.
|
||||
type amSessionStore struct {
|
||||
mutex sync.RWMutex
|
||||
sessions map[string]*amSession
|
||||
maxEntries int
|
||||
expiry time.Duration
|
||||
sweepRunning atomic.Bool
|
||||
mutex sync.RWMutex
|
||||
sessions map[string]*amSession
|
||||
maxEntries int
|
||||
expiry time.Duration
|
||||
}
|
||||
|
||||
// createAmSessionStore creates the session store.
|
||||
@@ -258,7 +262,6 @@ func createAmSessionStore(exp time.Duration) *amSessionStore {
|
||||
maxEntries: 0,
|
||||
expiry: exp,
|
||||
}
|
||||
rc.sweepRunning.Store(true)
|
||||
return rc
|
||||
}
|
||||
|
||||
@@ -339,9 +342,15 @@ func (st *amSessionStore) SessionInfo() (int, []string, int) {
|
||||
* tick - Channel that "pulses" periodically to run the task.
|
||||
* done - Channel we write to when we're done.
|
||||
*/
|
||||
func (st *amSessionStore) sweep(tick <-chan time.Time, done chan bool) {
|
||||
for range tick {
|
||||
if st.sweepRunning.Load() {
|
||||
func (st *amSessionStore) sweep(ctx context.Context, done chan bool) {
|
||||
tkr := time.NewTicker(SESSION_STORE_SWEEP_INTERVAL)
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
tkr.Stop()
|
||||
done <- true
|
||||
return
|
||||
case <-tkr.C:
|
||||
// phase 1 - identify expired sessions
|
||||
st.mutex.RLock()
|
||||
zap := make([]string, 0, len(st.sessions))
|
||||
@@ -366,11 +375,8 @@ func (st *amSessionStore) sweep(tick <-chan time.Time, done chan bool) {
|
||||
}
|
||||
st.mutex.Unlock()
|
||||
}
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
done <- true
|
||||
}
|
||||
|
||||
// sessionStore is the global session store.
|
||||
@@ -381,36 +387,27 @@ func setupSessionManager() func() {
|
||||
// get the time for the session to expire
|
||||
d, err := time.ParseDuration(config.GlobalConfig.Site.SessionExpire)
|
||||
if err != nil {
|
||||
d, err = time.ParseDuration("1h")
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
log.Errorf("invalid session timeout value: %s", config.GlobalConfig.Site.SessionExpire)
|
||||
d = DEFAULT_SESSION_EXPIRE
|
||||
}
|
||||
|
||||
// create session store
|
||||
sessionStore = createAmSessionStore(d)
|
||||
|
||||
// get the clock value to run sweeps
|
||||
d, err = time.ParseDuration("1s")
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
|
||||
// set up the sweep runner
|
||||
tkr := time.NewTicker(d)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan bool)
|
||||
go sessionStore.sweep(tkr.C, done)
|
||||
go sessionStore.sweep(ctx, done)
|
||||
return func() {
|
||||
// stop the sweep runner
|
||||
sessionStore.sweepRunning.Store(false)
|
||||
cancel()
|
||||
<-done
|
||||
tkr.Stop()
|
||||
}
|
||||
}
|
||||
|
||||
// SessionStoreInjector is middleware that injects the session store into the context variables.
|
||||
func SessionStoreInjector(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
return func(c *echo.Context) error {
|
||||
c.Set("AmSessionStore", sessionStore)
|
||||
return next(c)
|
||||
}
|
||||
|
||||
+2
-2
@@ -14,6 +14,7 @@ package ui
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
@@ -120,8 +121,7 @@ func AmLoadDialog(name string) (*Dialog, error) {
|
||||
f, err = extDialogs.Open(fmt.Sprintf("%s.yaml", name))
|
||||
if err != nil {
|
||||
f = nil
|
||||
pe := err.(*fs.PathError)
|
||||
if pe.Err == os.ErrInvalid || pe.Err == os.ErrNotExist {
|
||||
if errors.Is(err, os.ErrInvalid) || errors.Is(err, os.ErrNotExist) {
|
||||
err = nil
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -29,7 +29,7 @@ import (
|
||||
|
||||
"git.erbosoft.com/amy/amsterdam/database"
|
||||
"github.com/disintegration/imaging"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v5"
|
||||
)
|
||||
|
||||
//go:embed static_images/*
|
||||
@@ -64,7 +64,7 @@ func mimeTypeFromFilename(filename string) string {
|
||||
* Returns:
|
||||
* Standard Go error return.
|
||||
*/
|
||||
func AmServeImage(c echo.Context) error {
|
||||
func AmServeImage(c *echo.Context) error {
|
||||
components := strings.SplitAfter(c.Request().URL.Path, "/")
|
||||
var err error = nil
|
||||
if len(components) == 4 {
|
||||
@@ -105,7 +105,7 @@ func AmServeImage(c echo.Context) error {
|
||||
* Returns:
|
||||
* Standard Go error return.
|
||||
*/
|
||||
func AmServeVeniceCompatibleImage(c echo.Context) error {
|
||||
func AmServeVeniceCompatibleImage(c *echo.Context) error {
|
||||
id, err := strconv.Atoi(c.Param("id"))
|
||||
if err == nil {
|
||||
var img *database.ImageStore
|
||||
|
||||
+10
-10
@@ -21,17 +21,17 @@ import (
|
||||
|
||||
"git.erbosoft.com/amy/amsterdam/config"
|
||||
"git.erbosoft.com/amy/amsterdam/database"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v5"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// IPBanTest is middleware that handles the IP banning.
|
||||
func IPBanTest(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
return func(c *echo.Context) error {
|
||||
// Check IP banning.
|
||||
banmsg, banerr := database.AmTestIPBan(c.Request().Context(), c.RealIP())
|
||||
if banerr != nil {
|
||||
c.Logger().Warnf("address %s could not be tested: %v", c.RealIP(), banerr)
|
||||
log.Warnf("address %s could not be tested: %v", c.RealIP(), banerr)
|
||||
// but let the request pass anyway
|
||||
} else if banmsg != "" {
|
||||
amctxt := AmContextFromEchoContext(c)
|
||||
@@ -43,7 +43,7 @@ func IPBanTest(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
|
||||
// CookieLoginTest is middleware that handles cookie logins.
|
||||
func CookieLoginTest(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
return func(c *echo.Context) error {
|
||||
amctxt := AmContextFromEchoContext(c)
|
||||
// Check for cookie login.
|
||||
if amctxt.CurrentUser().IsAnon {
|
||||
@@ -77,11 +77,11 @@ func CookieLoginTest(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
|
||||
// SetCommunity is middleware that sets the community context based on the URL.
|
||||
func SetCommunity(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
return func(c *echo.Context) error {
|
||||
ctxt := AmContextFromEchoContext(c)
|
||||
err := ctxt.SetCommunityContext(ctxt.URLParam("cid"))
|
||||
if err != nil {
|
||||
return AmSendPageData(c, ctxt, "error", echo.NewHTTPError(http.StatusNotFound).SetInternal(err))
|
||||
return AmSendPageData(c, ctxt, "error", echo.NewHTTPError(http.StatusNotFound, err.Error()).Wrap(err))
|
||||
}
|
||||
var b strings.Builder
|
||||
b.WriteString("/comm/")
|
||||
@@ -94,10 +94,10 @@ func SetCommunity(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
|
||||
// ValidateConference is middleware that validates the user has access to the community's conference facility.
|
||||
func ValidateConference(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
return func(c *echo.Context) error {
|
||||
ctxt := AmContextFromEchoContext(c)
|
||||
comm := ctxt.CurrentCommunity() // set by middleware
|
||||
b, err := database.AmTestService(c.Request().Context(), comm, "Conference")
|
||||
b, err := database.AmTestService(c.Request().Context(), comm, database.AM_SVC_CONFERENCE)
|
||||
if err != nil {
|
||||
return AmSendPageData(c, ctxt, "error", err)
|
||||
}
|
||||
@@ -116,7 +116,7 @@ func ValidateConference(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
|
||||
// SetConference is middleware that sets the conference context based on the URL.
|
||||
func SetConference(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
return func(c *echo.Context) error {
|
||||
ctxt := AmContextFromEchoContext(c)
|
||||
conf, err := database.AmGetConferenceByAlias(ctxt.Ctx(), ctxt.CurrentCommunity().Id, ctxt.URLParam("confid"))
|
||||
if err != nil {
|
||||
@@ -144,7 +144,7 @@ func SetConference(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
|
||||
// SetTopic is middleware that sets the topic context based on the URL.
|
||||
func SetTopic(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
return func(c *echo.Context) error {
|
||||
ctxt := AmContextFromEchoContext(c)
|
||||
conf := ctxt.GetScratch("currentConference").(*database.Conference)
|
||||
|
||||
|
||||
+145
-72
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Amsterdam Web Communities System
|
||||
* Copyright (c) 2025 Erbosoft Metaverse Design Solutions, All Rights Reserved
|
||||
* Copyright (c) 2025-2026 Erbosoft Metaverse Design Solutions, All Rights Reserved
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
@@ -13,18 +13,82 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"git.erbosoft.com/amy/amsterdam/config"
|
||||
"git.erbosoft.com/amy/amsterdam/database"
|
||||
"github.com/klauspost/lctime"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v5"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// panicRecoveryErr is the error created for panic recovery.
|
||||
type panicRecoveryErr struct {
|
||||
Phase string // phase of operation
|
||||
Err error // error value
|
||||
Stack []byte // stack trace
|
||||
}
|
||||
|
||||
// Error returns the actual error string.
|
||||
func (e *panicRecoveryErr) Error() string {
|
||||
return fmt.Sprintf("[Panic Recovery in %s Phase] %s %s", e.Phase, e.Err.Error(), e.Stack)
|
||||
}
|
||||
|
||||
// Unwrap returns the error "nested" inside this error.
|
||||
func (e *panicRecoveryErr) Unwrap() error {
|
||||
return e.Err
|
||||
}
|
||||
|
||||
// doFrameRender renders the outer frame template with an inner template.
|
||||
func doFrameRender(ctxt *echo.Context, amctxt AmContext, statusCode int, innerPage string) error {
|
||||
if amctxt.FrameTitle() == "" {
|
||||
log.Errorf("*** NO FRAME TITLE set for path %s", amctxt.URLPath())
|
||||
amctxt.SetFrameTitle("<<< NO FRAME TITLE >>>")
|
||||
}
|
||||
amctxt.VarMap().Set("__innerPage", innerPage)
|
||||
menus := make([]*MenuDefinition, 2)
|
||||
switch amctxt.LeftMenu() {
|
||||
case "top":
|
||||
menus[0] = AmMenu(config.GlobalConfig.Site.TopMenuId)
|
||||
case "community":
|
||||
comm := amctxt.CurrentCommunity()
|
||||
if comm != nil {
|
||||
md, err := AmBuildCommunityMenu(ctxt.Request().Context(), comm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
menus[0] = md
|
||||
} else {
|
||||
menus[0] = AmMenu(config.GlobalConfig.Site.TopMenuId)
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("AmSendPageData(): unknown left menu context: %s", amctxt.LeftMenu())
|
||||
}
|
||||
menus[1] = AmMenu(config.GlobalConfig.Site.FixedMenuId)
|
||||
amctxt.VarMap().Set("__leftMenus", menus)
|
||||
ad, err := database.AmGetRandomAd(ctxt.Request().Context())
|
||||
if err != nil {
|
||||
ad = &database.Advert{
|
||||
AdId: -1,
|
||||
ImagePath: "",
|
||||
PathStyle: -1,
|
||||
Caption: nil,
|
||||
LinkURL: nil,
|
||||
}
|
||||
}
|
||||
amctxt.VarMap().Set("__bannerad", ad)
|
||||
amctxt.VarMap().Set("__debugMode", config.GlobalComputedConfig.DebugMode)
|
||||
if tmp := amctxt.GetScratch("frame_suppressLogin"); tmp != nil {
|
||||
amctxt.VarMap().Set("__suppressLogin", true)
|
||||
}
|
||||
return ctxt.Render(statusCode, config.GlobalConfig.Site.FrameTemplate, amctxt)
|
||||
}
|
||||
|
||||
/* AmSendPageData sends page data to the output based on the command string.
|
||||
* Parameters:
|
||||
* ctxt - The Echo context from the request.
|
||||
@@ -44,40 +108,56 @@ import (
|
||||
* Returns:
|
||||
* Standard Go error status.
|
||||
*/
|
||||
func AmSendPageData(ctxt echo.Context, amctxt AmContext, command string, data any) error {
|
||||
func AmSendPageData(ctxt *echo.Context, amctxt AmContext, command string, data any) error {
|
||||
// Enable panic recovery.
|
||||
if !config.CommandLine.DebugPanic {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
if r == http.ErrAbortHandler {
|
||||
panic(r)
|
||||
}
|
||||
tmperr, ok := r.(error)
|
||||
if !ok {
|
||||
tmperr = fmt.Errorf("%v", r)
|
||||
}
|
||||
stack := make([]byte, config.GlobalComputedConfig.PanicRecoveryStack)
|
||||
length := runtime.Stack(stack, false)
|
||||
log.Errorf("[Panic Recovery in SendData Phase] %s %s", tmperr.Error(), stack[:length])
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Preprocess certain commands into different ones.
|
||||
httprc := http.StatusOK
|
||||
switch command {
|
||||
case "error":
|
||||
message := ""
|
||||
if data == nil {
|
||||
message = fmt.Sprintf("Unspecified error in %s", ctxt.Request().URL.String())
|
||||
} else if he, ok := data.(*echo.HTTPError); ok {
|
||||
httprc = he.Code
|
||||
m1 := he.Message
|
||||
e1 := he.Unwrap()
|
||||
if m1 == nil || m1 == "" {
|
||||
if e1 == nil {
|
||||
message = fmt.Sprintf("Unspecified error in %s", ctxt.Request().URL.String())
|
||||
message := fmt.Sprintf("Unspecified error in %s", ctxt.Request().URL.String())
|
||||
if data != nil {
|
||||
if he, ok := data.(*echo.HTTPError); ok {
|
||||
httprc = he.Code
|
||||
m1 := he.Message
|
||||
e1 := he.Unwrap()
|
||||
if m1 == "" {
|
||||
if e1 != nil {
|
||||
message = e1.Error()
|
||||
}
|
||||
} else {
|
||||
message = e1.Error()
|
||||
if e1 == nil {
|
||||
message = fmt.Sprintf("%v", m1)
|
||||
} else {
|
||||
message = fmt.Sprintf("%v (%v)", m1, e1)
|
||||
}
|
||||
}
|
||||
} else if er, ok := data.(error); ok {
|
||||
message = er.Error()
|
||||
} else {
|
||||
if e1 == nil {
|
||||
message = fmt.Sprintf("%v", m1)
|
||||
} else {
|
||||
message = fmt.Sprintf("%v (%v)", m1, e1)
|
||||
}
|
||||
message = fmt.Sprintf("%v", data)
|
||||
}
|
||||
} else if er, ok := data.(error); ok {
|
||||
message = er.Error()
|
||||
} else {
|
||||
message = fmt.Sprintf("%v", data)
|
||||
}
|
||||
if httprc < 400 {
|
||||
httprc = http.StatusInternalServerError
|
||||
}
|
||||
amctxt.SetFrameTitle("Internal Server Error")
|
||||
amctxt.SetFrameTitle(http.StatusText(httprc))
|
||||
amctxt.VarMap().Set("error", message)
|
||||
if tmp := amctxt.GetSession("lastKnownGood"); tmp != nil {
|
||||
amctxt.VarMap().Set("recovery", tmp)
|
||||
@@ -98,6 +178,11 @@ func AmSendPageData(ctxt echo.Context, amctxt AmContext, command string, data an
|
||||
}
|
||||
|
||||
// Process commands.
|
||||
oldreq := ctxt.Request()
|
||||
ctx, cancel := context.WithTimeout(oldreq.Context(), time.Duration(config.GlobalConfig.Tuning.Timeouts.PageRender)*time.Second)
|
||||
defer cancel()
|
||||
ctxt.SetRequest(oldreq.WithContext(ctx))
|
||||
defer ctxt.SetRequest(oldreq)
|
||||
var err error
|
||||
switch command {
|
||||
case "bytes":
|
||||
@@ -113,47 +198,7 @@ func AmSendPageData(ctxt echo.Context, amctxt AmContext, command string, data an
|
||||
case "template":
|
||||
err = ctxt.Render(httprc, data.(string), amctxt)
|
||||
case "framed":
|
||||
if amctxt.FrameTitle() == "" {
|
||||
ctxt.Logger().Errorf("*** NO FRAME TITLE set for path %s", amctxt.URLPath())
|
||||
amctxt.SetFrameTitle("<<< NO FRAME TITLE >>>")
|
||||
}
|
||||
amctxt.VarMap().Set("__innerPage", data)
|
||||
menus := make([]*MenuDefinition, 2)
|
||||
switch amctxt.LeftMenu() {
|
||||
case "top":
|
||||
menus[0] = AmMenu("top")
|
||||
case "community":
|
||||
comm := amctxt.CurrentCommunity()
|
||||
if comm != nil {
|
||||
md, err := AmBuildCommunityMenu(ctxt.Request().Context(), comm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
menus[0] = md
|
||||
} else {
|
||||
menus[0] = AmMenu("top")
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("AmSendPageData(): unknown left menu context: %s", amctxt.LeftMenu())
|
||||
}
|
||||
menus[1] = AmMenu("fixed")
|
||||
amctxt.VarMap().Set("__leftMenus", menus)
|
||||
ad, err := database.AmGetRandomAd(ctxt.Request().Context())
|
||||
if err != nil {
|
||||
ad = &database.Advert{
|
||||
AdId: -1,
|
||||
ImagePath: "",
|
||||
PathStyle: -1,
|
||||
Caption: nil,
|
||||
LinkURL: nil,
|
||||
}
|
||||
}
|
||||
amctxt.VarMap().Set("__bannerad", ad)
|
||||
amctxt.VarMap().Set("__debugMode", config.GlobalComputedConfig.DebugMode)
|
||||
if tmp := amctxt.GetScratch("frame_suppressLogin"); tmp != nil {
|
||||
amctxt.VarMap().Set("__suppressLogin", true)
|
||||
}
|
||||
err = ctxt.Render(httprc, "frame.jet", amctxt)
|
||||
err = doFrameRender(ctxt, amctxt, httprc, data.(string))
|
||||
default:
|
||||
err = fmt.Errorf("AmSendPageData(): unknown rendering type: %s", command)
|
||||
}
|
||||
@@ -169,6 +214,34 @@ var expireTime string = lctime.Strftime("%c", time.Unix(1, 0))
|
||||
// AmPageFunc is the definition for an Amsterdam "page function" that handles most of the work and defers to the wrapper for rendering.
|
||||
type AmPageFunc func(AmContext) (string, any)
|
||||
|
||||
// callWrappedPageFunc calls the specified page functon inside a wrapper that handles timeouts and panic recovery.
|
||||
func callWrappedPageFunc(f AmPageFunc, ctxt *echo.Context, amctxt AmContext) (command string, arg any) {
|
||||
if !config.CommandLine.DebugPanic {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
if r == http.ErrAbortHandler {
|
||||
panic(r)
|
||||
}
|
||||
tmperr, ok := r.(error)
|
||||
if !ok {
|
||||
tmperr = fmt.Errorf("%v", r)
|
||||
}
|
||||
stack := make([]byte, config.GlobalComputedConfig.PanicRecoveryStack)
|
||||
length := runtime.Stack(stack, false)
|
||||
arg = &panicRecoveryErr{Phase: "PageFunc", Err: tmperr, Stack: stack[:length]}
|
||||
command = "error"
|
||||
}
|
||||
}()
|
||||
}
|
||||
oldreq := ctxt.Request()
|
||||
ctx, cancel := context.WithTimeout(oldreq.Context(), time.Duration(config.GlobalConfig.Tuning.Timeouts.PageExecute)*time.Second)
|
||||
defer cancel()
|
||||
ctxt.SetRequest(oldreq.WithContext(ctx))
|
||||
defer ctxt.SetRequest(oldreq)
|
||||
command, arg = f(amctxt)
|
||||
return
|
||||
}
|
||||
|
||||
/* AmWrap wraps the Amsterdam handler function in a wrapper that implements the spec for
|
||||
* Echo handler functions.
|
||||
* Parameters:
|
||||
@@ -177,7 +250,7 @@ type AmPageFunc func(AmContext) (string, any)
|
||||
* The wrapped function.
|
||||
*/
|
||||
func AmWrap(myfunc AmPageFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
return func(c *echo.Context) error {
|
||||
ctxt := AmContextFromEchoContext(c)
|
||||
|
||||
// Add the dynamic headers.
|
||||
@@ -186,16 +259,16 @@ func AmWrap(myfunc AmPageFunc) echo.HandlerFunc {
|
||||
c.Response().Header().Set("Expires", expireTime)
|
||||
|
||||
// Exec the wrapped function.
|
||||
command, arg := myfunc(ctxt)
|
||||
command, arg := callWrappedPageFunc(myfunc, c, ctxt)
|
||||
if command != "error" && command != "ipban" {
|
||||
ctxt.SetSession("lastKnownGood", ctxt.Locator())
|
||||
}
|
||||
if err := ctxt.SaveSession(); err != nil {
|
||||
c.Logger().Errorf("Session save error: %v", err)
|
||||
log.Errorf("Session save error: %v", err)
|
||||
return err
|
||||
}
|
||||
if err := AmSendPageData(c, ctxt, command, arg); err != nil {
|
||||
c.Logger().Errorf("Rendering error: %v", err)
|
||||
log.Errorf("Rendering error: %v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -203,7 +276,7 @@ func AmWrap(myfunc AmPageFunc) echo.HandlerFunc {
|
||||
}
|
||||
|
||||
// AmWithTempContext runs a page function with a temporary context. Used in error handling.
|
||||
func AmWithTempContext(c echo.Context, fn AmPageFunc) error {
|
||||
func AmWithTempContext(c *echo.Context, fn AmPageFunc) error {
|
||||
var ctxt AmContext = nil
|
||||
myctxt := c.Get("__amsterdam_context")
|
||||
if myctxt != nil {
|
||||
@@ -225,7 +298,7 @@ func AmWithTempContext(c echo.Context, fn AmPageFunc) error {
|
||||
}
|
||||
|
||||
// Call the function
|
||||
command, arg := fn(ctxt)
|
||||
command, arg := callWrappedPageFunc(fn, c, ctxt)
|
||||
|
||||
// Add the dynamic headers.
|
||||
c.Response().Header().Set("Pragma", "No-cache")
|
||||
@@ -233,7 +306,7 @@ func AmWithTempContext(c echo.Context, fn AmPageFunc) error {
|
||||
c.Response().Header().Set("Expires", expireTime)
|
||||
|
||||
if err := AmSendPageData(c, ctxt, command, arg); err != nil {
|
||||
c.Logger().Errorf("Rendering error: %v", err)
|
||||
log.Errorf("Rendering error: %v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
+1
-1
@@ -23,7 +23,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"git.erbosoft.com/amy/amsterdam/config"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v5"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
+23
-23
@@ -14,7 +14,6 @@ package ui
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
@@ -31,7 +30,7 @@ import (
|
||||
"github.com/CloudyKit/jet/v6"
|
||||
"github.com/CloudyKit/jet/v6/loaders/embedfs"
|
||||
"github.com/CloudyKit/jet/v6/loaders/multi"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v5"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -86,18 +85,16 @@ func makeYearRange(a jet.Arguments) reflect.Value {
|
||||
param := a.Get(0).Convert(reflect.TypeFor[string]()).String()
|
||||
yearRegex, _ := regexp.Compile(`year:(\S+)(\s+.+)?$`)
|
||||
m := yearRegex.FindStringSubmatch(param)
|
||||
if m != nil {
|
||||
count, err := strconv.Atoi(m[1])
|
||||
if err == nil {
|
||||
start_year := time.Now().Year()
|
||||
rc := &countRanger{i: -1, val: int64(start_year) + 1, step: -1, to: int64(start_year + count - 1)}
|
||||
return reflect.ValueOf(rc).Convert(reflect.TypeFor[jet.Ranger]())
|
||||
} else {
|
||||
return reflect.ValueOf(err)
|
||||
}
|
||||
} else {
|
||||
return reflect.ValueOf(errors.New("cannot locate year: marker in param"))
|
||||
if m == nil {
|
||||
a.Panicf("cannot locate year: marker in param %s", param)
|
||||
}
|
||||
count, err := strconv.Atoi(m[1])
|
||||
if err != nil {
|
||||
a.Panicf("bad string in yearRange: %s - %v", param, err)
|
||||
}
|
||||
start_year := time.Now().Year()
|
||||
rc := &countRanger{i: -1, val: int64(start_year) + 1, step: -1, to: int64(start_year + count - 1)}
|
||||
return reflect.ValueOf(rc).Convert(reflect.TypeFor[jet.Ranger]())
|
||||
}
|
||||
|
||||
// immediateIf returns its 2nd argument if its 1st argument is true, and its 3rd argument if its 1st argument is false.
|
||||
@@ -156,19 +153,19 @@ func displayDateTime(a jet.Arguments) reflect.Value {
|
||||
} else if p0.CanConvert(reflect.TypeFor[*time.Time]()) {
|
||||
ptr := p0.Convert(reflect.TypeFor[*time.Time]()).Interface().(*time.Time)
|
||||
if ptr == nil {
|
||||
return reflect.ValueOf("<<NIL>>")
|
||||
a.Panicf("null date/time value in displayDateTime")
|
||||
}
|
||||
timeval = *ptr
|
||||
} else {
|
||||
return reflect.ValueOf("<<BOGUS>>")
|
||||
a.Panicf("bogus date/time value in displayDateTime")
|
||||
}
|
||||
ctxt := a.Get(1).Convert(reflect.TypeFor[AmContext]()).Interface().(AmContext)
|
||||
prefs, err := ctxt.CurrentUser().Prefs(ctxt.Ctx())
|
||||
if err == nil {
|
||||
loc := prefs.Localizer()
|
||||
return reflect.ValueOf(loc.Strftime("%b %e, %Y %r", timeval))
|
||||
if err != nil {
|
||||
a.Panicf("unable to get prefs for current user in displayDateTime - %v", err)
|
||||
}
|
||||
return reflect.ValueOf(fmt.Sprintf("<<%v>>", err))
|
||||
loc := prefs.Localizer()
|
||||
return reflect.ValueOf(loc.Strftime("%b %e, %Y %r", timeval))
|
||||
}
|
||||
|
||||
// displayActivity displays an activity string formatted to the user's preferences.
|
||||
@@ -176,10 +173,10 @@ func displayActivity(a jet.Arguments) reflect.Value {
|
||||
timeval := a.Get(0).Convert(reflect.TypeFor[*time.Time]()).Interface().(*time.Time)
|
||||
ctxt := a.Get(1).Convert(reflect.TypeFor[AmContext]()).Interface().(AmContext)
|
||||
prefs, err := ctxt.CurrentUser().Prefs(ctxt.Ctx())
|
||||
if err == nil {
|
||||
return reflect.ValueOf(util.AmActivityString(timeval, prefs.Localizer()))
|
||||
if err != nil {
|
||||
a.Panicf("unable to get prefs for current user in displayActivity - %v", err)
|
||||
}
|
||||
return reflect.ValueOf(fmt.Sprintf("<<%v>>", err))
|
||||
return reflect.ValueOf(util.AmActivityString(timeval, prefs.Localizer()))
|
||||
}
|
||||
|
||||
// displayMemberCount displays the current member count of a community.
|
||||
@@ -309,6 +306,9 @@ func setupTemplates() {
|
||||
views.AddGlobal("AmsterdamVersion", config.AMSTERDAM_VERSION)
|
||||
views.AddGlobal("AmsterdamCopyright", config.AMSTERDAM_COPYRIGHT)
|
||||
views.AddGlobal("GlobalConfig", config.GlobalConfig)
|
||||
views.AddGlobal("PLSCOPE_COMMUNITY", database.PLSCOPE_COMMUNITY)
|
||||
views.AddGlobal("PLSCOPE_CONFERENCE", database.PLSCOPE_CONFERENCE)
|
||||
views.AddGlobal("PLSCOPE_TOPIC", database.PLSCOPE_TOPIC)
|
||||
views.AddGlobalFunc("iif", immediateIf)
|
||||
views.AddGlobalFunc("postRewrite", postRewrite)
|
||||
views.AddGlobalFunc("MakeIntRange", makeIntRange)
|
||||
@@ -363,7 +363,7 @@ type TemplateRenderer struct{}
|
||||
* Returns:
|
||||
* Standard Go error status.
|
||||
*/
|
||||
func (r *TemplateRenderer) Render(w io.Writer, name string, data any, c echo.Context) error {
|
||||
func (r *TemplateRenderer) Render(c *echo.Context, w io.Writer, name string, data any) error {
|
||||
defer util.MeasureTime(fmt.Sprintf("ui.Render(%s)", name))()
|
||||
|
||||
view, err := views.GetTemplate(name)
|
||||
|
||||
@@ -13,11 +13,11 @@
|
||||
<div class="mb-2">
|
||||
<div class=" flex items-baseline gap-2">
|
||||
<h1 class="text-blue-800 text-4xl font-bold">Find Posts</h1>
|
||||
{{ if scope == "community" }}
|
||||
{{ if scope == PLSCOPE_COMMUNITY }}
|
||||
<span class="text-blue-800 text-xl font-bold ml-2">in Community: {{ entityName }}</span>
|
||||
{{ else if scope == "conference" }}
|
||||
{{ else if scope == PLSCOPE_CONFERENCE }}
|
||||
<span class="text-blue-800 text-xl font-bold ml-2">in Conference: {{ entityName }}</span>
|
||||
{{ else if scope == "topic" }}
|
||||
{{ else if scope == PLSCOPE_TOPIC }}
|
||||
<span class="text-blue-800 text-xl font-bold ml-2">in Topic: {{ entityName | raw }}</span>
|
||||
{{ end }}
|
||||
</div>
|
||||
@@ -26,11 +26,11 @@
|
||||
|
||||
<!-- Backlink -->
|
||||
<div class="mb-4">
|
||||
{{ if scope == "community" }}
|
||||
{{ if scope == PLSCOPE_COMMUNITY }}
|
||||
<a class="text-blue-700 hover:text-blue-900 text-sm font-medium" href="{{ backlink }}">Return to Conference List</a>
|
||||
{{ else if scope == "conference" }}
|
||||
{{ else if scope == PLSCOPE_CONFERENCE }}
|
||||
<a class="text-blue-700 hover:text-blue-900 text-sm font-medium" href="{{ backlink }}">Return to Topic List</a>
|
||||
{{ else if scope == "topic" }}
|
||||
{{ else if scope == PLSCOPE_TOPIC }}
|
||||
<a class="text-blue-700 hover:text-blue-900 text-sm font-medium" href="{{ backlink }}">Return to Topic</a>
|
||||
{{ end }}
|
||||
</div>
|
||||
|
||||
+13
-7
@@ -26,7 +26,7 @@ import (
|
||||
"git.erbosoft.com/amy/amsterdam/ui"
|
||||
"git.erbosoft.com/amy/amsterdam/util"
|
||||
"github.com/biter777/countries"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v5"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -382,7 +382,7 @@ func ShowProfile(ctxt ui.AmContext) (string, any) {
|
||||
// Gather the info on the current user.
|
||||
user, err := database.AmGetUserByName(ctxt.Ctx(), ctxt.URLParam("uname"), nil)
|
||||
if err != nil {
|
||||
return "error", echo.NewHTTPError(http.StatusNotFound).SetInternal(err)
|
||||
return "error", echo.NewHTTPError(http.StatusNotFound, err.Error()).Wrap(err)
|
||||
}
|
||||
ci, err := user.ContactInfo(ctxt.Ctx())
|
||||
if err != nil {
|
||||
@@ -416,19 +416,24 @@ func ShowProfile(ctxt ui.AmContext) (string, any) {
|
||||
}
|
||||
var b strings.Builder
|
||||
if ci.Prefix != nil && *ci.Prefix != "" {
|
||||
b.WriteString(*ci.Prefix + " ")
|
||||
b.WriteString(*ci.Prefix)
|
||||
b.WriteString(" ")
|
||||
}
|
||||
if ci.GivenName != nil {
|
||||
b.WriteString(*ci.GivenName)
|
||||
}
|
||||
if ci.MiddleInit != nil && *ci.MiddleInit != "" && *ci.MiddleInit != " " {
|
||||
b.WriteString(" " + *ci.MiddleInit + ".")
|
||||
b.WriteString(" ")
|
||||
b.WriteString(*ci.MiddleInit)
|
||||
b.WriteString(".")
|
||||
}
|
||||
if ci.FamilyName != nil {
|
||||
b.WriteString(" " + *ci.FamilyName)
|
||||
b.WriteString(" ")
|
||||
b.WriteString(*ci.FamilyName)
|
||||
}
|
||||
if ci.Suffix != nil && *ci.Suffix != "" {
|
||||
b.WriteString(" " + *ci.Suffix)
|
||||
b.WriteString(" ")
|
||||
b.WriteString(*ci.Suffix)
|
||||
}
|
||||
ctxt.VarMap().Set("fullname", b.String())
|
||||
if user.Description != nil {
|
||||
@@ -460,7 +465,8 @@ func ShowProfile(ctxt ui.AmContext) (string, any) {
|
||||
b.WriteString(*ci.Region)
|
||||
}
|
||||
if ci.PostalCode != nil {
|
||||
b.WriteString(" " + *ci.PostalCode)
|
||||
b.WriteString(" ")
|
||||
b.WriteString(*ci.PostalCode)
|
||||
}
|
||||
ctxt.VarMap().Set("addrLast", b.String())
|
||||
if ci.Country != nil {
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"net"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -172,17 +171,6 @@ func Map[A, B any](in []A, fn func(A) B) []B {
|
||||
return rc
|
||||
}
|
||||
|
||||
// MyIPAddress returns the local IP address of this machine.
|
||||
func MyIPAddress() (net.IP, error) {
|
||||
conn, err := net.Dial("udp", "8.8.8.8:80")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer conn.Close()
|
||||
localAddr := conn.LocalAddr().(*net.UDPAddr)
|
||||
return localAddr.IP, nil
|
||||
}
|
||||
|
||||
// IIF is an "immediate-if" function returning its second argument if the first one is true, the third one if not.
|
||||
func IIF[A any](expr bool, v1, v2 A) A {
|
||||
if expr {
|
||||
|
||||
Reference in New Issue
Block a user