first version
This commit is contained in:
148
server/server.go
Normal file
148
server/server.go
Normal file
@@ -0,0 +1,148 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"embed"
|
||||
_ "embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/mileusna/useragent"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"retroHub/data"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type injector struct {
|
||||
Version string
|
||||
}
|
||||
|
||||
type userAgentInjector struct {
|
||||
injector
|
||||
UserAgent useragent.UserAgent
|
||||
}
|
||||
|
||||
type providerInjector struct {
|
||||
injector
|
||||
Provider data.Provider
|
||||
}
|
||||
|
||||
type errorInjector struct {
|
||||
injector
|
||||
StatusCode int
|
||||
StatusText string
|
||||
}
|
||||
|
||||
const version string = "0.1"
|
||||
|
||||
//go:embed templates
|
||||
var templates embed.FS
|
||||
|
||||
var (
|
||||
provider data.Provider
|
||||
indexTemplate *template.Template
|
||||
uaTemplate *template.Template
|
||||
errTemplate *template.Template
|
||||
|
||||
errLog *log.Logger
|
||||
)
|
||||
|
||||
func init() {
|
||||
indexTemplate = template.Must(template.ParseFS(templates, "templates/base.html", "templates/index.html"))
|
||||
uaTemplate = template.Must(template.ParseFS(templates, "templates/base.html", "templates/ua.html"))
|
||||
errTemplate = template.Must(template.ParseFS(templates, "templates/base.html", "templates/error.html"))
|
||||
|
||||
errLog = log.New(os.Stderr, "", log.LstdFlags)
|
||||
}
|
||||
|
||||
func Serve(contentProvider data.Provider, port uint) error {
|
||||
if contentProvider == nil {
|
||||
return errors.New("content provider cannot be nil")
|
||||
}
|
||||
provider = contentProvider
|
||||
|
||||
r := chi.NewRouter()
|
||||
r.Use(middleware.Logger)
|
||||
r.Use(panicHandler)
|
||||
|
||||
r.NotFound(notFoundHandler)
|
||||
r.Get("/", indexHandler)
|
||||
r.Get("/ua", userAgentHandler)
|
||||
|
||||
log.Printf("the server is up and running on %d", port)
|
||||
err := http.ListenAndServe(fmt.Sprintf(":%d", port), r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func panicHandler(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
errLog.Println("ERROR", r)
|
||||
injectedData := errorInjector{
|
||||
injector: injector{
|
||||
Version: version,
|
||||
},
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
StatusText: "Internal Server Error",
|
||||
}
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
err := errTemplate.ExecuteTemplate(w, "base", injectedData)
|
||||
if err != nil {
|
||||
errLog.Println("ERROR", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||
injectedData := providerInjector{
|
||||
injector: injector{
|
||||
Version: version,
|
||||
},
|
||||
Provider: provider,
|
||||
}
|
||||
err := indexTemplate.ExecuteTemplate(w, "base", injectedData)
|
||||
if err != nil {
|
||||
errLog.Println("ERROR", err)
|
||||
}
|
||||
}
|
||||
|
||||
func userAgentHandler(w http.ResponseWriter, r *http.Request) {
|
||||
injectedData := userAgentInjector{
|
||||
injector: injector{
|
||||
Version: version,
|
||||
},
|
||||
}
|
||||
userAgent := strings.TrimSpace(r.UserAgent())
|
||||
if len(userAgent) > 0 {
|
||||
injectedData.UserAgent = useragent.Parse(userAgent)
|
||||
}
|
||||
err := uaTemplate.ExecuteTemplate(w, "base", injectedData)
|
||||
if err != nil {
|
||||
errLog.Println("ERROR", err)
|
||||
}
|
||||
}
|
||||
|
||||
func notFoundHandler(w http.ResponseWriter, _ *http.Request) {
|
||||
injectedData := errorInjector{
|
||||
injector: injector{
|
||||
Version: version,
|
||||
},
|
||||
StatusCode: http.StatusNotFound,
|
||||
StatusText: "Not Found",
|
||||
}
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
err := errTemplate.ExecuteTemplate(w, "base", injectedData)
|
||||
if err != nil {
|
||||
errLog.Println("ERROR", err)
|
||||
}
|
||||
}
|
||||
19
server/templates/base.html
Normal file
19
server/templates/base.html
Normal file
@@ -0,0 +1,19 @@
|
||||
{{ define "base" }}
|
||||
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" content="text/html">
|
||||
<title>Retro Hub</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Retro Hub</h1>
|
||||
<p>Welcome to retro hub, this website reference some tools for older browsers</p>
|
||||
<hr/>
|
||||
<a href="/">List of websites</a> <a href="/ua">User-Agent information</a>
|
||||
<hr/>
|
||||
{{ template "content" . }}
|
||||
<hr/>
|
||||
<p>Retro Hub v{{ .Version }} powered by Go, made by thelilfrog (<a href="https://github.com/mojitaurelie">Github</a>, not available for old systems)</p>
|
||||
</body>
|
||||
</html>
|
||||
{{ end }}
|
||||
3
server/templates/error.html
Normal file
3
server/templates/error.html
Normal file
@@ -0,0 +1,3 @@
|
||||
{{ define "content" }}
|
||||
<h1>{{ .StatusCode }} {{ .StatusText }}</h1>
|
||||
{{ end }}
|
||||
34
server/templates/index.html
Normal file
34
server/templates/index.html
Normal file
@@ -0,0 +1,34 @@
|
||||
{{ define "content" }}
|
||||
{{ range $category := .Provider.Categories }}
|
||||
{{ $links := $category.Links }}
|
||||
<h2>{{ $category.Title }}</h2>
|
||||
{{ $length := len $links }} {{ if eq $length 0 }}
|
||||
<p>This category is empty for now :(</p>
|
||||
{{ else }}
|
||||
<table cellpadding="3" border="2" bgcolor="silver">
|
||||
<thead>
|
||||
<tr>
|
||||
<th bgcolor="white">Title</th>
|
||||
<th bgcolor="white">Information</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{ range $link := $links }}
|
||||
<tr>
|
||||
<td bgcolor="white">
|
||||
<a href="{{$link.URL}}">{{ $link.Title }}</a>
|
||||
</td>
|
||||
<td bgcolor="white">
|
||||
{{ $length := len $link.Description }} {{ if eq $length 0 }}
|
||||
<i>No description</i>
|
||||
{{ else }}
|
||||
{{$link.Description}}
|
||||
{{ end }}
|
||||
</td>
|
||||
</tr>
|
||||
{{ end }}
|
||||
</tbody>
|
||||
</table>
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
26
server/templates/ua.html
Normal file
26
server/templates/ua.html
Normal file
@@ -0,0 +1,26 @@
|
||||
{{ define "content" }}
|
||||
{{ $length := len .UserAgent.String }} {{ if eq $length 0 }}
|
||||
<i>No User-Agent provided by the browser</i>
|
||||
{{ else }}
|
||||
<p>Your User-Agent is <b>{{ .UserAgent.String }}</b></p>
|
||||
<h3>Detailled information:</h3>
|
||||
<ul>
|
||||
{{ $length := len .UserAgent.Name }} {{ if not (eq $length 0) }}
|
||||
<li>Browser name: <b>{{ .UserAgent.Name }}</b></li>
|
||||
{{ end }}
|
||||
{{ $length := len .UserAgent.Version }} {{ if not (eq $length 0) }}
|
||||
<li>Browser version: <b>{{ .UserAgent.Version }}</b></li>
|
||||
{{ end }}
|
||||
{{ $length := len .UserAgent.OS }} {{ if not (eq $length 0) }}
|
||||
<li>Browser OS: <b>{{ .UserAgent.OS }}</b></li>
|
||||
{{ end }}
|
||||
{{ $length := len .UserAgent.OSVersion }} {{ if not (eq $length 0) }}
|
||||
<li>Browser OS Version: <b>{{ .UserAgent.OSVersion }}</b></li>
|
||||
{{ end }}
|
||||
{{ $length := len .UserAgent.Device }} {{ if not (eq $length 0) }}
|
||||
<li>Device: <b>{{ .UserAgent.Device }}</b></li>
|
||||
{{ end }}
|
||||
</ul>
|
||||
<small>This detailled information is provided by <a href="https://github.com/mileusna/useragent/">github.com/mileusna/useragent</a></small>
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
Reference in New Issue
Block a user