Import project backup into new git repo
This commit is contained in:
commit
9606d8c2df
20 changed files with 1430 additions and 0 deletions
72
cmd/root.go
Normal file
72
cmd/root.go
Normal file
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
Copyright © 2021 AGECEM
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var cfgFile string
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "babillard",
|
||||
Short: "Application web pour afficher une rotation de nouvelles sous forme d'images",
|
||||
}
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||
func Execute() {
|
||||
cobra.CheckErr(rootCmd.Execute())
|
||||
}
|
||||
|
||||
func init() {
|
||||
cobra.OnInitialize(initConfig)
|
||||
|
||||
rootCmd.PersistentFlags().StringVarP(&cfgFile,
|
||||
"config", "c",
|
||||
"", "fichier de config (default: /etc/babillard/babillard-config.yaml)")
|
||||
}
|
||||
|
||||
// initConfig reads in config file and ENV variables if set.
|
||||
func initConfig() {
|
||||
if cfgFile != "" {
|
||||
// Use config file from the flag.
|
||||
viper.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
viper.AddConfigPath("/etc/babillard")
|
||||
viper.SetConfigType("yaml")
|
||||
viper.SetConfigName("babillard-config")
|
||||
}
|
||||
|
||||
viper.AutomaticEnv() // read in environment variables that match
|
||||
|
||||
// If a config file is found, read it in.
|
||||
if err := viper.ReadInConfig(); err == nil {
|
||||
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
|
||||
}
|
||||
}
|
119
cmd/server.go
Normal file
119
cmd/server.go
Normal file
|
@ -0,0 +1,119 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// serverCmd represents the server command
|
||||
var serverCmd = &cobra.Command{
|
||||
Use: "server",
|
||||
Short: "Démarrer le serveur web",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
runServer()
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(serverCmd)
|
||||
declareFlags()
|
||||
}
|
||||
|
||||
func declareFlags() {
|
||||
serverCmd.Flags().IntP("port", "p", 8080, "Port réseau à utiliser (config: \"server.port\")")
|
||||
viper.BindPFlag("server.port", serverCmd.Flags().Lookup("port"))
|
||||
|
||||
serverCmd.Flags().String("contenu_dir", "contenu", "Répertoire du contenu à exposer (config: \"server.contenu_dir\")")
|
||||
viper.BindPFlag("server.contenu_dir", serverCmd.Flags().Lookup("contenu_dir"))
|
||||
|
||||
serverCmd.Flags().String("static_dir", "static", "Répertoire des fichiers statiques à exposer (config: \"server.static_dir\")")
|
||||
viper.BindPFlag("server.static_dir", serverCmd.Flags().Lookup("static_dir"))
|
||||
}
|
||||
|
||||
func runServer() {
|
||||
log.Print("[I] Starting webserver")
|
||||
|
||||
port := fmt.Sprintf(":%d", viper.GetInt("server.port"))
|
||||
static_dir := viper.GetString("server.static_dir")
|
||||
|
||||
e := echo.New()
|
||||
|
||||
g := e.Group("")
|
||||
|
||||
e.Pre(middleware.RemoveTrailingSlash())
|
||||
|
||||
g.Static("/", static_dir)
|
||||
|
||||
g.GET("/api", showAPISpecs)
|
||||
g.GET("/api/contenu", showContenu)
|
||||
g.GET("/api/contenu/:filename", showFile)
|
||||
|
||||
e.Logger.Fatal(e.Start(port))
|
||||
|
||||
}
|
||||
|
||||
func listContenu(path string) ([]string, error) {
|
||||
var files []string
|
||||
f, err_open := os.Open(path)
|
||||
defer f.Close()
|
||||
|
||||
if err_open != nil {
|
||||
return nil, err_open
|
||||
}
|
||||
fileInfo, err_read := f.Readdir(-1)
|
||||
if err_read != nil {
|
||||
return nil, err_read
|
||||
}
|
||||
|
||||
for _, file := range fileInfo {
|
||||
if file.Name() != ".gitkeep" && file.Name() != "messages.txt" {
|
||||
files = append(files, file.Name())
|
||||
}
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
// Handlers
|
||||
|
||||
func showAPISpecs(c echo.Context) error {
|
||||
apispec := `agecem/babillard
|
||||
API Specifications
|
||||
-----
|
||||
'/' | GET | Affiche l'application 'slider'
|
||||
'/api' | GET | Affiche les spécifications API
|
||||
'/api/contenu' | GET | Affiche le nom des fichiers dans 'contenu/'
|
||||
'/api/contenu/{filename}' | GET | Affiche le fichier 'contenu/{filename}'
|
||||
-----`
|
||||
|
||||
return c.String(http.StatusOK, apispec)
|
||||
}
|
||||
|
||||
func showContenu(c echo.Context) error {
|
||||
contenu_dir := viper.GetString("server.contenu_dir")
|
||||
files, err := listContenu(contenu_dir)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return c.String(http.StatusOK, strings.Join(files, ";"))
|
||||
}
|
||||
|
||||
func showFile(c echo.Context) error {
|
||||
filename := c.Param("filename")
|
||||
contenu_dir := viper.GetString("server.contenu_dir")
|
||||
|
||||
if filename == ".gitkeep" {
|
||||
return c.String(http.StatusOK, "invalid file requested")
|
||||
}
|
||||
|
||||
return c.File(fmt.Sprintf("%s/%s", contenu_dir, filename))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue