init: go api with CI/CD
All checks were successful
Build & Deploy Go API / build (push) Successful in 33s
All checks were successful
Build & Deploy Go API / build (push) Successful in 33s
This commit is contained in:
44
main.go
Normal file
44
main.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Response struct {
|
||||
Service string `json:"service"`
|
||||
Message string `json:"message"`
|
||||
Time string `json:"time"`
|
||||
Hostname string `json:"hostname"`
|
||||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
port = "8080"
|
||||
}
|
||||
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
hostname, _ := os.Hostname()
|
||||
resp := Response{
|
||||
Service: "demo-go-api",
|
||||
Message: "Hello from Go API!",
|
||||
Time: time.Now().Format(time.RFC3339),
|
||||
Hostname: hostname,
|
||||
Version: os.Getenv("VERSION"),
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(resp)
|
||||
})
|
||||
|
||||
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("ok"))
|
||||
})
|
||||
|
||||
log.Printf("Listening on :%s", port)
|
||||
log.Fatal(http.ListenAndServe(":"+port, nil))
|
||||
}
|
||||
Reference in New Issue
Block a user