Browse Source

Add download endpoint

master
Josias 1 year ago
parent
commit
da3ce5e69d
4 changed files with 129 additions and 54 deletions
  1. +23
    -19
      README.md
  2. +31
    -0
      download/download.go
  3. +73
    -33
      main.go
  4. +2
    -2
      text/get.go

+ 23
- 19
README.md View File

@@ -1,6 +1,6 @@
# bibleget

A Go API for Heb12 that makes reading the Bible over the Internet simple.
A Go API for Heb12 that makes reading the Bible over the Web simple.

It is basically a web overlay for [heb12/heb12](https://code.heb12.com/Heb12/heb12).

@@ -15,7 +15,9 @@ It is basically a web overlay for [heb12/heb12](https://code.heb12.com/Heb12/heb
## Routes

- `/get` returns Bible text from references
- `/download` returns a URL for a whole OSIS work
- `/download** returns a URL for a whole OSIS work

**Note**: The endpoints are subject to change at any time, and are not yet stable.

### Get

@@ -28,22 +30,24 @@ Return example:
```
{
"errors": [],
"text": [
{
"version": "web",
"language": "en",
"reference": "Heb 4.12-13",
"book": "Heb",
"chapter": 4,
"from": 12,
"to": 13,
"wholeChapter": false,
"verses": [
"For the word of God is living, and active, and sharper than any two-edged sword, and piercing even to the dividing of soul and spirit, of both joints and marrow, and quick to discern the thoughts and intents of the heart.",
"There is no creature that is hidden from his sight, but all things are naked and laid open before the eyes of him with whom we have to do."
]
}
]
"data": {
"text": [
{
"version": "web",
"language": "en",
"reference": "Heb 4.12-13",
"book": "Heb",
"chapter": 4,
"from": 12,
"to": 13,
"wholeChapter": false,
"verses": [
"For the word of God is living, and active, and sharper than any two-edged sword, and piercing even to the dividing of soul and spirit, of both joints and marrow, and quick to discern the thoughts and intents of the heart.",
"There is no creature that is hidden from his sight, but all things are naked and laid open before the eyes of him with whom we have to do."
]
}
]
}
}
```

@@ -98,7 +102,7 @@ Blank quotes (`""`) means it uses the default Heb12 Gratis Bible directory (`~/.

## License

Copyright (C) 2020 Josias Allestad <justjosias@tutanota.com>
Copyright (C) 2020 Josias Allestad <justjosias@tutanota.com> and other Heb12 contributors

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by


+ 31
- 0
download/download.go View File

@@ -0,0 +1,31 @@
package download

import (
"os"
"strings"

"code.heb12.com/heb12/heb12/bible"
)

// GetFile returns the path of a tar file for the provided Bible version
func GetFile(bibleDir string, version string) (string, error) {
// We are using heb12/bible to get the manager because it has a useful trick for use with heb12/config.
// TODO: make this irrelevant with a separate module (or probably having it implemented in heb12/config)
hbible, err := bible.New(bibleDir)
if err != nil {
return "", err
}
manager := hbible.Manager

path, err := manager.GetPathShort(version)
if err != nil {
return "", err
}
// Since the former gives a path, remove the trailing slash and add ".tar" to the path
path = strings.TrimRight(path, "/") + ".tar"

// Make sure the file exists and is okay first
_, err = os.Stat(path)

return path, err
}

+ 73
- 33
main.go View File

@@ -6,76 +6,117 @@ import (
"io/ioutil"
"log"
"net/http"
"path"
"strconv"
"strings"

"github.com/pelletier/go-toml"

"code.heb12.com/heb12/bibleget/download"
"code.heb12.com/heb12/bibleget/text"
)

// will be moved to a configuration file later
const (
Port string = "5500"
DefaultVersion string = "web"
BibleDir string = ""
)

const configFile = "config.toml"

// Config is configuration for bibleget from the configFile
type Config struct {
Server Server
Bible Bible
Server server
Bible bible
}
type Server struct {
type server struct {
Port int
}
type Bible struct {
type bible struct {
Version string
Dir string
}

// Response is the data that is returned to the user on any request to bibleget
type Response struct {
Errors []string `json:"errors"`
Text []text.Text `json:"text"`
Errors []string `json:"errors"`
Data map[string]interface{} `json:"data"`
}

func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Nothing to see here. Something will be written later\n")
// Handler is the http.ResponseWriter and http.Requst data for the rest of the program
type Handler struct {
w http.ResponseWriter
r *http.Request
}

func getbible(w http.ResponseWriter, r *http.Request) {
errs := []string{}
err := r.ParseForm()
if err != nil {
log.Print(err)
errs = append(errs, err.Error()) //"Failed to parse GET arguments")
func shiftPath(p string) (head, tail string) {
p = path.Clean("/" + p)
i := strings.Index(p[1:], "/") + 1
if i <= 0 {
return p[1:], "/"
}
return p[1:i], p[i:]
}

func handler(w http.ResponseWriter, r *http.Request) {
config, err := readConfig()
if err != nil {
log.Fatal(err)
}

var head string
head, r.URL.Path = shiftPath(r.URL.Path)

var b []byte
errs := []string{}

var response Response
response.Data = make(map[string]interface{})

err = r.ParseForm()
if err != nil {
log.Print(err)
return
errs = append(errs, err.Error())
}

var version string
if len(r.Form["version"]) != 0 {
version = r.Form["version"][0]
} else {
version = DefaultVersion
version = config.Bible.Version
}

text, err := text.GetText(config.Bible.Dir, r.Form["reference"][0], version)
if err != nil {
log.Print(err)
errs = append(errs, err.Error()) //"Failed to get text")
switch head {
case "get":
var tmpText []text.Text
if len(r.Form["reference"]) == 0 {
errs = append(errs, "No reference provided")
} else {

tmpText, err = text.GetText(config.Bible.Dir, r.Form["reference"][0], version)
if err != nil {
log.Print(err)
errs = append(errs, err.Error())
}

}

response.Data["text"] = tmpText

case "download":
fmt.Println(config.Bible.Dir)

bibleTar, err := download.GetFile(config.Bible.Dir, version)
if err != nil {
errs = append(errs, "Failed to find tar file for "+version)
} else {
http.ServeFile(w, r, bibleTar)
}

default:

}
w.Header().Add("Content-Type", "application/json")

response := Response{
Errors: errs,
Text: text,
response.Errors = errs
b, err = json.MarshalIndent(response, "", " ")
if err != nil {
log.Fatal(err)
}
b, err := json.Marshal(response)
w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, "%v", string(b))
}

@@ -86,7 +127,6 @@ func main() {
}

http.HandleFunc("/", handler)
http.HandleFunc("/get/", getbible)

log.Println("Running on port", config.Server.Port)
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(config.Server.Port), nil))


+ 2
- 2
text/get.go View File

@@ -8,7 +8,7 @@ import (
"fmt"
)

// Text contains information about
// Text contains information about the text returned and the verses themselves
type Text struct {
Version string `json:"version"`
Language string `json:"language"`
@@ -51,7 +51,7 @@ func GetText(bibleDir string, reference string, version string) ([]Text, error)
}

return []Text{
Text{
{
Version: version,
Language: language,
Reference: strRef,


Loading…
Cancel
Save