Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(docker): made Apollo docker ready #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
FROM golang:alpine3.14 as builder

WORKDIR /build
COPY . /build

ENV CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64

RUN apk add --no-cache git && \
git apply disable_integrations.patch

RUN go build -a -tags netgo -ldflags '-w -extldflags "-static"' -o apollo cmd/apollo.go && \
chmod +x ./apollo

FROM alpine:3.14

LABEL maintainer="Polonio Davide <[email protected]>"
LABEL description="Docker image for Apollo personal search engine"

EXPOSE 8993

WORKDIR /opt/apollo
VOLUME /opt/apollo/data

RUN apk add --no-cache \
youtube-dl \
ffmpeg && \
touch .env

COPY --from=builder /build/apollo /opt/apollo/apollo
COPY static/ /opt/apollo/static/

ENTRYPOINT ["/opt/apollo/apollo"]
29 changes: 29 additions & 0 deletions disable_integrations.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
diff --git a/pkg/apollo/sources/source.go b/pkg/apollo/sources/source.go
index 3a4101d..7e20ebf 100644
--- a/pkg/apollo/sources/source.go
+++ b/pkg/apollo/sources/source.go
@@ -9,23 +9,5 @@ var sources map[string]schema.Record
//TODO: make sourcesMap a global so we don't keep passing large maps in parameters
//TODO: should return map[string]schema.Data so we have control over the IDs
func GetData(sourcesMap map[string]schema.Record) map[string]schema.Data {
- sources = sourcesMap
- //pass in number of sources
- sourcesNewData := make([]map[string]schema.Data, 4)
- data := make(map[string]schema.Data)
- athena := getAthena()
- sourcesNewData[0] = athena
- zeus := getZeus()
- sourcesNewData[1] = zeus
- kindle := getKindle()
- sourcesNewData[2] = kindle
- podcast := getPodcast()
- sourcesNewData[3] = podcast
- //add all data
- for _, sourceData := range sourcesNewData {
- for ID, newData := range sourceData {
- data[ID] = newData
- }
- }
- return data
+ return make(map[string]schema.Data)
}
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ require (
github.com/PuerkitoBio/goquery v1.7.1
github.com/go-shiori/go-readability v0.0.0-20210627123243-82cc33435520
github.com/gorilla/mux v1.7.4
github.com/joho/godotenv v1.3.0
github.com/json-iterator/go v1.1.11
github.com/kljensen/snowball v0.5.0
golang.org/x/net v0.0.0-20210614182718-04defd469f4e
github.com/spf13/viper v1.9.0
)
595 changes: 590 additions & 5 deletions go.sum

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions pkg/apollo/backend/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package backend

import (
"fmt"
"io/fs"
"log"
"os"
"path/filepath"

"github.com/amirgamil/apollo/pkg/apollo/schema"
"github.com/amirgamil/apollo/pkg/apollo/sources"
Expand Down Expand Up @@ -39,6 +41,12 @@ const localRecordsPath = "./data/local.json"
const sourcesPath = "./data/sources.json"

func createFile(path string) {
basePath := filepath.Dir(path)
if _, err := os.Stat(basePath); os.IsNotExist(err) {
if err := os.MkdirAll(basePath, fs.FileMode(0755)); err != nil {
log.Fatal("Unable to create parent folder: " + filepath.Dir(path))
}
}
f, errCreating := os.Create(path)
if errCreating != nil {
log.Fatal("Error, could not create database for path: ", path, " with: ", errCreating)
Expand Down
17 changes: 11 additions & 6 deletions pkg/apollo/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/amirgamil/apollo/pkg/apollo/backend"
"github.com/amirgamil/apollo/pkg/apollo/schema"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
jsoniter "github.com/json-iterator/go"
"github.com/spf13/viper"
)

func check(e error) {
Expand All @@ -40,11 +40,11 @@ func index(w http.ResponseWriter, r *http.Request) {
}

func scrape(w http.ResponseWriter, r *http.Request) {
linkToScraoe := r.FormValue("q")
linkToScrape := r.FormValue("q")
w.Header().Set("Content-Type", "application/json")
result, err := schema.Scrape(linkToScraoe)
result, err := schema.Scrape(linkToScrape)
if err != nil {
log.Fatal("Error trying to scrape a digital artifact!")
log.Println("Error trying to scrape " + linkToScrape)
w.WriteHeader(http.StatusExpectationFailed)
} else {
json.NewEncoder(w).Encode(result)
Expand Down Expand Up @@ -127,9 +127,14 @@ func authenticatePassword(w http.ResponseWriter, r *http.Request) {
}

func isValidPassword(password string) bool {
err := godotenv.Load()
viper.AddConfigPath(".")
viper.SetConfigType("env")
viper.SetConfigName(".env")
viper.AutomaticEnv()

err := viper.ReadInConfig()
check(err)
truePass := os.Getenv("PASSWORD")
truePass := viper.Get("PASSWORD")
return truePass == password
}

Expand Down
13 changes: 9 additions & 4 deletions static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,15 +349,20 @@ class DigitalFootPrint extends Component {
if (response.ok) {
return response.json()
} else {
Promise.reject(response)
return {error: true}
}
}).then(data => {
this.showModal = false;
this.data.update({title: data["title"], content: data["content"]});
if (data["error"] === true) {
this.modalText = "Error while scraping the provided link";
this.render();
} else {
this.showModal = false;
this.data.update({title: data["title"], content: data["content"]});
}
// window.scrollBy(0, document.body.scrollHeight);
}).catch(ex => {
console.log("Exception trying to fetch the article: ", ex)
this.modalText = "Error scraping, sorry!";
this.modalText = "The server encountered an error while serving the request";
this.render();
})
}
Expand Down