sample-golang icon indicating copy to clipboard operation
sample-golang copied to clipboard

Deploy simple hello world golang app using digitalocean app platform

Open TawsifKarim opened this issue 3 years ago • 1 comments
trafficstars

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)

}

TawsifKarim avatar Aug 13 '22 07:08 TawsifKarim

@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)
	}

thinkverse avatar Feb 26 '23 05:02 thinkverse