commit 1a9481bd83066563c2439f8d4a07b47604bfdb07 Author: Maƫl Gramain Date: Sun Mar 19 15:07:40 2023 +0100 Init diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..dfcc76c --- /dev/null +++ b/Makefile @@ -0,0 +1,61 @@ +EXTENSION ?= +DIST_DIR ?= dist/ +GOOS ?= linux +ARCH ?= $(shell uname -m) +BUILDINFOSDET ?= + +SOFT_NAME := epee_service +SOFT_VERSION := $(shell git describe --tags $(git rev-list --tags --max-count=1)) +VERSION_PKG := $(shell echo $(SOFT_VERSION) | sed 's/^v//g') +ARCH := x86_64 +LICENSE := AGPL-3 +URL := https://git.gnous.eu/enpls/epee-service +DESCRIPTION := EnPLS Epee (Enpls Proxy EnginE) Utility Service +BUILDINFOS := ($(shell date +%FT%T%z)$(BUILDINFOSDET)) +LDFLAGS := '-X main.version=$(SOFT_VERSION) -X main.buildinfos=$(BUILDINFOS)' + +OUTPUT_SOFT := $(DIST_DIR)frite-$(SOFT_VERSION)-$(GOOS)-$(ARCH)$(EXTENSION) + +.PHONY: vet +vet: + go vet main.go + +.PHONY: test +test: + go test -v ./... + +.PHONY: prepare +prepare: + mkdir -p $(DIST_DIR) + +.PHONY: clean +clean: + rm -rf $(DIST_DIR) + +.PHONY: build +build: prepare + go build -ldflags $(LDFLAGS) -o $(OUTPUT_SOFT) + +.PHONY: package-deb +package-deb: prepare + fpm -s dir -t deb -n $(SOFT_NAME) -v $(VERSION_PKG) \ + --description "$(DESCRIPTION)" \ + --url "$(URL)" \ + --architecture $(ARCH) \ + --license "$(LICENSE)" \ + --package $(DIST_DIR) \ + $(OUTPUT_SOFT)=/usr/bin/frite-web \ + extra/links.txt.example=/etc/frite/links.txt \ + extra/frite-web.service=/usr/lib/systemd/system/frite-web.service + +.PHONY: package-rpm +package-rpm: prepare + fpm -s dir -t rpm -n $(SOFT_NAME) -v $(VERSION_PKG) \ + --description "$(DESCRIPTION)" \ + --url "$(URL)" \ + --architecture $(ARCH) \ + --license "$(LICENSE) "\ + --package $(DIST_DIR) \ + $(OUTPUT_SOFT)=/usr/bin/frite-web \ + extra/links.txt.example=/etc/frite/links.txt \ + extra/frite-web.service=/usr/lib/systemd/system/frite-web.service \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6cdbb29 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.gnous.eu/enpls/epee-service + +go 1.19 diff --git a/main.go b/main.go new file mode 100644 index 0000000..d7b9a0c --- /dev/null +++ b/main.go @@ -0,0 +1,77 @@ +package main + +import ( + "embed" + "encoding/json" + "fmt" + "html/template" + "log" + "net/http" +) + +//go:embed static +var staticFS embed.FS + +//go:embed templates +var templatesFS embed.FS + +// Error model +type Error struct { + Title string `json:"title"` + Message string `json:"message"` +} + +func main() { + port := "5900" + + http.Handle("/static/", http.FileServer(http.FS(staticFS))) + http.HandleFunc("/", errorHandler) + + log.Printf("Listening on :%s...", port) + err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil) + if err != nil { + log.Fatal(err) + } +} + +func errorHandler(w http.ResponseWriter, r *http.Request) { + errorCode := r.URL.Query().Get("error") + + // Load error messages from JSON file + jsonData, err := templatesFS.ReadFile("templates/errors.json") + if err != nil { + generateInternalServerError(w, err) + return + } + errors := make(map[string]Error) + err = json.Unmarshal(jsonData, &errors) + if err != nil { + generateInternalServerError(w, err) + return + } + errorData := errors[errorCode] + if errorData.Title == "" { + errorData.Title = "Error" + errorData.Message = "An error occurred." + } + + // Render error page + tmpl, err := templatesFS.ReadFile("templates/error.html") + if err != nil { + generateInternalServerError(w, err) + return + } + t, err := template.New("error").Parse(string(tmpl)) + if err != nil { + generateInternalServerError(w, err) + return + } + t.Execute(w, errorData) +} + +func generateInternalServerError(w http.ResponseWriter, err error) { + log.Println(err) + w.Header().Set("Content-Type", "application/json; charset=utf-8") + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(`{"error": "Internal Server Error"}`)) +} diff --git a/static/Nunito-VariableFont_wght.ttf b/static/Nunito-VariableFont_wght.ttf new file mode 100644 index 0000000..87c50a8 Binary files /dev/null and b/static/Nunito-VariableFont_wght.ttf differ diff --git a/static/main.css b/static/main.css new file mode 100644 index 0000000..48d2109 --- /dev/null +++ b/static/main.css @@ -0,0 +1,61 @@ +@import url('https://fonts.googleapis.com/css2?family=Nunito:wght@500;800&display=swap'); + +* { + margin: 0px; + padding: 0px; +} + +body { + font-family: 'Nunito', sans-serif; + background-color: #f1efe9; + color: #282828; + font-weight: 500; +} + +h1 { + font-weight: 800; +} + +.container { + display: flex; + min-height: 100vh; + flex-direction: column; +} + +.main { + flex: 1; +} + +.header { + padding: 20px; + text-align: center; +} + +.main { + width: 80%; + margin: 0 auto; + margin-top: 5vh; +} + +.main h1 { + font-size: 8vh; +} + +.main p { + padding-top: 0.5vh; +} + +.footer { + background-color: #282828; + color: #f1efe9; + padding: 20px; + text-align: center; +} + +.main a { + color: #7d5fff; +} + +.footer a { + color: #f1efe9; +} \ No newline at end of file diff --git a/templates/error.html b/templates/error.html new file mode 100644 index 0000000..d6c0390 --- /dev/null +++ b/templates/error.html @@ -0,0 +1,29 @@ + + + + + + + + {{.Title}} + + + + +
+
+

EnPLS Network

+
+
+

{{.Title}}

+

{{.Message}}

+

If you believe that this situation is unusual, please don't hesitate to contact us. +

+ +
+ + + \ No newline at end of file diff --git a/templates/errors.json b/templates/errors.json new file mode 100644 index 0000000..741b480 --- /dev/null +++ b/templates/errors.json @@ -0,0 +1,22 @@ +{ + "404": { + "title": "404, Page Not Found.", + "message": "Ooops! It looks like the ressource you were looking for is no were to be found." + }, + "403": { + "title": "403, Access Denied.", + "message": "Ooops! It looks like you don't have the right to access this ressource." + }, + "502": { + "title": "502, Backend Unavailable.", + "message": "Ooops! The requested backend service is unavailable. This error might be due to an ongoing maintenance or a service disruption. You might want to take a look at our service status page." + }, + "503": { + "title": "503, Service Unavailable.", + "message": "Ooops! The requested service is unavailable. This error might be due to an ongoing maintenance or a service disruption. You might want to take a look at our service status page." + }, + "500": { + "title": "500, Internal Server Error.", + "message": "Ooops! It looks like something went wrong on our side. We are working on it and will be back shortly." + } +} \ No newline at end of file