feat: add app code

- journal domain package
- httpserver package
- html templates
- main.go in root dir
This commit is contained in:
Vojtěch Mareš 2025-04-22 22:01:53 +02:00
parent 3cc4d28aac
commit 943922a6e1
Signed by: vojtech.mares
GPG key ID: C6827B976F17240D
20 changed files with 1032 additions and 0 deletions

34
httpserver/errors.go Normal file
View file

@ -0,0 +1,34 @@
package httpserver
import "net/http"
var (
ErrorPageBadRequest = `<!DOCTYPE html><html><head><title>Bad request | Journal</title><style>html { font-family: Consolas, monospace; } #error { margin: 0 auto; margin-top: 8rem; max-width: 40rem; } h1 { font-size: 3rem; } p { font-size: 1.5rem; margin-top: -1rem; }</style></head><body><div id="error"><h1>Bad request</h1><p>error 400</p></div></body></html>` // 400
ErrorPageForbidden = `<!DOCTYPE html><html><head><title>Forbidden | Journal</title><style>html { font-family: Consolas, monospace; } #error { margin: 0 auto; margin-top: 8rem; max-width: 40rem; } h1 { font-size: 3rem; } p { font-size: 1.5rem; margin-top: -1rem; }</style></head><body><div id="error"><h1>Forbidden</h1><p>error 403</p></div></body></html>` // 403
ErrorPageNotFound = `<!DOCTYPE html><html><head><title>Not found | Journal</title><style>html { font-family: Consolas, monospace; } #error { margin: 0 auto; margin-top: 8rem; max-width: 40rem; } h1 { font-size: 3rem; } p { font-size: 1.5rem; margin-top: -1rem; }</style></head><body><div id="error"><h1>Not found</h1><p>error 404</p></div></body></html>` // 404
ErrorPageInternalServerError = `<!DOCTYPE html><html><head><title>Internal server error | Journal</title></head><style>html { font-family: Consolas, monospace; } #error { margin: 0 auto; margin-top: 8rem; max-width: 40rem; } h1 { font-size: 3rem; } p { font-size: 1.5rem; margin-top: -1rem; }</style><body><div id="error"><h1>Internal server error</h1><p>error 500</p></div></body></html>` // 500
)
func BadRequest(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(ErrorPageBadRequest))
}
func Forbidden(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusForbidden)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(ErrorPageForbidden))
}
func NotFound(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(ErrorPageNotFound))
}
func InternalServerError(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(ErrorPageInternalServerError))
}

18
httpserver/favicon.go Normal file
View file

@ -0,0 +1,18 @@
package httpserver
import "net/http"
type Favicon struct {
favicon []byte
}
func NewFavicon(favicon []byte) *Favicon {
return &Favicon{
favicon: favicon,
}
}
func (f *Favicon) FaviconHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/x-icon")
w.Write(f.favicon)
}

74
httpserver/middleware.go Normal file
View file

@ -0,0 +1,74 @@
package httpserver
import (
"context"
"log"
"net/http"
"time"
)
type Middleware http.HandlerFunc
func Authenticated(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Perform authentication here
// For example, check for a valid token in the request header
token := r.Header.Get("Authorization")
if token == "" {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
// If authentication is successful, call the next handler
next.ServeHTTP(w, r)
})
}
type LoggingMiddleware struct {
logger *log.Logger
}
func NewLoggingMiddleware(logger *log.Logger) *LoggingMiddleware {
return &LoggingMiddleware{logger: logger}
}
func (m *LoggingMiddleware) Logging(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
next.ServeHTTP(w, r)
m.logger.Println("Request:", r.Method, r.URL.Path, "Duration:", time.Since(start))
})
}
type Timeout struct {
Timeout time.Duration
}
func (m *Timeout) TimeoutMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), m.Timeout)
defer cancel()
r = r.WithContext(ctx)
ch := make(chan struct{})
defer close(ch)
//
go func() {
next.ServeHTTP(w, r)
ch <- struct{}{}
}()
select {
case <-ch:
// Request completed within timeout
case <-ctx.Done():
// Request timed out
http.Error(w, "Request timed out", http.StatusGatewayTimeout)
}
})
}
func NewTimeout(timeout time.Duration) *Timeout {
return &Timeout{Timeout: timeout}
}

39
httpserver/renderer.go Normal file
View file

@ -0,0 +1,39 @@
package httpserver
import (
"fmt"
"html/template"
"io/fs"
"log"
"net/http"
)
type TemplateRenderer struct {
templatesFS fs.FS
}
func NewTemplateRenderer(templatesFS fs.FS) *TemplateRenderer {
return &TemplateRenderer{
templatesFS: templatesFS,
}
}
func (r *TemplateRenderer) Render(w http.ResponseWriter, templatePath string, data interface{}) {
tmpl, err := template.ParseFS(r.templatesFS, fmt.Sprintf("templates/%s", templatePath), "templates/layout.html")
if err != nil {
InternalServerError(w, nil)
log.Printf("failed to parse template %s: %v", templatePath, err)
return
}
err = tmpl.Execute(w, data)
if err != nil {
InternalServerError(w, nil)
log.Printf("failed to execute template %s: %v", templatePath, err)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
}