|
@@ -1,20 +1,37 @@
|
|
|
/*
|
|
|
-Copyright © 2022 NAME HERE <EMAIL ADDRESS>
|
|
|
+Copyright © 2022 Tone
|
|
|
|
|
|
+This program is free software: you can redistribute it and/or modify
|
|
|
+it under the terms of the GNU Affero General Public License as published by
|
|
|
+the Free Software Foundation, either version 3 of the License, or
|
|
|
+(at your option) any later version.
|
|
|
+
|
|
|
+This program is distributed in the hope that it will be useful,
|
|
|
+but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
+GNU Affero General Public License for more details.
|
|
|
+
|
|
|
+You should have received a copy of the GNU Affero General Public License
|
|
|
+along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
*/
|
|
|
package cmd
|
|
|
|
|
|
import (
|
|
|
+ "errors"
|
|
|
+ "fmt"
|
|
|
"html/template"
|
|
|
"io/ioutil"
|
|
|
- "log"
|
|
|
"net/http"
|
|
|
"os"
|
|
|
"path/filepath"
|
|
|
"strings"
|
|
|
+ "time"
|
|
|
|
|
|
"github.com/dustin/go-humanize"
|
|
|
+ "github.com/justinas/alice"
|
|
|
+ "github.com/rs/zerolog/hlog"
|
|
|
"github.com/spf13/cobra"
|
|
|
+ "github.com/spf13/viper"
|
|
|
"github.com/t0n3/sakuin/web"
|
|
|
)
|
|
|
|
|
@@ -42,31 +59,70 @@ var dataDir string
|
|
|
var serveCmd = &cobra.Command{
|
|
|
Use: "serve",
|
|
|
Short: "Start the HTTP server",
|
|
|
- Run: func(cmd *cobra.Command, args []string) {
|
|
|
- dataDir = "tests"
|
|
|
- log.Println("Starting Sakuin HTTP Server")
|
|
|
- mux := http.NewServeMux()
|
|
|
- mux.Handle("/assets/", web.AssetsHandler("/assets/", "dist"))
|
|
|
- mux.HandleFunc("/", serve)
|
|
|
- http.ListenAndServe(":3000", mux)
|
|
|
- },
|
|
|
+ Run: serve,
|
|
|
}
|
|
|
|
|
|
func init() {
|
|
|
rootCmd.AddCommand(serveCmd)
|
|
|
+ serveCmd.Flags().StringP("data-dir", "d", "", "Directory containing data that Sakuin will serve")
|
|
|
+ serveCmd.Flags().IntP("port", "p", 3000, "Port to listen to")
|
|
|
+ serveCmd.Flags().String("listen-addr", "0.0.0.0", "Address to listen to")
|
|
|
+
|
|
|
+ viper.BindPFlag("data-dir", serveCmd.Flags().Lookup("data-dir"))
|
|
|
+ viper.BindPFlag("port", serveCmd.Flags().Lookup("port"))
|
|
|
+ viper.BindPFlag("listen-addr", serveCmd.Flags().Lookup("listen-addr"))
|
|
|
+}
|
|
|
+
|
|
|
+func serve(cmd *cobra.Command, args []string) {
|
|
|
+ dataDir = viper.GetString("data-dir")
|
|
|
|
|
|
- // Here you will define your flags and configuration settings.
|
|
|
+ if dataDir == "" {
|
|
|
+ log.Fatal().Err(errors.New("please specify a data directory, can't be empty"))
|
|
|
+ }
|
|
|
|
|
|
- // Cobra supports Persistent Flags which will work for this command
|
|
|
- // and all subcommands, e.g.:
|
|
|
- // serveCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
|
+ _, err := os.Stat(dataDir)
|
|
|
+ if err != nil {
|
|
|
+ if os.IsNotExist(err) {
|
|
|
+ log.Fatal().Err(errors.New("please specify a valid data directory"))
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- // Cobra supports local flags which will only run when this command
|
|
|
- // is called directly, e.g.:
|
|
|
- // serveCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
|
+ middleware := alice.New()
|
|
|
+
|
|
|
+ // Install the logger handler with default output on the console
|
|
|
+ middleware = middleware.Append(hlog.NewHandler(log))
|
|
|
+
|
|
|
+ // Install some provided extra handler to set some request's context fields.
|
|
|
+ // Thanks to that handler, all our logs will come with some prepopulated fields.
|
|
|
+ middleware = middleware.Append(hlog.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
|
|
|
+ hlog.FromRequest(r).Info().
|
|
|
+ Str("method", r.Method).
|
|
|
+ Stringer("url", r.URL).
|
|
|
+ Int("status", status).
|
|
|
+ Int("size", size).
|
|
|
+ Dur("duration", duration).
|
|
|
+ Msg("")
|
|
|
+ }))
|
|
|
+ middleware = middleware.Append(hlog.RemoteAddrHandler("ip"))
|
|
|
+ middleware = middleware.Append(hlog.UserAgentHandler("user_agent"))
|
|
|
+ middleware = middleware.Append(hlog.RefererHandler("referer"))
|
|
|
+ middleware = middleware.Append(hlog.RequestIDHandler("req_id", "Request-Id"))
|
|
|
+
|
|
|
+ handler := middleware.Then(http.HandlerFunc(serverHandler))
|
|
|
+ assetsHandler := middleware.Then(web.AssetsHandler("/assets/", "dist"))
|
|
|
+
|
|
|
+ port := viper.GetInt("port")
|
|
|
+ address := viper.GetString("listen-addr")
|
|
|
+
|
|
|
+ mux := http.NewServeMux()
|
|
|
+ mux.Handle("/assets/", assetsHandler)
|
|
|
+ mux.Handle("/", handler)
|
|
|
+
|
|
|
+ log.Info().Msgf("Starting Sakuin HTTP Server on %s:%d", address, port)
|
|
|
+ http.ListenAndServe(fmt.Sprintf("%s:%d", address, port), mux)
|
|
|
}
|
|
|
|
|
|
-func serve(w http.ResponseWriter, r *http.Request) {
|
|
|
+func serverHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
// Filepath, from the root data dir
|
|
|
fp := filepath.Join(dataDir, filepath.Clean(r.URL.Path))
|
|
|
// Cleaned filepath, without the root data dir, used for template rendering purpose
|
|
@@ -76,8 +132,9 @@ func serve(w http.ResponseWriter, r *http.Request) {
|
|
|
info, err := os.Stat(fp)
|
|
|
if err != nil {
|
|
|
if os.IsNotExist(err) {
|
|
|
- http.NotFound(w, r)
|
|
|
- log.Printf("404 - %s\n", cfp)
|
|
|
+ notFound, _ := template.ParseFS(web.NotFound, "404.html")
|
|
|
+ w.WriteHeader(http.StatusNotFound)
|
|
|
+ notFound.ExecuteTemplate(w, "404.html", nil)
|
|
|
return
|
|
|
}
|
|
|
}
|
|
@@ -86,7 +143,7 @@ func serve(w http.ResponseWriter, r *http.Request) {
|
|
|
if info.IsDir() {
|
|
|
files, err := ioutil.ReadDir(fp)
|
|
|
if err != nil {
|
|
|
- log.Fatal(err)
|
|
|
+ log.Error().Err(err)
|
|
|
}
|
|
|
|
|
|
// Init template variables
|
|
@@ -123,7 +180,7 @@ func serve(w http.ResponseWriter, r *http.Request) {
|
|
|
tmpl, err := template.ParseFS(web.Index, "index.html")
|
|
|
if err != nil {
|
|
|
// Log the detailed error
|
|
|
- log.Println(err.Error())
|
|
|
+ log.Error().Err(err)
|
|
|
// Return a generic "Internal Server Error" message
|
|
|
http.Error(w, http.StatusText(500), 500)
|
|
|
return
|
|
@@ -131,16 +188,16 @@ func serve(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
// Return file listing in the template
|
|
|
if err := tmpl.ExecuteTemplate(w, "index.html", templateVars); err != nil {
|
|
|
- log.Println(err.Error())
|
|
|
+ log.Error().Err(err)
|
|
|
http.Error(w, http.StatusText(500), 500)
|
|
|
}
|
|
|
- log.Printf("200 - DIR %s\n", "/")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
if !info.IsDir() {
|
|
|
- http.ServeFile(w, r, fp)
|
|
|
- log.Printf("200 - FILE %s\n", cfp)
|
|
|
+ content, _ := os.Open(fp)
|
|
|
+ w.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", info.Name()))
|
|
|
+ http.ServeContent(w, r, fp, info.ModTime(), content)
|
|
|
return
|
|
|
}
|
|
|
}
|