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

Migrating to Go and Dockerizing #11

Open
wants to merge 11 commits 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
10 changes: 3 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
/vendor/
composer.lock
composer.phar
/var/
*.swp
.idea/
/config/config.yml
/web/js/config.js
/web/s/
/gen/
/tmp/
*-linux-amd64
21 changes: 21 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
GOFLAGS := --ldflags '-w -linkmode external'
CC := $(shell which musl-clang)

all: images

STATIC_FILES := $(wildcard static/*)

images: coverservice/coverservice-linux-amd64 \
coverservice/Dockerfile \
nginx/Dockerfile \
docker-compose.yml
docker-compose build

COMMON_FILES := $(wildcard common/*.go)

coverservice/coverservice-linux-amd64: \
$(COMMON_FILES) \
coverservice/main.go \
$(wildcard coverservice/service/*.go)
cd coverservice && \
CC=${CC} go build ${GOFLAGS} -o coverservice-linux-amd64
16 changes: 0 additions & 16 deletions bin/console

This file was deleted.

27 changes: 27 additions & 0 deletions common/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package common

import (
"net/http"
"strconv"
"encoding/json"
)

const kInternalError = "An internal error occurred"

type ErrorResponse struct {
Error string `json:"error"`
}


func WriteJsonResponse(w http.ResponseWriter, status int, data []byte) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Length", strconv.Itoa(len(data)))
w.WriteHeader(status)
w.Write(data)
}


func WriteInternalErrorResponse(w http.ResponseWriter) {
data, _ := json.Marshal(ErrorResponse{kInternalError})
WriteJsonResponse(w, http.StatusInternalServerError, data)
}
18 changes: 18 additions & 0 deletions common/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package common


import "github.com/gorilla/mux"


func NewRouter(routes Routes) *mux.Router {
router := mux.NewRouter().StrictSlash(true)

for _, route := range routes {
router.Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(route.HandlerFunc)
}

return router
}
13 changes: 13 additions & 0 deletions common/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package common

import "net/http"


type Route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
}

type Routes []Route
36 changes: 36 additions & 0 deletions common/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package common

import (
"os"
"io"
)

func CopyFile(src, dst string) (int64, error) {
sf, err := os.Open(src)
if err != nil {
return 0, err
}
defer sf.Close()
df, err := os.Create(dst)
if err != nil {
return 0, err
}
defer df.Close()
return io.Copy(df, sf)
}


func MoveFile(src, dst string) error {
err := os.Rename(src, dst)
if err != nil {
_, err = CopyFile(src, dst)
if err != nil {
return err
}
err = os.Remove(src)
if err != nil {
return err
}
}
return nil
}
19 changes: 19 additions & 0 deletions common/webserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package common


import (
"net/http"
"github.com/sirupsen/logrus"
)


func StartWebServer(port string, routes Routes) {
r := NewRouter(routes)
http.Handle("/", r)
logrus.Infof("Starting HTTP service at port %s", port)
err := http.ListenAndServe(":" + port, nil)
if err != nil {
logrus.Errorf("An error ocurred starting HTTP listener at port %s", port)
logrus.Error("Error: " + err.Error())
}
}
36 changes: 0 additions & 36 deletions composer.json

This file was deleted.

3 changes: 0 additions & 3 deletions config/config.yml.dist

This file was deleted.

19 changes: 0 additions & 19 deletions config/dev.php

This file was deleted.

6 changes: 0 additions & 6 deletions config/prod.php

This file was deleted.

17 changes: 17 additions & 0 deletions coverservice/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM ubuntu:16.04

EXPOSE 8000

RUN apt update && \
apt install -y --no-install-recommends texlive-latex-base texlive-fonts-recommended musl && \
rm -rf /var/lib/apt/lists/*

WORKDIR "/srv/coverservice"

COPY coverservice/docker-entrypoint.sh /entrypoint.sh
COPY img/ucsp.png img/
COPY templates/cover.tex templates/
COPY coverservice/coverservice-linux-amd64 server

ENTRYPOINT ["/entrypoint.sh"]
CMD ["./server"]
5 changes: 5 additions & 0 deletions coverservice/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

mkdir -p tmp gen

exec "$@"
15 changes: 15 additions & 0 deletions coverservice/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"math/rand"
"time"
"github.com/sirupsen/logrus"
"github.com/ACMUCSP/mkcaratula/common"
"github.com/ACMUCSP/mkcaratula/coverservice/service"
)

func main() {
logrus.Infof("Starting %s", service.AppName)
rand.Seed(time.Now().UnixNano())
common.StartWebServer("8000", service.Routes)
}
Loading