Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

☁️ Live reload for Go apps

License

NotificationsYou must be signed in to change notification settings

MoritzKrafeld/air

 
 

Repository files navigation

GoCodacy BadgeGo Report Cardcodecov

air

English |简体中文 |繁體中文

Motivation

When I started developing websites in Go and usinggin framework, it was a pitythat gin lacked a live-reloading function. So I searched around and triedfresh, it seems not muchflexible, so I intended to rewrite it better. Finally, Air's born.In addition, great thanks topilu, no fresh, no air :)

Air is yet another live-reloading command line utility for developing Go applications. Runair in your project root directory, leave it alone,and focus on your code.

Note: This tool has nothing to do with hot-deploy for production.

Features

  • Colorful log output
  • Customize build or any command
  • Support excluding subdirectories
  • Allow watching new directories after Air started
  • Better building process

Overwrite specify configuration from arguments

Support air config fields as arguments:

If you want to config build command and run command, you can use like the following command without the config file:

air --build.cmd"go build -o bin/api cmd/run.go" --build.bin"./bin/api"

Use a comma to separate items for arguments that take a list as input:

air --build.cmd"go build -o bin/api cmd/run.go" --build.bin"./bin/api" --build.exclude_dir"templates,build"

Installation

Viago install (Recommended)

With go 1.22 or higher:

go install github.com/air-verse/air@latest

Via install.sh

# binary will be $(go env GOPATH)/bin/aircurl -sSfL https://raw.githubusercontent.com/air-verse/air/master/install.sh| sh -s -- -b$(go env GOPATH)/bin# or install it into ./bin/curl -sSfL https://raw.githubusercontent.com/air-verse/air/master/install.sh| sh -sair -v
# binary will be /usr/local/bin/aircurl -sSfL https://goblin.run/github.com/air-verse/air| sh# to put to a custom pathcurl -sSfL https://goblin.run/github.com/air-verse/air| PREFIX=/tmp sh

Docker/Podman

Please pull this Docker imagecosmtrek/air.

docker/podman run -it --rm \    -w"<PROJECT>" \    -e"air_wd=<PROJECT>" \    -v$(pwd):<PROJECT> \    -p<PORT>:<APP SERVER PORT> \    cosmtrek/air    -c<CONF>

Docker/Podman .${SHELL}rc

if you want to use air continuously like a normal app, you can create a function in your ${SHELL}rc (Bash, Zsh, etc…)

air() {  podman/docker run -it --rm \    -w"$PWD" -v"$PWD":"$PWD" \    -p"$AIR_PORT":"$AIR_PORT" \    docker.io/cosmtrek/air"$@"}

<PROJECT> is your project path in container, eg: /go/exampleif you want to enter the container, Please add --entrypoint=bash.

For example

One of my project runs in Docker:

docker run -it --rm \  -w"/go/src/github.com/cosmtrek/hub" \  -v$(pwd):/go/src/github.com/cosmtrek/hub \  -p 9090:9090 \  cosmtrek/air

Another example:

cd /go/src/github.com/cosmtrek/hubAIR_PORT=8080 air -c"config.toml"

this will replace$PWD with the current directory,$AIR_PORT is the port where to publish and$@ is to accept arguments of the application itself for example -c

Usage

For less typing, you could addalias air='~/.air' to your.bashrc or.zshrc.

First enter into your project

cd /path/to/your_project

The simplest usage is run

# firstly find `.air.toml` in current directory, if not found, use defaultsair -c .air.toml

You can initialize the.air.toml configuration file to the current directory with the default settings running the following command.

air init

After this, you can just run theair command without additional arguments, and it will use the.air.toml file for configuration.

air

For modifying the configuration refer to theair_example.toml file.

Runtime arguments

You can pass arguments for running the built binary by adding them after the air command.

# Will run ./tmp/main benchair bench# Will run ./tmp/main server --port 8080air server --port 8080

You can separate the arguments passed for the air command and the built binary with-- argument.

# Will run ./tmp/main -hair -- -h# Will run air with custom config and pass -h argument to the built binaryair -c .air.toml -- -h

Docker Compose

services:my-project-with-air:image:cosmtrek/air# working_dir value has to be the same of mapped volumeworking_dir:/project-packageports:      -<any>:<any>environment:      -ENV_A=${ENV_A}      -ENV_B=${ENV_B}      -ENV_C=${ENV_C}volumes:      -./project-relative-path/:/project-package/

Debug

air -d prints all logs.

Installation and Usage for Docker users who don't want to use air image

Dockerfile

# Choose whatever you want, version >= 1.16FROM golang:1.22-alpineWORKDIR /appRUN go install github.com/air-verse/air@latestCOPY go.mod go.sum ./RUN go mod downloadCMD ["air","-c",".air.toml"]

docker-compose.yaml

version:"3.8"services:web:build:context:.# Correct the path to your Dockerfiledockerfile:Dockerfileports:      -8080:3000# Important to bind/mount your codebase dir to /app dir for live reloadvolumes:      -./:/app

Q&A

"command not found: air" or "No such file or directory"

export GOPATH=$HOME/xxxxxexport PATH=$PATH:$GOROOT/bin:$GOPATH/binexport PATH=$PATH:$(go env GOPATH)/bin<---- Confirm this linein you profile!!!

Error under wsl when ' is included in the bin

Should use\ to escape the `' in the bin. related issue:#305

Question: how to do hot compile only and do not run anything?

#365

[build]cmd ="/usr/bin/true"

How to Reload the Browser Automatically on Static File Changes

Refer to issue#512 for additional details.

  • Ensure your static files ininclude_dir,include_ext, orinclude_file.
  • Ensure your HTML has a</body> tag
  • Activate the proxy by configuring the following config:
[proxy]enabled =trueproxy_port = <air proxy port>app_port = <your server port>

Development

Please note that it requires Go 1.16+ since I usego mod to manage dependencies.

# Fork this project# Clone itmkdir -p$GOPATH/src/github.com/cosmtrekcd$GOPATH/src/github.com/cosmtrekgit clone git@github.com:<YOUR USERNAME>/air.git# Install dependenciescd airmake ci# Explore it and happy hacking!make install

Pull requests are welcome.

Release

# Checkout to mastergit checkout master# Add the version that needs to be releasedgit tag v1.xx.x# Push to remotegit push origin v1.xx.x# The CI will process and release a new version. Wait about 5 min, and you can fetch the latest version

Star History

Star History Chart

Sponsor

Buy Me A Coffee

Give huge thanks to lots of supporters. I've always been remembering your kindness.

License

GNU General Public License v3.0

About

☁️ Live reload for Go apps

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go87.4%
  • Shell10.2%
  • Makefile1.5%
  • Other0.9%

[8]ページ先頭

©2009-2025 Movatter.jp