# Application

The FastSchema Application is an instance of the FastSchema Web Framework that you can use to create and run web applications.

Source: https://fastschema.com/docs/framework/application

The FastSchema Application is an instance of the FastSchema Web Framework that you can use to create and run web applications.

The Application provides a number of methods and properties that allow you to configure and customize the behavior of your web application.

## Creating Application

```go
app, err := fastschema.New(&fs.Config{})

if err != nil {
  log.Fatal(err)
}

app.AddResource(fs.Get("/", func(c fs.Context, _ any) (string, error) {
  return "Hello World", nil
}))

app.Start()
```

The above code creates a new instance of the FastSchema Application using default configuration options and starts the application.

The default FastSchema Application includes all the features and functionality of a full BaaS, including real-time data syncing, automatic database generation, ready-to-use RESTful APIs, and an intuitive Admin Panel for effortless content management.

## Configuration

You can customize the behavior of the FastSchema Application by providing a configuration object when creating a new instance of the Application.

```go
app, err := fastschema.New(&fs.Config{
  Port:               "8001",
  BaseURL:            "http://localhost:8001",
  HideResourcesInfo:  true,
})
```

The `fs.Config` struct allows you to configure the following options:

```go
type Config struct {
	Dir               string         `json:"dir"`
	AppName           string         `json:"app_name"`
	AppKey            string         `json:"app_key"`
	Port              string         `json:"port"`
	BaseURL           string         `json:"base_url"`
	DashURL           string         `json:"dash_url"`
	APIBaseName       string         `json:"api_base_name"`
	DashBaseName      string         `json:"dash_base_name"`
	Logger            logger.Logger  `json:"-"`
	LoggerConfig      *logger.Config `json:"logger_config"` // If Logger is set, LoggerConfig will be ignored
	DB                db.Client      `json:"-"`
	DBConfig          *db.Config     `json:"db_config"` // If DB is set, DBConfig will be ignored
	StorageConfig     *StorageConfig `json:"storage_config"`
	AuthConfig        *AuthConfig    `json:"auth_config"`
	MailConfig        *MailConfig    `json:"mail_config"`
	SystemSchemas     []any          `json:"-"` // types to build the system schemas
	Hooks             *Hooks         `json:"-"`
	HideResourcesInfo bool           `json:"hide_resources_info"`
}
```

### Dir

The `Dir` option allows you to specify the directory where the FastSchema Application should use to store files and data.

If you do not specify a directory, the FastSchema Application will use the current working directory.

### AppKey

The `AppKey` option allows you to specify a unique key for your FastSchema Application. This key is used to encrypt and decrypt sensitive data in the FastSchema Application.

If you do not specify an `AppKey`, the FastSchema Application will use the environment variable `APP_KEY`.

If the `APP_KEY` environment variable is not set, the FastSchema Application will generate a random key and store it in the `data/.env` file.

### Port

The `Port` option allows you to specify the port number that the FastSchema Application should use to listen for incoming requests.

The default port is `8000`.

### BaseURL

The default `BaseURL` is `http://localhost:<port>`, this is useful when you are running the FastSchema Application locally.

When you deploy the FastSchema Application to a production environment, you should set the `BaseURL` to the URL of your production server.

### DashURL

The `DashURL` option allows you to specify the URL where the FastSchema Admin Panel should be accessible.

The default `DashURL` is `http://localhost:<port>/admin`.

### APIBaseName

By default, all the API routes are prefixed with `/api`, you can change this prefix by setting the `APIBaseName` option.

### DashBaseName

By default, all the Admin Panel routes are prefixed with `/admin`, you can change this prefix by setting the `DashBaseName` option.

<Callout type="warn">

Changing the `APIBaseName` and `DashBaseName` affect the functionality of the FastSchema Admin Panel and the Admin frontend source code must be updated accordingly.

The `APIBaseName` and `DashBaseName` should not contain any special characters, spaces or slashes.

They must also differ from each other. Giving both the same value would let the Admin Panel answer
every API request, so FastSchema refuses to start in that case.

</Callout>

### Logger

The `Logger` option allows you to specify a custom logger that the FastSchema Application should use to log messages.

If no logger is specified, the FastSchema Application will use the default logger which logs messages to the console and saves them to a log file `data/logs/app.log`.

For more information on how to create a custom logger, see the [Logger](/docs/framework/logging/) documentation.

### DB

The `DB` option allows you to specify a custom database client that the FastSchema Application should use to connect to the database.

If no database client is specified, the FastSchema Application will use the default database client.

The default DB client will also looking for the configuration from the environment variables. If there is no environment variable set, it will use a SQLite database.

For more information on how to create a custom database client, see the [Database](/docs/framework/database/) documentation.

### StorageConfig

The `StorageConfig` option allows you to specify the configuration for the storage system that the FastSchema Application should use to store files and data.

If no storage configuration is specified, the FastSchema Application will use a local storage system as its default storage system.

For more information on how to create a custom storage system, see the [Storage](/docs/framework/storage/) documentation.

### AuthConfig

The `AuthConfig` option allows you to specify the configuration for the authentication system that the FastSchema Application should use to authenticate users.

### MailConfig

The `MailConfig` option allows you to specify the configuration for the mail system that the FastSchema Application should use to send emails.

### HideResourcesInfo

The `HideResourcesInfo` option allows you to disable printing the resources information in the console when the FastSchema Application starts.

### SystemSchemas

The `SystemSchemas` option allows you to specify the types that the FastSchema Application should use to build the system schemas.

This accepts a list of any types that you want to use to build the system schemas.

For more information on how to create system schema types, see the [System Schema](/docs/framework/database/system-schema) documentation.

## Methods

The FastSchema Application provides a number of methods that allow you to interact with the application and customize its behavior.

It implements the `fs.App` interface which includes the following methods:

```go
type App interface {
	Key() string
	Name() string
	Config() *Config
	SchemaBuilder() *schema.Builder
	DB() db.Client
	Resources() *ResourcesManager
	Reload(ctx context.Context, migration *db.Migration) (err error)
	Logger() logger.Logger
	UpdateCache(ctx context.Context) error
	Roles() []*Role
	Disk(names ...string) Disk
	Disks() []Disk
	GetAuthProvider(name string) AuthProvider

	Mailer(...string) Mailer
	Mailers() []Mailer

	AddResource(resource *Resource)
	AddMiddlewares(hooks ...Middleware)

	Hooks() *Hooks

	OnPreResolve(hooks ...Middleware)
	OnPostResolve(hooks ...Middleware)

	OnPreDBQuery(hooks ...db.PreDBQuery)
	OnPostDBQuery(hooks ...db.PostDBQuery)

	OnPreDBExec(hooks ...db.PreDBExec)
	OnPostDBExec(hooks ...db.PostDBExec)

	OnPreDBCreate(hooks ...db.PreDBCreate)
	OnPostDBCreate(hooks ...db.PostDBCreate)

	OnPreDBUpdate(hooks ...db.PreDBUpdate)
	OnPostDBUpdate(hooks ...db.PostDBUpdate)

	OnPreDBDelete(hooks ...db.PreDBDelete)
	OnPostDBDelete(hooks ...db.PostDBDelete)
}
```

### AddResource

The `AddResource` method allows you to add a new resource to the FastSchema Application.

```go
app.AddResource(fs.Get("/", func(c fs.Context, _ any) (string, error) {
  return "Hello World", nil
}))
```

### AddMiddlewares

The `AddMiddlewares` method allows you to add one or more middleware functions to the FastSchema Application.

```go
app.AddMiddlewares(func(c Context) error {
  return nil
})
```

### OnPreResolve, OnPostResolve, OnPostDBGet

These methods allow you to add hooks to the FastSchema Application.

For more information on how to use hooks, see the [Hooks](/docs/framework/hooks/) documentation.
