server

package module
Version: v0.0.0-...-389da1f Latest Latest
Warning

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

Go to latest
Published: May 31, 2021 License: AGPL-3.0 Imports: 34 Imported by: 0

README

Thinking of You API server

Build Status

The goal of the api server is to provide basic device state delivery for client apps and a simple message interface for publishing and retreiving media to one or more target inboxes.

API Documentation

For now, refer to the OpenAPI spec file in the api directory for endpoint and response documentation.

Database

An embedded sqlite3 database is used for persistent storage in this project. SQLite is a reliable and comprehensive embedded database used in many industries and applications. For this project, it gives a well understood and reliable basis for data persistence that should allow for minimal maintainance to keep the server running once deployed. Use the database/init_dev.sqlite.sql file for initalizing a testing instance of the database.

Create Test Database
# install sqlite3 cli tool
sudo apt install sqlite3
# pipe in database file
sqlite3 ./tofu.db < ./database/init_dev.sqlite.sql
Compiling with sqlite3

One caviate of using the sqlite3 database engine is that the C stdlib must be present on the target deployment platform and the compiling environment must have a C compiler toolchain to build the binary. See the library docs for more info.

Run the Application

After creating the database file, use go run to compile and run the server.

go run ./cmd/server/
Run in Docker

If you don't want to compile and run from source, or you want to quickly switch between tagged releases, you can run the app in docker using the published container images.

docker run --rm \
	-p "8080:8080" \
	-v $(pwd):/data \
	-e TOFU_DATA=/data \
	-e GIN_MODE=debug \
	dkr-reg.sysreturn.net/auxreturn/thinking-of-you:latest

Configuration

The server accepts the following configuration via environment variables

# http listen address and port
TOFU_ADDR=
TOFU_PORT="8080"

# filepath and name of main database file 
TOFU_DB_FILE=tofu.db

# s3 style storage endpoint id and secret
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=

# s3 bucket to store uploaded files
# both AWS_ACCESS_KEY_ID and TOFU_STORAGE_BUCKET must be defined for 
# s3 external storage to become active. Otherwise, local filesystem is used.
TOFU_STORAGE_BUCKET=

# region and endpoint settings for s3 api
AWS_REGION=
TOFU_STORAGE_ENDPOINT=

# file prefix for files uploaded to message storage
TOFU_STORAGE_PREFIX=uploads

# use the digitaloceans cdn download url instead of the regular spaces download url
TOFU_STORAGE_DO_CDN=FALSE

Testing the Application

Start with the main client entrypoint

curl --user alice:alice http://localhost:8080/manifest

you should get the following response

{
	"id": "alice",
	"name": "Alice",
	"icon": "https://randomuser.me/api/portraits/women/67.jpg",
	"targets": [{
		"id": "bob",
		"name": "Bob",
		"icon": "https://randomuser.me/api/portraits/men/32.jpg"
	}, {
		"id": "carol",
		"name": "Carol",
		"icon": "https://randomuser.me/api/portraits/women/16.jpg"
	}, {
		"id": "dave",
		"name": "Dave",
		"icon": "https://randomuser.me/api/portraits/men/61.jpg"
	}, {
		"id": "erin",
		"name": "Erin",
		"icon": "https://randomuser.me/api/portraits/women/15.jpg"
	}, {
		"id": "testgroup",
		"name": "Test Group"
	}]
}
Test User credentials

All test users have a user name and password set. The password is the same as their id, so in curl use

curl --user alice:alice http://...
curl --user bob:bob http://...
...etc

Documentation

Overview

Copyright (C) 2021 Sysreturn Community Contributors SPDX-License-Identifier: AGPL-3.0-or-later

scheduled and async jobs. Jobs struct holds app state. Scheduler starts jobs, jobs enqueue tasks, tasks to the actual work

Copyright (C) 2021 Sysreturn Community Contributors SPDX-License-Identifier: AGPL-3.0-or-later

Copyright (C) 2021 Sysreturn Community Contributors SPDX-License-Identifier: AGPL-3.0-or-later

Index

Constants

View Source
const (
	DefaultStoragePrefix = "uploads"
)

Variables

View Source
var (
	ErrNotFound = errors.New("NotFound")
)

Functions

func NewDB

func NewDB(dbFile string) *sql.DB

func WithRoutes

func WithRoutes(srv *Server) error

func WithWeb

func WithWeb(srv *Server) error

Types

type Group

type Group struct {
	Id      string
	Name    string
	Forward []string
}

type Jobs

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

func NewJobScheduler

func NewJobScheduler(db *sql.DB, stor Storage) *Jobs

func (*Jobs) Shutdown

func (jobs *Jobs) Shutdown()

func (*Jobs) Start

func (jobs *Jobs) Start()

type Manifest

type Manifest struct {
	Id      string   `json:"id"`
	Name    string   `json:"name"`
	Icon    string   `json:"icon,omitempty"`
	Targets []Target `json:"targets"`
}

type Message

type Message struct {
	Id          string `json:"id"`
	Sender      string `json:"sender"`
	Target      string `json:"target"`
	Received    string `json:"received"`
	URL         string `json:"url"`
	ContentType string `json:"content_type"`
}

inbox types

type Server

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

func NewServer

func NewServer(opts ...ServerOptionFunc) (*Server, error)

func (*Server) AdminRole

func (srv *Server) AdminRole(c *gin.Context)

func (*Server) BasicAuth

func (srv *Server) BasicAuth(c *gin.Context)

func (*Server) ListenAndServe

func (srv *Server) ListenAndServe(addr string) *http.Server

type ServerOptionFunc

type ServerOptionFunc func(*Server) error

func WithDB

func WithDB(db *sql.DB) ServerOptionFunc

func WithStorage

func WithStorage(stor Storage) ServerOptionFunc

type Storage

type Storage interface {
	Store(name string, filetype string, reader io.Reader) error
	DownloadURL(name string) (string, error)
	Delete(name string) error
	List() []string
}

type StorageFS

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

func NewStorageFS

func NewStorageFS(root ...string) *StorageFS

func (*StorageFS) Delete

func (s *StorageFS) Delete(name string) error

func (*StorageFS) DownloadURL

func (s *StorageFS) DownloadURL(name string) (string, error)

func (*StorageFS) List

func (s *StorageFS) List() []string

func (*StorageFS) Store

func (s *StorageFS) Store(name, filetype string, reader io.Reader) error

type StorageS3

type StorageS3 struct {
	S3Client      *s3.Client
	PresignClient *s3.PresignClient
	Uploader      *s3manager.Uploader
	Prefix        string
	Bucket        string
}

func NewStorageS3

func NewStorageS3(config StorageS3Config) (*StorageS3, error)

func (*StorageS3) Delete

func (s *StorageS3) Delete(name string) error

func (*StorageS3) DownloadURL

func (s *StorageS3) DownloadURL(name string) (string, error)

func (*StorageS3) List

func (s *StorageS3) List() []string

func (*StorageS3) Store

func (s *StorageS3) Store(name, filetype string, reader io.Reader) error

type StorageS3Config

type StorageS3Config struct {
	AccessKey, AccessSecret, Region, Endpoint, Bucket, Prefix string
}

type Target

type Target struct {
	Id   string `json:"id"`
	Type string `json:"-"`
	Name string `json:"name"`
	Icon string `json:"icon,omitempty"`
}

device manifest response types

type User

type User struct {
	Id      string
	Name    string
	Icon    string
	Targets []string
}

document types

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL