|
@@ -1,9 +1,9 @@
|
|
package web
|
|
package web
|
|
|
|
|
|
import (
|
|
import (
|
|
- "fmt"
|
|
+ "io"
|
|
"net/http"
|
|
"net/http"
|
|
- "time"
|
|
+ "strings"
|
|
|
|
|
|
"code.osinet.fr/fgm/kurz/domain"
|
|
"code.osinet.fr/fgm/kurz/domain"
|
|
"github.com/gorilla/mux"
|
|
"github.com/gorilla/mux"
|
|
@@ -22,41 +22,49 @@ Template variables:
|
|
|
|
|
|
type bytes = []byte
|
|
type bytes = []byte
|
|
|
|
|
|
-func build403(w http.ResponseWriter, short string) (bytes, error) {
|
|
+func build403(w http.ResponseWriter, short string) error {
|
|
- type Short403Data struct {
|
|
+ data := struct {
|
|
- FullyQualifiedAssetsBaseURL string
|
|
+ FullyQualifiedShortURL string
|
|
- FullyQualifiedShortURL string
|
|
+ Globals
|
|
- FullyQualifiedSiteURL string
|
|
+ }{
|
|
- RefreshDelay time.Duration
|
|
+ short,
|
|
- SiteName string
|
|
+ globals,
|
|
}
|
|
}
|
|
|
|
|
|
- data := Short403Data{}
|
|
+ sw := &strings.Builder{}
|
|
- tmpl.Execute(w, data)
|
|
+ err := tmpl.ExecuteTemplate(sw, "403", data)
|
|
- return bytes(""), nil
|
|
+ if err != nil {
|
|
|
|
+ w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
+ } else {
|
|
|
|
+ w.WriteHeader(http.StatusForbidden)
|
|
|
|
+ io.Copy(w, strings.NewReader(sw.String()))
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return err
|
|
}
|
|
}
|
|
|
|
|
|
-func build404(w http.ResponseWriter, short string) (bytes, error) {
|
|
+func build404(w http.ResponseWriter, short string) error {
|
|
- type Short404Data struct {
|
|
+ data := struct {
|
|
- AssetsVersion int
|
|
+ FullyQualifiedShortURL string
|
|
- FullyQualifiedAssetsBaseURL string
|
|
+ Globals
|
|
- FullyQualifiedShortURL string
|
|
+ }{
|
|
- FullyQualifiedSiteURL string
|
|
+ short,
|
|
- FullyQualifiedTargetURL string
|
|
+ globals,
|
|
- RefreshDelay time.Duration
|
|
|
|
- SiteName string
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- data := Short404Data{}
|
|
+ sw := &strings.Builder{}
|
|
- err := tmpl.Execute(w, data)
|
|
+ err := tmpl.ExecuteTemplate(sw, "404", data)
|
|
if err != nil {
|
|
if err != nil {
|
|
- fmt.Println(err)
|
|
+ w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
+ } else {
|
|
|
|
+ w.WriteHeader(http.StatusNotFound)
|
|
|
|
+ io.Copy(w, strings.NewReader(sw.String()))
|
|
}
|
|
}
|
|
- return bytes(""), nil
|
|
+ return err
|
|
}
|
|
}
|
|
|
|
|
|
-func build451(w http.ResponseWriter, short string) (bytes, error) {
|
|
+func build451(w http.ResponseWriter, short string) error {
|
|
- return nil, nil
|
|
+ return nil
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -84,42 +92,18 @@ func handleGetShort(w http.ResponseWriter, r *http.Request) {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
- var status int
|
|
|
|
switch domainErr.Kind {
|
|
switch domainErr.Kind {
|
|
case domain.ShortNotFound:
|
|
case domain.ShortNotFound:
|
|
- status = http.StatusNotFound
|
|
+ build404(w, short)
|
|
- if page, err := build404(w, short); err != nil {
|
|
|
|
- status = http.StatusInternalServerError
|
|
|
|
- w.WriteHeader(status)
|
|
|
|
- } else {
|
|
|
|
- w.WriteHeader(status)
|
|
|
|
- w.Write(page)
|
|
|
|
- }
|
|
|
|
|
|
|
|
case domain.TargetBlockedError:
|
|
case domain.TargetBlockedError:
|
|
- status = http.StatusForbidden
|
|
+ build403(w, short)
|
|
- if page, err := build403(w, short); err != nil {
|
|
|
|
- status = http.StatusInternalServerError
|
|
|
|
- w.WriteHeader(status)
|
|
|
|
- } else {
|
|
|
|
- w.WriteHeader(status)
|
|
|
|
- w.Write(page)
|
|
|
|
- }
|
|
|
|
|
|
|
|
case domain.TargetCensoredError:
|
|
case domain.TargetCensoredError:
|
|
- status = http.StatusUnavailableForLegalReasons
|
|
+ build451(w, short)
|
|
- if page, err := build451(w, short); err != nil {
|
|
|
|
- status = http.StatusInternalServerError
|
|
|
|
- w.WriteHeader(status)
|
|
|
|
- } else {
|
|
|
|
- w.WriteHeader(status)
|
|
|
|
- w.Write(page)
|
|
|
|
- }
|
|
|
|
|
|
|
|
default:
|
|
default:
|
|
|
|
|
|
- status = http.StatusInternalServerError
|
|
+ w.WriteHeader(http.StatusInternalServerError)
|
|
- w.WriteHeader(status)
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|