Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal and planning for API framework #1

Closed
6 tasks done
ddysher opened this issue Sep 15, 2017 · 11 comments
Closed
6 tasks done

Proposal and planning for API framework #1

ddysher opened this issue Sep 15, 2017 · 11 comments
Assignees

Comments

@ddysher
Copy link
Member

ddysher commented Sep 15, 2017

Introduction

api framework, or http web framework, is a long standing engineering backlog across engineering teams. The proposal aims to shed some light on how we want to move forward. This is by no means a thorough design, we hope to gather enough feedback to get going. Feel free to comment.

Background

No guideline is provided at this moment to start up a new apiserver from scratch, results in a divergent approaches on setting up new web projects. No conventions enforced at framework level also results in bugs due to breaking apis, validation errors, etc. On the other hand, engineering effort is wasted on solving the same range of problems which should ideally be solved in an API framework.

Goal

  • Reduce api level errors and inconsistency
  • Improve engineering productivity via removing repeated work, adding code generation, etc
  • Adding new resource type should only require defining struct definition
  • Adding validation should only require declaring validation method as part of struct definition
  • Consistent behavior, structure and layout across all golang server projects
  • Out-of-box server instrumentation, e.g. metrics, tracing, profiling, etc

Proposal

Survey

Below is a short survey on existing projects:

  • kubernetes-admin style (go-restful)
    • routing wrapper around go-restful
    • a well-defined layout
    • count for 70% of all related projects
  • loadbalancer-admin style (gin)
    • use gin directly
    • no well-defined project layout
    • count for 20% of all related projects
  • liubo style (go-restful)
    • mirrors closely with kubernetes style
    • count for 10% of all related projects

Requirements

Following is all the required functionalities for our framework.

Routing, Request & Response

  • Routes mapping from request to function
    • support for path parameter (e.g. /api/v1/animals/{id})
    • support for query parameter (e.g. /api/v1/animals?type=dog)
    • support regex
  • Grouping Routes
    • routes can be grouped for better arragement, but doesn't have to be first class concept
  • Request/Response API object
    • to/from structs in json
    • easy access to path,query,header parameters
  • Middleware support
    • general middleware support is required for developer to add custom middleware
    • required default middleware: request logging, recovery, tracing
  • Contextual process chain and parameter injection
    • all handlers accepts a context value representing a request context
    • requset parameters can be validated and injected into handler, see below
  • API error convention
    • define canonical error code and type based on api convention
  • multipart/urlencoded form support (low priority)
  • file upload support (low priority)
  • grpc support (low priority)

Instrumentation

  • Provide default metrics at well-known endpoints for prometheus to scrape
    • need to define set of default metrics
  • Tracing should be provided by default to allow better troubleshooting
    • follow opentracing, which is industry standard
    • use jaeger as tracing system
  • Profiling can be enabled in debug mode for troubleshooting
    • use go profile

Validation

  • Provide default validation on api types with json, e.g. required, within a range, etc
  • Support custom validations defined by developers on api types
  • support validation on all parameters (path, query, etc)

Usability

  • A working project should be brought up with a few lines of code using the framework
  • Framework must follow engineering conventions to help developers focus on business logic
  • OpenAPI (swagger 2.0) specification can be generated automatically with no extra work
  • Provides a well-established layout conforming to golang project layout
  • Easy and standard configuration
  • A reasonable support for websocket

Performance

  • Performance is not a hard requirement for initial stage; but it should not introduce observable latency

None-requirements

A list of functionalities commonly seen in other frameworks that are not in our scope

  • https support. While it's not hard to serve https in go, https is out of scope since all services are expected to be running behind API gateway
  • html rendering, orm, etc. Let's stick it to a simple restful framework
  • testing. testing should be a different library

Design and Implementation

Project Timeline

  • Due 09.15: launch the project (@ddysher)
  • Due 09.23: draft feature set (@ddysher)
  • Due 09.29: finalize feature set and roadmap (@ddysher)
  • Due 12.01: check in most features (framework, cli, validation, metrics)
  • Due 12.20: check in all features, with reasonable test coverage (openapi, tracing)
  • Due 01.31: documentation, examples, website should be ready

Feature Design and Planning

Following is a list of planned features, their respective design and planning.

Meetings

Regular meetings will be held to discuss above topics.

Assignees

References

@zoumo
Copy link
Contributor

zoumo commented Sep 15, 2017

we should support OpenAPI 3.0 specification.

@walktall
Copy link
Contributor

How about https://github.com/go-chi/chi?

@xiaoq17
Copy link

xiaoq17 commented Sep 15, 2017

Shall we track this in sprint, or kill it in freestyle?

@jimexist
Copy link
Contributor

  • common middlewares
  • required and optional parameter parsing
  • swagger integration

@jimexist
Copy link
Contributor

  • suggestion/style guide for CQRS?

@jimexist
Copy link
Contributor

Non-golang, but highly recommended read about other great HTTP API frameworks:

@ddysher
Copy link
Member Author

ddysher commented Sep 16, 2017

@zoumo It's too early to say open api 3.0 should be supported, plus you can't find a reliable library at this moment. 2.0 is enough for us now.

we should support OpenAPI 3.0 specification.

@walktall looks like chi only offers router. The main consideration for listing go-restful here is that we can leverage kubernetes's codegen; but we can definitely look at other packages.

How about https://github.com/go-chi/chi?

@xiaoq17 framework is quite important now, i'd like to track it in sprint, for 1. predictable delivery time (hopefully); 2. help negotiate tasks with other product requirements. we can decide this next week.

Shall we track this in sprint, or kill it in freestyle?

@jimexist these are already mentioned in the issue

common middlewares
swagger integration

@jimexist can you elaborate the use case?

required and optional parameter parsing

@jimexist
Copy link
Contributor

@ddysher regarding required and optional parameter, the one i missed most is from Dropwizard

@kdada
Copy link
Collaborator

kdada commented Sep 18, 2017

@jimexist Golang has tags for struct fields. But there is no tags for parameters of methods/functions. It's even no parameter name in reflection.

We have two ways to achieve it:

  • Use a user-defined descriptor to describe methods/functions.
  • Use a pre-compiler as a wrapper for golang compiler to generate code.

@ddysher ddysher self-assigned this Sep 22, 2017
@ddysher ddysher changed the title Draft: proposal for api framework Proposal and planning for API framework Oct 31, 2017
@ddysher
Copy link
Member Author

ddysher commented Nov 25, 2017

@caicloud/review-framework I've updated our timeline, PTAL.

@ddysher
Copy link
Member Author

ddysher commented Feb 7, 2018

Done 🚀

I'll create a doc to archive the issue.

@ddysher ddysher closed this as completed Feb 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants