33
cmd/http.go
Normal file
33
cmd/http.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type ResponseWriter struct {
|
||||
http.ResponseWriter
|
||||
StatusCode int
|
||||
}
|
||||
|
||||
func (w *ResponseWriter) WriteHeader(statusCode int) {
|
||||
w.StatusCode = statusCode
|
||||
w.ResponseWriter.WriteHeader(statusCode)
|
||||
}
|
||||
|
||||
type Handler struct {
|
||||
http.Handler
|
||||
}
|
||||
|
||||
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
rw := &ResponseWriter{w, 0}
|
||||
h.Handler.ServeHTTP(rw, r)
|
||||
logrus.Println(r.RemoteAddr, r.Method, rw.StatusCode, r.URL)
|
||||
}
|
||||
|
||||
func HttpServe(dir string, bind string) {
|
||||
handler := &Handler{http.FileServer(http.Dir(dir))}
|
||||
http.Handle("/", handler)
|
||||
logrus.Printf("[http] Listening on %s\n", bind)
|
||||
logrus.Fatal(http.ListenAndServe(bind, nil))
|
||||
}
|
||||
38
cmd/root.go
Normal file
38
cmd/root.go
Normal file
@@ -0,0 +1,38 @@
|
||||
// Copyright (C) 2026 Shin'ya Minazuki
|
||||
// Tjis file is part of Yomi
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
bind string
|
||||
root string
|
||||
)
|
||||
|
||||
// String set at compile-time
|
||||
var Version string
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "yomi",
|
||||
Short: "A staging HTTP server for static content",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
logrus.Printf("Yomi v%s is starting...", Version)
|
||||
HttpServe(root, bind)
|
||||
},
|
||||
Version: Version,
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.Flags().StringVarP(&bind, "bind", "b", "127.0.0.1:8000", "")
|
||||
rootCmd.Flags().StringVarP(&root, "root", "r", ".", "")
|
||||
}
|
||||
Reference in New Issue
Block a user