瀏覽代碼

Refactored api/ and web/ to web/api and web/ui. Added web/i18n.

Frederic G. MARAND 5 年之前
父節點
當前提交
1cf887ba2c
共有 43 個文件被更改,包括 110 次插入11 次删除
  1. 7 0
      .idea/bashsupport_project.xml
  2. 8 0
      .idea/runConfigurations/Generate.xml
  3. 13 0
      .idea/runConfigurations/Go_generate.xml
  4. 3 0
      Makefile
  5. 1 1
      cmd/kurzd/dist.config.yml
  6. 2 0
      cmd/kurzd/kurzd.go
  7. 4 4
      cmd/kurzd/serve.go
  8. 1 0
      go.mod
  9. 0 0
      infrastructure/infrastructure.go
  10. 0 0
      web/api/api.go
  11. 0 0
      web/api/api_testing.go
  12. 0 0
      web/api/get.go
  13. 0 0
      web/api/get_test.go
  14. 0 0
      web/api/post.go
  15. 0 0
      web/api/post_test.go
  16. 25 0
      web/i18n/i18n_test.go
  17. 40 0
      web/i18n/i18ner.go
  18. 1 1
      web/ui/get_root.go
  19. 1 1
      web/ui/get_short.go
  20. 1 1
      web/ui/gorilla_imported.go
  21. 1 1
      web/ui/post_target.go
  22. 0 0
      web/ui/public/css/index.html
  23. 0 0
      web/ui/public/css/plusvite.css
  24. 0 0
      web/ui/public/css/riff.css
  25. 0 0
      web/ui/public/favicon.ico
  26. 0 0
      web/ui/public/images/index.html
  27. 0 0
      web/ui/public/images/lights.jpg
  28. 0 0
      web/ui/public/images/valid-xhtml10.gif
  29. 0 0
      web/ui/public/images/vcss.gif
  30. 0 0
      web/ui/public/index.html
  31. 0 0
      web/ui/public/js/index.html
  32. 0 0
      web/ui/templates/201.gohtml
  33. 0 0
      web/ui/templates/403.gohtml
  34. 0 0
      web/ui/templates/404.gohtml
  35. 0 0
      web/ui/templates/409.gohtml
  36. 0 0
      web/ui/templates/451.gohtml
  37. 0 0
      web/ui/templates/home.gohtml
  38. 0 0
      web/ui/templates/layout/analytics.gohtml
  39. 0 0
      web/ui/templates/layout/flashes.gohtml
  40. 0 0
      web/ui/templates/layout/footer.gohtml
  41. 0 0
      web/ui/templates/layout/inlinecss.gohtml
  42. 1 1
      web/ui/web.go
  43. 1 1
      web/ui/web_test.go

+ 7 - 0
.idea/bashsupport_project.xml

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="BashSupportProjectSettings">
+    <option name="autocompleteBuiltinVars" value="true" />
+    <option name="projectInterpreter" value="/bin/bash" />
+  </component>
+</project>

+ 8 - 0
.idea/runConfigurations/Generate.xml

@@ -0,0 +1,8 @@
+<component name="ProjectRunConfigurationManager">
+  <configuration default="false" name="Generate" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile">
+    <makefile filename="$PROJECT_DIR$/Makefile" target="generate -v cmd" workingDirectory="$PROJECT_DIR$" arguments="">
+      <envs />
+    </makefile>
+    <method v="2" />
+  </configuration>
+</component>

+ 13 - 0
.idea/runConfigurations/Go_generate.xml

@@ -0,0 +1,13 @@
+<component name="ProjectRunConfigurationManager">
+  <configuration default="false" name="Go generate" type="BashConfigurationType" factoryName="Bash">
+    <module name="kurz" />
+    <option name="INTERPRETER_OPTIONS" value="" />
+    <option name="INTERPRETER_PATH" value="" />
+    <option name="PROJECT_INTERPRETER" value="true" />
+    <option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
+    <option name="PARENT_ENVS" value="true" />
+    <option name="SCRIPT_NAME" value="go" />
+    <option name="PARAMETERS" value="generate -v" />
+    <method v="2" />
+  </configuration>
+</component>

+ 3 - 0
Makefile

@@ -12,3 +12,6 @@ cmd/kurz/kurz: $(wildcard cmd/kurz/*.go)
 
 
 cmd/kurzd/kurzd: $(wildcard api/*.go) $(wildcard cmd/kurzd/*.go) $(wildcard domain/*.go) $(wildcard infrastructure/*.go) $(wildcard migrations/*.go) $(wildcard web/*.go)
 cmd/kurzd/kurzd: $(wildcard api/*.go) $(wildcard cmd/kurzd/*.go) $(wildcard domain/*.go) $(wildcard infrastructure/*.go) $(wildcard migrations/*.go) $(wildcard web/*.go)
 	cd cmd/kurzd && go build
 	cd cmd/kurzd && go build
+
+generate:
+	go generate -v

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

@@ -5,7 +5,7 @@ database:
   test_dsn: <user>:<pass>@[<server>]/<database>
   test_dsn: <user>:<pass>@[<server>]/<database>
 web:
 web:
   # May be absolute or relative to the current directory.
   # May be absolute or relative to the current directory.
-  assetsPath: web/public
+  assetsPath: web/ui/public
   # The initial version cache buster on Kurz start. Will probably not remain in config.
   # The initial version cache buster on Kurz start. Will probably not remain in config.
   assetsVersion: 1
   assetsVersion: 1
   listenAddress: ":3000"
   listenAddress: ":3000"

+ 2 - 0
cmd/kurzd/kurzd.go

@@ -1,6 +1,8 @@
 // Package kurzd contains the Kurz daemon command.
 // Package kurzd contains the Kurz daemon command.
 package main
 package main
 
 
+//go:generate gotext -srclang=en update -out=catalog.go -lang=fr,de,en
+
 import (
 import (
 	"fmt"
 	"fmt"
 	"os"
 	"os"

+ 4 - 4
cmd/kurzd/serve.go

@@ -9,10 +9,10 @@ import (
 	"os/signal"
 	"os/signal"
 	"time"
 	"time"
 
 
-	"code.osinet.fr/fgm/kurz/web"
+	"code.osinet.fr/fgm/kurz/web/ui"
 	"github.com/spf13/viper"
 	"github.com/spf13/viper"
 
 
-	"code.osinet.fr/fgm/kurz/api"
+	"code.osinet.fr/fgm/kurz/web/api"
 	"code.osinet.fr/fgm/kurz/domain"
 	"code.osinet.fr/fgm/kurz/domain"
 	"code.osinet.fr/fgm/kurz/infrastructure"
 	"code.osinet.fr/fgm/kurz/infrastructure"
 	"github.com/gorilla/mux"
 	"github.com/gorilla/mux"
@@ -76,12 +76,12 @@ func serveHandler(_ *cobra.Command, args []string) {
 	viper.SetDefault("web.assetsPath", "web/public")
 	viper.SetDefault("web.assetsPath", "web/public")
 	assetsPath := viper.Get("web.assetsPath").(string)
 	assetsPath := viper.Get("web.assetsPath").(string)
 	webConfig := viper.Get("web").(map[string]interface{})
 	webConfig := viper.Get("web").(map[string]interface{})
-	web.SetupGlobals(webConfig)
+	ui.SetupGlobals(webConfig)
 
 
 	// Set up Web API and UI routes.
 	// Set up Web API and UI routes.
 	router := mux.NewRouter()
 	router := mux.NewRouter()
 	api.SetupRoutes(router)
 	api.SetupRoutes(router)
-	web.SetupUI(router, assetsPath)
+	ui.SetupUI(router, assetsPath)
 	http.Handle("/", router)
 	http.Handle("/", router)
 
 
 	// Start a server that can handle a SIGINT to shutdown.
 	// Start a server that can handle a SIGINT to shutdown.

+ 1 - 0
go.mod

@@ -11,6 +11,7 @@ require (
 	github.com/spf13/cobra v0.0.3
 	github.com/spf13/cobra v0.0.3
 	github.com/spf13/pflag v1.0.3 // indirect
 	github.com/spf13/pflag v1.0.3 // indirect
 	github.com/spf13/viper v1.2.1
 	github.com/spf13/viper v1.2.1
+	golang.org/x/text v0.3.0
 	google.golang.org/appengine v1.3.0 // indirect
 	google.golang.org/appengine v1.3.0 // indirect
 	gopkg.in/yaml.v2 v2.2.1
 	gopkg.in/yaml.v2 v2.2.1
 )
 )

+ 0 - 0
infrastructure/infratructure.go → infrastructure/infrastructure.go


+ 0 - 0
api/api.go → web/api/api.go


+ 0 - 0
api/api_testing.go → web/api/api_testing.go


+ 0 - 0
api/get.go → web/api/get.go


+ 0 - 0
api/get_test.go → web/api/get_test.go


+ 0 - 0
api/post.go → web/api/post.go


+ 0 - 0
api/post_test.go → web/api/post_test.go


+ 25 - 0
web/i18n/i18n_test.go

@@ -0,0 +1,25 @@
+package i18n
+
+import (
+	"net/http"
+)
+
+func HelloHandlerFunc(w http.ResponseWriter, r *http.Request) {
+	p := Printer(r)
+
+	p.Fprintf(w, "Hello, world")
+}
+
+// helloHandler implements http.Handler.
+type helloHandler struct {}
+
+func (h helloHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	p := Printer(r)
+
+	p.Fprintf(w, "Hello, world")
+}
+
+func ExampleWithPrinter() {
+	http.Handle("/h", WithPrinter(helloHandler{}))
+	http.Handle("/hf", WithPrinter(http.HandlerFunc(HelloHandlerFunc)))
+}

+ 40 - 0
web/i18n/i18ner.go

@@ -0,0 +1,40 @@
+package i18n
+
+import (
+	"context"
+	"errors"
+	"net/http"
+
+	"golang.org/x/text/message"
+)
+
+/*
+WithPrinter() is a higher-order function injecting into the request context a
+printer for the language best matching the Accept-Language header in the incoming
+request.
+
+The wrapped handler can use Printer() to get a message printer instance
+configured for the best available language for the request.
+*/
+func WithPrinter(h http.Handler) http.Handler {
+	h2 := func(w http.ResponseWriter, r *http.Request) {
+		// Utilise le matcher de message.DefaultCatalog.
+		tag := message.MatchLanguage(r.Header.Get("Accept-Language"))
+		c := context.WithValue(r.Context(), "printer", message.NewPrinter(tag))
+		h.ServeHTTP(w, r.WithContext(c))
+	}
+	return http.HandlerFunc(h2)
+}
+
+/*
+Printer() returns a message printer configured for the language best matching the
+Accept-Language in the request, or panic if the handler invoking it was not
+wrapped by a WithPrinter() call.
+ */
+func Printer(r *http.Request) *message.Printer {
+	p, ok := r.Context().Value("printer").(*message.Printer)
+	if !ok {
+		panic(errors.New("trying to use i18n.Printer in a handler not wrapped with i18.WithPrinter"))
+	}
+	return p
+}

+ 1 - 1
web/get_root.go → web/ui/get_root.go

@@ -1,4 +1,4 @@
-package web
+package ui
 
 
 import (
 import (
 	"io"
 	"io"

+ 1 - 1
web/get_short.go → web/ui/get_short.go

@@ -1,4 +1,4 @@
-package web
+package ui
 
 
 import (
 import (
 	"fmt"
 	"fmt"

+ 1 - 1
web/gorilla_imported.go → web/ui/gorilla_imported.go

@@ -1,4 +1,4 @@
-package web
+package ui
 
 
 // mapToPairs converts a string map to a slice of string pairs
 // mapToPairs converts a string map to a slice of string pairs
 // Part of the mux_test.go file in Gorilla Mux, used under its BSD-like license.
 // Part of the mux_test.go file in Gorilla Mux, used under its BSD-like license.

+ 1 - 1
web/post_target.go → web/ui/post_target.go

@@ -1,4 +1,4 @@
-package web
+package ui
 
 
 import (
 import (
 	"fmt"
 	"fmt"

+ 0 - 0
web/public/css/index.html → web/ui/public/css/index.html


+ 0 - 0
web/public/css/plusvite.css → web/ui/public/css/plusvite.css


+ 0 - 0
web/public/css/riff.css → web/ui/public/css/riff.css


+ 0 - 0
web/public/favicon.ico → web/ui/public/favicon.ico


+ 0 - 0
web/public/images/index.html → web/ui/public/images/index.html


+ 0 - 0
web/public/images/lights.jpg → web/ui/public/images/lights.jpg


+ 0 - 0
web/public/images/valid-xhtml10.gif → web/ui/public/images/valid-xhtml10.gif


+ 0 - 0
web/public/images/vcss.gif → web/ui/public/images/vcss.gif


+ 0 - 0
web/public/index.html → web/ui/public/index.html


+ 0 - 0
web/public/js/index.html → web/ui/public/js/index.html


+ 0 - 0
web/templates/201.gohtml → web/ui/templates/201.gohtml


+ 0 - 0
web/templates/403.gohtml → web/ui/templates/403.gohtml


+ 0 - 0
web/templates/404.gohtml → web/ui/templates/404.gohtml


+ 0 - 0
web/templates/409.gohtml → web/ui/templates/409.gohtml


+ 0 - 0
web/templates/451.gohtml → web/ui/templates/451.gohtml


+ 0 - 0
web/templates/home.gohtml → web/ui/templates/home.gohtml


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


+ 0 - 0
web/templates/layout/flashes.gohtml → web/ui/templates/layout/flashes.gohtml


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


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


+ 1 - 1
web/web.go → web/ui/web.go

@@ -5,7 +5,7 @@ These routes are exposed by running SetupUI(listenAddress), which is enough to
 configure the Kurz domain API. Be sure to also configure the domain SPI to have
 configure the Kurz domain API. Be sure to also configure the domain SPI to have
 a complete application.
 a complete application.
 */
 */
-package web
+package ui
 
 
 import (
 import (
 	"errors"
 	"errors"

+ 1 - 1
web/web_test.go → web/ui/web_test.go

@@ -1,4 +1,4 @@
-package web
+package ui
 
 
 import (
 import (
 	"fmt"
 	"fmt"