diff --git a/.air.toml b/.air.toml index a09d3ac..f467c2d 100644 --- a/.air.toml +++ b/.air.toml @@ -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 = [] diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 32b4cce..a2151ba 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -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 diff --git a/Taskfile.yml b/Taskfile.yml index 47ef6ec..49d3d73 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -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: diff --git a/fly.toml b/fly.toml index 8b0ff5d..18433dc 100644 --- a/fly.toml +++ b/fly.toml @@ -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" @@ -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 @@ -54,4 +48,5 @@ path = "/healthcheck" protocol = "http" timeout = "2s" tls_skip_verify = false + [services.http_checks.headers] diff --git a/main.go b/main.go index 6c6df5d..89e15e9 100644 --- a/main.go +++ b/main.go @@ -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") @@ -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) @@ -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) { @@ -108,3 +124,7 @@ func getContentType(filePath string) string { return contentType } + +func validatePort(port int) bool { + return port > 0 && port <= 65535 +}