diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..6f660fb --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +.gitignore export-ignore +*.spec export-ignore diff --git a/.gitignore b/.gitignore index d3beee5..be5ff92 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,5 @@ _testmain.go *.test *.prof +# Binaries +/bin diff --git a/Makefile b/Makefile index 1eff657..f2e0618 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,36 @@ -bin/server: cmd/server/main.go pkg/*/*.go go.mod go.sum - go build -o bin/server cmd/server/main.go +NAME = netoik-api +VERSION = $(shell [ -d ".git" ] && git describe | sed "s/-/./g") +BRANCH = $(shell [ -d ".git" ] && git branch --show-current) +SYSCONFDIR = $(shell rpm --eval "%{_sysconfdir}") +UNITDIR = $(shell rpm --eval "%{_unitdir}") +BINDIR = $(shell rpm --eval "%{_bindir}") +VARDIR = $(shell rpm --eval "%{_var}") + +.PHONY: build build: bin/server + +bin/server: cmd/server/main.go pkg/*/*.go go.mod go.sum + mkdir --parents bin + CGO_ENABLED=0 go build -o "$@" ./cmd/server/main.go + +.PHONY: version +version: + @echo $(VERSION) + +.PHONY: tarball +tarball: $(HOME)/rpmbuild/SOURCES/$(NAME)-$(VERSION).tar.gz + +$(HOME)/rpmbuild/SOURCES/$(NAME)-%.tar.gz: . + git archive --format=tar.gz --output=$@ --prefix=$(NAME)-$(VERSION)/ --worktree-attributes --verbose $(BRANCH) + +.PHONY: install +install: + mkdir --parents $(DESTDIR)$(VARDIR)/$(NAME)/captcha + mkdir --parents $(DESTDIR)$(SYSCONFDIR)/$(NAME) + install server.conf.sample $(DESTDIR)$(SYSCONFDIR)/$(NAME)/server.conf.sample + install server.conf.sample $(DESTDIR)$(SYSCONFDIR)/$(NAME)/server.conf + mkdir --parents $(DESTDIR)$(UNITDIR) + install systemd/netoik-api-server.service $(DESTDIR)$(UNITDIR)/netoik-api-server.service + mkdir --parents $(DESTDIR)$(BINDIR) + install bin/server $(DESTDIR)$(BINDIR)/$(NAME)-server diff --git a/bin/server b/bin/server deleted file mode 100755 index 1921404..0000000 Binary files a/bin/server and /dev/null differ diff --git a/cmd/server/main.go b/cmd/server/main.go index c591f8e..eb2fb53 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -4,9 +4,9 @@ import ( "flag" "fmt" "net/http" - "netoik.io/netoik-website/pkg/captcha" - "netoik.io/netoik-website/pkg/conf" - "netoik.io/netoik-website/pkg/contact" + "netoik.io/netoik-api/pkg/captcha" + "netoik.io/netoik-api/pkg/conf" + "netoik.io/netoik-api/pkg/contact" "os" ) diff --git a/go.mod b/go.mod index 9cfb7a6..ce059a3 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,11 @@ -module netoik.io/netoik-website +module netoik.io/netoik-api -go 1.18 +go 1.19 require ( - github.com/dchest/captcha v0.0.0-20200903113550-03f5f0333e1f // indirect - github.com/pelletier/go-toml v1.9.5 // indirect - gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect - gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect + github.com/dchest/captcha v1.0.0 + github.com/pelletier/go-toml v1.9.5 + gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df ) + +require gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect diff --git a/go.sum b/go.sum index e2d36b8..53d6eaf 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/dchest/captcha v0.0.0-20200903113550-03f5f0333e1f h1:q/DpyjJjZs94bziQ7YkBmIlpqbVP7yw179rnzoNVX1M= -github.com/dchest/captcha v0.0.0-20200903113550-03f5f0333e1f/go.mod h1:QGrK8vMWWHQYQ3QU9bw9Y9OPNfxccGzfb41qjvVeXtY= +github.com/dchest/captcha v1.0.0 h1:vw+bm/qMFvTgcjQlYVTuQBJkarm5R0YSsDKhm1HZI2o= +github.com/dchest/captcha v1.0.0/go.mod h1:7zoElIawLp7GUMLcj54K9kbw+jEyvz2K0FDdRRYhvWo= github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk= diff --git a/netoik-api.spec b/netoik-api.spec new file mode 100644 index 0000000..22c99a5 --- /dev/null +++ b/netoik-api.spec @@ -0,0 +1,50 @@ +%undefine _missing_build_ids_terminate_build +%define debug_package %{nil} + +Name: netoik-api +Version: %(make version) +Release: 1%{?dist} +Summary: Netoik rest API server + +License: GPLv3 +URL: https://www.netoik.io/api +Source0: %{name}-%{version}.tar.gz + +Buildarch: x86_64 +BuildRequires: make + +%description +Netoik rest API server + +%prep +%autosetup -v + +%build +%make_build + +%install +%make_install + +%pre +useradd --no-create-home --system --user-group %{name} + +%post +systemctl stop %{name}-server.service +systemctl daemon-reload +systemctl start %{name}-server.service + +%preun +systemctl stop %{name}-server.service + +%postun +systemctl daemon-reload +userdel %{name} + +%files +%attr(755, root, root) %dir %{_var}/%{name} +%attr(755, %{name}, %{name}) %dir %{_var}/%{name}/captcha +%attr(755, root, root) %dir %{_sysconfdir}/%{name} +%attr(644, root, root) %{_sysconfdir}/%{name}/server.conf.sample +%attr(644, root, root) %config(noreplace) %{_sysconfdir}/%{name}/server.conf +%attr(644, root, root) %{_unitdir}/%{name}-server.service +%attr(755, root, root) %{_bindir}/%{name}-server diff --git a/pkg/captcha/captcha.go b/pkg/captcha/captcha.go index 6fb752b..3e86a17 100644 --- a/pkg/captcha/captcha.go +++ b/pkg/captcha/captcha.go @@ -5,8 +5,8 @@ import ( "fmt" "github.com/dchest/captcha" "net/http" - "netoik.io/netoik-website/pkg/api" - "netoik.io/netoik-website/pkg/conf" + "netoik.io/netoik-api/pkg/api" + "netoik.io/netoik-api/pkg/conf" "os" "path/filepath" "time" diff --git a/pkg/contact/contact.go b/pkg/contact/contact.go index 49dc240..a00d79c 100644 --- a/pkg/contact/contact.go +++ b/pkg/contact/contact.go @@ -7,8 +7,8 @@ import ( "github.com/dchest/captcha" "gopkg.in/gomail.v2" "net/http" - "netoik.io/netoik-website/pkg/api" - "netoik.io/netoik-website/pkg/conf" + "netoik.io/netoik-api/pkg/api" + "netoik.io/netoik-api/pkg/conf" "os" "path/filepath" ) diff --git a/server.conf.sample b/server.conf.sample index 1a27baf..1a4834f 100644 --- a/server.conf.sample +++ b/server.conf.sample @@ -7,5 +7,5 @@ smtp_username = "" smtp_password = "" smtp_receiver = "" -captcha_directory = "/tmp/netoik-api/captcha" +captcha_directory = "/var/netoik-api/captcha" captcha_expiration = "1h" diff --git a/systemd/netoik-api.service b/systemd/netoik-api-server.service similarity index 56% rename from systemd/netoik-api.service rename to systemd/netoik-api-server.service index f9b0980..e89b5fd 100644 --- a/systemd/netoik-api.service +++ b/systemd/netoik-api-server.service @@ -3,8 +3,9 @@ Description=Netoïk rest api After=network.target [Service] -User=www-data -ExecStart=/usr/netoik-api/bin/server -c /etc/netoik-api/server.conf +User=netoik-api +Group=netoik-api +ExecStart=/usr/bin/netoik-api-server -c /etc/netoik-api/server.conf Restart=always [Install]