Web based admin interface for Go, inspired by Django admin.
- Single user login (TODO: Implement support for custom login / user handlers).
- Register and group structs as "models" that map to your database manually or via an ORM.
- Set custom attributes via each struct field's tag to choose which columns are shown in lists, searchable etc (see below).
- Search, list and sort rows.
- Custom formatting of values like time.Time etc.
- Override / add custom fields with custom validation, formatting etc (may not work at the moment, but will soon).
- Auto generate forms from structs for easy content management. Foreign keys and ManyToMany relationships are supported, as long as target struct is also registered (choose by ID or via popup window).
See the example app in /example for a working test app.
// Set up admin
a, err := admin.New("/admin", "sqlite3", "db.sqlite")
if err != nil {
panic(err)
}
// Override settings as needed
a.Title = "Example admin"
a.NameTransform = snakeString // Optional (for ORM support)
a.User("admin", "example") // Username / password to log in.
group, err := a.Group("Blog")
if err != nil {
panic(err)
}
group.RegisterModel(new(Category))
group.RegisterModel(new(BlogPost))
// Get a http.Handler to attach to your router/mux.
adminHandler, err := a.Handler()
if err != nil {
panic(err)
}
// Serve admin.
router := http.NewServeMux()
router.Handle("/admin/", adminHandler)
A model is just a struct
type Page struct {
Id int
Name string `admin:"list search width=6"` // Listed, searchable and half width (12 columns is full width)
Slug string `admin:"list search width=6"` // Listed, searchable and half width (displayed to the right of Name in edit form)
Content string `admin:"list textarea label='Page content'"`
Added time.Time `admin:"list label='Publish date' format='02.01.2006'"`
}
NameTransform
is a function that takes a string and returns a string. It's used to transform struct field names to database table names. For example, Beego ORM uses snake case versions of struct fields for table / column names, so it'll convert "CompanyEmployee" to "company_employee". This is optional, so if no NameTransform
is specified, lookups in the database will use the CamelCase versions like in Go.
Additional options can be provided in the admin
struct tag, as in the example above. If more than one is used, separate them by a single space
. Multiple word values must be single quoted. Currently, these are supported:
-
Skip / hide column (id / first column can't be hidden)list
Show column in list viewlist='FieldName'
is available for pointers /ForeignKeyField
s and will display RelatedField.FieldName instead of its Id value.
search
Make column searchableblank
Allow this field to be empty.null
Only works ifblank
is used. Instead of inserting empty values, NULL will be used for empty fields.field=file
Lets you specify a non-default field type.url
andfile
are currently supportedfile
also takes an optionalupload_to='some/path'
label='Custom name'
Custom label for columndefault='My default value'
Default value in "new"/"create" formwidth=4
Custom field width / column width (Optional, if not specified, 12 / full width is default)format='01.02.2006'
For time.Time fieldstextarea
Used by string / text field to display field as a textarea instead of an input
This project is still early in development. More documentation and features will be added over time.