Skip to content

Commit

Permalink
introduce port and host flags, with updates to deploy workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
martinjirku committed Dec 18, 2023
1 parent 26e56d5 commit b8287d0
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 43 deletions.
4 changes: 2 additions & 2 deletions .air.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
args_bin = []
args_bin = ["--host", "localhost", "--public", "./dist"]
bin = "./tmp/main"
cmd = "go build -o ./tmp/main ./cmd/web/main.go"
cmd = "go build -o ./tmp/main ./main.go"
delay = 200
exclude_dir = ["assets", "tmp", "vendor", "node_modules", "styles", "public"]
exclude_file = []
Expand Down
26 changes: 1 addition & 25 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,6 @@ jobs:
build-mcmamina:
runs-on: ubuntu-latest
steps:
- uses: pnpm/action-setup@v2
with:
version: 8
- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: 20

- name: Install golang
uses: actions/setup-go@v4
with:
go-version: "1.21.5"

- name: ⬇️ Checkout repo
uses: actions/checkout@v2

- name: Install dependencies
run: pnpm install && pnpm build

- name: Install go dependencies
run: go mod download && go install github.com/a-h/templ/cmd/templ@latest

- name: Build BE
run: templ generate && go build -o mcmamina ./main.go

- uses: actions/checkout@v2
- uses: superfly/flyctl-actions/setup-flyctl@master
- run: flyctl deploy
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ tasks:
- npx vite build -w
serverwatch:
cmds:
- air --build.cmd "go build -o ./tmp/main ./main.go --public ./dist"
- air --build.cmd "go build -o ./tmp/main ."

buildserver:
cmds:
Expand Down
13 changes: 4 additions & 9 deletions fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@
#

app = "mcmamina-go"

kill_signal = "SIGINT"
kill_timeout = 5
# primary_region = "waw"

# [http_service]
# internal_port = 8080
# force_https = true
# auto_stop_machines = true
# auto_start_machines = true
# min_machines_running = 0
# processes = ["app"]

[[vm]]
cpu_kind = "shared"
Expand All @@ -21,11 +17,9 @@ app = "mcmamina-go"

[[services]]
internal_port = 8080
processes = ["mcmamina"]
protocol = "tcp"
script_checks = []


[services.concurrency]
hard_limit = 25
soft_limit = 20
Expand Down Expand Up @@ -54,4 +48,5 @@ path = "/healthcheck"
protocol = "http"
timeout = "2s"
tls_skip_verify = false

[services.http_checks.headers]
32 changes: 26 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,28 @@ func main() {
setupWebserver(log, calendarService)
}

type configuration struct {
publicPath string
port int
host string
}

func setupWebserver(log *slog.Logger, calendarService *services.CalendarService) {
config := configuration{}
router := mux.NewRouter()
var publicPath string
flag.StringVar(&publicPath, "public", "", "Usage description of the flag")
flag.StringVar(&config.publicPath, "public", "", "Usage description of the flag")
flag.StringVar(&config.host, "host", "0.0.0.0", "specify the app host")
flag.IntVar(&config.port, "port", 8080, "specfiy the port application will listen")
flag.Parse()

if !validatePort(config.port) {
log.Error("invalid port")
panic("invalid port")
}

var workingFolder fs.FS
log.Info("reading public folder")
if publicPath == "" {
if config.publicPath == "" {
log.Info("No public folder found, using embed FS")
log.Info("accessing embed FS")
folder, err := fs.Sub(distFS, "dist")
Expand All @@ -52,8 +65,8 @@ func setupWebserver(log *slog.Logger, calendarService *services.CalendarService)
}
workingFolder = folder
} else {
log.Info(fmt.Sprintf("serving specified location %s", publicPath))
workingFolder = os.DirFS(publicPath)
log.Info(fmt.Sprintf("serving specified location %s", config.publicPath))
workingFolder = os.DirFS(config.publicPath)
}

cssService := services.NewCSS(workingFolder, log)
Expand All @@ -72,7 +85,10 @@ func setupWebserver(log *slog.Logger, calendarService *services.CalendarService)
// MCMAMINA <<-- GENERATED CODE

handleFiles(router, http.FS(workingFolder))
http.ListenAndServe("0.0.0.0:8080", router)

addr := fmt.Sprintf("%s:%d", config.host, config.port)
log.Info(fmt.Sprintf("starting server at %s", addr))
http.ListenAndServe(addr, router)
}

func handleFiles(r *mux.Router, folder http.FileSystem) {
Expand Down Expand Up @@ -108,3 +124,7 @@ func getContentType(filePath string) string {

return contentType
}

func validatePort(port int) bool {
return port > 0 && port <= 65535
}

0 comments on commit b8287d0

Please sign in to comment.