feat: better route function
All checks were successful
/ deploy (push) Successful in 48s

This commit is contained in:
ange 2024-12-10 03:04:52 +00:00
parent 805d417a9a
commit aaac5d780b
Signed by: ange
GPG Key ID: 9E0C4157BB7BEB1D
10 changed files with 103 additions and 51 deletions

View File

@ -1,10 +1,9 @@
ARG VER=1.23
FROM "docker.io/golang:$VER" as build
FROM docker.io/golang:1.23 AS build
WORKDIR /build/
ARG VER
COPY main.go .
RUN printf "module yw5n\ngo $VER" > go.mod && CGO_ENABLED=0 go build -o /app
COPY go.mod go.sum .
RUN go mod download
COPY src/ .
RUN CGO_ENABLED=0 go build -o /app
FROM scratch
COPY --from=build /app /app

View File

@ -4,3 +4,10 @@ services:
build: .
ports:
- "3000:3000"
develop:
watch:
- action: rebuild
path: src/
- action: sync+restart
path: html/
target: /html

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module main
go 1.23.3

0
go.sum Normal file
View File

View File

@ -8,7 +8,7 @@
<body>
<div id="header">
<a id="headerLink" href="/">Ange DUHAYON</a>
<span id="headerSubtitle">Engineer DevOps</span>
<span id="headerSubtitle">DevOps Engineer</span>
<span class="right" style="font-size: .4em;">Website heavily inspired from <a href="//suckless.org/" target="_blank" rel="noreferrer noopener">suckless.org</a></span>
</div>
<div id="menu">

View File

@ -8,7 +8,7 @@
<body>
<div id="header">
<a id="headerLink" href="/">Ange DUHAYON</a>
<span id="headerSubtitle">Engineer DevOps</span>
<span id="headerSubtitle">DevOps Engineer</span>
<span class="right" style="font-size: .4em;">Website heavily inspired from <a href="//suckless.org/" target="_blank" rel="noreferrer noopener">suckless.org</a></span>
</div>
<div id="menu">

View File

@ -8,7 +8,7 @@
<body>
<div id="header">
<a id="headerLink" href="/">Ange DUHAYON</a>
<span id="headerSubtitle">Engineer DevOps</span>
<span id="headerSubtitle">DevOps Engineer</span>
<span class="right" style="font-size: .4em;">Website heavily inspired from <a href="//suckless.org/" target="_blank" rel="noreferrer noopener">suckless.org</a></span>
</div>
<div id="menu">
@ -21,27 +21,25 @@
<a href="//git.gmoker.com/yw5n/" target="_blank" rel="noreferrer noopener">source</a>
</span>
</div>
<hr class="hidden"/>
<div id="content">
<!--
<div id="nav">
<!--
<ul>
<li><a href="#formation">Formation</a></li>
<li><a href="#experience">Experience</a></li>
<li><a href="#competences">Competences</a></li>
</ul>
-->
<img src="static/2404.webp" alt="Ange DUHAYON" width="128">
</div>
-->
<div id="main">
<h1>4th year EPITECH student</h1>
<img src="static/2404.webp" alt="Ange DUHAYON" width="128">
<h2 id="formation">FORMATION</h2>
<h3><b>EPITECH Toulouse</b> - Promo 2026</h3>
<h3><b>EPITECH Toulouse</b> - Bachelor 2024</h3>
<ul>
<li>C/C++ (system programming, AI, ciphering, games)</li>
<li>Free languages (math, functional programming)</li>
<li>DevOps (GitHub Actions, Docker, Ansible, Kubernetes)</li>
<li>DevOps (GitHub Actions, Docker, Ansible, K8S)</li>
</ul>
<h2 id="experience">EXPERIENCE</h2>
@ -51,6 +49,11 @@
<li>Server setup (Debian, Docker/K8s, web server, customer application)</li>
<li>CI/CD, real-time monitoring</li>
</ul>
<h3><b>Self hosting</b> - DevOps</h3>
JANUARY 2022 - TODAY
<ul>
<li>Debian + K8S + Gitea CI/CD + Nextcloud + Matrix.org + Jellyfin + SearXNG</li>
</ul>
<h3><b>EPITECH CodingClub association, Toulouse</b> - Cobra</h3>
JANUARY 2021 - JUNE 2024
<ul>
@ -60,9 +63,8 @@
JUNE 2022 - JULY 2023
<ul>
<li>CI/CD, K8S monitoring</li>
<li>Authentication solution (LDAP, SSO, Webhook)</li>
<li>Authentication solution (LDAP, SSO, Webhook) + Python backend</li>
<li>Mail server (Postfix, Dovecot)</li>
<li>Python backend</li>
<li>Customer communication and troubleshooting</li>
</ul>
@ -73,7 +75,7 @@
<tr><td>Python</td> <td>Git</td></tr>
<tr><td>Bash</td> <td>Vim</td></tr>
<tr><td>Go</td> <td>Docker</td></tr>
<tr><td>JavaScript</td><td>Kubernetes</td></tr>
<tr><td>JavaScript</td><td>K8S</td></tr>
</table>
</div>

32
main.go
View File

@ -1,32 +0,0 @@
package main
import (
"log"
"net/http"
"path/filepath"
)
func html(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/style.css" {
http.ServeFile(w, r, "/html/style.css")
return
}
if len(r.URL.Path) > len("/static/") && r.URL.Path[:len("/static/")] == "/static/" {
http.ServeFile(w, r, r.URL.Path)
return
}
if r.URL.Path == "/" {
http.ServeFile(w, r, "/html/index.html")
return
}
http.ServeFile(w, r, filepath.Join("/html", r.URL.Path + ".html"))
}
func main() {
http.HandleFunc("/", html)
err := http.ListenAndServe(":3000", nil)
if err != nil {
log.Fatal(err)
}
}

15
src/main.go Normal file
View File

@ -0,0 +1,15 @@
package main
import (
"log"
"net/http"
)
func main() {
http.HandleFunc("/", Route)
err := http.ListenAndServe(":3000", nil)
if err != nil {
log.Fatal(err)
}
}

58
src/route.go Normal file
View File

@ -0,0 +1,58 @@
package main
import (
"context"
"fmt"
"net/http"
"path/filepath"
"regexp"
"slices"
)
type URLParam struct{}
type W = http.ResponseWriter
type R = *http.Request
var routes = []struct {
methods []string
regex *regexp.Regexp
handler http.HandlerFunc
}{
{[]string{"GET"}, URL(""), func(w W, r R) {
http.ServeFile(w, r, filepath.Join("html", "index.html"))
}},
{[]string{"GET"}, URL("/style\\.css"), func(w W, r R) {
http.ServeFile(w, r, "/html/style.css")
}},
{[]string{"GET"}, URL("/static/.+"), func(w W, r R) {
http.ServeFile(w, r, r.URL.Path)
}},
{[]string{"GET"}, URL("/[^/]+"), func(w W, r R) {
http.ServeFile(w, r, filepath.Join("html", r.URL.Path + ".html"))
}},
}
func URL(s string) *regexp.Regexp {
return regexp.MustCompile("^" + s + "/?$")
}
func Route(w http.ResponseWriter, r *http.Request) {
for _, rt := range routes {
matches := rt.regex.FindStringSubmatch(r.URL.Path)
if len(matches) > 0 {
if !slices.Contains(rt.methods, r.Method) {
w.Header().Set("Allow", r.Method)
http.Error(
w, "405 method not allowed", http.StatusMethodNotAllowed,
)
fmt.Println(r.Method, r.URL.Path)
}
rt.handler(w, r.WithContext(
context.WithValue(r.Context(), URLParam{}, matches[1:])),
)
return
}
}
http.NotFound(w, r)
}