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 Comment ¶
type Comment struct { ID int `json:"Id"` Content string `json:"content"` PhotoId int `json:"PhotoId"` UserId int `json:"userId"` Date time.Time `json:"date"` }
func (*Comment) FromDatabase ¶
func (*Comment) RightComment ¶
RightComment checks the validity of the comment.
func (*Comment) ToDatabase ¶
ToDatabase returns the comment in a database-compatible representation
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 Photo ¶
type Photo struct { ID int `json:"id"` UserId int `json:"userid"` Path string `json:"path"` Likes string `json:"likes"` Comments string `json:"comments"` Date time.Time `json:"date"` }
func (*Photo) FromDatabase ¶
func (*Photo) ToDatabase ¶
ToDatabase returns the photo in a database-compatible representation
type Router ¶
type Router interface { // Handler returns an HTTP handler for APIs provided in this package Handler() http.Handler // Close terminates any resource used in the package Close() error }
Router is the package API interface representing an API handler builder
type User ¶
type User struct { ID int `json:"Id"` Name string `json:"Name"` ProfilePic int `json:"ProfilePic"` Followers string `json:"Followers"` Banned string `json:"Banned"` Photos string `json:"Photos"` }
User struct represent a user in every data exchange with the external world via REST API. JSON tags have been added to the struct to conform to the OpenAPI specifications regarding JSON key names. Note: there is a similar struct in the database package. See User.FromDatabase (below) to understand why.
func (*User) FromDatabase ¶
func (*User) ToDatabase ¶
ToDatabase returns the user in a database-compatible representation
Source Files ¶
- api-context-wrapper.go
- api-handler.go
- api.go
- banUser.go
- commentPhoto.go
- deletePhoto.go
- doLogin.go
- followUser.go
- getFollowingUsers.go
- getImage.go
- getLogo.go
- getMyStream.go
- getUserByName.go
- getUserName.go
- getUserProfile.go
- likePhoto.go
- liveness.go
- setMyUserName.go
- shutdown.go
- struct.go
- unbanUser.go
- uncommentPhoto.go
- unfollowUser.go
- unlikePhoto.go
- uploadLogo.go
- uploadPhoto.go
Directories ¶
Path | Synopsis |
---|---|
Package reqcontext contains the request context.
|
Package reqcontext contains the request context. |