🚧 Created sample helloworld HTTP server

This commit is contained in:
Maxim Lebedev 2023-11-08 02:04:17 +06:00
parent af56446472
commit f37554eb00
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
3 changed files with 98 additions and 0 deletions

2
go.mod
View File

@ -1,3 +1,5 @@
module source.toby3d.me/toby3d/home
go 1.21.3
require github.com/caarlos0/env/v10 v10.0.0

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/caarlos0/env/v10 v10.0.0 h1:yIHUBZGsyqCnpTkbjk8asUlx6RFhhEs+h7TOBdgdzXA=
github.com/caarlos0/env/v10 v10.0.0/go.mod h1:ZfulV76NvVPw3tm591U4SwL3Xx9ldzBP9aGxzeN7G18=

94
main.go Normal file
View File

@ -0,0 +1,94 @@
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"runtime"
"runtime/pprof"
"syscall"
"github.com/caarlos0/env/v10"
"source.toby3d.me/toby3d/home/internal/domain"
)
var (
config = new(domain.Config)
logger = log.New(os.Stdout, "home\t", log.Lmsgprefix|log.LstdFlags|log.LUTC)
)
var cpuProfilePath, memProfilePath string
func init() {
flag.StringVar(&cpuProfilePath, "cpuprofile", "", "set path to saving CPU memory profile")
flag.StringVar(&memProfilePath, "memprofile", "", "set path to saving pprof memory profile")
flag.Parse()
if err := env.ParseWithOptions(config, env.Options{
Prefix: "HOME_",
}); err != nil {
logger.Fatalln("cannot unmarshal configuration into domain:", err)
}
}
func main() {
ctx := context.Background()
server := &http.Server{
Addr: config.AddrPort().String(),
Handler: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprintln(w, "hello, world!")
}),
}
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
if cpuProfilePath != "" {
cpuProfile, err := os.Create(cpuProfilePath)
if err != nil {
logger.Fatalln("could not create CPU profile:", err)
}
defer cpuProfile.Close()
if err = pprof.StartCPUProfile(cpuProfile); err != nil {
logger.Fatalln("could not start CPU profile:", err)
}
defer pprof.StopCPUProfile()
}
go func(server *http.Server) {
logger.Printf("starting server on %d...", config.Port)
if err := server.ListenAndServe(); err != nil {
logger.Fatalln("cannot listen and serve server:", err)
}
}(server)
<-done
if err := server.Shutdown(ctx); err != nil {
logger.Fatalln("failed shutdown server:", err)
}
if memProfilePath == "" {
return
}
memProfile, err := os.Create(memProfilePath)
if err != nil {
logger.Fatalln("could not create memory profile:", err)
}
defer memProfile.Close()
runtime.GC() // NOTE(toby3d): get up-to-date statistics
if err = pprof.WriteHeapProfile(memProfile); err != nil {
logger.Fatalln("could not write memory profile:", err)
}
}