Documentation
¶
Overview ¶
Package api provides functions to get the environment file path.
Index ¶
- Variables
- func AddCacheItem(id string, data *model.LookupResponse) error
- func GetCacheById(id string) (*model.LookupResponse, error)
- func GetEnvFilePath() string
- func GetGeoInfo(ipaddress string) (model.GeoData, int)
- func InitDatabase()
- func Record(lookupRequest *model.LookupRequest) error
- type CacheItem
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func AddCacheItem ¶
func AddCacheItem(id string, data *model.LookupResponse) error
AddCacheItem sets an item in the cache for the given key
func GetCacheById ¶
func GetCacheById(id string) (*model.LookupResponse, error)
GetCacheById retrieves an item from the cache for the given key
func GetEnvFilePath ¶
func GetEnvFilePath() string
GetEnvFilePath returns the path to the .env file in the current executable's directory. If the executable's path is not available, it falls back to the caller's path.
If an error occurs while retrieving the executable's path, it logs the error and returns an empty string.
The function does not return any error, as it only logs errors and does not propagate them.
Example:
envPath := api.GetEnvFilePath() fmt.Println(envPath)
func GetGeoInfo ¶
GetGeoInfo accepts an IP Address to perform a lookup of the geolocation information of the IP Address form KeyCDNs' Geo service. This service is rate limited to a maximum of 3 calls per second.
func InitDatabase ¶
func InitDatabase()
InitDatabase initializes the database connection and migrates the LookupRequest model.
It retrieves the DSN from the environment variable "CONNECTION" and logs the connection details.
It then opens a connection to the database using GORM and configures it. If an error occurs during this process, it panics.
After the connection is established, it attempts to auto-migrate the LookupRequest model. If an error occurs during this process, it logs the error and continues.
This function is called once at the start of the application to set up the database.
func Record ¶
func Record(lookupRequest *model.LookupRequest) error
Record records a new lookup request in the database.
Parameters:
- lookupRequest: A pointer to a model.LookupRequest object containing the details of the lookup request.
Returns:
- nil if the lookup request is successfully recorded, otherwise an error.
Example:
```
lookupRequest := &model.LookupRequest{/* details */}
err := Record(lookupRequest)
if err != nil {
log.Fatal(err)
}
```
Types ¶
type CacheItem ¶
type CacheItem []struct {
ID string `json:"id"`
Data *model.LookupResponse `json:"data"`
}
CacheItem struct holds the string key and the cached pointer of the item being cached