visit
$ mify init todo-app
$ cd todo-app
$ mify add service todo-backend
.
├── go-services
│ ├── cmd
│ │ ├── dev-runner
│ │ │ └── main.go
│ │ └── todo-backend
│ │ ├── Dockerfile
│ │ └── main.go
│ ├── go.mod
│ ├── go.sum
│ └── internal
│ ├── pkg
│ │ └── generated
│ │ ├── configs
│ │ │ └── ...
│ │ ├── consul
│ │ │ └── ...
│ │ ├── logs
│ │ │ └── ...
│ │ └── metrics
│ │ └── ...
│ └── todo-backend
│ ├── app
│ │ ├── request_extra.go
│ │ ├── router
│ │ │ └── router.go
│ │ └── service_extra.go
│ └── generated
│ ├── api
| | └── ...
│ ├── app
│ │ └── ...
│ ├── apputil
│ │ └── ...
│ └── core
│ └── ...
├── schemas
│ └── todo-backend
│ ├── api
│ │ └── api.yaml
│ └── service.mify.yaml
└── workspace.mify.yaml
Mify loosely follows one of the common Go layouts, which is suitable for multiple services in one repository. In internal/pkg/generated
there are common libraries for configs, logs, and metrics that can be reused for multiple services. Your service go-to directory is in internal/todo-backend
.
You can find the OpenAPI schema for the todo-backend in schemas/todo-backend/api/api.yaml
file. Schemas directory in the root of the workspace is a place where all service configs related to Mify are stored.
POST /todos
for adding new to-do notes.PUT,GET,DELETE /todos/{id}
for updating, retrieving, and deleting them.
Replace the previous schema with this one and run mify generate
. You can run it each time you update the schema and it will regenerate all the changed stuff.
$ cd go-services
$ go mod tidy
$ go run ./cmd/todo-backend
You can see the service port in starting api server
log message, copy it to Postman, and try calling some API handler:
You can see that the handler returned nothing, which is expected because as the error suggests, it’s not implemented yet.
First, we need to create a model for the todo note, we’ll put it in the domain
package
in go-services/internal/todo-backend/domain/todo.go
:
This is also a good place to define the interface for storage, which is useful for decoupling persistence logic from the application. In this tutorial, we’ll use mock storage, in memory, but Mify also supports Postgres, which we can add later in a follow-up article. Let’s put storage in go-services/internal/todo-backend/storage/todo_mem.go:
go-services/internal/todo-backend/handlers/todos/service.go
for POST method, and
go-services/internal/todo-backend/handlers/todos/id/service.go
for others.
go-services/internal/todo-backend/handlers/todos/service.go
:
import (
"net/http"
"strconv"
"example.com/namespace/todo-app/go-services/internal/todo-backend/domain"
"example.com/namespace/todo-app/go-services/internal/todo-backend/generated/api"
"example.com/namespace/todo-app/go-services/internal/todo-backend/generated/apputil"
"example.com/namespace/todo-app/go-services/internal/todo-backend/generated/core"
"example.com/namespace/todo-app/go-services/internal/todo-backend/handlers"
)
go-services/internal/todo-backend/handlers/todos/id/service.go
:
import (
"errors"
"fmt"
"net/http"
"strconv"
"example.com/namespace/todo-app/go-services/internal/todo-backend/domain"
"example.com/namespace/todo-app/go-services/internal/todo-backend/generated/api"
"example.com/namespace/todo-app/go-services/internal/todo-backend/generated/apputil"
"example.com/namespace/todo-app/go-services/internal/todo-backend/generated/core"
"example.com/namespace/todo-app/go-services/internal/todo-backend/handlers"
"example.com/namespace/todo-app/go-services/internal/todo-backend/storage"
)
go-services/internal/todo-backend/handlers/common.go
:
Check if it is added with a GET request:
Update it with a PUT request:
Delete it:
And run GET once again to check if it was deleted: