Quellcode durchsuchen

Use config file instead of hardcoded values

master
Ted Jameson vor 1 Jahr
Ursprung
Commit
947092a3fd
7 geänderte Dateien mit 90 neuen und 19 gelöschten Zeilen
  1. +2
    -0
      .gitignore
  2. +23
    -14
      README.md
  3. +6
    -0
      example.config.toml
  4. +1
    -0
      go.mod
  5. +6
    -0
      go.sum
  6. +48
    -2
      main.go
  7. +4
    -3
      text/get.go

+ 2
- 0
.gitignore Datei anzeigen

@@ -1 +1,3 @@
*~
bibleget
config.toml

+ 23
- 14
README.md Datei anzeigen

@@ -27,6 +27,7 @@ Example:
Return example:
```
{
"version": "web",
"text": [
{
"reference": "Heb 4.12-13",
@@ -43,7 +44,7 @@ Return example:
}
```

## Installation
## Quick Install

This is a basic installation guide (for a server).

@@ -51,23 +52,14 @@ Prerequisites:
- Go (version 1.11 or higher)
- git

First, make a user for Heb12 (not strictly necessary, but useful).
Download the Gratis Bibles to a directory and choose the split Bible branch.
```
# adduser --disabled-login heb12
# su heb12
```

Then download the Gratis Bibles to the Heb12 storage directory and choose the split Bible branch.

```
$ mkdir -p ~/.local/share/heb12
$ git clone https://github.com/gratis-bible/bible ~/.local/share/heb12/gratis
$ cd ~/.local/share/heb12/gratis
$ git clone https://github.com/gratis-bible/bible gratisbibles
$ cd gratisbibles
$ git checkout split
```

Next, download and build bibleget.

```
$ cd ~
$ git clone https://code.heb12.com/heb12/bibleget
@@ -75,6 +67,12 @@ $ cd bibleget
$ go build
```

Copy the example configuration to `config.toml` and edit `Dir` to the appropriate directory (see "Configuration" below)
```
$ cp example.config.toml config.toml
$ nano config.toml
```

Finally, run it.

```
@@ -82,4 +80,15 @@ $ screen # so you can detatch without killing it
(screen) $ ./api
```

Now bibleget will be running on port 5500.
Now bibleget will be running on port 5500 (or any other port you set in `config.toml`).

## Configuration

The default config file is `config.toml`, but you can move it wherever if you change the `configFile` variable in `main.go`.

To change the port that bibleget uses, change `5500` in `Port: 5500` to whatever port you want it to run on.

To change the Bible translation returned when the user doesn't specify one, change `Version: "web"` to the translation you would prefer.

Make sure to change the value of `Dir` under `Bible` to where you downloaded the Gratis Bibles (without the trailing slash). If you followed the above installation instructions exactly, it should be `../gratisbibles`.
Blank quotes (`""`) means it uses the default Heb12 Gratis Bible directory (`~/.local/share/heb12/gratis`).

+ 6
- 0
example.config.toml Datei anzeigen

@@ -0,0 +1,6 @@
[Server]
Port = 5500

[Bible]
Version = "web"
Dir = ""

+ 1
- 0
go.mod Datei anzeigen

@@ -6,4 +6,5 @@ require (
code.heb12.com/heb12/bref v0.1.7
code.heb12.com/heb12/heb12 v0.0.0-20200328185632-a961a42a11fb
github.com/muesli/go-app-paths v0.2.1 // indirect
github.com/pelletier/go-toml v1.6.0
)

+ 6
- 0
go.sum Datei anzeigen

@@ -2,7 +2,13 @@ code.heb12.com/heb12/bref v0.1.7 h1:QTfGWvRJrxLxQgJUGowNkWm00wDY5U51n4h/6PHTISs=
code.heb12.com/heb12/bref v0.1.7/go.mod h1:1FEc9irKn5RWN/GId1dJp+Wa2bH3jW3uvSnJtBols4k=
code.heb12.com/heb12/heb12 v0.0.0-20200328185632-a961a42a11fb h1:X/6ItQr58z3KnjzKwaIlUOPfS9iLOYRNzwPPqXGLnKM=
code.heb12.com/heb12/heb12 v0.0.0-20200328185632-a961a42a11fb/go.mod h1:PsO4koeXLJyx0d8OQaFUR3FCsB5hYfPnSWgnczonCgM=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/muesli/go-app-paths v0.2.1 h1:Qi+2igkDX2aPqyRddp7P0sMQIBwBqhkfQfNcjdGjL6Y=
github.com/muesli/go-app-paths v0.2.1/go.mod h1:SxS3Umca63pcFcLtbjVb+J0oD7cl4ixQWoBKhGEtEho=
github.com/pelletier/go-toml v1.6.0 h1:aetoXYr0Tv7xRU/V4B4IZJ2QcbtMUFoNb3ORp7TzIK4=
github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

+ 48
- 2
main.go Datei anzeigen

@@ -2,8 +2,14 @@ package main

import (
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"

"github.com/pelletier/go-toml"

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

// will be moved to a configuration file later
@@ -13,6 +19,20 @@ const (
BibleDir string = ""
)

const configFile = "config.toml"

type Config struct {
Server Server
Bible Bible
}
type Server struct {
Port int
}
type Bible struct {
Version string
Dir string
}

func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Nothing to see here. Something will be written later\n")
}
@@ -23,6 +43,12 @@ func getbible(w http.ResponseWriter, r *http.Request) {
log.Print(err)
}

config, err := readConfig()
if err != nil {
log.Print(err)
return
}

var version string
if len(r.Form["version"]) != 0 {
version = r.Form["version"][0]
@@ -30,7 +56,7 @@ func getbible(w http.ResponseWriter, r *http.Request) {
version = DefaultVersion
}

response, err := GetText(r.Form["reference"][0], version)
response, err := text.GetText(config.Bible.Dir, r.Form["reference"][0], version)
if err != nil {
log.Print(err)
}
@@ -39,7 +65,27 @@ func getbible(w http.ResponseWriter, r *http.Request) {
}

func main() {
config, err := readConfig()
if err != nil {
log.Print(err)
return // since with a faulty configuration, it can't get the text
}

http.HandleFunc("/", handler)
http.HandleFunc("/get/", getbible)
log.Fatal(http.ListenAndServe(":"+Port, nil))

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

func readConfig() (Config, error) {
bytes, err := ioutil.ReadFile(configFile)
if err != nil {
log.Fatalf("Failed to read config file %s, due to error: %v", configFile, err)
}

var config Config
toml.Unmarshal(bytes, &config)

return config, nil
}

get.go → text/get.go Datei anzeigen

@@ -1,4 +1,5 @@
package main
// Package text uses heb12/bible to put Bible text into JSON, XML, HTML, or plaintext for returning by the API
package text

import (
"code.heb12.com/heb12/bref"
@@ -23,8 +24,8 @@ type Text struct {
}

// GetText parses a text reference, and then calls GetJSON
func GetText(reference string, version string) (string, error) {
hbible, err := bible.New(BibleDir)
func GetText(bibleDir string, reference string, version string) (string, error) {
hbible, err := bible.New(bibleDir)
if err != nil {
return "", err
}

Laden…
Abbrechen
Speichern