Documentation
¶
Overview ¶
Example (ApiMarkup) ¶
Example_apiMarkup demonstrates the HTML cleaning function.
package main
import (
"fmt"
"github.com/Defacto2/server/handler/api"
)
// Example HTML with various attributes and tags.
const src = `<div class="content" id="main">
<h1 style="color: blue;">Welcome</h1>
<p class="intro">This is <strong>important</strong> content with <a href="https://example.com" class="external" title="Visit Example">a link</a>.</p>
<p>More content with <span data-info="test">data attributes</span> and <a name="anchor">anchor without href</a>.</p>
</div>`
func main() {
cleaned := api.Markup(src)
fmt.Println("Original HTML:")
fmt.Println(src)
fmt.Println("\nCleaned HTML (preserves structure, removes presentation):")
fmt.Println(cleaned)
}
Output: Original HTML: <div class="content" id="main"> <h1 style="color: blue;">Welcome</h1> <p class="intro">This is <strong>important</strong> content with <a href="https://example.com" class="external" title="Visit Example">a link</a>.</p> <p>More content with <span data-info="test">data attributes</span> and <a name="anchor">anchor without href</a>.</p> </div> Cleaned HTML (preserves structure, removes presentation): <div><h1>Welcome</h1><p>This is <strong>important</strong> content with <a href="https://example.com">a link</a>.</p><p>More content with <span>data attributes</span> and anchor without href.</p></div>
Example (Cleaning) ¶
Example_cleaning demonstrates the HTML cleaning function.
package main
import (
"fmt"
"github.com/Defacto2/server/handler/internal/simple"
)
// Example HTML with various attributes and tags.
const src = `<div class="content" id="main">
<h1 style="color: blue;">Welcome</h1>
<p class="intro">This is <strong>important</strong> content with <a href="https://example.com" class="external" title="Visit Example">a link</a>.</p>
<p>More content with <span data-info="test">data attributes</span> and <a name="anchor">anchor without href</a>.</p>
</div>`
func main() {
plain := simple.CleanHTML(src)
fmt.Println("\nPlain text (all HTML tags removed):")
fmt.Println(plain)
}
Output: Plain text (all HTML tags removed): Welcome This is important content with a link. More content with data attributes and anchor without href.
Example (ErrorHandling) ¶
Example_errorHandling demonstrates error cases.
package main
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"github.com/Defacto2/server/handler/api"
"github.com/labstack/echo/v5"
)
func main() {
e := echo.New()
apiGroup := e.Group("/api/v1")
apiGroup.GET("/milestones/year/:year", api.MilestoneYearAPI)
apiGroup.GET("/milestones/range/:range", api.MilestoneYearsAPI)
// Example 1: Invalid year format
req := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/api/v1/milestones/year/invalid", nil)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
fmt.Printf("Invalid year format: Status %d\n", rec.Code)
// Example 2: Invalid range format
req = httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/api/v1/milestones/range/invalid", nil)
rec = httptest.NewRecorder()
e.ServeHTTP(rec, req)
fmt.Printf("Invalid range format: Status %d\n", rec.Code)
// Example 3: Start year > end year
req = httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/api/v1/milestones/range/2000-1990", nil)
rec = httptest.NewRecorder()
e.ServeHTTP(rec, req)
fmt.Printf("Start year > end year: Status %d\n", rec.Code)
}
Output: Invalid year format: Status 400 Invalid range format: Status 400 Start year > end year: Status 400
Example (HtmlCleaning) ¶
Example_htmlCleaning demonstrates the HTML cleaning functions.
package main
import (
"fmt"
"github.com/Defacto2/server/handler/api"
"github.com/Defacto2/server/handler/internal/simple"
)
func main() {
html := `<p class="test">This has <a href="https://example.com" class="link">a link</a> and <span style="color: red;">formatting</span>.</p>`
cleaned := api.Markup(html)
plain := simple.CleanHTML(html)
fmt.Println("✓ HTML Cleaning Functions:")
fmt.Printf(" Original: %s\n", html)
fmt.Printf(" Cleaned: %s\n", cleaned)
fmt.Printf(" Plain: %s\n", plain)
}
Output: ✓ HTML Cleaning Functions: Original: <p class="test">This has <a href="https://example.com" class="link">a link</a> and <span style="color: red;">formatting</span>.</p> Cleaned: <p>This has <a href="https://example.com">a link</a> and <span>formatting</span>.</p> Plain: This has a link and formatting.
Example (MilestonesAll) ¶
Example_milestonesAll demonstrates getting all milestones.
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"github.com/Defacto2/server/handler/api"
"github.com/Defacto2/server/handler/app"
"github.com/labstack/echo/v5"
)
func main() {
e := echo.New()
apiGroup := e.Group("/api/v1")
apiGroup.GET("/milestones", api.MilestonesAPI)
req := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/api/v1/milestones", nil)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
switch rec.Code {
case http.StatusOK:
fmt.Println("✓ GET /api/v1/milestones - Success")
// Parse the response
var milestones app.Milestones
if err := json.NewDecoder(rec.Body).Decode(&milestones); err == nil {
fmt.Printf(" Found %d milestones\n", len(milestones))
if len(milestones) > 0 {
fmt.Printf(" First milestone year: %d\n", milestones[0].Year)
fmt.Printf(" Has clean HTML content: %t\n", len(milestones[0].ContentHTML) > 0)
fmt.Printf(" Has plain text content: %t\n", len(milestones[0].Content) > 0)
}
}
default:
fmt.Printf("✗ GET /api/v1/milestones - Failed with status %d\n", rec.Code)
}
}
Output: ✓ GET /api/v1/milestones - Success Found 122 milestones First milestone year: 1971 Has clean HTML content: true Has plain text content: true
Example (MilestonesDecade) ¶
Example_milestonesDecade demonstrates getting milestones by decade.
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"github.com/Defacto2/server/handler/api"
"github.com/Defacto2/server/handler/app"
"github.com/labstack/echo/v5"
)
func main() {
e := echo.New()
apiGroup := e.Group("/api/v1")
apiGroup.GET("/milestones/decade/:decade", api.MilestoneDecadeAPI)
req := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/api/v1/milestones/decade/1990s", nil)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
switch rec.Code {
case http.StatusOK:
fmt.Println("✓ GET /api/v1/milestones/decade/1990s - Success")
var milestones app.Milestones
if err := json.NewDecoder(rec.Body).Decode(&milestones); err == nil {
fmt.Printf(" Found %d milestones from the 1990s\n", len(milestones))
}
case http.StatusNotFound:
fmt.Println("✓ GET /api/v1/milestones/decade/1990s - No milestones found (expected)")
default:
fmt.Printf("✗ GET /api/v1/milestones/decade/1990s - Failed with status %d\n", rec.Code)
}
}
Output: ✓ GET /api/v1/milestones/decade/1990s - Success Found 35 milestones from the 1990s
Example (MilestonesHighlights) ¶
Example_milestonesHighlights demonstrates getting highlighted milestones.
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"github.com/Defacto2/server/handler/api"
"github.com/Defacto2/server/handler/app"
"github.com/labstack/echo/v5"
)
func main() {
e := echo.New()
apiGroup := e.Group("/api/v1")
apiGroup.GET("/milestones/highlights", api.MilestoneHighlightsAPI)
req := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/api/v1/milestones/highlights", nil)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
switch rec.Code {
case http.StatusOK:
fmt.Println("✓ GET /api/v1/milestones/highlights - Success")
var milestones app.Milestones
if err := json.NewDecoder(rec.Body).Decode(&milestones); err == nil {
fmt.Printf(" Found %d highlighted milestones\n", len(milestones))
}
case http.StatusNotFound:
fmt.Println("✓ GET /api/v1/milestones/highlights - No highlighted milestones found (expected)")
default:
fmt.Printf("✗ GET /api/v1/milestones/highlights - Failed with status %d\n", rec.Code)
}
}
Output: ✓ GET /api/v1/milestones/highlights - Success Found 32 highlighted milestones
Example (MilestonesRange) ¶
Example_milestonesRange demonstrates getting milestones by year range.
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"github.com/Defacto2/server/handler/api"
"github.com/Defacto2/server/handler/app"
"github.com/labstack/echo/v5"
)
func main() {
e := echo.New()
apiGroup := e.Group("/api/v1")
apiGroup.GET("/milestones/range/:range", api.MilestoneYearsAPI)
req := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/api/v1/milestones/range/1990-2000", nil)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
switch rec.Code {
case http.StatusOK:
fmt.Println("✓ GET /api/v1/milestones/range/1990-2000 - Success")
var milestones app.Milestones
if err := json.NewDecoder(rec.Body).Decode(&milestones); err == nil {
fmt.Printf(" Found %d milestones in range 1990-2000\n", len(milestones))
}
case http.StatusNotFound:
fmt.Println("✓ GET /api/v1/milestones/range/1990-2000 - No milestones found (expected)")
default:
fmt.Printf("✗ GET /api/v1/milestones/range/1990-2000 - Failed with status %d\n", rec.Code)
}
}
Output: ✓ GET /api/v1/milestones/range/1990-2000 - Success Found 38 milestones in range 1990-2000
Example (MilestonesYear) ¶
Example_milestonesYear demonstrates getting milestones by year.
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"github.com/Defacto2/server/handler/api"
"github.com/Defacto2/server/handler/app"
"github.com/labstack/echo/v5"
)
func main() {
e := echo.New()
apiGroup := e.Group("/api/v1")
apiGroup.GET("/milestones/year/:year", api.MilestoneYearAPI)
req := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/api/v1/milestones/year/1995", nil)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
switch rec.Code {
case http.StatusOK:
fmt.Println("✓ GET /api/v1/milestones/year/1995 - Success")
var milestones app.Milestones
if err := json.NewDecoder(rec.Body).Decode(&milestones); err == nil {
fmt.Printf(" Found %d milestones for year 1995\n", len(milestones))
}
case http.StatusNotFound:
fmt.Println("✓ GET /api/v1/milestones/year/1995 - No milestones found (expected)")
default:
fmt.Printf("✗ GET /api/v1/milestones/year/1995 - Failed with status %d\n", rec.Code)
}
}
Output: ✓ GET /api/v1/milestones/year/1995 - Success Found 3 milestones for year 1995
Index ¶
- Constants
- func AreaCodeAPI(c *echo.Context) error
- func AreacodeSearchAPI(c *echo.Context) error
- func AreacodesAPI(c *echo.Context) error
- func ArtifactAPIs(sl *slog.Logger, c *echo.Context, db *sql.DB, uri string) error
- func ArtifactsAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error
- func ArtifactsNewAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error
- func ArtistsAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error
- func BoardsAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error
- func CategoriesAPI(c *echo.Context, db *sql.DB) error
- func CategoryAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error
- func CodersAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error
- func DemozooAPI(c *echo.Context) error
- func FileAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error
- func GroupsAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error
- func MagazinesAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error
- func Markup(src string) string
- func MilestoneDecadeAPI(c *echo.Context) error
- func MilestoneHighlightsAPI(c *echo.Context) error
- func MilestoneYearAPI(c *echo.Context) error
- func MilestoneYearsAPI(c *echo.Context) error
- func MilestonesAPI(c *echo.Context) error
- func MusiciansAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error
- func PlatformAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error
- func PlatformsAPI(c *echo.Context, db *sql.DB) error
- func RegionAPI(c *echo.Context) error
- func RegionsAPI(c *echo.Context) error
- func ReleaserAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error
- func ScenerAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error
- func ScenersAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error
- func SitesAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error
- func TagAPI(sl *slog.Logger, c *echo.Context, db *sql.DB, name string) error
- func TagsAPI(c *echo.Context, db *sql.DB, category, platform bool) error
- func WebsitesAPI(c *echo.Context) error
- func WritersAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error
- type ArtifactAPI
- type EntityAPI
Examples ¶
Constants ¶
const ( APIBase = "/api/v1" // API Base URI (any changes need to be reflected in apiinfo.tmpl) APIVer = "1.0.1" // API Version gets shown in the HTTP header replies )
Variables ¶
This section is empty.
Functions ¶
func AreaCodeAPI ¶
AreaCodeAPI returns details for a specific area code.
func AreacodeSearchAPI ¶
AreacodeSearchAPI searches for area codes or regions by query.
func AreacodesAPI ¶
AreacodesAPI returns all North American Numbering Plan (NANP) area codes.
func ArtifactAPIs ¶
ArtifactAPIs returns a list of all files filtered by the provided uri string.
func ArtifactsAPI ¶
ArtifactsAPI returns a list of all files ordered by "oldest".
func ArtifactsNewAPI ¶
ArtifactsNewAPI returns a list of all files ordered by "new-uploads".
func CategoriesAPI ¶
CategoriesAPI returns all categories.
func CategoryAPI ¶
CategoryAPI returns a list of files from any category tag.
func DemozooAPI ¶
DemozooAPI returns a list of all groups with their Demozoo IDs.
func MagazinesAPI ¶
MagazinesAPI is the handler for the magazines API endpoint.
func Markup ¶
Markup removes CSS classes and attributes from HTML for API responses. Keeps semantic HTML tags but removes presentation-specific markup.
func MilestoneDecadeAPI ¶
MilestoneDecadeAPI returns milestones for a specific decade.
func MilestoneHighlightsAPI ¶
MilestoneHighlightsAPI returns only highlighted milestones.
func MilestoneYearAPI ¶
MilestoneYearAPI returns milestones for a specific year.
func MilestoneYearsAPI ¶
MilestoneYearsAPI returns milestones within a year range.
func MilestonesAPI ¶
MilestonesAPI returns all milestones.
func PlatformAPI ¶
PlatformAPI returns a list of files from any category tag.
func PlatformsAPI ¶
PlatformsAPI returns all platforms.
func RegionsAPI ¶
RegionsAPI returns all region in the North American Numbering Plan (NANP).
func ReleaserAPI ¶
ReleaserAPI returns details for a specific releaser or group.
func ScenersAPI ¶
ScenersAPI returns a list of Sceners ScenersAPI builds the ReleaserAPI list from model data.
func TagsAPI ¶
TagsAPI returns artifact tags.
- Set categories true to return all categories.
- Set platform true to return all platforms.
- Set both to true to return all category and platform tags.
Setting both to false will return an empty JSON response.
func WebsitesAPI ¶
WebsitesAPI returns all websites from the website page.
Types ¶
type ArtifactAPI ¶
type ArtifactAPI struct {
Summary artifactAPI `json:"artifact"`
FileMeta filemetaAPI `json:"download"`
ArtMeta artmetaAPI `json:"meta"`
Relationships []relationAPI `json:"relationships"`
}
ArtifactAPI represents an artifact file for API responses.
type EntityAPI ¶
type EntityAPI struct {
ID string `json:"id"`
Name string `json:"name"`
Title string `json:"title"`
URLs enityURLs `json:"urls"`
Stats totalsAPI `json:"statistics"`
Websites any `json:"websites,omitempty"`
Sixteen string `json:"sixteen,omitempty"`
Janeway string `json:"janeway,omitempty"`
Demozoo string `json:"demozoo,omitempty"`
Csdb string `json:"csdb,omitempty"`
}
EntityAPI represents a group, scener, or releaser for API responses.
func ReleasersAPI ¶
ReleasersAPI builds the ReleaserAPI list from model data.