Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deploy simple hello world golang app using digitalocean app platform #9

Open
TawsifKarim opened this issue Aug 13, 2022 · 1 comment

Comments

@TawsifKarim
Copy link

Hello, I am new to digitalocean app platform. I have created a simple hello world golang application and tried to deploy it with digitalocean. But I am getting Deploy Error: Health checks. The reason im posting this in this repo, is that, this repo works perfectly fine with digitalocean app platform. But mine one doesnt. I need to know which step am I missing.

my go.mod file:

module github.com/tawsifkarim/do-app

go 1.17

require github.com/gorilla/mux v1.8.0

Here is my code:

package main

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/gorilla/mux"
)

func main() {
	r := mux.NewRouter()

	r.HandleFunc("/", rootHandler)

	port := os.Getenv("PORT")
	if port == "" {
		port = "80"
	}

	fmt.Println("starting server at port:", port)

	err := http.ListenAndServe("localhost:"+port, r)
	if err != nil {
		log.Fatal("Something went wrong starting the server", err)
	}
}

type Person struct {
	Name    string `json:"name"`
	Purpose string `json:"purpose"`
}

func rootHandler(w http.ResponseWriter, r *http.Request) {
	defer r.Body.Close()

	x := Person{
		Name:    "tawsif",
		Purpose: "testing do app platform",
	}

	bt, err := json.Marshal(x)
	if err != nil {
		fmt.Println("json marshal error", err.Error())
	}

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusOK)
	w.Write(bt)

}

@thinkverse
Copy link
Contributor

thinkverse commented Feb 26, 2023

@TawsifKarim I know this is really late, but the issue is your ListenAndServe call, you shouldn't specify localhost. That is a loopback interface and is not exposed externally. You can read more in "How to Troubleshoot Apps in App Platform", specifically the HTTP Service on localhost vs 0.0.0.0.

	fmt.Println("starting server at port:", port)

-	err := http.ListenAndServe("localhost:"+port, r)
+	err := http.ListenAndServe(":"+port, r)
	if err != nil {
		log.Fatal("Something went wrong starting the server", err)
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants