data.yaml
This commit is contained in:
42
srv/main.go
Normal file
42
srv/main.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func run() error {
|
||||
f := os.Args[1]
|
||||
b, err := os.ReadFile(f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var data struct {
|
||||
Routes map[string]string
|
||||
}
|
||||
if err := yaml.Unmarshal(b, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return http.ListenAndServe(":3001", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
k := r.URL.Path
|
||||
|
||||
v, ok := data.Routes[k]
|
||||
if !ok {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
w.Write([]byte(v))
|
||||
}))
|
||||
}
|
||||
Reference in New Issue
Block a user