Git Product home page Git Product logo

user-api-old's Introduction

Resonate User/Track API

This is the main Resonate User/Track API written in Go, still WIP and not yet in production. If you're looking for the API that is currently running with http://beta.resonate.is, please check its documentation here.

The User/Track API uses Twirp, a RPC framework for service-to-service communication emphasizing simplicity and minimalism. Learn more about Twirp at its website. It also uses go-pg PostgreSQL ORM. Its structure is based on the Twirp starter kit Twisk.

Table of Contents

Project Structure

The project structure mostly follows THIS example repository and Twirp best practices, except for the services that live in internal/server/<service>.

Getting started

Prerequisites

Dev database setup

  • Create user and database as follow (as found in the local config file in ./conf.local.yaml):

username = "resonate_dev_user"

password = "password"

dbname = "resonate_dev"

Add following postgres extensions: "hstore" and "uuid-ossp"

From ./cmd/migration:

  • Init migrations
$ go run *.go init
  • Run migrations
$ go run *.go

Dependencies

Dep is used as dependency management tool. vendor/ folder contains project dependencies and should be in sync with Gopkg.toml and Gopkg.lock.

Tools

It is also available in MacOS through Homebrew:

$ brew install protobuf
  • Install retool. It helps manage go tools like commands and linters. protoc-gen-go and protoc-gen-twirp plugins were installed into _tools folder using retool.

Build the generators and tool dependencies:

$ retool build

Then, to run the protoc command and autogenerate Go code for the server interface, make sure to prefix with retool do, for example:

$ retool do protoc --proto_path=$GOPATH/src:. --twirp_out=. --go_out=. ./rpc/user/service.proto

Running the server

First, put this repo into $GOPATH/src

Then, run the server

$ go run ./cmd/api/main.go

Alternatively, you can build and run an executable binary

$ cd ./cmd/api/
$ go build
$ ./api

Implementing new services

  1. Under proto folder, create a new one named after your service.

  2. Define your proto file. If you are not familiar with Protobufs, you can read more about it here. You can use already existing proto files (eg rpc/user/service.proto) as a template.

  3. Run retool do protoc --proto_path=$GOPATH/src:. --twirp_out=. --go_out=. ./rpc/<service>/service.proto where <service> is your new service name.

  4. Implement the service interface from service.twirp.go in internal/server/tenant.

  5. Wire up everything in cmd/api/main.go.

Documentation

  • Check out doc/api folder for API documentation. But we'll be transitioning to OpenAPI Specification and Swagger.
  • doc/database contains autogenerated database documentation, using SchemaSpy. Open doc/database/index.html in your browser to check it out.

Testing

We use Ginkgo and Gomega for testing.

You need to create the testing database and run migrations manually before running tests.

  • Create user and database as follow:

username = "resonate_testing_user"

password = ""

dbname = "resonate_testing"

Add following extensions: "hstore" and "uuid-ossp" (TODO: add them on initial migration)

  • Run migrations from ./cmd/migration
$ go run *.go init testing
$ go run *.go testing
  • Run tests ./internal/server/<service>
$ go test

Or run all tests using ginkgo CLI from ./

$ ginkgo -r

Roadmap

(see our projects for more details)

  • Switch from API Blueprint to OpenAPI and autogenerated API documentation using SwaggerUI
  • Implement logging interfaces for every service
  • Add JWT based authentication and role-based access control (see internal/iam) to existing services

Contributing

Please check out the Contributing guide for guidelines about how to proceed.

We expect contributors to abide by our underlying code of conduct.

user-api-old's People

Contributors

blushi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

user-api-old's Issues

protoc-gen-go is missing in tools dependencies

Go Version: go1.12.9 darwin/amd64
OS: osx 10.14.5

  1. Clone a fresh repo
  2. Run retool build
  3. Run retool do protoc --proto_path=$GOPATH/src:. --twirp_out=. --go_out=. ./rpc/user/service.proto

This error is thrown:

protoc-gen-go: program not found or is not executable
Please specify a program using absolute path or make sure the program is available in your PATH system variable
--go_out: protoc-gen-go: Plugin failed with status code 1.
retool: fatal err: failed on 'protoc --proto_path=/Users/eburns/go/src:. --twirp_out=. --go_out=. ./rpc/helloworld/service.proto': exit status 1

It looks like retool doesn't clean up the /bin/ dir on sync or build, so when reproducing, you'd probably want to nuke that folder.

Switch to OpenAPI and autogenerated API documentation using SwaggerUI

The current API doc is using API Blueprint description language. We'd like to switch to OpenAPI which will allow us to generate documentation automatically. See some indications below:

  • there's already an endpoint /openapi/swaggerui that returns the list of Swagger specs in the browser. Currently it only shows the IAM and User services doc. The User doc has to be rebuilt and doc for the other services has to be added - based in the existing documentation

  • there's already a tool installed protoc-gen-twirp_swagger that can generate swagger files from twirp services. You have first to add annotations to the proto file (see for instance https://github.com/resonatecoop/user-api/blob/master/rpc/iam/service.proto)
    Then run

retool do protoc \
            -I vendor/github.com/grpc-ecosystem/grpc-gateway/ \
            -I vendor/ \
            --proto_path=$GOPATH/src:. \
            --twirp_out=. \
            --go_out=. \
            --twirp_swagger_out=openapi/ \
            ./rpc/track/service.proto

to autogenerate files (for the track service in this example)

  • As some services use types that are imported from other services, please respect this order to generate files: tag, address, track, trackgroup, user, usergroup

  • Finally, add service link to openapi/swagger.html.

Add JWT based authentication and role-based access control

Prerequisites:

The user-api has currently no authentication/authorization implemented.

The tools to implement that are already there:

Regarding the models, there are already:

  • role with 4 predefined roles
  • tenant
  • user model already has auth related fields: Password, Token, RoleId, TenantId, LastLogin and LastPasswordChange but they need to be added to user protobuf.

TODOs:

  1. Within services, the Server struct need to have additional data fields: an RBACService, a Securer and an AuthService:
type AuthService interface {
	GetUser(context.Context) *model.AuthUser
}

Requests authorization need to be implemented in there.
Please contact @blushi to discuss specifications.

Some inspiration from the Twisk project.

  1. Database migration:
  • Add Role and Tenant tables
  • Insert default roles and tenant(s)
  1. Adapt cmd/api/main.go
  2. Don't forget about tests!

Implement User Feed

This social feed should return:

  • Last added albums and tracks from followed artist user groups
  • Last added public playlists from followed user groups
  • Last recommended artists from followed user groups (if they set their recommended artists as public)

Various feed implementations should be discussed here first.
Should we build our one or look into third party services such as https://getstream.io/ for instance?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.