Configuration
The go-advanced-admin panel is designed to be highly configurable, allowing you to tailor it to your specific needs. This guide provides a comprehensive overview of all the configuration options available in the admin panel. You'll learn how to set up database connections, authentication, authorization, logging, templating, and manage static files.
Table of Contents
Introduction
Configuring the admin panel correctly is crucial for ensuring that it works seamlessly with your application's requirements. The configuration is managed through the AdminConfig
struct, which provides various options to customize the behavior and appearance of the admin panel.
AdminConfig Structure
The AdminConfig
struct is the core of the configuration system. It includes several fields that allow you to customize different aspects of the admin panel.
Configuration Options
Name
Type:
string
Description: The name of your admin panel. This will be displayed in the UI, typically in the header or title.
Default:
"Site Administration"
Example:
Prefix
Type:
string
Description: The URL prefix for the admin panel routes. This allows you to mount the admin panel under a specific path.
Default:
"admin"
Example:
With this configuration, the admin panel will be accessible at /dashboard
.
Renderer
Type:
TemplateRenderer
Description: Handles template rendering for the admin panel. You can use the default renderer or provide a custom one to change how templates are processed.
Default:
NewDefaultTemplateRenderer()
Example:
AssetsPrefix
Type:
string
Description: The URL prefix for static assets like CSS and JavaScript files.
Default:
"admin-assets"
Example:
Static files will be served from /static-assets
.
GroupPrefix
Type:
string
Description: An additional prefix that can be used if you're grouping multiple services under a common path.
Default:
""
Example:
All admin routes will be prefixed with /api/v1
.
DefaultInstancesPerPage
Type:
uint
Description: The default number of instances (records) to display per page in list views.
Default:
10
Example:
NavBarGenerators
Type:
[]NavBarGenerator
Description: A slice of functions that generate navigation bar items. This allows you to customize the navigation bar dynamically based on the context.
Default: A set of default navigation items.
Example:
UserFetcher
Type:
func(ctx interface{}) (userID interface{}, repr string, err error)
Description: A function used to fetch the current user's information from the context. This is essential for logging and permissions.
Default:
nil
Example:
LogStore
Type:
logging.LogStore
Description: Manages how logs are stored. You can use the default in-memory log store or implement a custom one.
Default:
logging.NewInMemoryLogStore(100)
Example:
LogStoreLevel
Type:
logging.LogStoreLevel
Description: Determines the verbosity level of logging. Higher levels include all lower levels.
Default:
logging.LogStoreLevelPanelView
Available Levels:
logging.LogStoreLevelDelete
logging.LogStoreLevelCreate
logging.LogStoreLevelUpdate
logging.LogStoreLevelInstanceView
logging.LogStoreLevelListView
logging.LogStoreLevelPanelView
Example:
With this configuration, logs for updates, deletes, and creates will be recorded.
Database Connection
While the database connection isn't directly configured within AdminConfig
, it's essential to set up your ORM integrator correctly. The admin panel uses an ORM integrator to interact with your database models.
Example with GORM:
Authentication
Authentication is managed through the UserFetcher
function and the context provided by your web framework.
Setting Up UserFetcher:
This function extracts the user information from the request context, which is then used for permissions and logging.
Authorization
Authorization is handled by implementing the PermissionFunc
, which checks if a user has the necessary permissions to perform certain actions.
For more information on the permission checker, please check out the Permissions Guide.
Example Permission Checker:
Registering the Permission Checker:
Logging
Configuring the Log Store Level
The LogStoreLevel
determines what actions are logged.
Example:
This will log all create actions and any actions with a higher priority.
Configuring the Log Store
You can use the default in-memory log store or implement a custom one.
For more information on this, please check out the Logging Guide.
Using the Default Log Store:
Implementing a Custom Log Store:
Registering the Custom Log Store:
Templating
The admin panel uses a TemplateRenderer
to render HTML templates. You can customize this to change the look and feel of the admin panel.
For more information on this, please check out the Template Rendering Guide and the Templates Guide.
Using the Default Renderer
The default renderer uses Go's html/template
package.
Implementing a Custom Renderer
Static Files
Static files like CSS and JavaScript are served from the path specified by AssetsPrefix
.
Configuring AssetsPrefix
Static files will now be served from /assets
.
Serving Custom Static Files
If you have custom static files, ensure that your web framework is configured to serve them from the AssetsPrefix
path.
Default Configuration
If you don't provide a custom configuration, the admin panel uses the DefaultAdminConfig
:
This default configuration sets up the admin panel with sensible defaults, but you can override any of these settings by providing your own configuration.
Next Steps
Permissions Guide: Learn how to implement custom permissions.
Templates Guide: Customize the look and feel of the admin panel.
Logging Guide: Dive deeper into configuring logging.
Feedback
We strive to keep our documentation clear and up-to-date. If you have any feedback or suggestions, please let us know by opening an issue or submitting a pull request.
Thank you for using go-advanced-admin!