sonolus

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 23, 2026 License: MIT Imports: 23 Imported by: 0

README

Sonolus Server Go

基于 Go 和 Gin 的 Sonolus 服务器开发工具包,来自原实现 sonolus-express

链接

功能

  • 自动设置所有必要路由,符合 Sonolus 服务器规范。
  • 提供用于与各路由交互的处理器。
  • 加载由 sonolus-pack 创建的包。

安装

go get github.com/WindowsSov8forUs/sonolus-server-go

示例

package main

import (
	"log"
	"net/http"

	sonolus "github.com/WindowsSov8forUs/sonolus-server-go"
	"github.com/gin-gonic/gin"
)

func main() {
	server := sonolus.New(sonolus.Options{})
	if err := server.Load("./pack"); err != nil {
		log.Fatal(err)
	}

	share := gin.New()
	sonolus.InstallSpaShare(share, "./public")

	mux := http.NewServeMux()
	mux.Handle("/sonolus/", server.Handler())
	mux.Handle("/", share)

	port := "3000"
	log.Println("Server listening at port", port)
	if err := http.ListenAndServe(":"+port, mux); err != nil {
		log.Fatal(err)
	}
}

文档

Sonolus
New(options)

创建 Sonolus 服务器。

  • options.Address: 服务器地址(结尾不应包含 /)。
  • options.FallbackLocale: 当无法使用用户首选区域设置时使用的回退区域设置。
  • options.Configuration: 服务器配置。
  • options.Upload: 上传选项。
  • options[type]: 物品组配置,type 可以是:PostPlaylistLevelSkinBackgroundEffectParticleEngineReplayRoomUser
  • options[type].Creates: 物品组的创建表单。
  • options[type].Searches: 物品组的搜索表单。
  • options[type].Community.Actions: 物品组的社区操作表单。
Address

New(options) 传入。

FallbackLocale

New(options) 传入。

Router

已安装的 Gin router。

Title

服务器标题。

Description

服务器描述。

Banner

服务器横幅。

SessionHandler

认证会话处理器。

AuthenticateHandler

认证处理器。

ServerInfoHandler

请求服务器信息的处理器。

Multiplayer.CreateHandler

创建多人游戏房间的处理器。

Multiplayer.JoinHandler

加入多人游戏房间的处理器。

[type]

物品组。

type 可以是:PostPlaylistLevelSkinBackgroundEffectParticleEngineReplayRoomUser

[type].Items

物品。

可以动态修改。

[type].Creates

创建表单。

[type].Searches

搜索表单。

[type].InfoHandler

请求物品信息的处理器。

[type].ListHandler

请求物品列表的处理器。

[type].CreateHandler

创建物品的处理器。

[type].PreUploadHandler

上传物品的前置处理器。

[type].UploadHandler

上传物品的处理器。

[type].DetailsHandler

请求物品详情的处理器。

[type].SubmitActionHandler

提交物品操作的处理器。

[type].PreUploadActionHandler

上传物品操作的前置处理器。

[type].UploadActionHandler

上传物品操作的处理器。

[type].CommunityActions

社区操作表单。

[type].CommunityInfoHandler

请求物品社区信息的处理器。

[type].SubmitCommunityActionHandler

提交物品社区操作的处理器。

[type].PreUploadCommunityActionHandler

上传物品社区操作的前置处理器。

[type].UploadCommunityActionHandler

上传物品社区操作的处理器。

[type].CommunityCommentActions

社区评论操作表单。

[type].CommunityCommentListHandler

请求物品社区评论列表的处理器。

[type].SubmitCommunityCommentActionHandler

提交物品社区评论操作的处理器。

[type].PreUploadCommunityCommentActionHandler

上传物品社区评论操作的前置处理器。

[type].UploadCommunityCommentActionHandler

上传物品社区评论操作的处理器。

[type].LeaderboardDetailsHandler

请求物品排行榜详情的处理器。

[type].LeaderboardRecordListHandler

请求物品排行榜记录列表的处理器。

[type].LeaderboardRecordDetailsHandler

请求物品排行榜记录详情的处理器。

Load(path)

加载由 sonolus-pack 创建的包。

  • path: 包的文件路径。
AddBytes(data, hash)

添加字节资源。返回可用于访问资源的 Srl

  • data: 数据。
  • hash: 资源哈希,留空时会自动计算。
AddFile(path, hash)

添加文件资源。返回可用于访问资源的 Srl

  • path: 数据的文件路径。
  • hash: 资源哈希,留空时会自动计算。
Localize(text, locale)

使用目标区域设置和回退区域设置本地化文本。

  • text: 要本地化的文本。
  • locale: 目标区域设置。
InstallSpaShare(router, root)

创建 Sonolus SPA 分享服务器。

  • router: 要安装路由的 Gin router。
  • root: 静态 SPA 文件的根目录。
InstallRedirectShare(router, root)

创建 Sonolus 重定向分享服务器。

  • router: 要安装路由的 Gin router。
  • root: 重定向根路径。
CreateFilterItems(props...)(items, keywords)

按关键字过滤物品。

  • props: 要搜索的字段。
  • items: 物品。
  • keywords: 关键字。
PaginateItems(items, page, perPage)

按页码和每页数量分页物品。

  • items: 物品。
  • page: 页码。
  • perPage: 每页数量,传入 0 时默认为 20。
在 Go 中使用自定义字段

使用结构体嵌入:

type MyLevelItemModel struct {
	sonolus.LevelItemModel
	Meta struct {
		// 用户自定义元信息
	}
}

Documentation

Overview

Package sonolus 提供基于 Gin 的 Sonolus 服务器构建工具。

Index

Constants

This section is empty.

Variables

View Source
var ParseFormValue = model.ParseFormValue
View Source
var ParseFormsValue = model.ParseFormsValue
View Source
var ParseOptionsValue = model.ParseOptionsValue
View Source
var ParseSearchesValue = model.ParseSearchesValue
View Source
var SerializeRawFormValue = model.SerializeRawFormValue
View Source
var ToBackgroundItem = model.ToBackgroundItem
View Source
var ToEffectItem = model.ToEffectItem
View Source
var ToEngineItem = model.ToEngineItem
View Source
var ToLevelItem = model.ToLevelItem
View Source
var ToParticleItem = model.ToParticleItem
View Source
var ToPlaylistItem = model.ToPlaylistItem
View Source
var ToPostItem = model.ToPostItem
View Source
var ToReplayItem = model.ToReplayItem
View Source
var ToRoomItem = model.ToRoomItem
View Source
var ToServerForm = model.ToServerForm
View Source
var ToServerForms = model.ToServerForms
View Source
var ToSkinItem = model.ToSkinItem
View Source
var ToTags = model.ToTags
View Source
var ToUserItem = model.ToUserItem

Functions

func InstallRedirectShare

func InstallRedirectShare(router gin.IRouter, root string)

func InstallSpaShare

func InstallSpaShare(router gin.IRouter, root string)

func PaginateItems

func PaginateItems[T any](items []T, page int, perPage int) (int, []T)

Types

type BackgroundItemModel

type BackgroundItemModel = model.BackgroundItemModel

type BaseResolver

type BaseResolver = model.BaseResolver

type CollectionItemOption

type CollectionItemOption = model.CollectionItemOption

type CommunityCommentListHandler

type CommunityCommentListHandler func(Context, string, int, string) (CommunityCommentListModel, *ServerError)

type CommunityCommentListModel

type CommunityCommentListModel struct {
	PageCount int
	Cursor    string
	Comments  []CommunityCommentModel
}

type CommunityCommentModel

type CommunityCommentModel struct {
	Name       string
	Author     database.LocalizationText
	AuthorUser string
	Time       float64
	Content    database.LocalizationText
	Actions    PickForms
}

type CommunityInfoHandler

type CommunityInfoHandler func(Context, string) (CommunityInfoModel, *ServerError)

type CommunityInfoModel

type CommunityInfoModel struct {
	Actions     PickForms
	TopComments []CommunityCommentModel
}

type Configuration

type Configuration struct {
	Options OptionsModel
}

type Context

type Context struct {
	Session      string
	Localization string
	Options      map[string]any
	RawOptions   map[string]any
	// contains filtered or unexported fields
}

Context contains Sonolus request metadata extracted from headers and query.

func (Context) Localize

func (ctx Context) Localize(text database.LocalizationText) string

type EffectItemModel

type EffectItemModel = model.EffectItemModel

type EngineItemModel

type EngineItemModel = model.EngineItemModel

type FileOption

type FileOption = model.FileOption

type FilterItems

type FilterItems[T any] = model.FilterItems[T]

func CreateFilterItems

func CreateFilterItems[T any](props ...string) FilterItems[T]

type FormModel

type FormModel = model.FormModel

type FormValue

type FormValue = model.FormValue

type FormsModel

type FormsModel = model.FormsModel

type ItemDetailsHandler

type ItemDetailsHandler[T ItemModel] func(Context, string) (ItemDetailsModel[T], *ServerError)

type ItemDetailsModel

type ItemDetailsModel[T ItemModel] struct {
	Item         T
	Description  database.LocalizationText
	Actions      PickForms
	HasCommunity bool
	Leaderboards []coreserver.ServerItemLeaderboard
	Sections     []ItemSectionModel
}

type ItemGroup

type ItemGroup[T ItemModel, R any] struct {
	Type  core.ItemType
	Path  string
	Items []T

	Creates                 FormsModel
	Searches                FormsModel
	Actions                 FormsModel
	CommunityActions        FormsModel
	CommunityCommentActions FormsModel

	InfoHandler    ItemInfoHandler[T]
	ListHandler    ItemListHandler[T]
	DetailsHandler ItemDetailsHandler[T]

	CreateHandler          CreateItemHandler
	PreUploadHandler       PreUploadItemHandler
	UploadHandler          UploadItemHandler
	SubmitActionHandler    SubmitItemActionHandler
	PreUploadActionHandler PreUploadItemActionHandler
	UploadActionHandler    UploadItemActionHandler

	CommunityInfoHandler                   CommunityInfoHandler
	SubmitCommunityActionHandler           SubmitCommunityActionHandler
	PreUploadCommunityActionHandler        PreUploadCommunityActionHandler
	UploadCommunityActionHandler           UploadCommunityActionHandler
	CommunityCommentListHandler            CommunityCommentListHandler
	SubmitCommunityCommentActionHandler    SubmitCommunityCommentActionHandler
	PreUploadCommunityCommentActionHandler PreUploadCommunityCommentActionHandler
	UploadCommunityCommentActionHandler    UploadCommunityCommentActionHandler
	LeaderboardDetailsHandler              LeaderboardDetailsHandler
	LeaderboardRecordListHandler           LeaderboardRecordListHandler
	LeaderboardRecordDetailsHandler        LeaderboardRecordDetailsHandler

	ToItem ToItem[T, R]
	Filter FilterItems[T]
}

func NewItemGroup

func NewItemGroup[T ItemModel, R any](typ core.ItemType, path string, options ItemGroupOptions, toItem ToItem[T, R], filter FilterItems[T]) *ItemGroup[T, R]

type ItemGroupOptions

type ItemGroupOptions struct {
	Creates   FormsModel
	Searches  FormsModel
	Actions   FormsModel
	Community struct {
		Actions FormsModel
		Comment struct {
			Actions FormsModel
		}
	}
}

type ItemInfoHandler

type ItemInfoHandler[T ItemModel] func(Context, string) (ItemInfoModel[T], *ServerError)

type ItemInfoModel

type ItemInfoModel[T ItemModel] struct {
	Title            database.LocalizationText
	Creates          PickForms
	Searches         PickForms
	QuickSearchValue FormValue
	Sections         []ItemSectionModel
	Banner           *core.Srl
}

type ItemListHandler

type ItemListHandler[T ItemModel] func(Context, FormValue, int, string) (ItemListModel[T], *ServerError)

type ItemListModel

type ItemListModel[T ItemModel] struct {
	Title            database.LocalizationText
	PageCount        int
	Cursor           string
	Items            []T
	Searches         PickForms
	QuickSearchValue FormValue
}

type ItemModel

type ItemModel = model.ItemModel

type ItemSectionModel

type ItemSectionModel struct {
	Title       database.LocalizationText
	Icon        core.Icon
	Description database.LocalizationText
	Help        database.LocalizationText
	ItemType    core.ItemType
	Items       any
	Search      *ItemSectionSearch
}

type ItemSectionSearch

type ItemSectionSearch struct {
	Value FormValue
	Form  any
}

type LeaderboardDetailsHandler

type LeaderboardDetailsHandler func(Context, string, string) (LeaderboardDetailsModel, *ServerError)

type LeaderboardDetailsModel

type LeaderboardDetailsModel struct {
	TopRecords []LeaderboardRecordModel
}

type LeaderboardRecordDetailsModel

type LeaderboardRecordDetailsModel struct {
	Replays []ReplayItemModel
}

type LeaderboardRecordListModel

type LeaderboardRecordListModel struct {
	PageCount int
	Cursor    string
	Records   []LeaderboardRecordModel
}

type LeaderboardRecordModel

type LeaderboardRecordModel struct {
	Name       string
	Rank       database.LocalizationText
	Player     string
	PlayerUser string
	Value      database.LocalizationText
}

type LevelItemModel

type LevelItemModel = model.LevelItemModel

type LevelResult

type LevelResult struct {
	Submits          FormsModel
	InfoHandler      func(Context) (LevelResultInfoModel, *ServerError)
	SubmitHandler    func(Context, resource.ReplayItem, FormValue) (coreserver.ServerSubmitLevelResultResponse, *ServerError)
	PreUploadHandler func(Context, string) *ServerError
	UploadHandler    func(Context, string, []*UploadedFile) (coreserver.ServerUploadLevelResultResponse, *ServerError)
}

func NewLevelResult

func NewLevelResult(options LevelResultOptions) *LevelResult

type LevelResultInfoModel

type LevelResultInfoModel struct {
	Submits PickForms
}

type LevelResultOptions

type LevelResultOptions struct {
	Submits FormsModel
}

type Localize

type Localize = model.Localize

type MultiOption

type MultiOption = model.MultiOption

type MultiOptionValue

type MultiOptionValue = model.MultiOptionValue

type MultiOptionValueModel

type MultiOptionValueModel = model.MultiOptionValueModel

type OptionBase

type OptionBase = model.OptionBase

type OptionModel

type OptionModel = model.OptionModel

type Options

type Options struct {
	Address        string
	FallbackLocale string
	Configuration  Configuration
	Upload         UploadOptions

	Post        ItemGroupOptions
	Playlist    ItemGroupOptions
	Level       ItemGroupOptions
	Skin        ItemGroupOptions
	Background  ItemGroupOptions
	Effect      ItemGroupOptions
	Particle    ItemGroupOptions
	Engine      ItemGroupOptions
	Replay      ItemGroupOptions
	Room        ItemGroupOptions
	User        ItemGroupOptions
	LevelResult LevelResultOptions
}

type OptionsModel

type OptionsModel = model.OptionsModel

type ParticleItemModel

type ParticleItemModel = model.ParticleItemModel

type PickForms

type PickForms = model.PickForms

type PlaylistItemModel

type PlaylistItemModel = model.PlaylistItemModel

type PostItemModel

type PostItemModel = model.PostItemModel

type PreUploadCommunityActionHandler

type PreUploadCommunityActionHandler func(Context, string, string) *ServerError

type PreUploadCommunityCommentActionHandler

type PreUploadCommunityCommentActionHandler func(Context, string, string, string) *ServerError

type PreUploadItemActionHandler

type PreUploadItemActionHandler func(Context, string, string) *ServerError

type PreUploadItemHandler

type PreUploadItemHandler func(Context, string) *ServerError

type ReplayItemModel

type ReplayItemModel = model.ReplayItemModel

type RoomItemModel

type RoomItemModel = model.RoomItemModel

type SelectOption

type SelectOption = model.SelectOption

type SelectOptionValue

type SelectOptionValue = model.SelectOptionValue

type ServerError

type ServerError struct {
	Status  int
	Message database.LocalizationText
}

func MessageError

func MessageError(status int, message database.LocalizationText) *ServerError

func StatusError

func StatusError(status int) *ServerError

type ServerInfoHandler

type ServerInfoHandler func(Context) (ServerInfoModel, *ServerError)

type ServerInfoModel

type ServerInfoModel struct {
	Title         database.LocalizationText
	Description   database.LocalizationText
	Buttons       []coreserver.ServerInfoButton
	Configuration struct {
		Options OptionsModel
	}
	Banner *core.Srl
}

type ServerItemOption

type ServerItemOption = model.ServerItemOption

type ServerItemsOption

type ServerItemsOption = model.ServerItemsOption

type SessionHandler

type SessionHandler func(Context) *ServerError

type SkinItemModel

type SkinItemModel = model.SkinItemModel

type SliderOption

type SliderOption = model.SliderOption

type Sonolus

func New

func New(options Options) *Sonolus

func (*Sonolus) AddBytes

func (s *Sonolus) AddBytes(data []byte, hash string) core.Srl

func (*Sonolus) AddFile

func (s *Sonolus) AddFile(path string, hash string) (core.Srl, error)

func (*Sonolus) AddressValue

func (s *Sonolus) AddressValue() string

func (*Sonolus) BackgroundItems

func (s *Sonolus) BackgroundItems() []BackgroundItemModel

func (*Sonolus) EffectItems

func (s *Sonolus) EffectItems() []EffectItemModel

func (*Sonolus) EngineItems

func (s *Sonolus) EngineItems() []EngineItemModel

func (*Sonolus) FallbackLocaleValue

func (s *Sonolus) FallbackLocaleValue() string

func (*Sonolus) Handler

func (s *Sonolus) Handler() http.Handler

func (*Sonolus) Install

func (s *Sonolus) Install(router gin.IRouter)

func (*Sonolus) LevelItems

func (s *Sonolus) LevelItems() []LevelItemModel

func (*Sonolus) Load

func (s *Sonolus) Load(path string) error

func (*Sonolus) Localize

func (s *Sonolus) Localize(text database.LocalizationText, locale string) string

func (*Sonolus) ParticleItems

func (s *Sonolus) ParticleItems() []ParticleItemModel

func (*Sonolus) PlaylistItems

func (s *Sonolus) PlaylistItems() []PlaylistItemModel

func (*Sonolus) PostItems

func (s *Sonolus) PostItems() []PostItemModel

func (*Sonolus) ReplayItems

func (s *Sonolus) ReplayItems() []ReplayItemModel

func (*Sonolus) RoomItems

func (s *Sonolus) RoomItems() []RoomItemModel

func (*Sonolus) SkinItems

func (s *Sonolus) SkinItems() []SkinItemModel

func (*Sonolus) UserItems

func (s *Sonolus) UserItems() []UserItemModel

type TextAreaOption

type TextAreaOption = model.TextAreaOption

type TextOption

type TextOption = model.TextOption

type ToItem

type ToItem[T ItemModel, R any] = model.ToItem[T, R]

type ToggleOption

type ToggleOption = model.ToggleOption

type UploadOptions

type UploadOptions struct {
	MaxMemory int64
	MaxCount  int
}

type UploadedFile

type UploadedFile struct {
	FieldName string
	FileName  string
	Size      int64
	Data      []byte
}

type UserItemModel

type UserItemModel = model.UserItemModel

Directories

Path Synopsis
Package model provides public data models, form parsing, option parsing, and item conversion helpers used by sonolus-server-go.
Package model provides public data models, form parsing, option parsing, and item conversion helpers used by sonolus-server-go.
Package share provides Gin route installers for Sonolus web share behavior.
Package share provides Gin route installers for Sonolus web share behavior.

Jump to

Keyboard shortcuts

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