Documentation
¶
Overview ¶
Package api exposes the main API engine. All HTTP APIs are handled here - so-called "business logic" should be here, or in a dedicated package (if that logic is complex enough).
To use this package, you should create a new instance with New() passing a valid Config. The resulting Router will have the Router.Handler() function that returns a handler that can be used in a http.Server (or in other middlewares).
Example:
// Create the API router
apirouter, err := api.New(api.Config{
Logger: logger,
Database: appdb,
})
if err != nil {
logger.WithError(err).Error("error creating the API server instance")
return fmt.Errorf("error creating the API server instance: %w", err)
}
router := apirouter.Handler()
// ... other stuff here, like middleware chaining, etc.
// Create the API server
apiserver := http.Server{
Addr: cfg.Web.APIHost,
Handler: router,
ReadTimeout: cfg.Web.ReadTimeout,
ReadHeaderTimeout: cfg.Web.ReadTimeout,
WriteTimeout: cfg.Web.WriteTimeout,
}
// Start the service listening for requests in a separate goroutine
apiserver.ListenAndServe()
See the `main.go` file inside the `cmd/webapi` for a full usage example.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Logger where log entries are sent
Logger logrus.FieldLogger
// Database is the instance of database.AppDatabase where data are saved
Database database.AppDatabase
}
Config is used to provide dependencies and configuration to the New function.
Source Files
¶
- api-context-wrapper.go
- api-handler.go
- api.go
- ban-user.go
- comment-photo.go
- delete-image-folder.go
- delete-photo.go
- delete-user-profile.go
- do-login.go
- follow-user.go
- get-banned-users.go
- get-image.go
- get-media-comments.go
- get-media-likes.go
- get-media.go
- get-my-stream.go
- get-photo.go
- get-user-followers.go
- get-user-followings.go
- get-user-list.go
- get-user-media.go
- get-user-profile.go
- get-username.go
- like-photo.go
- liveness.go
- set-my-username.go
- shutdown.go
- unban-user.go
- uncomment-photo.go
- unfollow-user.go
- unlike-photo.go
- update-user-profile.go
- upload-photo.go
Directories
¶
| Path | Synopsis |
|---|---|
|
Package reqcontext contains the request context.
|
Package reqcontext contains the request context. |
Click to show internal directories.
Click to hide internal directories.