From b8f960f058e28f7651d529c6a101a164c069bdd0 Mon Sep 17 00:00:00 2001 From: viktorstrate Date: Wed, 19 Feb 2020 21:33:28 +0100 Subject: [PATCH] Add docker support for new api --- api/Dockerfile | 9 ++++++++ api/database/mysql.go | 39 ++++++++++++++++++++++++++++---- api/example.env | 4 ++-- api/go.mod | 1 + api/go.sum | 7 ++++++ api/server/server.go | 23 +++++++++++++++---- docker-compose.proxy-example.yml | 31 +++++++++++++------------ docker/neo4j/Dockerfile | 15 ------------ 8 files changed, 87 insertions(+), 42 deletions(-) create mode 100644 api/Dockerfile delete mode 100644 docker/neo4j/Dockerfile diff --git a/api/Dockerfile b/api/Dockerfile new file mode 100644 index 00000000..003809bc --- /dev/null +++ b/api/Dockerfile @@ -0,0 +1,9 @@ +FROM golang:latest + +WORKDIR /go/src/app +COPY . . + +RUN go get -d -v ./... +RUN go install -v ./... + +CMD ["server"] \ No newline at end of file diff --git a/api/database/mysql.go b/api/database/mysql.go index 1ceb452a..9a8e2888 100644 --- a/api/database/mysql.go +++ b/api/database/mysql.go @@ -5,12 +5,14 @@ import ( "log" "net/url" "os" + "time" // Load mysql driver _ "github.com/go-sql-driver/mysql" "github.com/golang-migrate/migrate" "github.com/golang-migrate/migrate/database/mysql" + // Migrate from file _ "github.com/golang-migrate/migrate/source/file" ) @@ -31,13 +33,40 @@ func SetupDatabase() *sql.DB { log.Printf("Connecting to database: %s", address) - db, err := sql.Open("mysql", address.String()) - if err != nil { - log.Fatalf("Could not connect to database: %s\n", err.Error()) + var db *sql.DB + tryCount := 0 + + for { + db, err = sql.Open("mysql", address.String()) + if err != nil { + if tryCount < 4 { + tryCount++ + log.Printf("WARN: Could not connect to database: %s. Will retry after 1 second", err.Error()) + time.Sleep(time.Second) + continue + } else { + log.Fatalln("ERROR: Could not connect to database, exiting") + } + } + + break } - if err := db.Ping(); err != nil { - log.Fatalf("Could not connect to database: %s\n", err.Error()) + tryCount = 0 + + for { + if err := db.Ping(); err != nil { + if tryCount < 4 { + tryCount++ + log.Printf("Could not ping database: %s. Will retry after 1 second", err.Error()) + time.Sleep(time.Second) + continue + } else { + log.Fatalln("ERROR: Could not ping database, exiting") + } + } + + break } db.SetMaxOpenConns(24) diff --git a/api/example.env b/api/example.env index 3df9deb5..66ece317 100644 --- a/api/example.env +++ b/api/example.env @@ -2,5 +2,5 @@ MYSQL_URL=user:password@/dbname -API_PORT=4001 -API_ENDPOINT=http://localhost:4001/ \ No newline at end of file +API_ENDPOINT=http://localhost:4001/ +API_LISTEN_PORT=4001 \ No newline at end of file diff --git a/api/go.mod b/api/go.mod index 6f953100..8496097f 100644 --- a/api/go.mod +++ b/api/go.mod @@ -14,6 +14,7 @@ require ( github.com/nf/cr2 v0.0.0-20180623103828-4699471a17ed github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 github.com/rs/cors v1.6.0 + github.com/sirupsen/logrus v1.4.2 github.com/vektah/gqlparser v1.2.0 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 golang.org/x/image v0.0.0-20200119044424-58c23975cae1 diff --git a/api/go.sum b/api/go.sum index f2578f50..15c6e782 100644 --- a/api/go.sum +++ b/api/go.sum @@ -24,6 +24,7 @@ github.com/hashicorp/golang-lru v0.5.0 h1:CL2msUPvZTLb5O648aiLNJw3hnBxN2+1Jq8rCO github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= @@ -44,8 +45,12 @@ github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/shurcooL/httpfs v0.0.0-20171119174359-809beceb2371/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg= github.com/shurcooL/vfsgen v0.0.0-20180121065927-ffb13db8def0/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw= +github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= @@ -60,6 +65,8 @@ golang.org/x/image v0.0.0-20200119044424-58c23975cae1/go.mod h1:FeLwcggjj3mMvU+o golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/tools v0.0.0-20190125232054-d66bd3c5d5a6/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190515012406-7d7faa4812bd h1:oMEQDWVXVNpceQoVd1JN3CQ7LYJJzs5qWqZIUcxXHHw= diff --git a/api/server/server.go b/api/server/server.go index a81d8a45..c468bbb7 100644 --- a/api/server/server.go +++ b/api/server/server.go @@ -3,9 +3,12 @@ package main import ( "log" "net/http" + "net/url" "os" + "path" "github.com/go-chi/chi" + "github.com/go-chi/chi/middleware" "github.com/go-chi/cors" "github.com/joho/godotenv" @@ -26,7 +29,7 @@ func main() { log.Println("No .env file found") } - port := os.Getenv("API_PORT") + port := os.Getenv("API_LISTEN_PORT") if port == "" { port = defaultPort } @@ -42,6 +45,8 @@ func main() { router := chi.NewRouter() router.Use(auth.Middleware(db)) + router.Use(middleware.Logger) + router.Use(cors.New(cors.Options{ AllowedOrigins: []string{"http://localhost:4001", "http://localhost:1234", "*"}, AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, @@ -59,11 +64,19 @@ func main() { Directives: graphqlDirective, } - router.Handle("/", handler.Playground("GraphQL playground", "/graphql")) - router.Handle("/graphql", handler.GraphQL(photoview_graphql.NewExecutableSchema(graphqlConfig))) + endpointURL, err := url.Parse(os.Getenv("API_ENDPOINT")) + if err != nil { + log.Println("WARN: Environment variable API_ENDPOINT not specified") + endpointURL, _ = url.Parse("/") + } - router.Mount("/photo", routes.PhotoRoutes(db)) + router.Route(endpointURL.Path, func(router chi.Router) { + router.Handle("/", handler.Playground("GraphQL playground", path.Join(endpointURL.Path, "/graphql"))) + router.Handle("/graphql", handler.GraphQL(photoview_graphql.NewExecutableSchema(graphqlConfig))) - log.Printf("🚀 Graphql playground ready at http://localhost:%s/", port) + router.Mount("/photo", routes.PhotoRoutes(db)) + }) + + log.Printf("🚀 Graphql playground ready at %s", endpointURL.String()) log.Fatal(http.ListenAndServe(":"+port, router)) } diff --git a/docker-compose.proxy-example.yml b/docker-compose.proxy-example.yml index d687a1ed..a14a08a4 100644 --- a/docker-compose.proxy-example.yml +++ b/docker-compose.proxy-example.yml @@ -1,33 +1,32 @@ -version: '3' +version: "3" services: - neo4j: - build: ./docker/neo4j - expose: - - 7474 - - 7687 + db: + image: mariadb + restart: always environment: - - NEO4J_dbms_security_procedures_unrestricted=apoc.* - - NEO4J_apoc_import_file_enabled=true - - NEO4J_apoc_export_file_enabled=true - - NEO4J_dbms_shell_enabled=true + - MYSQL_DATABASE=photoview + - MYSQL_USER=photoview + - MYSQL_PASSWORD=photo-secret + - MYSQL_RANDOM_ROOT_PASSWORD=1 volumes: - - db_data:/data + - db_data:/var/lib/mysql api: build: ./api + restart: always expose: - 80 depends_on: - - neo4j + - db environment: - - NEO4J_URI=bolt://neo4j:7687 + - MYSQL_URL=photoview:photo-secret@tcp(db)/photoview - PHOTO_CACHE=/app/cache # Change This: The publicly exposed url for the api # For example if the server is available from the domain example.com, # change this value to http://example.com/api - API_ENDPOINT=http://localhost:8080/api - - GRAPHQL_LISTEN_PORT=80 + - API_LISTEN_PORT=80 volumes: # Change This: Link photo paths from the host machine # Change this to the directory where your photos are located on your server. @@ -45,6 +44,7 @@ services: # For example if the server is available from the domain example.com, # change this value to http://example.com/api/graphql GRAPHQL_ENDPOINT: http://localhost:8080/api/graphql + restart: always expose: - 80 depends_on: @@ -52,6 +52,7 @@ services: proxy: image: nginx + restart: always volumes: - ./docker/nginx-proxy/default.conf:/etc/nginx/conf.d/default.conf ports: @@ -63,4 +64,4 @@ services: volumes: db_data: - api_cache: \ No newline at end of file + api_cache: diff --git a/docker/neo4j/Dockerfile b/docker/neo4j/Dockerfile deleted file mode 100644 index 8acc12e4..00000000 --- a/docker/neo4j/Dockerfile +++ /dev/null @@ -1,15 +0,0 @@ -FROM neo4j:3.4.5 - -ENV NEO4J_AUTH=neo4j/letmein - -ENV APOC_VERSION 3.4.0.2 -ENV APOC_URI https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/download/${APOC_VERSION}/apoc-${APOC_VERSION}-all.jar -RUN wget -P /var/lib/neo4j/plugins ${APOC_URI} - -ENV GRAPHQL_VERSION 3.4.0.1 -ENV GRAPHQL_URI https://github.com/neo4j-graphql/neo4j-graphql/releases/download/${GRAPHQL_VERSION}/neo4j-graphql-${GRAPHQL_VERSION}.jar -RUN wget -P /var/lib/neo4j/plugins ${GRAPHQL_URI} - -EXPOSE 7474 7473 7687 - -CMD ["neo4j"] \ No newline at end of file