godrive

package
v0.0.0-...-16e10c0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2023 License: Apache-2.0 Imports: 40 Imported by: 0

Documentation

Index

Constants

View Source
const SessionCookieName = "X-Session-ID"

Variables

View Source
var (
	ErrFileNotFound      = errors.New("file not found")
	ErrFileAlreadyExists = errors.New("file already exists")
	ErrUserNotFound      = errors.New("user not found")
)
View Source
var UserInfoKey = authKey{}

Functions

func FormatBuildVersion

func FormatBuildVersion(version string, commit string, buildTime time.Time) string

Types

type Auth

type Auth struct {
	Verifier *oidc.IDTokenVerifier
	Config   *oauth2.Config
	Provider *oidc.Provider

	// session id <-> id token
	Sessions   map[string]*Session
	SessionsMu sync.Mutex
	// state <-> nonce
	States   map[string]string
	StatesMu sync.Mutex
}

type AuthAction

type AuthAction string
const (
	AuthActionDeny  AuthAction = "deny"
	AuthActionAllow AuthAction = "allow"
	AuthActionLogin AuthAction = "login"
)

type AuthConfig

type AuthConfig struct {
	Secure               bool          `cfg:"secure"`
	Issuer               string        `cfg:"issuer"`
	ClientID             string        `cfg:"client_id"`
	ClientSecret         string        `cfg:"client_secret"`
	RedirectURL          string        `cfg:"redirect_url"`
	RefreshTokenLifespan time.Duration `cfg:"refresh_token_lifespan"`
	DefaultHome          string        `cfg:"default_home"`
	Groups               AuthGroups    `cfg:"groups"`
}

func (AuthConfig) String

func (c AuthConfig) String() string

type AuthGroups

type AuthGroups struct {
	Admin  string `cfg:"admin"`
	User   string `cfg:"user"`
	Viewer string `cfg:"viewer"`
	Guest  bool   `cfg:"guest"`
}

func (AuthGroups) String

func (c AuthGroups) String() string

type BaseVariables

type BaseVariables struct {
	Theme string
	Auth  bool
	User  TemplateUser
}

type Config

type Config struct {
	Log        LogConfig      `cfg:"log"`
	DevMode    bool           `cfg:"dev_mode"`
	Debug      bool           `cfg:"debug"`
	ListenAddr string         `cfg:"listen_addr"`
	Database   DatabaseConfig `cfg:"database"`
	Storage    StorageConfig  `cfg:"storage"`
	Auth       *AuthConfig    `cfg:"auth"`
	Otel       *OtelConfig    `cfg:"otel"`
}

func (Config) String

func (c Config) String() string

type DB

type DB struct {
	// contains filtered or unexported fields
}

func NewDB

func NewDB(ctx context.Context, cfg DatabaseConfig, schema string) (*DB, error)

func (*DB) Close

func (d *DB) Close() error

func (*DB) CreateFile

func (d *DB) CreateFile(ctx context.Context, path string, size uint64, contentType string, description string, userID string) (*File, error)

func (*DB) DeleteFile

func (d *DB) DeleteFile(ctx context.Context, path string) error

func (*DB) FindFiles

func (d *DB) FindFiles(ctx context.Context, path string) ([]File, error)

func (*DB) GetAllUsers

func (d *DB) GetAllUsers(ctx context.Context) ([]User, error)

func (*DB) GetFile

func (d *DB) GetFile(ctx context.Context, path string) (*File, error)

func (*DB) GetUser

func (d *DB) GetUser(ctx context.Context, id string) (*User, error)

func (*DB) GetUserByName

func (d *DB) GetUserByName(ctx context.Context, username string) (*User, error)

func (*DB) GetUsers

func (d *DB) GetUsers(ctx context.Context, ids []string) ([]User, error)

func (*DB) UpdateFile

func (d *DB) UpdateFile(ctx context.Context, path string, newPath string, size uint64, contentType string, description string) error

func (*DB) UpsertUser

func (d *DB) UpsertUser(ctx context.Context, id string, username string, email string, home string) error

type DatabaseConfig

type DatabaseConfig struct {
	Type  DatabaseType `cfg:"type"`
	Debug bool         `cfg:"debug"`

	// SQLite
	Path string `cfg:"path"`

	// PostgreSQL
	Host     string `cfg:"host"`
	Port     int    `cfg:"port"`
	Username string `cfg:"username"`
	Password string `cfg:"password"`
	Database string `cfg:"database"`
	SSLMode  string `cfg:"ssl_mode"`
}

func (DatabaseConfig) PostgresDataSourceName

func (c DatabaseConfig) PostgresDataSourceName() string

func (DatabaseConfig) String

func (c DatabaseConfig) String() string

type DatabaseType

type DatabaseType string
const (
	DatabaseTypePostgres DatabaseType = "postgres"
	DatabaseTypeSQLite   DatabaseType = "sqlite"
)

type ErrorResponse

type ErrorResponse struct {
	Message   string `json:"message"`
	Status    int    `json:"status"`
	Path      string `json:"path"`
	RequestID string `json:"request_id"`
}

type ExecuteTemplateFunc

type ExecuteTemplateFunc func(w io.Writer, name string, data any) error

type File

type File struct {
	Path        string    `db:"path"`
	Size        uint64    `db:"size"`
	ContentType string    `db:"content_type"`
	Description string    `db:"description"`
	UserID      string    `db:"user_id"`
	Username    *string   `db:"username"`
	CreatedAt   time.Time `db:"created_at"`
	UpdatedAt   time.Time `db:"updated_at"`
}

type FileRequest

type FileRequest struct {
	Size        uint64 `json:"size"`
	Description string `json:"description"`
	Dir         string `json:"dir"`
}

type IndexVariables

type IndexVariables struct {
	BaseVariables
	Path      string
	PathParts []string
	Files     []TemplateFile
}

type LogConfig

type LogConfig struct {
	Level     slog.Level `cfg:"level"`
	Format    string     `cfg:"format"`
	AddSource bool       `cfg:"add_source"`
}

func (LogConfig) String

func (c LogConfig) String() string

type MetricsConfig

type MetricsConfig struct {
	ListenAddr string `cfg:"listen_addr"`
}

func (MetricsConfig) String

func (c MetricsConfig) String() string

type OtelConfig

type OtelConfig struct {
	InstanceID string         `cfg:"instance_id"`
	Trace      *TraceConfig   `cfg:"trace"`
	Metrics    *MetricsConfig `cfg:"metrics"`
}

func (OtelConfig) String

func (c OtelConfig) String() string

type Server

type Server struct {
	// contains filtered or unexported fields
}

func NewServer

func NewServer(version string, cfg Config, db *DB, auth *Auth, storage Storage, tracer trace.Tracer, meter metric.Meter, assets http.FileSystem, tmpl ExecuteTemplateFunc, js WriterFunc, css WriterFunc) *Server

func (*Server) Auth

func (s *Server) Auth(next http.Handler) http.Handler

func (*Server) Callback

func (s *Server) Callback(w http.ResponseWriter, r *http.Request)

func (*Server) CheckAuth

func (s *Server) CheckAuth(allowedFunc func(r *http.Request, info *UserInfo) AuthAction) func(next http.Handler) http.Handler

func (*Server) Close

func (s *Server) Close()

func (*Server) DeleteFiles

func (s *Server) DeleteFiles(w http.ResponseWriter, r *http.Request)

func (*Server) GetFiles

func (s *Server) GetFiles(w http.ResponseWriter, r *http.Request)

func (*Server) GetSettings

func (s *Server) GetSettings(w http.ResponseWriter, r *http.Request)

func (*Server) GetVersion

func (s *Server) GetVersion(w http.ResponseWriter, _ *http.Request)

func (*Server) Login

func (s *Server) Login(w http.ResponseWriter, r *http.Request)

func (*Server) Logout

func (s *Server) Logout(w http.ResponseWriter, r *http.Request)

func (*Server) MoveFiles

func (s *Server) MoveFiles(w http.ResponseWriter, r *http.Request)

func (*Server) PatchFile

func (s *Server) PatchFile(w http.ResponseWriter, r *http.Request)

func (*Server) PostFile

func (s *Server) PostFile(w http.ResponseWriter, r *http.Request)

func (*Server) Routes

func (s *Server) Routes() http.Handler

func (*Server) Start

func (s *Server) Start()

func (*Server) ToTemplateUser

func (s *Server) ToTemplateUser(info *UserInfo) TemplateUser

type Session

type Session struct {
	AccessToken  string
	Expiry       time.Time
	RefreshToken string
	IDToken      string
}

type SettingsVariables

type SettingsVariables struct {
	BaseVariables
	Users []TemplateUser
}

type Storage

type Storage interface {
	GetObject(ctx context.Context, filePath string, start *int64, end *int64) (io.ReadCloser, error)
	MoveObject(ctx context.Context, from string, to string) error
	PutObject(ctx context.Context, filePath string, size uint64, reader io.Reader, contentType string) error
	DeleteObject(ctx context.Context, filePath string) error
}

func NewStorage

func NewStorage(ctx context.Context, config StorageConfig, tracer trace.Tracer) (Storage, error)

type StorageConfig

type StorageConfig struct {
	Type  StorageType `cfg:"type"`
	Debug bool        `cfg:"debug"`

	// Local
	Path  string `cfg:"path"`
	Umask int    `cfg:"umask"`

	// S3
	Endpoint        string `cfg:"endpoint"`
	AccessKeyID     string `cfg:"access_key_id"`
	SecretAccessKey string `cfg:"secret_access_key"`
	Bucket          string `cfg:"bucket"`
	Region          string `cfg:"region"`
	Secure          bool   `cfg:"secure"`
}

func (StorageConfig) String

func (c StorageConfig) String() string

type StorageType

type StorageType string
const (
	StorageTypeLocal StorageType = "local"
	StorageTypeS3    StorageType = "s3"
)

type TemplateFile

type TemplateFile struct {
	IsDir       bool
	Path        string
	Dir         string
	Name        string
	Size        uint64
	Description string
	Date        time.Time
	Owner       string
	IsOwner     bool
}

type TemplateUser

type TemplateUser struct {
	ID      string
	Name    string
	Email   string
	Home    string
	IsAdmin bool
	IsUser  bool
	IsGuest bool
}

type TraceConfig

type TraceConfig struct {
	Endpoint string `cfg:"endpoint"`
	Insecure bool   `cfg:"insecure"`
}

func (TraceConfig) String

func (c TraceConfig) String() string

type UpdateFile

type UpdateFile struct {
	Path        string    `db:"path"`
	NewPath     string    `db:"new_path"`
	Size        uint64    `db:"size"`
	ContentType string    `db:"content_type"`
	Description string    `db:"description"`
	UpdatedAt   time.Time `db:"updated_at"`
}

type User

type User struct {
	ID       string `db:"id"`
	Username string `db:"username"`
	Groups   string `db:"groups"`
	Email    string `db:"email"`
	Home     string `db:"home"`
}

type UserInfo

type UserInfo struct {
	oidc.UserInfo
	Home     string   `json:"home"`
	Audience []string `json:"aud"`
	Groups   []string `json:"groups"`
	Username string   `json:"preferred_username"`
}

func GetUserInfo

func GetUserInfo(r *http.Request) *UserInfo

type WarningResponse

type WarningResponse struct {
	Message   string `json:"message"`
	Status    int    `json:"status"`
	Path      string `json:"path"`
	RequestID string `json:"request_id"`
}

type WriterFunc

type WriterFunc func(w io.Writer) error

Jump to

Keyboard shortcuts

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