config

package
v0.0.0-...-15592ba Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2023 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package config contains types and constants related to server configuration.

Index

Constants

View Source
const (
	DatastoreKind    = "Config"
	DatastoreKeyName = "active"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Users contains information about users who can access the server.
	Users []User `json:"users"`

	// SongBucket contains the name of the Google Cloud Storage bucket holding song files.
	SongBucket string `json:"songBucket,omitempty"`
	// CoverBucket contains the name of the Google Cloud Storage bucket holding album cover images.
	CoverBucket string `json:"coverBucket,omitempty"`

	// SongBaseURL contains the slash-terminated URL under which song files are stored.
	// This is used for testing.
	// Exactly one of SongBucket and SongBaseURL must be set.
	SongBaseURL string `json:"songBaseUrl,omitempty"`
	// CoverBaseURL contains the slash-terminated URL under which album cover images are stored.
	// This is used for testing.
	// Exactly one of CoverBucket and CoverBaseURL must be set.
	CoverBaseURL string `json:"coverBaseUrl,omitempty"`

	// Presets contains default search presets.
	Presets []SearchPreset `json:"presets"`

	// Minify describes whether the server should minify JavaScript, HTML, and CSS code
	// and bundle all JavaScript code into a single file. Defaults to true if unset.
	Minify *bool `json:"minify"`

	// MaxGuestSongRequestsPerHour contains the maximum rate at which each guest
	// user can send requests to the /song endpoint. Unlimited if 0 or negative.
	MaxGuestSongRequestsPerHour int `json:"maxGuestSongRequestsPerHour,omitempty"`
}

Config holds the App Engine server's configuration.

func Load

func Load(ctx context.Context) (*Config, error)

Load attempts to load the server's config from various locations. ctx must be an App Engine context.

func Parse

func Parse(jsonData []byte) (*Config, error)

Parse unmarshals jsonData, validates it, and returns the resulting config.

func (*Config) GetUser

func (cfg *Config) GetUser(req *http.Request) (user *User, name string)

GetUser attempts to find the user from cfg.Users that sent req. This method does not identify cron requests; use GetUserType for that. The returned User object is a shallow copy of the entry from cfg with its Password field cleared. If the request was unauthenticated or the user is not listed in cfg.Users, nil is returned. A username or email address that can be used in logging is returned if possible, even if the the request is not from a known user.

func (*Config) GetUserType

func (cfg *Config) GetUserType(req *http.Request) (utype UserType, name string)

GetUserType returns a UserType describing the user who sent req. If the request was unauthenticated or the user is not listed in cfg.Users, 0 is returned. A username or email address that can be used in logging is returned if possible, even if the the request is not from a known user.

type SavedConfig

type SavedConfig struct {
	JSON string `datastore:"json,noindex"`
}

SavedConfig is used to store a JSON-marshaled Config in Datastore.

type SearchPreset

type SearchPreset struct {
	// Name contains a human-readable name describing the preset.
	Name string `json:"name"`
	// Tags contains a space-separated tag expression, e.g. "guitar -banjo".
	Tags string `json:"tags"`
	// MinRating contains a minimum rating as number of stars in [1, 5].
	MinRating int `json:"minRating"`
	// Unrated specifies that only unrated songs should be returned.
	Unrated bool `json:"unrated"`
	// FirstPlayed limits results to songs first played within the given interval:
	//   0 - no restriction
	//   1 - last day
	//   2 - last week
	//   3 - last month
	//   4 - last three months
	//   5 - last six months
	//   6 - last year
	//   7 - last three years
	//   8 - last five years
	FirstPlayed int `json:"firstPlayed"`
	// LastPlayed limits results to songs last played before the given interval.
	// See FirstPlayed for values.
	LastPlayed int `json:"lastPlayed"`
	// OrderByLastPlayed specifies that songs should be ordered by the last time
	// they were played (in ascending order).
	OrderByLastPlayed bool `json:"orderByLastPlayed"`
	// MaxPlays specifies the maximum number of times that each song has been played.
	// -1 specifies that there is no restriction on the number of plays.
	MaxPlays int `json:"maxPlays"`
	// FirstTrack specifies that only albums' first tracks should be returned.
	FirstTrack bool `json:"firstTrack"`
	// Shuffle specifies that the returned songs should be shuffled.
	Shuffle bool `json:"shuffle"`
	// Play specifies that returned songs should be played automatically.
	// The current playlist is replaced.
	Play bool `json:"play"`
}

SearchPreset specifies a search preset to display.

func (*SearchPreset) UnmarshalJSON

func (p *SearchPreset) UnmarshalJSON(data []byte) error

type User

type User struct {
	// Email contains an email address for Google authentication, used for the web interface.
	Email string `json:"email"`

	// Username contains a username for HTTP basic auth, used by the Android client and the nup command-line executable.
	Username string `json:"username"`
	// Password contains a password for HTTP basic auth.
	Password string `json:"password"`

	// Admin is true if this user should have elevated permissions.
	// This should only be set for the HTTP basic auth account used by the nup command-line executable.
	Admin bool `json:"admin"`
	// Guest is true if this user should have reduced permissions.
	// Guest users are not allowed to rate/tag songs or report plays.
	// This can be set for HTTP basic auth accounts used by the Android app.
	// Guest mode is not supported for email accounts (i.e. the web interface).
	Guest bool `json:"guest"`

	// Presets contains custom search presets for this user.
	// If empty, Config.Presets will be used instead.
	Presets []SearchPreset `json:"presets"`

	// ExcludedTags contains a list of tags used to filter songs.
	ExcludedTags []string `json:"excludedTags"`
}

User contains information about a user allowed to access the server.

func (*User) Name

func (u *User) Name() string

Name returns a human-readable string identifying u.

func (*User) Type

func (u *User) Type() UserType

Type returns u's type.

type UserType

type UserType uint32

UserType describes the level of access granted to a user. A given user will have a single type, but UserType values can be masked together to make it easier to check permissions. 0 represents a non-user.

const (
	// NormalUser indicates a non-admin, non-guest user.
	NormalUser UserType = 1 << iota
	// AdminUser indicates a user with its Admin field set to true.
	AdminUser
	// GuestUser indicates a user with its Guest field set to true.
	GuestUser
	// CronUser indicates a request issued by App Engine cron jobs.
	CronUser
)

Jump to

Keyboard shortcuts

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