Skip to content

Commit

Permalink
Merge pull request #39 from evan-buss/development
Browse files Browse the repository at this point in the history
OpenBooks v4
  • Loading branch information
evan-buss committed Sep 19, 2021
2 parents 3c426e9 + 50f4395 commit 1961646
Show file tree
Hide file tree
Showing 81 changed files with 5,380 additions and 1,665 deletions.
12 changes: 4 additions & 8 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.191.0/containers/go/.devcontainer/base.Dockerfile

# [Choice] Go version: 1, 1.16, 1.15
ARG VARIANT="1.16"
# NOTE: Temporary workaround because Microsoft doesn't publish
# ARM images for their devcontainers yet. AMD64 containers
# use way too much CPU on M1 so I've built the image for ARM.
# FROM mcr.microsoft.com/vscode/devcontainers/go:0-${VARIANT}
FROM evanbuss/devcontainers-go:latest
ARG VARIANT="1.17"
FROM mcr.microsoft.com/vscode/devcontainers/go:${VARIANT}

# [Choice] Node.js version: none, lts, 16, 14, 12, 10
ARG NODE_VERSION="none"
RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi

# [Optional] Uncomment this section to install additional OS packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
# && apt-get -y install --no-install-recommends <your-package-list-here>
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& apt-get -y install --no-install-recommends tmux

# [Optional] Uncomment the next line to use go get to install anything else you need
# RUN go get -x <your-dependency-or-tool>
Expand Down
9 changes: 5 additions & 4 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
"build": {
"dockerfile": "Dockerfile",
"args": {
// Update the VARIANT arg to pick a version of Go: 1, 1.16, 1.15
"VARIANT": "1.16",
// Options
"VARIANT": "1.17",
"NODE_VERSION": "16"
}
},
Expand All @@ -25,7 +23,10 @@
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"golang.Go"
"golang.Go",
"eamodio.gitlens",
"bradlc.vscode-tailwindcss",
"esbenp.prettier-vscode"
],
// Use 'forwardPorts' to make a list of ports inside the container available locally.
"forwardPorts": [
Expand Down
Binary file added .github/home_v2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# start persist
openbooks
!openbooks/
*.DS_Store
**/dist
# end persist
Expand Down
35 changes: 35 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch OpenBooks Server",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "cmd/openbooks",
"args": [
"server",
"--log",
"--server",
"localhost"
],
},
{
"name": "Launch OpenBooks CLI",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "cmd/openbooks",
"args": [
"cli",
"download",
"--server",
"localhost",
"!test"
],
}
]
}
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ COPY --from=web /web/ .
ENV CGO_ENABLED=0
RUN go get -d -v ./...
RUN go install -v ./...
WORKDIR /go/src/cmd/openbooks/
RUN go build

FROM gcr.io/distroless/static as app
WORKDIR /app
COPY --from=build /go/src/openbooks .
COPY --from=build /go/src/cmd/openbooks/openbooks .

EXPOSE 80
VOLUME [ "/books" ]
Expand Down
26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

Openbooks allows you to download ebooks from irc.irchighway.net quickly and easily.

![home](https://raw.githubusercontent.com/evan-buss/openbooks/master/.github/home.png)
![search results](https://raw.githubusercontent.com/evan-buss/openbooks/master/.github/search.png)
![openbooks](https://raw.githubusercontent.com/evan-buss/openbooks/master/.github/home_v2.png)

## Getting Started

Expand All @@ -22,7 +21,7 @@ Openbooks allows you to download ebooks from irc.irchighway.net quickly and easi
- Basic config
- `docker run -p 8080:80 evanbuss/openbooks`
- Config to perist all eBook files to disk
- `docker run -p 8080:80 -v /home/evan/Downloads/books:/books evanbuss/openbooks --persist`
- `docker run -p 8080:80 -v /home/evan/Downloads/openbooks:/books evanbuss/openbooks --persist`

### Setting the Base Path

Expand All @@ -33,6 +32,14 @@ OpenBooks server doesn't have to be hosted at the root of your webserver. The ba
- Binary
- `./openbooks server --basepath /openbooks/`

## Usage

For a complete list of features use the `--help` flags on all subcommands.
For example `openbooks cli --help or openbooks cli download --help`. There are
two modes; Server or CLI. In CLI mode you interact and download books through
a terminal interface. In server mode the application runs as a web application
that you can visit in your browser.

## Development

### Install the dependencies
Expand All @@ -51,6 +58,19 @@ OpenBooks server doesn't have to be hosted at the root of your webserver. The ba

- `go build`

### Mock Development Server

- The mock server allows you to debug responses and requests to simplified IRC / DCC
servers that mimic the responses received from IRC Highway.
-
```bash
cd cmd/mock_server
go run .
# Another Terminal
cd cmd/openbooks
go run . server --server localhost --log
```

## Why / How

- I wrote this as an easier way to search and download books from irchighway.net. It handles all the extraction and data processing for you. You just have to click the book you want. Hopefully you find it much easier than the IRC interface.
Expand Down
14 changes: 7 additions & 7 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ echo "Building React App."
cd server/app
npm install
npm run build
cd ../..
cd ../../cmd/openbooks

echo "Building binaries for various platforms.";
env CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o build/openbooks.exe
env CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o build/openbooks_mac
env CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -o build/openbooks_mac_arm
env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/openbooks_linux
env CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o build/openbooks_linux_arm
echo "Building binaries for various platforms."
env CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o ../../build/openbooks.exe
env CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o ../../build/openbooks_mac
env CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -o ../../build/openbooks_mac_arm
env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ../../build/openbooks_linux
env CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o ../../build/openbooks_linux_arm
128 changes: 65 additions & 63 deletions cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,92 +1,94 @@
package cli

import (
"bufio"
"context"
"fmt"
"log"
"os"
"os/signal"
"strings"
"syscall"

"github.com/evan-buss/openbooks/core"
"github.com/evan-buss/openbooks/irc"
)

// Config is used to configure CLI mode settings.
type Config struct {
UserName string // Username to use when connecting to IRC
Log bool // True if IRC messages should be logged
Dir string
Server string
irc *irc.Conn
}

// Reader is a way to recieve input from the user
var reader *bufio.Reader

// IRC is the current IRC connection
var conn *irc.Conn

// Start instantiates the OpenBooks CLI interface
func Start(config Config) {
conn := irc.New(config.UserName, "OpenBooks CLI")

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
conn.Disconnect()
os.Exit(1)
}()

// StartInteractive instantiates the OpenBooks CLI interface
func StartInteractive(config Config) {
fmt.Println("=======================================")
fmt.Println(" Welcome to OpenBooks ")
fmt.Println("=======================================")

core.Join(conn)
conn := instantiate(config)
config.irc = conn

cwd, err := os.Getwd()
if err != nil {
log.Fatalln("Could not get current working directory.", err)
ctx, cancel := context.WithCancel(context.Background())
registerShutdown(conn, cancel)

handler := fullHandler(config)
if config.Log {
file := config.setupLogger(handler)
defer file.Close()
}

exitSignal := make(chan struct{})
go core.ReadDaemon(conn, config.Log, Handler{cwd}, exitSignal)
go core.StartReader(ctx, conn, handler)
terminalMenu(conn)

fmt.Println("Connection established...")
<-ctx.Done()
}

fmt.Print("\r")
func StartDownload(config Config, download string) {
conn := instantiate(config)
defer conn.Close()
ctx, cancel := context.WithCancel(context.Background())

reader = bufio.NewReader(os.Stdin)
handler := core.EventHandler{}
handler[core.BookResult] = func(text string) {
fmt.Printf("%sReceived file response.\n", clearLine)
config.downloadHandler(text)
cancel()
}
if config.Log {
file := config.setupLogger(handler)
defer file.Close()
}

fmt.Printf("Sending download request.")
go core.StartReader(ctx, conn, handler)
core.DownloadBook(conn, download)
fmt.Printf("%sSent download request.", clearLine)
fmt.Printf("Waiting for file response.")

// Get the first input
// Reader, IRC
menu()
// We make a channel to block forever. We want the reader daemon to run forever
<-exitSignal
registerShutdown(conn, cancel)
<-ctx.Done()
}

//reader *bufio.Reader, irc *irc.Conn
func menu() {
fmt.Print("\ns)search\ng)et book\nd)one\n~> ")

input, _ := reader.ReadString('\n')
input = strings.TrimRight(input, "\n")
input = strings.TrimRight(input, "\r")

switch input {
case "s":
fmt.Print("@search ")
message, _ := reader.ReadString('\n')
core.SearchBook(conn, message)
case "g":
fmt.Print("Download String: ")
message, _ := reader.ReadString('\n')
core.DownloadBook(conn, message)
case "d":
fmt.Println("Disconnecting.")
conn.Disconnect()
os.Exit(0)
default:
fmt.Println("Invalid Selection.")
menu()
func StartSearch(config Config, query string) {
conn := instantiate(config)
defer conn.Close()
ctx, cancel := context.WithCancel(context.Background())

handler := core.EventHandler{}
handler[core.SearchResult] = func(text string) {
fmt.Printf("%sReceived file response.\n", clearLine)
config.searchHandler(text)
cancel()
}
handler[core.MatchesFound] = config.matchesFoundHandler
if config.Log {
file := config.setupLogger(handler)
defer file.Close()
}

fmt.Printf("Sending search request.")
go core.StartReader(ctx, conn, handler)
core.SearchBook(conn, query)
fmt.Printf("%sSent search request.", clearLine)
fmt.Printf("Waiting for file response.")

registerShutdown(conn, cancel)
<-ctx.Done()
}

0 comments on commit 1961646

Please sign in to comment.