This repository has been archived on 2024-01-11. You can view files and clone it, but cannot push or open issues or pull requests.
admin/main.go

78 lines
1.2 KiB
Go
Raw Normal View History

2022-01-26 17:00:22 +00:00
package main
import (
"fmt"
2022-01-26 19:28:26 +00:00
"io/ioutil"
"log"
2022-01-26 17:00:22 +00:00
"net/http"
2022-01-27 20:21:09 +00:00
"text/template"
2022-01-26 19:28:26 +00:00
"gopkg.in/yaml.v3"
2022-01-26 17:00:22 +00:00
)
2022-01-27 20:21:09 +00:00
// Config types
// ------------
2022-01-26 19:28:26 +00:00
type ServerConfig struct {
Name string
Homepage string
}
type AdminConfig struct {
WebRoot string
2022-01-27 20:21:09 +00:00
Port int
LogPath string
2022-01-26 19:28:26 +00:00
}
type AuthConfig struct {
GiteaURL string
AuthorizedUsers []string
}
type Config struct {
Server ServerConfig
Admin AdminConfig
Auth AuthConfig
}
var config = new(Config)
func loadConfig() (err error) {
configfile, err := ioutil.ReadFile("config.yml")
if err != nil {
return err
}
err = yaml.Unmarshal(configfile, &config)
return err
}
2022-01-27 20:21:09 +00:00
func handler(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
var tmpl string
if path == "/" {
tmpl = "templates/home.html"
} else {
tmpl = fmt.Sprintf("templates/%s", path)
}
t, _ := template.ParseFiles(tmpl)
t.Execute(w, config)
}
2022-01-26 17:00:22 +00:00
func main() {
2022-01-26 19:28:26 +00:00
err := loadConfig()
2022-01-27 20:21:09 +00:00
fmt.Print(config)
2022-01-26 19:28:26 +00:00
if err != nil {
2022-01-27 20:21:09 +00:00
log.Fatalf("couldn't load config: %s", err)
2022-01-26 19:28:26 +00:00
}
2022-01-27 20:21:09 +00:00
http.Handle("/resources/",
http.StripPrefix("/resources/",
http.FileServer(http.Dir("./resources"))))
http.HandleFunc("/", handler)
2022-01-26 17:00:22 +00:00
2022-01-26 19:28:26 +00:00
err = http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal(err)
}
2022-01-26 17:00:22 +00:00
}