시리즈: Go
go
60 줄
· 업데이트 2026-04-18
main.go
Go/Baasid/cmd/server/main.go
// Package main bootstraps the Baasid HTTP API server.
//
// @title Baasid Backend API
// @version 1.1
// @description 用戶登入成功後,Server 產生 JWT;之後商品 CRUD 請求皆須於 **Header** 帶入 `Authorization: Bearer <token>`。
// @termsOfService http://swagger.io/terms/
//
// @contact.name API Support
// @contact.url https://github.com/example/baasid
// @contact.email support@example.com
//
// @license.name MIT
// @license.url https://opensource.org/licenses/MIT
//
// @host localhost:8080
// @BasePath /
//
// @securityDefinitions.apikey BearerAuth
// @in header
// @name Authorization
// @description 填入 `Bearer ` 後接 JWT,例如:`Bearer eyJhbGciOiJIUzI1NiIs...`
package main
import (
"log"
"baasid/internal/config"
"baasid/internal/database"
"baasid/internal/router"
// Generated OpenAPI docs (run `make swagger` or `swag init` after changing annotations).
_ "baasid/docs"
"github.com/joho/godotenv"
)
func main() {
// Load key=value pairs from a local .env file into the process environment.
// This call is intentionally silent when .env is missing so production
// can rely purely on real environment variables.
if err := godotenv.Load(); err != nil {
log.Printf("main: no .env loaded (%v) — using OS environment only", err)
}
cfg, err := config.Load()
if err != nil {
log.Fatalf("config: %v", err)
}
db, err := database.Connect(cfg.DatabaseURL)
if err != nil {
log.Fatalf("database: %v", err)
}
e := router.NewEcho(cfg, db)
log.Printf("HTTP listening on %s (Swagger: http://127.0.0.1%s/swagger/index.html)\n", cfg.ServerAddr, cfg.ServerAddr)
if err := e.Start(cfg.ServerAddr); err != nil {
log.Fatalf("server: %v", err)
}
}
관련 글
Go
go
업데이트 2026-04-18
main.go
main.go — go source code from the Go learning materials (Go/Baasid/cmd/hashpw/main.go).
글 읽기 →
Go
go
업데이트 2026-04-18
docs.go
docs.go — go source code from the Go learning materials (Go/Baasid/docs/docs.go).
글 읽기 →
Go
go
업데이트 2026-04-18
jwt.go
jwt.go — go source code from the Go learning materials (Go/Baasid/internal/auth/jwt.go).
글 읽기 →
Go
go
업데이트 2026-04-18
password.go
password.go — go source code from the Go learning materials (Go/Baasid/internal/auth/password.go).
글 읽기 →
Go
go
업데이트 2026-04-18
password_test.go
password_test.go — go source code from the Go learning materials (Go/Baasid/internal/auth/password_test.go).
글 읽기 →
Go
go
업데이트 2026-04-18
config.go
config.go — go source code from the Go learning materials (Go/Baasid/internal/config/config.go).
글 읽기 →