Skip to content

Commit

Permalink
Add flag to stop a running server
Browse files Browse the repository at this point in the history
  • Loading branch information
cluttrdev committed Mar 12, 2023
1 parent 5d8fc74 commit 87b9cf9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.2.0] - 2023-03-12

### Added

- Enable stopping server using cli flag

## [0.1.1] - 2023-02-27

### Fixed
Expand All @@ -15,7 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

Initial version, providing a working proof-of-concept.

[0.1.1]: https://github.com/cluttrdev/showdown/compare/v0.1.1...v0.1.0
[0.2.0]: https://github.com/cluttrdev/showdown/compare/v0.1.1...v0.2.0
[0.1.1]: https://github.com/cluttrdev/showdown/compare/v0.1.0...v0.1.1
[0.1.0]: https://github.com/cluttrdev/showdown/releases/tag/v0.1.0


6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ tar -zxf showdown_${RELEASE_TAG}_linux_x86_64.tar.gz showdown
install ./showdown /usr/local/bin/showdown
```

Alternatively, you can install it using the standard Go tools.

```shell
go install github.com/cluttrdev/showdown@latest
```

## Usage

To preview a Markdown formatted file `example.md` simply run
Expand Down
6 changes: 6 additions & 0 deletions cmd/showdown/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,9 @@ func (app *Application) render() ([]byte, error) {

return out, nil
}

func StopServer(port uint16) error {
url := fmt.Sprintf("http://127.0.0.1:%d/shutdown", port)
_, err := http.Get(url)
return err
}
21 changes: 17 additions & 4 deletions cmd/showdown/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,27 @@ import (
var cmd = &cobra.Command{
Use: "showdown [file]",
Short: "Live markdown previewer",
Args: cobra.ExactArgs(1),
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
file := args[0]

port, err := cmd.Flags().GetUint16("port")
if err != nil {
return err
}

stop, err := cmd.Flags().GetBool("stop")
if err != nil {
return err
}

if stop {
return StopServer(port)
}

if len(args) == 0 {
return fmt.Errorf("Missing file argument")
}
file := args[0]

app := NewApplication(file)
return app.run(port)
},
Expand All @@ -32,5 +44,6 @@ func main() {
}

func init() {
cmd.Flags().Uint16P("port", "p", 1337, "the port on which the server will listen")
cmd.Flags().Uint16P("port", "p", 1337, "the port the server listens on")
cmd.Flags().Bool("stop", false, "stop a running server")
}

0 comments on commit 87b9cf9

Please sign in to comment.