59 lines
936 B
Go
59 lines
936 B
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
func main() {
|
|
if err := run(); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
type (
|
|
Data struct {
|
|
Routes map[string]Route
|
|
}
|
|
Route struct {
|
|
Body string
|
|
Redirect string
|
|
}
|
|
)
|
|
|
|
func run() error {
|
|
f := os.Args[1]
|
|
return http.ListenAndServe(":3001", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
b, err := os.ReadFile(f)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
type Route struct {
|
|
}
|
|
var data Data
|
|
if err := yaml.Unmarshal(b, &data); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
k := r.URL.Path
|
|
|
|
v, ok := data.Routes[k]
|
|
if !ok {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
if u := v.Redirect; u != "" {
|
|
http.Redirect(w, r, u, http.StatusSeeOther)
|
|
} else {
|
|
w.Header().Set("Content-Type", "text/html")
|
|
w.Write([]byte(v.Body))
|
|
}
|
|
}))
|
|
}
|