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 2 years ago • 1 comments

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