This server has been developed by:
- Start the server:
go run cmd/main.go
(running on port3333
) - Format the code:
go fmt ./...
- Build everything:
go build ./...
- Run tests:
go test ./...
Here are recommendations and some rules for contributing to this project.
We suggest using Visual Studio Code with the extensions you find in .vscode/extensions.json
. You should get
a recommendation to install them when opening the project.
It is mandatory to enable save actions in your IDE! You will need to configure save actions both for Go (go fmt
) and the .editorconfig
file. If you use Visual Studio Code, you can use the extension Save Actions
which is already configured for this project.
This project uses ConventionalCommits as style guide for commit messages. Please note that every commit message will be scanned on push and fails in case the rules are not met!
Pushing on main
is forbidden (exceptions apply to @yannickkirschen, as he is silly enough
to deal with Git hell. If he breaks the git history with a force push, he buys the team a shot in a bar of their choice).
Please create a pull request and wait for a review. When merging, squashing is enabled. That means, all commits
will result in a single commit having the title you provide when merging. Please double check this title, as it will be
linted as well!
auth
: authentication and authorization (see Authentication and Authorization)care_tips
: access to care tipscmd
: main applications for this project, executable via the command line.config
: configuration model (read frombuddy.json
, see Configuration)controller
: business logic for working with controllersdb
: database session model (see Access the database)plant
: business logic for working with plantssensor
: business logic for working with sensorsutils
: utility functions (see utils)
docs/sql
: SQL scripts for developmentscripts/generate_sensor_data.py
: generates random sensor data for testing purposesauth-proposal.md
: proposal for authentication and authorizationplantbuddy.sqlite
: the database used for developmentserver-requests.http
: example requests for the API
The configuration file buddy.json
is located in the root directory of the project. Properties
are mapped to the structure config.Config
. If you want to add a new property, you have to
add it to the structure and to the buddy.json
file.
Accessing the configuration is done via the global variable config.PlantBuddyConfig
.
For accessing the database, we use a wrapping session to handle the connection. Our goal is to have one session for each HTTP request. That means you have to create a session at the beginning of the logic inside a request's handler. The session is closed at the end of the handler.
var session = db.NewSession()
defer session.Close()
err := session.Open()
if err != nil {
return nil, err
}
// Do something with session
See Code structure on how to use session in a repository pattern.
We use basic auth to authenticate. Take a look at buddy-default.md
to learn about the default users
we provide. There are two roles:
Admin
: manage usersGardener
: normal user
We want to have a dedicated package for every business domain. I.e. the package plant
contains
all logic for working with plants and the following files:
plant-model.go
: data model uses for plantsplant-endpoint.go
: contains the HTTP handlers for the plant domainplant-database.go
: contains the repository interface to access the databaseplant-sqlite.go
: contains the implementation of the repository interface for SQLite
All repository implementations provide a function to create a new repository. This function always looks roughly like this:
func NewRepository(session *db.Session) (*PlantSqliteRepository, error) {
if !session.IsOpen() {
return nil, errors.New("session is not open")
}
return &PlantSqliteRepository{db: session.DB}, nil
}
Please take a look on existing structures and functions to learn more on naming stuff.
Here are the utilities we developed to make life easier.
As we use basic HTTP routing, it is difficult to get a path parameter. That's why we
created the function utils.PathParameterFilter(path string, prefix string) (int64, error)
to solve
this issue.
Example: assume your path is /v1/hello/123
and your prefix is /v1/hello/
. Calling
PathParameterFilter
will result in 123
as a path parameter (int64
).
Get number of lines of code: git ls-files | grep '\.go' | xargs wc -l