Skip to content

Commit 1961646

Browse files
authored
Merge pull request #39 from evan-buss/development
OpenBooks v4
2 parents 3c426e9 + 50f4395 commit 1961646

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+5380
-1665
lines changed

.devcontainer/Dockerfile

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.191.0/containers/go/.devcontainer/base.Dockerfile
22

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

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

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

1915
# [Optional] Uncomment the next line to use go get to install anything else you need
2016
# RUN go get -x <your-dependency-or-tool>

.devcontainer/devcontainer.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
"build": {
66
"dockerfile": "Dockerfile",
77
"args": {
8-
// Update the VARIANT arg to pick a version of Go: 1, 1.16, 1.15
9-
"VARIANT": "1.16",
10-
// Options
8+
"VARIANT": "1.17",
119
"NODE_VERSION": "16"
1210
}
1311
},
@@ -25,7 +23,10 @@
2523
},
2624
// Add the IDs of extensions you want installed when the container is created.
2725
"extensions": [
28-
"golang.Go"
26+
"golang.Go",
27+
"eamodio.gitlens",
28+
"bradlc.vscode-tailwindcss",
29+
"esbenp.prettier-vscode"
2930
],
3031
// Use 'forwardPorts' to make a list of ports inside the container available locally.
3132
"forwardPorts": [

.github/home_v2.png

474 KB
Loading

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# start persist
22
openbooks
3+
!openbooks/
34
*.DS_Store
45
**/dist
56
# end persist

.vscode/launch.json

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Launch OpenBooks Server",
9+
"type": "go",
10+
"request": "launch",
11+
"mode": "debug",
12+
"program": "cmd/openbooks",
13+
"args": [
14+
"server",
15+
"--log",
16+
"--server",
17+
"localhost"
18+
],
19+
},
20+
{
21+
"name": "Launch OpenBooks CLI",
22+
"type": "go",
23+
"request": "launch",
24+
"mode": "debug",
25+
"program": "cmd/openbooks",
26+
"args": [
27+
"cli",
28+
"download",
29+
"--server",
30+
"localhost",
31+
"!test"
32+
],
33+
}
34+
]
35+
}

Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ COPY --from=web /web/ .
1313
ENV CGO_ENABLED=0
1414
RUN go get -d -v ./...
1515
RUN go install -v ./...
16+
WORKDIR /go/src/cmd/openbooks/
1617
RUN go build
1718

1819
FROM gcr.io/distroless/static as app
1920
WORKDIR /app
20-
COPY --from=build /go/src/openbooks .
21+
COPY --from=build /go/src/cmd/openbooks/openbooks .
2122

2223
EXPOSE 80
2324
VOLUME [ "/books" ]

README.md

+23-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

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

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

109
## Getting Started
1110

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

2726
### Setting the Base Path
2827

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

35+
## Usage
36+
37+
For a complete list of features use the `--help` flags on all subcommands.
38+
For example `openbooks cli --help or openbooks cli download --help`. There are
39+
two modes; Server or CLI. In CLI mode you interact and download books through
40+
a terminal interface. In server mode the application runs as a web application
41+
that you can visit in your browser.
42+
3643
## Development
3744

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

5259
- `go build`
5360

61+
### Mock Development Server
62+
63+
- The mock server allows you to debug responses and requests to simplified IRC / DCC
64+
servers that mimic the responses received from IRC Highway.
65+
-
66+
```bash
67+
cd cmd/mock_server
68+
go run .
69+
# Another Terminal
70+
cd cmd/openbooks
71+
go run . server --server localhost --log
72+
```
73+
5474
## Why / How
5575

5676
- 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.

build.sh

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ echo "Building React App."
44
cd server/app
55
npm install
66
npm run build
7-
cd ../..
7+
cd ../../cmd/openbooks
88

9-
echo "Building binaries for various platforms.";
10-
env CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o build/openbooks.exe
11-
env CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o build/openbooks_mac
12-
env CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -o build/openbooks_mac_arm
13-
env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/openbooks_linux
14-
env CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o build/openbooks_linux_arm
9+
echo "Building binaries for various platforms."
10+
env CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o ../../build/openbooks.exe
11+
env CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o ../../build/openbooks_mac
12+
env CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -o ../../build/openbooks_mac_arm
13+
env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ../../build/openbooks_linux
14+
env CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o ../../build/openbooks_linux_arm

cli/cli.go

+65-63
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,94 @@
11
package cli
22

33
import (
4-
"bufio"
4+
"context"
55
"fmt"
6-
"log"
7-
"os"
8-
"os/signal"
9-
"strings"
10-
"syscall"
116

127
"github.com/evan-buss/openbooks/core"
138
"github.com/evan-buss/openbooks/irc"
149
)
1510

16-
// Config is used to configure CLI mode settings.
1711
type Config struct {
1812
UserName string // Username to use when connecting to IRC
1913
Log bool // True if IRC messages should be logged
14+
Dir string
15+
Server string
16+
irc *irc.Conn
2017
}
2118

22-
// Reader is a way to recieve input from the user
23-
var reader *bufio.Reader
24-
25-
// IRC is the current IRC connection
26-
var conn *irc.Conn
27-
28-
// Start instantiates the OpenBooks CLI interface
29-
func Start(config Config) {
30-
conn := irc.New(config.UserName, "OpenBooks CLI")
31-
32-
c := make(chan os.Signal, 1)
33-
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
34-
go func() {
35-
<-c
36-
conn.Disconnect()
37-
os.Exit(1)
38-
}()
39-
19+
// StartInteractive instantiates the OpenBooks CLI interface
20+
func StartInteractive(config Config) {
4021
fmt.Println("=======================================")
4122
fmt.Println(" Welcome to OpenBooks ")
4223
fmt.Println("=======================================")
4324

44-
core.Join(conn)
25+
conn := instantiate(config)
26+
config.irc = conn
4527

46-
cwd, err := os.Getwd()
47-
if err != nil {
48-
log.Fatalln("Could not get current working directory.", err)
28+
ctx, cancel := context.WithCancel(context.Background())
29+
registerShutdown(conn, cancel)
30+
31+
handler := fullHandler(config)
32+
if config.Log {
33+
file := config.setupLogger(handler)
34+
defer file.Close()
4935
}
5036

51-
exitSignal := make(chan struct{})
52-
go core.ReadDaemon(conn, config.Log, Handler{cwd}, exitSignal)
37+
go core.StartReader(ctx, conn, handler)
38+
terminalMenu(conn)
5339

54-
fmt.Println("Connection established...")
40+
<-ctx.Done()
41+
}
5542

56-
fmt.Print("\r")
43+
func StartDownload(config Config, download string) {
44+
conn := instantiate(config)
45+
defer conn.Close()
46+
ctx, cancel := context.WithCancel(context.Background())
5747

58-
reader = bufio.NewReader(os.Stdin)
48+
handler := core.EventHandler{}
49+
handler[core.BookResult] = func(text string) {
50+
fmt.Printf("%sReceived file response.\n", clearLine)
51+
config.downloadHandler(text)
52+
cancel()
53+
}
54+
if config.Log {
55+
file := config.setupLogger(handler)
56+
defer file.Close()
57+
}
58+
59+
fmt.Printf("Sending download request.")
60+
go core.StartReader(ctx, conn, handler)
61+
core.DownloadBook(conn, download)
62+
fmt.Printf("%sSent download request.", clearLine)
63+
fmt.Printf("Waiting for file response.")
5964

60-
// Get the first input
61-
// Reader, IRC
62-
menu()
63-
// We make a channel to block forever. We want the reader daemon to run forever
64-
<-exitSignal
65+
registerShutdown(conn, cancel)
66+
<-ctx.Done()
6567
}
6668

67-
//reader *bufio.Reader, irc *irc.Conn
68-
func menu() {
69-
fmt.Print("\ns)search\ng)et book\nd)one\n~> ")
70-
71-
input, _ := reader.ReadString('\n')
72-
input = strings.TrimRight(input, "\n")
73-
input = strings.TrimRight(input, "\r")
74-
75-
switch input {
76-
case "s":
77-
fmt.Print("@search ")
78-
message, _ := reader.ReadString('\n')
79-
core.SearchBook(conn, message)
80-
case "g":
81-
fmt.Print("Download String: ")
82-
message, _ := reader.ReadString('\n')
83-
core.DownloadBook(conn, message)
84-
case "d":
85-
fmt.Println("Disconnecting.")
86-
conn.Disconnect()
87-
os.Exit(0)
88-
default:
89-
fmt.Println("Invalid Selection.")
90-
menu()
69+
func StartSearch(config Config, query string) {
70+
conn := instantiate(config)
71+
defer conn.Close()
72+
ctx, cancel := context.WithCancel(context.Background())
73+
74+
handler := core.EventHandler{}
75+
handler[core.SearchResult] = func(text string) {
76+
fmt.Printf("%sReceived file response.\n", clearLine)
77+
config.searchHandler(text)
78+
cancel()
9179
}
80+
handler[core.MatchesFound] = config.matchesFoundHandler
81+
if config.Log {
82+
file := config.setupLogger(handler)
83+
defer file.Close()
84+
}
85+
86+
fmt.Printf("Sending search request.")
87+
go core.StartReader(ctx, conn, handler)
88+
core.SearchBook(conn, query)
89+
fmt.Printf("%sSent search request.", clearLine)
90+
fmt.Printf("Waiting for file response.")
91+
92+
registerShutdown(conn, cancel)
93+
<-ctx.Done()
9294
}

0 commit comments

Comments
 (0)