shakesearch

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 21, 2020 License: MIT Imports: 4 Imported by: 0

README

shakesearch

Go Report Card Go Reference

Test it yourself

This is deployed right now. Head over to https://shakesearch.micahparks.com or use the command line:

curl "https://shakesearch.micahparks.com/api/search?q=Porter&maxResults=2" | python3 -m json.tool

Output:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   165  100   165    0     0    283      0 --:--:-- --:--:-- --:--:--   282
[
    {
        "line": "PORTER.",
        "lineNumbers": [
            81739,
            81764,
            81771,
            81783
        ],
        "matchedIndexes": [
            0,
            1,
            2,
            3,
            4,
            5
        ]
    },
    {
        "line": "A PORTER",
        "lineNumbers": [
            50287
        ],
        "matchedIndexes": [
            2,
            3,
            4,
            5,
            6,
            7
        ]
    }
]

Screenshot

I kept most of the lovely frontend the same, but I fixed up the backend and added new features as instructed.

porterQuery.png

You can click on the hyperlink of the line number to jump to a snippet from Shakespeare's complete works. From there you can scroll up and down for a few hundred lines, but it's only a snippet. Transferring the entire document would take too long.

porterSnippet.png

New features

  • Swagger 2.0 specification file. This helps communicate API and data structures to other developers.
  • Line numbers link to snippets of the complete works.
    • This uses Go templating.
  • Fuzzy search the given query using fuzzy.
  • Rate limit requests with tollbooth.
  • Using a logger (zap).
  • Deployment assets: Dockerfile, docker-compose.yml, and Caddyfile.
  • Proper error handing.
  • Configurable hostname, port, and file location for Shakespeare's works in the file system.
  • Limit the number of results using query.
  • More data returned from search endpoint.
  • Endpoints moved to /api. Ex: /api/search.
  • Separation of frontend and backend code. (Using Caddy as a file server).
  • HTTPS (thanks to Let's Encrypt and Caddy).
  • No more panics when a query causes an impossible index. Ex. Search for something near the top or bottom of the file like Project.
  • Bold the text that matched the query.
  • Line number where the text came from.
  • Less whitespace in memory.

Configuration

Environment variable table:

Name Description Default Value Example Value
HOST The host to bind the service to. localhost 0.0.0.0
PORT The port to bind the service to. random 30000
SHAKESPEARES_WORKS The full or relative file path to a text file containing the complete works of Shakespeare. completeworks.txt /home/william/works.txt
SNIPPET_TEMPLATE The full or relative file path to the HTML template for a snippet of Shakespeare's works. snippet.gohtml customTempalte.gohtml

Generated code and directories explained

This project uses a swagger 2.0 API (swagger.yml) and the goswagger project. The swagger.yml file was used to generate the code in the cmd, models, and restapi directories. One file was edited at restapi/configure_shakesearch.go.

The other directories are not from the code generator. Both configure and endpoints contain hand written code and the static directory was given, except static/app.js was modified a bit to help show matching results with bold text.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Line added in v0.2.0

type Line struct {
	Number uint
	Text   string
}

Line represents a line in Shakespeare's works.

type Lines added in v0.2.0

type Lines []Line

Lines is an array of Line. It is used as typing information for the Go template.

type ShakeSearcher

type ShakeSearcher struct {
	// contains filtered or unexported fields
}

ShakeSearcher is the data structure that holds the unique lines of Shakespeare's complete works in a convent format to search. The format is a map of all lines and a slice of the line numbers they belong to.

func NewShakeSearcher

func NewShakeSearcher(filePath string) (shakeSearcher *ShakeSearcher, err error)

NewShakeSearcher loads the give file as a slice of string with trimmed space.

func (*ShakeSearcher) Search

func (s *ShakeSearcher) Search(maxMatches int, query string) (results []*models.Result)

Search does a fuzzy search of the complete works of Shakespeare and returns a slice of results with a length less than or equal to the maxMatches. A maxMatches with a value of -1 will return all matches.

func (*ShakeSearcher) SurroundingWorks added in v0.2.0

func (s *ShakeSearcher) SurroundingWorks(lineNumber int, maxLines uint) (lines Lines)

SurroundingWorks retrieves the line numbers surrounding the given line. The number of lines returned will be less than or equal to maxLines.

Directories

Path Synopsis
cmd
Package restapi shakesearch A ProlificLabs take-home challenge.
Package restapi shakesearch A ProlificLabs take-home challenge.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL