Settings functional, start adding logging

Modifying settings also formats it nicely, as a bonus.
Also we using shortuuid instead of normal uuidv4 now because its the same
length as what I used in the python version.
This commit is contained in:
Harvey Tindall
2020-07-31 22:07:09 +01:00
parent 024c0b56aa
commit 326b274329
4 changed files with 119 additions and 61 deletions
+55 -29
View File
@@ -7,38 +7,40 @@ import (
"fmt"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/lithammer/shortuuid/v3"
"gopkg.in/ini.v1"
"io/ioutil"
"log"
"os"
"path/filepath"
)
// Username is JWT!
type User struct {
UserID uuid.UUID `json:"id"`
Username string `json:"username"`
Password string `json:"password"`
UserID string `json:"id"`
Username string `json:"username"`
Password string `json:"password"`
}
type appContext struct {
config *ini.File
config_path string
configBase_path string
configBase map[string]interface{}
data_path string
local_path string
cssFile string
bsVersion int
jellyfinLogin bool
users []User
jf Jellyfin
authJf Jellyfin
datePattern string
timePattern string
storage Storage
validator Validator
email Emailer
config *ini.File
config_path string
configBase_path string
configBase map[string]interface{}
data_path string
local_path string
cssFile string
bsVersion int
jellyfinLogin bool
users []User
jf Jellyfin
authJf Jellyfin
datePattern string
timePattern string
storage Storage
validator Validator
email Emailer
info, debug, err *log.Logger
}
func GenerateSecret(length int) (string, error) {
@@ -73,7 +75,20 @@ func main() {
ctx.config_path = "/home/hrfee/.jf-accounts/config.ini"
ctx.data_path = "/home/hrfee/.jf-accounts"
ctx.local_path = "data"
ctx.loadConfig()
if ctx.loadConfig() != nil {
ctx.err.Fatalf("Failed to load config file \"%s\"", ctx.config_path)
}
ctx.info = log.New(os.Stdout, "INFO: ", log.Ltime)
ctx.err = log.New(os.Stdout, "INFO: ", log.Ltime|log.Lshortfile)
if ctx.config.Section("ui").Key("debug").MustBool(true) {
ctx.debug = log.New(os.Stdout, "DEBUG: ", log.Ltime|log.Lshortfile)
} else {
ctx.debug = log.New(nil, "", 0)
}
ctx.debug.Printf("Loaded config file \"%s\"", ctx.config_path)
if val, _ := ctx.config.Section("ui").Key("bs5").Bool(); val {
ctx.cssFile = "bs5-jf.css"
ctx.bsVersion = 5
@@ -82,6 +97,7 @@ func main() {
ctx.bsVersion = 4
}
// ctx.storage.formatter, _ = strftime.New("%Y-%m-%dT%H:%M:%S.%f")
ctx.debug.Println("Loading storage")
ctx.storage.invite_path = filepath.Join(ctx.data_path, "invites.json")
ctx.storage.loadInvites()
ctx.storage.emails_path = filepath.Join(ctx.data_path, "emails.json")
@@ -96,7 +112,7 @@ func main() {
ctx.configBase_path = filepath.Join(ctx.local_path, "config-base.json")
config_base, _ := ioutil.ReadFile(ctx.configBase_path)
json.Unmarshal(config_base, &ctx.configBase)
//bson.UnmarshalExtJSON(config_base, true, &ctx.configBase)
themes := map[string]string{
"Jellyfin (Dark)": fmt.Sprintf("bs%d-jf.css", ctx.bsVersion),
"Bootstrap (Light)": fmt.Sprintf("bs%d.css", ctx.bsVersion),
@@ -105,23 +121,32 @@ func main() {
if val, ok := themes[ctx.config.Section("ui").Key("theme").String()]; ok {
ctx.cssFile = val
}
ctx.debug.Printf("Using css file \"%s\"", ctx.cssFile)
secret, err := GenerateSecret(16)
if err != nil {
panic(err)
ctx.err.Fatal(err)
}
os.Setenv("JFA_SECRET", secret)
ctx.jellyfinLogin = true
if val, _ := ctx.config.Section("ui").Key("jellyfin_login").Bool(); !val {
ctx.jellyfinLogin = false
user := User{}
user.UserID, _ = uuid.NewUUID()
user.UserID = shortuuid.New()
user.Username = ctx.config.Section("ui").Key("username").String()
user.Password = ctx.config.Section("ui").Key("password").String()
ctx.users = append(ctx.users, user)
} else {
ctx.debug.Println("Using Jellyfin for authentication")
}
server := ctx.config.Section("jellyfin").Key("server").String()
ctx.jf.init(server, "jfa-go", "0.1", "hrfee-arch", "hrfee-arch")
ctx.jf.authenticate(ctx.config.Section("jellyfin").Key("username").String(), ctx.config.Section("jellyfin").Key("password").String())
var status int
_, status, err = ctx.jf.authenticate(ctx.config.Section("jellyfin").Key("username").String(), ctx.config.Section("jellyfin").Key("password").String())
if status != 200 || err != nil {
ctx.err.Fatalf("Failed to authenticate with Jellyfin @ %s: Code %d", server, status)
}
ctx.info.Printf("Authenticated with %s", server)
ctx.authJf.init(server, "jfa-go", "0.1", "auth", "auth")
ctx.loadStrftime()
@@ -133,7 +158,6 @@ func main() {
"numbers": ctx.config.Section("password_validation").Key("number").MustInt(0),
"special characters": ctx.config.Section("password_validation").Key("special").MustInt(0),
}
if !ctx.config.Section("password_validation").Key("enabled").MustBool(false) {
for key := range validatorConf {
validatorConf[key] = 0
@@ -143,6 +167,7 @@ func main() {
ctx.email.init(ctx)
ctx.debug.Println("Loading routes")
router := gin.Default()
router.Use(static.Serve("/", static.LocalFile("data/static", false)))
router.Use(static.Serve("/invite/", static.LocalFile("data/static", false)))
@@ -161,6 +186,7 @@ func main() {
api.POST("/modifyUsers", ctx.ModifyEmails)
api.POST("/setDefaults", ctx.SetDefaults)
api.GET("/getConfig", ctx.GetConfig)
router.Run(":8080")
api.POST("/modifyConfig", ctx.ModifyConfig)
addr := fmt.Sprintf("%s:%d", ctx.config.Section("ui").Key("host").String(), ctx.config.Section("ui").Key("port").MustInt(8056))
router.Run(addr)
}