Skip to content
Snippets Groups Projects
Verified Commit c07ca9c9 authored by Andrej Ramašeuski's avatar Andrej Ramašeuski
Browse files

Golang version

parent 9bf9e73d
No related branches found
No related tags found
No related merge requests found
Pipeline #20189 passed
**
!piratar
!piratar.conf
!default.jpg
.env 0 → 100644
export OCTOPUS=https://chobotnice.pirati.cz/graphql/
export OCTPUS_DEBUG=1
image: docker:20.10.9
image: docker:latest
variables:
DOCKER_TLS_CERTDIR: "/certs"
IMAGE_VER: 2.1.0
services:
- docker:20.10.9-dind
- docker:dind
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
......@@ -13,7 +12,8 @@ before_script:
build:
stage: build
script:
- VERSION=`cat VERSION`
- docker pull $CI_REGISTRY_IMAGE:latest || true
- docker build --cache-from $CI_REGISTRY_IMAGE:latest --tag $CI_REGISTRY_IMAGE:$IMAGE_VER --tag $CI_REGISTRY_IMAGE:latest .
- docker push $CI_REGISTRY_IMAGE:$IMAGE_VER
- docker build --cache-from $CI_REGISTRY_IMAGE:latest --tag $CI_REGISTRY_IMAGE:$VERSION --tag $CI_REGISTRY_IMAGE:latest .
- docker push $CI_REGISTRY_IMAGE:$VERSION
- docker push $CI_REGISTRY_IMAGE:latest
FROM alpine:latest
RUN apk update && apk add perl-mojolicious perl-io-socket-ssl perl-app-cpanminus make
RUN cpanm GraphQL::Client
FROM golang:alpine AS build-stage
ADD . ./
RUN CGO_ENABLED=0 GOOS=linux go build -o /piratar
ADD . /opt/piratar
WORKDIR /opt/piratar
USER nobody
FROM alpine
WORKDIR /app
COPY --from=build-stage /piratar /app
COPY default.jpg /app
EXPOSE 3000
CMD /opt/piratar/piratar daemon -c 3000
ENTRYPOINT ["/app/piratar"]
3.0.0
services:
app:
image: piratar
environment:
OCTOPUS: https://chobotnice.pirati.cz/graphql/
ports:
- "3000:3000"
go.mod 0 → 100644
module pirates/piratar
go 1.22.2
require pirates/piratar/octopus v0.0.0-00010101000000-000000000000
require (
github.com/google/uuid v1.6.0 // indirect
github.com/machinebox/graphql v0.2.2 // indirect
github.com/matryer/is v1.4.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
)
replace pirates/piratar/octopus => ./octopus
main.go 0 → 100644
package main
import (
"fmt"
"log"
"path"
"strings"
"net/http"
"pirates/piratar/octopus"
)
func main() {
http.HandleFunc("/", handler)
port := 3000 // TODO: ENV
fmt.Printf("Piratar service listening on port %d...\n", port)
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil {
log.Fatalf("Server failed: %v", err)
}
}
func handler(w http.ResponseWriter, r *http.Request) {
// Extrakce username nebo UUID
id := strings.ToLower(path.Base(r.URL.Path))
if dotIndex := strings.LastIndex(id, "."); dotIndex != -1 {
id = id[:dotIndex]
}
// Profilova fotka nebo default
if photo, err := octopus.ProfilePhoto(id); err == nil {
fmt.Printf("Piratar for %s: %s\n", id, photo)
http.Redirect(w, r, photo, http.StatusFound)
} else {
fmt.Printf("Piratar for %s not found\n", id)
w.Header().Set("Content-Type", "image/jpeg")
http.ServeFile(w, r, "default.jpg")
}
}
module pirates/piratar/octopus
go 1.22.2
package octopus
import (
"os"
"fmt"
"log"
"errors"
"context"
"github.com/google/uuid"
"github.com/machinebox/graphql"
)
func ProfilePhoto (id string) (string, error) {
var keyname string
var response struct {
AllPeople struct {
Edges []struct {
Node struct {
ProfilePhoto string `json:"profilePhoto"`
} `json:"node"`
} `json:"edges"`
} `json:"allPeople"`
}
if _, err := uuid.Parse(id); err == nil {
keyname = "keycloakId"
} else {
keyname = "username"
}
// GraphAPI klient
octopus := graphql.NewClient(os.Getenv("OCTOPUS"))
if os.Getenv("DEBUG") != "" {
octopus.Log = func(s string) { log.Println(s) }
}
// Request
req := graphql.NewRequest(fmt.Sprintf(
`query profilePhoto($username: String!) {
allPeople(filters: {%s: {iExact: $username}}) {
edges { node { profilePhoto } }
}}
`, keyname ))
req.Var("username", id)
// define a Context for the request
ctx := context.Background()
// run it and capture the response
if err := octopus.Run(ctx, req, &response); err != nil {
log.Fatal(err)
}
if people := response.AllPeople.Edges; len(people) == 1 {
return people[0].Node.ProfilePhoto, nil
} else {
return "", errors.New("User not found")
}
}
#!/usr/bin/env perl
use Mojolicious::Lite -signatures;
use Mojo::UserAgent;
use Mojolicious::Static;
use GraphQL::Client;
use Mojo::Util qw(dumper);
get '/*url' => sub ($c) {
my $username = lc($c->param('url'));
$username =~ s/^.*\///;
$username =~ s/\.(png|jpg|gif)$//;
$c->app->log->debug("Username: $username");
my $gq = GraphQL::Client->new(url => $ENV{OCTOPUS});
my $rc = $gq->execute(qq[ query MyQuery {
allPeople(filters: {username: {iExact: "$username"}}) {
edges { node { profilePhoto } }
}}],
);
if ($rc->{errors}) {
$c->app->log->warn("Octopus error: " . dumper $rc->{errors});
}
elsif ( $rc->{data} ) {
$c->app->log->debug("Octopus response: " . dumper $rc->{data});
my $user = $rc->{data}{allPeople}{edges}[0]{node};
if ( my $photo = $user->{profilePhoto}) {
$c->app->log->info("Avatar for user $username: $photo");
$c->redirect_to($photo);
return;
}
}
$c->app->log->info("Avatar for user $username: FALLBACK");
$c->reply->file('default.jpg');
};
app->start;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment