안타깝게도 포트폴리오 배포를 위해 유용하게 사용됐던 Heroku 를 더 이상 사용할 수 없게 되어 대체하여 사용할 수 있는 다른 사이트를 검색하였다.
그 중 가장 많은 사람들이 이야기해준 Fly.io 를 살펴보았다.
우선 공식문서에서 Go 언어에서 적용하는 방법을 검색했고, 내용은 아래와 같다.
Fly.io 도 heroku 와 마찬가지로 배포 가능한 이미지로 패키징한다.
예제를 따라 하면서 사용방법을 알아보았다.
우선 아래 명령어를 CLI 에 입력하여 Go 언어 연습용 코드를 다운 받는다.
$ git clone <https://github.com/fly-apps/go-example>
package main
import (
"embed"
"html/template"
"log"
"net/http"
"os"
)
//go:embed templates/*
var resources embed.FS
var t = template.Must(template.ParseFS(resources, "templates/*"))
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
data := map[string]string{
"Region": os.Getenv("FLY_REGION"),
}
t.ExecuteTemplate(w, "index.html.tmpl", data)
})
log.Println("listening on", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<h1>Hello from Fly</h1>
{{ if .Region }}
<h2>I'm running in the {{.Region}} region</h2>
{{end}}
</body>
</html>
$ go build