mirror of
https://github.com/skidoodle/pastebin
synced 2025-10-14 09:44:48 +02:00
improve flag parsing
This commit is contained in:
28
main.go
28
main.go
@@ -9,17 +9,28 @@ import (
|
|||||||
"github.com/csehviktor/pastebin/store"
|
"github.com/csehviktor/pastebin/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
type cli struct {
|
||||||
addrPtr := flag.String("addr", ":3000", "port to listen on")
|
addr string
|
||||||
maxSizePtr := flag.Int("max-size", 32*1024, "maximum size of a paste in bytes")
|
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()
|
flag.Parse()
|
||||||
|
|
||||||
addr := *addrPtr
|
return cli
|
||||||
maxSize := int64(*maxSizePtr)
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cli := parse_flags()
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
store := store.NewMemoryStore()
|
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) {
|
mux.HandleFunc("GET /style.css", func(w http.ResponseWriter, r *http.Request) {
|
||||||
http.ServeFile(w, r, "view/style.css")
|
http.ServeFile(w, r, "view/style.css")
|
||||||
@@ -29,8 +40,9 @@ func main() {
|
|||||||
mux.HandleFunc("GET /{id}", httpHandler.HandleGet)
|
mux.HandleFunc("GET /{id}", httpHandler.HandleGet)
|
||||||
mux.HandleFunc("GET /{id}/{theme}", httpHandler.HandleGet)
|
mux.HandleFunc("GET /{id}/{theme}", httpHandler.HandleGet)
|
||||||
|
|
||||||
slog.Info("starting http server", "addr", addr, "maxSize", maxSize)
|
slog.Info("starting http server", "addr", cli.addr, "maxSize", cli.maxSize)
|
||||||
err := http.ListenAndServe(addr, mux)
|
|
||||||
|
err := http.ListenAndServe(cli.addr, mux)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user