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 ¶
View Source
var ErrNoAuth = errors.New("unauthenticated")
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.
type PostFE ¶
type PostFE struct { PostID int64 `json:"postID"` ImageB64 string `json:"imageB64"` PubTime string `json:"pubTime"` Caption string `json:"caption"` Author int64 `json:"author"` LikeCount int `json:"likeCount"` Comments []int64 `json:"comments"` }
Here we don't want to return the likes explicitly, but rather the like count
type PostParams ¶
Source Files ¶
- api-handler.go
- api.go
- block.go
- comment.go
- delete-comment.go
- follow.go
- get-auth-token.go
- get-blocked.go
- get-comment.go
- get-feed.go
- get-followers.go
- get-following.go
- get-likes.go
- get-post.go
- get-profile.go
- internal-server-error.go
- is-comment-liked.go
- is-liked.go
- like-comment.go
- like-post.go
- liveness.go
- login.go
- new-post.go
- rm-follower.go
- rm-post.go
- search-user.go
- set-propic.go
- set-username.go
- shutdown.go
- unblock.go
- unfollow.go
- unlike-comment.go
- unlike-post.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.