package main

import (
	"fmt"
	"io"
	"net/http"
	"os"
	"time"
)

func clock(w http.ResponseWriter, r *http.Request) {
	currentTime := float64(time.Now().UnixMicro())/float64(1e6)
	io.WriteString(w, fmt.Sprintf("%07f", currentTime))
}

func main() {
	port, ok := os.LookupEnv("BACKEND_PORT")
	if !ok {
		//Default value 5050
		port = "5050"
	}

	mux := http.NewServeMux()

	mux.HandleFunc("/clock", clock)

	fmt.Printf("Starting backend server in port %s\n", port)
	err := http.ListenAndServe(fmt.Sprintf(":%s", port), mux)
	if err != nil {
		fmt.Printf("Failed to start server: %v\n", err)
	}
}
