Skip to content

Commit

Permalink
Use new practices
Browse files Browse the repository at this point in the history
  • Loading branch information
Divkix committed Mar 22, 2023
1 parent 986c4da commit 888bd97
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
10 changes: 5 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Build Stage: Build bot using the alpine image
# Build Stage: Build bot using the golang image
FROM golang:1.20-alpine AS builder
WORKDIR /app
COPY . .
RUN CGO_ENABLED=0 GOOS=`go env GOHOSTOS` GOARCH=`go env GOHOSTARCH` go build -o out/aliveimage

# Run Stage: Run bot using the bot and doppler binary copied from build stage
FROM alpine:3.17
COPY --from=builder /app/out/aliveimage /
CMD ["/aliveimage"]
# Final Stage: Use a smaller base image and only include necessary files
FROM scratch
COPY --from=builder /app/out/aliveimage /app/aliveimage
CMD ["/app/aliveimage"]
40 changes: 20 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,39 @@ import (
"github.com/uptrace/bunrouter"
)

// start is the time when the server started
var start time.Time

// PORT is the port the server is listening on
var PORT int = func() int {
port := os.Getenv("PORT")
if port == "" {
return 80
func main() {
// Use the log package's default logger
log.SetFlags(log.LstdFlags | log.Lmicroseconds)

// Get the port from the environment variable or default to 8080
port, err := strconv.Atoi(os.Getenv("PORT"))
if err != nil {
port = 8080
}
// convert string to int
returnPort, _ := strconv.Atoi(port)
return returnPort
}()

func main() {
// set the time when the server started
start = time.Now()
// create a new bunrouter
// Create a new bunrouter
router := bunrouter.New()

// add a route for the index
// Add a route for the index
router.GET("/", indexHandler)

// start the server
log.Printf("listening on http://localhost:%d\n", PORT)
// Start the server
log.Printf("Listening on :%d\n", port)

// listen and serve
log.Println(http.ListenAndServe(fmt.Sprintf(":%d", PORT), router))
// Use http.ListenAndServe() directly instead of logging the return value
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), router); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}

// indexHandler is the handler for the index route
func indexHandler(w http.ResponseWriter, req bunrouter.Request) error {
// return a JSON response with the status and uptime, truncated to seconds
// Set the content type to JSON
w.Header().Set("Content-Type", "application/json")

// Return a JSON response with the status and uptime, truncated to seconds
return bunrouter.JSON(w,
bunrouter.H{
"status": "alive",
Expand Down

0 comments on commit 888bd97

Please sign in to comment.