web
Fiber is an Express inspired web framework build on top of Fasthttp, the fastest HTTP engine for Go. Designed to ease things up for fast development with zero memory allocation and performance in mind.
fiberapiangularweb
环境:macos 10.15.4 + go 1.14.1 + fiber 1.11.1 + angular 9.1.6
新建项目
fiblar-demo
md fiblar-demo && cd fiblar-demo
go mod
go mod init github.com/kiyonlin/fiblar-demo
main.go
package main
import (
"github.com/gofiber/fiber"
"log"
)
func main() {
// Create new Fiber instance
app := fiber.New()
// serve Single Page application on "/public"
// assume static file at dist folder
app.Static("/", "public")
// intercept api routes
apiGroup := app.Group("/api")
{
apiGroup.Get("/user", func(c *fiber.Ctx) {
c.JSON(fiber.Map{"id": 1, "name": "kiyon"})
})
}
// other routes just return `public/index.html`, angular will handle them
app.Get("/*", func(c *fiber.Ctx) {
if err := c.SendFile("public/index.html"); err != nil {
c.Next(fiber.ErrInternalServerError)
}
})
// Start server on http://localhost:3000
log.Fatal(app.Listen(3000))
}
angular
angular
ng new web
web/angular.json
{
"other-configs": {},
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "../public"
}
}
}
angularwebpublicfiberpublic
web/src/app/app.component.tsapi
// ...
export class AppComponent {
title = 'web';
user: any;
constructor(private http: HttpClient) {
this.http.get('/api/user').subscribe(data => this.user = data);
}
}
/api/useruserweb/src/app/app.component.html
<!-- ... -->
<!-- Resources -->
<h1>User</h1>
<p>{{user | json}}</p>
<h2>Resources</h2>
<p>Here are some links to help you get started:</p>
<!-- ... -->
web/package.json
{
"scripts": {
"build": "ng build --prod",
"build:watch": "ng build --watch"
}
}
运行项目
.air.conf
root = "."
tmp_dir = "tmp"
[build]
cmd = "go build -o ./tmp/main ./main.go"
bin = "tmp/main"
include_ext = ["go", "toml"]
exclude_dir = ["assets", "tmp", "vendor", "web"]
aircd web && yarn run build:watchlocalhost:3000
总结
fiberangularweb
fiberangularhtmlapiapiangular
完整demo,以上。