improve flag parsing

This commit is contained in:
csehviktor
2025-08-02 00:09:43 +02:00
parent d4f5869642
commit bf40237ba6

28
main.go
View File

@@ -9,17 +9,28 @@ import (
"github.com/csehviktor/pastebin/store"
)
func main() {
addrPtr := flag.String("addr", ":3000", "port to listen on")
maxSizePtr := flag.Int("max-size", 32*1024, "maximum size of a paste in bytes")
type cli struct {
addr string
maxSize int64
}
func parse_flags() *cli {
cli := &cli{}
flag.StringVar(&cli.addr, "addr", ":3000", "socket address to bind to")
flag.Int64Var(&cli.maxSize, "max-size", 32*1024, "maximum size of a paste in bytes")
flag.Parse()
addr := *addrPtr
maxSize := int64(*maxSizePtr)
return cli
}
func main() {
cli := parse_flags()
mux := http.NewServeMux()
store := store.NewMemoryStore()
httpHandler := handler.NewHandler(store, maxSize)
httpHandler := handler.NewHandler(store, cli.maxSize)
mux.HandleFunc("GET /style.css", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "view/style.css")
@@ -29,8 +40,9 @@ func main() {
mux.HandleFunc("GET /{id}", httpHandler.HandleGet)
mux.HandleFunc("GET /{id}/{theme}", httpHandler.HandleGet)
slog.Info("starting http server", "addr", addr, "maxSize", maxSize)
err := http.ListenAndServe(addr, mux)
slog.Info("starting http server", "addr", cli.addr, "maxSize", cli.maxSize)
err := http.ListenAndServe(cli.addr, mux)
if err != nil {
panic(err)
}