A More Secure Internet Connection for Your Home https://fen.gg
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

76 lines
2.3 KiB

  1. package main
  2. //
  3. // Fengg Security Gateway Server Application
  4. // Copyright (C) 2020 Lukas Matt <support@fen.gg>
  5. //
  6. // This program is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License.
  9. //
  10. // This program 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
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. //
  18. import (
  19. "fmt"
  20. "tea.fen.gg/fengg/server/config"
  21. "tea.fen.gg/fengg/server/model"
  22. "tea.fen.gg/fengg/server/helper"
  23. "tea.fen.gg/fengg/server/handler"
  24. "tea.fen.gg/fengg/server/netfilter"
  25. "tea.fen.gg/fengg/server/types"
  26. "tea.fen.gg/fengg/server/worker"
  27. "github.com/golang-migrate/migrate/v4"
  28. _ "github.com/golang-migrate/migrate/v4/database/postgres"
  29. _ "github.com/golang-migrate/migrate/v4/source/file"
  30. )
  31. // init setup default configuration variables and run database migrations
  32. func init() {
  33. // setup module logging
  34. helper.Logger(logger)
  35. handler.Logger(logger)
  36. model.Logger(logger)
  37. netfilter.Logger(logger)
  38. types.Logger(logger)
  39. worker.Logger(logger)
  40. // configure database access
  41. dbConnect := fmt.Sprintf(
  42. "postgres://%s:%s@%s/%s?sslmode=disable",
  43. config.App.Database.User,
  44. config.App.Database.Password,
  45. config.App.Database.Addr,
  46. config.App.Database.Name,
  47. )
  48. model.DatabaseAuth("postgres", dbConnect)
  49. // run database migrations
  50. migrateDB, err := migrate.New("file://db/migrations", dbConnect)
  51. if err != nil {
  52. logger.Fatal().Err(err).Msg("cannot migrate database")
  53. }
  54. migrateDB.Log = types.MigrateLogger{Logger: logger}
  55. err = migrateDB.Up()
  56. if err != nil && err != migrate.ErrNoChange {
  57. logger.Fatal().Err(err).Msg("cannot migrate database")
  58. }
  59. wlConfig := model.NewConfiguration(types.WhiteListConfiguration)
  60. if !wlConfig.Exists() {
  61. whitelist := map[string]interface{}{
  62. "entries": []string{"127.0.0.0/8", "::1/128"},
  63. }
  64. err = wlConfig.MarshalAndCreate(whitelist)
  65. if err != nil {
  66. logger.Fatal().Err(err).Msg("cannot marshal and create configuration")
  67. }
  68. }
  69. }