123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- package cli
- import (
- "net"
- "net/http"
- "github.com/annybs/ezdb"
- "github.com/annybs/shorty/internal/api"
- "github.com/annybs/shorty/internal/types"
- )
- type StartCmd struct {
- AllPath string `help:"All-path (SHORTY_ALL_PATH)" default:"${all_path}"`
- DatabasePath string `short:"d" help:"Database path (SHORTY_DATABASE_PATH)" default:"${database_path}"`
- Host string `short:"h" help:"HTTP bind host (SHORTY_HOST)" default:"${host}"`
- Port string `short:"p" help:"HTTP port (SHORTY_PORT)" default:"${port}"`
- Token string `short:"t" help:"Bearer token (SHORTY_TOKEN)" default:"${token}"`
- }
- func (c *StartCmd) Run(ctx *Context) error {
- errc := make(chan error)
- marshaler := ezdb.JSON[*types.Redirect](func() *types.Redirect {
- return &types.Redirect{}
- })
- db := ezdb.Memory(
- ezdb.LevelDB(c.DatabasePath, marshaler, nil),
- )
- if err := db.Open(); err != nil {
- return err
- }
- a := &api.API{
- DB: db,
- Log: ctx.Log,
- AllPath: c.AllPath,
- Token: c.Token,
- }
- addr := net.JoinHostPort(c.Host, c.Port)
- go func() {
- errc <- http.ListenAndServe(addr, a)
- }()
- ctx.Log.Info().Msgf("Listening at %s", addr)
- return <-errc
- }
|