Procházet zdrojové kódy

Web UI: first executing template set.

Frederic G. MARAND před 5 roky
rodič
revize
7b0f677e11

+ 2 - 1
cmd/kurzd/dist.config.yml

@@ -8,4 +8,5 @@ api:
 web:
   address: ":3000"
   # May be absolute or relative to the "kurzd" binary.
-  assetsPath: ../../web/public
+  assetsPath: ../../web/public
+  siteName: "Kurz"

+ 2 - 0
cmd/kurzd/serve.go

@@ -4,6 +4,7 @@ import (
 	"code.osinet.fr/fgm/kurz/web"
 	"context"
 	"database/sql"
+	"github.com/davecgh/go-spew/spew"
 	"github.com/spf13/viper"
 	"log"
 	"net/http"
@@ -70,6 +71,7 @@ func serveHandler(_ *cobra.Command, args []string) {
 
 	router := mux.NewRouter()
 	api.SetupRoutes(router)
+	spew.Dump(viper.Get("web"))
 	web.SetupRoutes(router, assetsPath)
 	http.Handle("/", router)
 

+ 1 - 0
go.mod

@@ -2,6 +2,7 @@ module code.osinet.fr/fgm/kurz
 
 require (
 	github.com/BurntSushi/toml v0.3.1 // indirect
+	github.com/davecgh/go-spew v1.1.1
 	github.com/go-sql-driver/mysql v1.4.0
 	github.com/gorilla/context v1.1.1 // indirect
 	github.com/gorilla/mux v1.6.2

+ 79 - 3
web/get_short.go

@@ -1,11 +1,64 @@
 package web
 
 import (
+	"fmt"
+	"net/http"
+	"time"
+
 	"code.osinet.fr/fgm/kurz/domain"
 	"github.com/gorilla/mux"
-	"net/http"
 )
 
+/*
+Template variables:
+
+  - FullyQualifiedAssetsBaseURL http://plusvite-cdn.osinet.eu
+  - FullyQualifiedShortURL      (short string)
+  - FullyQualifiedSiteURL       http://plusvite.net/
+  - FullyQualifiedTargetURL     (target string)
+  - RefreshDelay                (seconds int)
+  - SiteName                    PlusVite
+*/
+
+type bytes = []byte
+
+func build403(w http.ResponseWriter, short string) (bytes, error) {
+	type Short403Data struct {
+		FullyQualifiedAssetsBaseURL string
+		FullyQualifiedShortURL      string
+		FullyQualifiedSiteURL       string
+		RefreshDelay                time.Duration
+		SiteName                    string
+	}
+
+	data := Short403Data{}
+	tmpl.Execute(w, data)
+	return bytes(""), nil
+}
+
+func build404(w http.ResponseWriter, short string) (bytes, error) {
+	type Short404Data struct {
+		AssetsVersion               int
+		FullyQualifiedAssetsBaseURL string
+		FullyQualifiedShortURL      string
+		FullyQualifiedSiteURL       string
+		FullyQualifiedTargetURL     string
+		RefreshDelay                time.Duration
+		SiteName                    string
+	}
+
+	data := Short404Data{}
+	err := tmpl.Execute(w, data)
+	if err != nil {
+		fmt.Println(err)
+	}
+	return bytes(""), nil
+}
+
+func build451(w http.ResponseWriter, short string) (bytes, error) {
+	return nil, nil
+}
+
 // handleGetShort handles path /<short>
 func handleGetShort(w http.ResponseWriter, r *http.Request) {
 	short, ok := mux.Vars(r)["short"]
@@ -36,14 +89,37 @@ func handleGetShort(w http.ResponseWriter, r *http.Request) {
 	switch domainErr.Kind {
 	case domain.ShortNotFound:
 		status = http.StatusNotFound
+		if page, err := build404(w, short); err != nil {
+			status = http.StatusInternalServerError
+			w.WriteHeader(status)
+		} else {
+			w.WriteHeader(status)
+			w.Write(page)
+		}
+
 	case domain.TargetBlockedError:
 		status = http.StatusForbidden
+		if page, err := build403(w, short); err != nil {
+			status = http.StatusInternalServerError
+			w.WriteHeader(status)
+		} else {
+			w.WriteHeader(status)
+			w.Write(page)
+		}
+
 	case domain.TargetCensoredError:
 		status = http.StatusUnavailableForLegalReasons
+		if page, err := build451(w, short); err != nil {
+			status = http.StatusInternalServerError
+			w.WriteHeader(status)
+		} else {
+			w.WriteHeader(status)
+			w.Write(page)
+		}
+
 	default:
 		// TargetInvalid is not supposed to happen in this case, so it is an internal error too.
 		status = http.StatusInternalServerError
+		w.WriteHeader(status)
 	}
-
-	w.WriteHeader(status)
 }

+ 4 - 4
web/templates/201.gohtml

@@ -3,8 +3,8 @@
 <head>
     <title>{{.SiteName}}: vers {{.FullyQualifiedTargetURL}}</title>
     <link rel="shortcut icon" href="{{.FullyQualifiedAssetsBaseURL}}/favicon.ico?v=1">
-    <link rel="stylesheet" type="text/css" href="{{.FullyQualifiedAssetsBaseURL}}/css/plusvite.css?v={{.AssetsVersion}}" />
-    <link rel="stylesheet" type="text/css" href="{{.FullyQualifiedAssetsBaseURL}}/css/riff.css?v={{.AssetsVersion}}" />
+    <link rel="stylesheet" type="text/css" href="{{.FullyQualifiedAssetsBaseURL}}/public/css/plusvite.css?v={{.AssetsVersion}}" />
+    <link rel="stylesheet" type="text/css" href="{{.FullyQualifiedAssetsBaseURL}}/public/css/riff.css?v={{.AssetsVersion}}" />
     {{template "analytics" .}}
     {{template "inlinecss"}}
 </head>
@@ -15,9 +15,9 @@
         <p>Vous pouvez dorénavant aller plus vite vers</p>
         <pre><a href="{{.FullyQualifiedTargetURL}}" class="url-source">{{.FullyQualifiedTargetURL}}</a></pre>
         <p>en utilisant le nouvel URL</p>
-        <pre><a href=".FullyQualifiedShortURL">.FullyQualifiedShortURL</a></pre>
+        <pre><a href="{{.FullyQualifiedShortURL}}">{{.FullyQualifiedShortURL}}</a></pre>
         <p>pour y faire référence.</p>
-        <p><a href=".FullyQualifiedSiteURL">Retour</a></p>
+        <p><a href="{{.FullyQualifiedSiteURL}}">Retour</a></p>
     </div>
 
     {{template "footer"}}

+ 2 - 2
web/templates/404.gohtml

@@ -3,8 +3,8 @@
 <head>
     <title>{{.SiteName}}: {{.FullyQualifiedShortURL}}</title>
     <link rel="shortcut icon" href="{{.FullyQualifiedAssetsBaseURL}}/favicon.ico?v=1">
-    <link rel="stylesheet" type="text/css" href="{{.FullyQualifiedAssetsBaseURL}}/css/plusvite.css?v={{.AssetsVersion}}" />
-    <link rel="stylesheet" type="text/css" href="{{.FullyQualifiedAssetsBaseURL}}/css/riff.css?v={{.AssetsVersion}}" />
+    <link rel="stylesheet" type="text/css" href="{{.FullyQualifiedAssetsBaseURL}}/public/css/plusvite.css?v={{.AssetsVersion}}" />
+    <link rel="stylesheet" type="text/css" href="{{.FullyQualifiedAssetsBaseURL}}/public/css/riff.css?v={{.AssetsVersion}}" />
     {{template "analytics" .}}
     {{template "inlinecss"}}
     <meta http-equiv="Refresh" content="{{.RefreshDelay}};{{.FullyQualifiedSiteURL}}" />

+ 2 - 2
web/templates/home.gohtml

@@ -3,8 +3,8 @@
 <head>
     <title>Plus Vite sur le Net: service gratuit d'URLs raccourcis</title>
     <link rel="shortcut icon" href="{{.FullyQualifiedAssetsBaseURL}}/favicon.ico?v=1">
-    <link rel="stylesheet" type="text/css" href="{{.FullyQualifiedAssetsBaseURL}}/css/plusvite.css?v={{.AssetsVersion}}" />
-    <link rel="stylesheet" type="text/css" href="{{.FullyQualifiedAssetsBaseURL}}/css/riff.css?v={{.AssetsVersion}}" />
+    <link rel="stylesheet" type="text/css" href="{{.FullyQualifiedAssetsBaseURL}}/public/css/plusvite.css?v={{.AssetsVersion}}" />
+    <link rel="stylesheet" type="text/css" href="{{.FullyQualifiedAssetsBaseURL}}/public/css/riff.css?v={{.AssetsVersion}}" />
     {{template "analytics" .}}
     {{template "inlinecss"}}
 </head>

+ 0 - 0
web/templates/layout/_analytics.gohtml → web/templates/layout/analytics.gohtml


+ 0 - 0
web/templates/layout/_footer.gohtml → web/templates/layout/footer.gohtml


+ 0 - 0
web/templates/layout/_inlinecss.gohtml → web/templates/layout/inlinecss.gohtml


+ 18 - 9
web/web.go

@@ -8,6 +8,8 @@ a complete application.
 package web
 
 import (
+	"github.com/davecgh/go-spew/spew"
+	"html/template"
 	"net/http"
 	"path/filepath"
 
@@ -30,22 +32,17 @@ const (
 	HtmlTypeRegex = HtmlType
 )
 
-/**
-FullyQualifiedAssetsBaseURL	http://plusvite-cdn.osinet.eu
-FullyQualifiedShortURL		(short string)
-FullyQualifiedSiteURL		http://plusvite.net/
-FullyQualifiedTargetURL		(target string)
-RefreshDelay				(seconds int)
-SiteName					PlusVite
-*/
+var tmpl *template.Template
+
 // SetupRoutes() configures Web UI routes on the passed mux.Router.
 func SetupRoutes(router *mux.Router, configAssetsPath string) {
+	const assetsPrefix = "/public"
 	absAssetsDir, err := filepath.Abs(configAssetsPath)
 	if err != nil {
 		panic(err)
 	}
 	fs := http.FileServer(http.Dir(absAssetsDir))
-	router.PathPrefix("/public").Handler(http.StripPrefix("/public", fs))
+	router.PathPrefix(assetsPrefix).Handler(http.StripPrefix(assetsPrefix, fs))
 	router.Handle("/favicon.ico", fs)
 
 	// BUG(fgm): improve Accept header matchers once https://github.com/golang/go/issues/19307 is completed.
@@ -60,4 +57,16 @@ func SetupRoutes(router *mux.Router, configAssetsPath string) {
 	router.HandleFunc("/", handleGetRoot).
 		Methods("GET", "HEAD").
 		Name(RouteGetRoot)
+
+	base, _ := filepath.Abs(configAssetsPath + "/../templates/")
+	spew.Dump(base)
+	layout := base + "/layout"
+	tmpl = template.Must(template.ParseFiles(
+		base+"/201.gohtml",
+		base+"/404.gohtml",
+		base+"/home.gohtml",
+		layout+"/analytics.gohtml",
+		layout+"/footer.gohtml",
+		layout+"/inlinecss.gohtml",
+	))
 }