api

package
v1.14.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 12, 2026 License: GPL-3.0 Imports: 34 Imported by: 0

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

Examples

Constants

View Source
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

func AreaCodeAPI(c *echo.Context) error

AreaCodeAPI returns details for a specific area code.

func AreacodeSearchAPI

func AreacodeSearchAPI(c *echo.Context) error

AreacodeSearchAPI searches for area codes or regions by query.

func AreacodesAPI

func AreacodesAPI(c *echo.Context) error

AreacodesAPI returns all North American Numbering Plan (NANP) area codes.

func ArtifactAPIs

func ArtifactAPIs(sl *slog.Logger, c *echo.Context, db *sql.DB, uri string) error

ArtifactAPIs returns a list of all files filtered by the provided uri string.

func ArtifactsAPI

func ArtifactsAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error

ArtifactsAPI returns a list of all files ordered by "oldest".

func ArtifactsNewAPI

func ArtifactsNewAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error

ArtifactsNewAPI returns a list of all files ordered by "new-uploads".

func ArtistsAPI

func ArtistsAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error

func BoardsAPI

func BoardsAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error

BoardsAPI is the handler for the BBS API endpoint.

func CategoriesAPI

func CategoriesAPI(c *echo.Context, db *sql.DB) error

CategoriesAPI returns all categories.

func CategoryAPI

func CategoryAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error

CategoryAPI returns a list of files from any category tag.

func CodersAPI

func CodersAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error

func DemozooAPI

func DemozooAPI(c *echo.Context) error

DemozooAPI returns a list of all groups with their Demozoo IDs.

func FileAPI

func FileAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error

FileAPI returns a single file by its obfuscated ID.

func GroupsAPI

func GroupsAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error

GroupsAPI returns a list of all releasers/groups with pagination.

func MagazinesAPI

func MagazinesAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error

MagazinesAPI is the handler for the magazines API endpoint.

func Markup

func Markup(src string) string

Markup removes CSS classes and attributes from HTML for API responses. Keeps semantic HTML tags but removes presentation-specific markup.

func MilestoneDecadeAPI

func MilestoneDecadeAPI(c *echo.Context) error

MilestoneDecadeAPI returns milestones for a specific decade.

func MilestoneHighlightsAPI

func MilestoneHighlightsAPI(c *echo.Context) error

MilestoneHighlightsAPI returns only highlighted milestones.

func MilestoneYearAPI

func MilestoneYearAPI(c *echo.Context) error

MilestoneYearAPI returns milestones for a specific year.

func MilestoneYearsAPI

func MilestoneYearsAPI(c *echo.Context) error

MilestoneYearsAPI returns milestones within a year range.

func MilestonesAPI

func MilestonesAPI(c *echo.Context) error

MilestonesAPI returns all milestones.

func MusiciansAPI

func MusiciansAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error

func PlatformAPI

func PlatformAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error

PlatformAPI returns a list of files from any category tag.

func PlatformsAPI

func PlatformsAPI(c *echo.Context, db *sql.DB) error

PlatformsAPI returns all platforms.

func RegionAPI

func RegionAPI(c *echo.Context) error

RegionAPI returns a specific region by its abbreviation.

func RegionsAPI

func RegionsAPI(c *echo.Context) error

RegionsAPI returns all region in the North American Numbering Plan (NANP).

func ReleaserAPI

func ReleaserAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error

ReleaserAPI returns details for a specific releaser or group.

func ScenerAPI

func ScenerAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error

ScenerAPI returns details for a specific scener.

func ScenersAPI

func ScenersAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error

ScenersAPI returns a list of Sceners ScenersAPI builds the ReleaserAPI list from model data.

func SitesAPI

func SitesAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error

SitesAPI is the handler for the FTP sites API endpoint.

func TagAPI

func TagAPI(sl *slog.Logger, c *echo.Context, db *sql.DB, name string) error

TagAPI returns a list of files from any category or platform tag.

func TagsAPI

func TagsAPI(c *echo.Context, db *sql.DB, category, platform bool) error

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

func WebsitesAPI(c *echo.Context) error

WebsitesAPI returns all websites from the website page.

func WritersAPI

func WritersAPI(sl *slog.Logger, c *echo.Context, db *sql.DB) error

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

func ReleasersAPI(rels model.Releasers) []EntityAPI

ReleasersAPI builds the ReleaserAPI list from model data.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL