github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/system/gate/server/controllers/index.go (about) 1 // This file is part of the Smart Home 2 // Program complex distribution https://github.com/e154/smart-home 3 // Copyright (C) 2016-2023, Filippov Alex 4 // 5 // This library is free software: you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 3 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Library General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library. If not, see 17 // <https://www.gnu.org/licenses/>. 18 19 package controllers 20 21 import ( 22 "encoding/json" 23 "fmt" 24 "html/template" 25 "io/fs" 26 "net/http" 27 28 "github.com/e154/smart-home/version" 29 ) 30 31 // ControllerIndex ... 32 type ControllerIndex struct { 33 *ControllerCommon 34 } 35 36 // NewControllerIndex ... 37 func NewControllerIndex(common *ControllerCommon) *ControllerIndex { 38 return &ControllerIndex{ 39 ControllerCommon: common, 40 } 41 } 42 43 // Index ... 44 func (c ControllerIndex) Index(publicAssets fs.FS) http.Handler { 45 serverVersion, _ := json.Marshal(version.GetVersion()) 46 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 47 b := map[string]interface{}{ 48 "run_mode": c.ControllerCommon.Mode, 49 "server_version": string(serverVersion), 50 } 51 templates := template.Must(template.New("index").ParseFS(publicAssets, "index.html")) 52 53 err := templates.ExecuteTemplate(w, "index.html", &b) 54 if err != nil { 55 http.Error(w, fmt.Sprintf("index: couldn't parse template: %v", err), http.StatusInternalServerError) 56 return 57 } 58 }) 59 }