Finally Framework
This commit is contained in:
9
internal/api/albums.go
Normal file
9
internal/api/albums.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (api *API) albumsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
}
|
||||
24
internal/api/api.go
Normal file
24
internal/api/api.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"reichard.io/imagini/internal/db"
|
||||
"reichard.io/imagini/internal/auth"
|
||||
)
|
||||
|
||||
type API struct {
|
||||
Router *http.ServeMux
|
||||
Auth *auth.AuthManager
|
||||
DB *db.DBManager
|
||||
}
|
||||
|
||||
func NewApi(db *db.DBManager, auth *auth.AuthManager) *API {
|
||||
api := &API{
|
||||
Router: http.NewServeMux(),
|
||||
DB: db,
|
||||
Auth: auth,
|
||||
}
|
||||
api.registerRoutes()
|
||||
return api
|
||||
}
|
||||
75
internal/api/auth.go
Normal file
75
internal/api/auth.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"time"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"reichard.io/imagini/internal/models"
|
||||
// "github.com/lestrrat-go/jwx/jwt"
|
||||
// "github.com/lestrrat-go/jwx/jwa"
|
||||
// log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// https://www.calhoun.io/pitfalls-of-context-values-and-how-to-avoid-or-mitigate-them/
|
||||
// https://pace.dev/blog/2018/05/09/how-I-write-http-services-after-eight-years.html
|
||||
// https://medium.com/@benbjohnson/standard-package-layout-7cdbc8391fc1#333c
|
||||
// https://www.alexedwards.net/blog/organising-database-access <---- best
|
||||
// - TLDR: Do what you're doing, but use closeures for the handlers
|
||||
func (api *API) loginHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
errorJSON(w, "Method is not supported.", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Decode into Struct
|
||||
var creds models.APICredentials
|
||||
err := json.NewDecoder(r.Body).Decode(&creds)
|
||||
if err != nil {
|
||||
errorJSON(w, "Invalid parameters.", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Validate
|
||||
if creds.User == "" || creds.Password == "" {
|
||||
errorJSON(w, "Invalid parameters.", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: Is user already logged in? If so refresh token, if different user, kill session and log in new user?
|
||||
|
||||
// Do login
|
||||
resp := api.Auth.AuthenticateUser(creds)
|
||||
if resp == true {
|
||||
// Return Success
|
||||
cookie := http.Cookie{
|
||||
Name: "Token",
|
||||
Value: "testToken",
|
||||
}
|
||||
http.SetCookie(w, &cookie)
|
||||
successJSON(w, "Login success.", http.StatusOK)
|
||||
}else {
|
||||
// Return Failure
|
||||
errorJSON(w, "Invalid credentials.", http.StatusUnauthorized)
|
||||
}
|
||||
}
|
||||
|
||||
func (api *API) logoutHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method is not supported.", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Do logout
|
||||
|
||||
// TODO: Clear Session Server Side
|
||||
|
||||
// Tell Client to Expire Token
|
||||
cookie := &http.Cookie{
|
||||
Name: "Token",
|
||||
Value: "",
|
||||
Path: "/",
|
||||
Expires: time.Unix(0, 0),
|
||||
HttpOnly: true,
|
||||
}
|
||||
http.SetCookie(w, cookie)
|
||||
}
|
||||
9
internal/api/info.go
Normal file
9
internal/api/info.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (api *API) infoHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
}
|
||||
9
internal/api/media_items.go
Normal file
9
internal/api/media_items.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (api *API) mediaItemsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
}
|
||||
40
internal/api/middlewares.go
Normal file
40
internal/api/middlewares.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Middleware func(http.Handler) http.Handler
|
||||
|
||||
func MultipleMiddleware(h http.Handler, m ...Middleware) http.Handler {
|
||||
if len(m) < 1 {
|
||||
return h
|
||||
}
|
||||
wrapped := h
|
||||
for i := len(m) - 1; i >= 0; i-- {
|
||||
wrapped = m[i](wrapped)
|
||||
}
|
||||
return wrapped
|
||||
}
|
||||
|
||||
// func authMiddleware(h http.Handler) http.Handler {
|
||||
// return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// _, ok := ValidateUserToken(r)
|
||||
//
|
||||
// if ok {
|
||||
// next.ServeHTTP(w, r)
|
||||
// } else {
|
||||
// w.WriteHeader(http.StatusUnauthorized)
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
|
||||
func logMiddleware(h http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
log.SetOutput(os.Stdout)
|
||||
log.Println(r.Method, r.URL)
|
||||
h.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
33
internal/api/routes.go
Normal file
33
internal/api/routes.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (api *API) registerRoutes() {
|
||||
api.Router.HandleFunc("/MediaItems", api.mediaItemsHandler)
|
||||
api.Router.HandleFunc("/Upload", api.uploadHandler)
|
||||
api.Router.HandleFunc("/Albums", api.albumsHandler)
|
||||
api.Router.HandleFunc("/Logout", api.logoutHandler)
|
||||
api.Router.HandleFunc("/Login", api.loginHandler)
|
||||
api.Router.HandleFunc("/Users", api.usersHandler)
|
||||
api.Router.HandleFunc("/Tags", api.tagsHandler)
|
||||
api.Router.HandleFunc("/Info", api.infoHandler)
|
||||
api.Router.HandleFunc("/Me", api.meHandler)
|
||||
}
|
||||
|
||||
// https://stackoverflow.com/a/59764037
|
||||
func errorJSON(w http.ResponseWriter, err string, code int) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.WriteHeader(code)
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{"error": err})
|
||||
}
|
||||
|
||||
func successJSON(w http.ResponseWriter, msg string, code int) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.WriteHeader(code)
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{"success": msg})
|
||||
}
|
||||
9
internal/api/tags.go
Normal file
9
internal/api/tags.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (api *API) tagsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
}
|
||||
9
internal/api/upload.go
Normal file
9
internal/api/upload.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (api *API) uploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
}
|
||||
39
internal/api/users.go
Normal file
39
internal/api/users.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (api *API) usersHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodPost {
|
||||
// CREATE
|
||||
} else if r.Method == http.MethodPut {
|
||||
// UPDATE / REPLACE
|
||||
} else if r.Method == http.MethodPatch {
|
||||
// UPDATE / MODIFY
|
||||
} else if r.Method == http.MethodDelete {
|
||||
// DELETE
|
||||
} else if r.Method == http.MethodGet {
|
||||
// GET
|
||||
} else {
|
||||
errorJSON(w, "Method is not supported.", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (api *API) meHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
errorJSON(w, "Method is not supported.", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Get Authenticated User & Return Object
|
||||
authCookie, err := r.Cookie("Token")
|
||||
if err != nil {
|
||||
log.Error("[routes] ", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Info("[routes] INFO: ", authCookie)
|
||||
}
|
||||
@@ -3,16 +3,26 @@ package auth
|
||||
import (
|
||||
"errors"
|
||||
"gorm.io/gorm"
|
||||
"reichard.io/imagini/internal/query"
|
||||
"reichard.io/imagini/internal/db"
|
||||
"reichard.io/imagini/internal/models"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func AuthenticateUser(db *gorm.DB, creds models.APICredentials) bool {
|
||||
type AuthManager struct {
|
||||
DB *db.DBManager
|
||||
}
|
||||
|
||||
func NewMgr(db *db.DBManager) *AuthManager {
|
||||
return &AuthManager{
|
||||
DB: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (auth *AuthManager) AuthenticateUser(creds models.APICredentials) bool {
|
||||
// By Username
|
||||
foundUser, err := query.User(db, models.User{Username: creds.User})
|
||||
foundUser, err := auth.DB.User(models.User{Username: creds.User})
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
foundUser, err = query.User(db, models.User{Email: creds.User})
|
||||
foundUser, err = auth.DB.User(models.User{Email: creds.User})
|
||||
}
|
||||
|
||||
// Error Checking
|
||||
|
||||
@@ -14,7 +14,7 @@ type Config struct {
|
||||
ListenPort string
|
||||
}
|
||||
|
||||
func NewConfig() *Config {
|
||||
func Load() *Config {
|
||||
return &Config{
|
||||
DBType: getEnv("DATABASE_TYPE", "SQLite"),
|
||||
DBName: getEnv("DATABASE_NAME", "imagini"),
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
package context
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"reichard.io/imagini/internal/query"
|
||||
"reichard.io/imagini/internal/config"
|
||||
)
|
||||
|
||||
type ImaginiContext struct {
|
||||
DB *gorm.DB
|
||||
Config *config.Config
|
||||
}
|
||||
|
||||
func NewImaginiContext() *ImaginiContext {
|
||||
c := config.NewConfig()
|
||||
db := query.NewDB(c)
|
||||
return &ImaginiContext{
|
||||
DB: db,
|
||||
Config: c,
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package query
|
||||
package db
|
||||
|
||||
import (
|
||||
"path"
|
||||
@@ -12,40 +12,46 @@ import (
|
||||
"reichard.io/imagini/internal/models"
|
||||
)
|
||||
|
||||
func NewDB(c *config.Config) *gorm.DB {
|
||||
type DBManager struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewMgr(c *config.Config) *DBManager {
|
||||
gormConfig := &gorm.Config{
|
||||
PrepareStmt: true,
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
}
|
||||
var db *gorm.DB
|
||||
|
||||
// Create manager
|
||||
dbm := &DBManager{}
|
||||
|
||||
if c.DBType == "SQLite" {
|
||||
dbLocation := path.Join(c.ConfigPath, "imagini.db")
|
||||
db, _ = gorm.Open(sqlite.Open(dbLocation), gormConfig)
|
||||
dbm.db, _ = gorm.Open(sqlite.Open(dbLocation), gormConfig)
|
||||
} else {
|
||||
log.Fatal("Unsupported Database")
|
||||
}
|
||||
|
||||
// Initialize database
|
||||
db.AutoMigrate(&models.ServerSetting{})
|
||||
db.AutoMigrate(&models.User{})
|
||||
db.AutoMigrate(&models.MediaItem{})
|
||||
db.AutoMigrate(&models.Tag{})
|
||||
db.AutoMigrate(&models.Album{})
|
||||
dbm.db.AutoMigrate(&models.ServerSetting{})
|
||||
dbm.db.AutoMigrate(&models.User{})
|
||||
dbm.db.AutoMigrate(&models.MediaItem{})
|
||||
dbm.db.AutoMigrate(&models.Tag{})
|
||||
dbm.db.AutoMigrate(&models.Album{})
|
||||
|
||||
// Determine whether to bootstrap
|
||||
var count int64
|
||||
db.Model(&models.User{}).Count(&count)
|
||||
dbm.db.Model(&models.User{}).Count(&count)
|
||||
if count == 0 {
|
||||
bootstrapDatabase(db)
|
||||
dbm.bootstrapDatabase()
|
||||
}
|
||||
|
||||
return db
|
||||
return dbm
|
||||
}
|
||||
|
||||
func bootstrapDatabase(db *gorm.DB) {
|
||||
func (dbm *DBManager) bootstrapDatabase() {
|
||||
log.Info("[query] Bootstrapping database.")
|
||||
err := CreateUser(db, models.User{
|
||||
err := dbm.CreateUser(models.User{
|
||||
Username: "admin",
|
||||
Password: "admin",
|
||||
AuthType: "Local",
|
||||
@@ -56,7 +62,7 @@ func bootstrapDatabase(db *gorm.DB) {
|
||||
}
|
||||
}
|
||||
|
||||
func ItemsFromAlbum(db *gorm.DB, user models.User, album models.Album) []models.MediaItem {
|
||||
func (dbm *DBManager) ItemsFromAlbum(user models.User, album models.Album) []models.MediaItem {
|
||||
var mediaItems []models.MediaItem
|
||||
// db.Table("media_albums").
|
||||
// Select("media_item.*").
|
||||
@@ -64,7 +70,7 @@ func ItemsFromAlbum(db *gorm.DB, user models.User, album models.Album) []models.
|
||||
// Where("media_albums.album_id = ? AND media_items.User = ?", albumID, userID).
|
||||
|
||||
|
||||
db.
|
||||
dbm.db.
|
||||
//Where("album = ? AND user = ?", albumID, userID).
|
||||
Find(&mediaItems)
|
||||
return mediaItems
|
||||
@@ -1,4 +1,4 @@
|
||||
package query
|
||||
package db
|
||||
|
||||
import "errors"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package query
|
||||
package db
|
||||
|
||||
import (
|
||||
"errors"
|
||||
@@ -9,9 +9,9 @@ import (
|
||||
"reichard.io/imagini/internal/models"
|
||||
)
|
||||
|
||||
func CreateUser(db *gorm.DB, user models.User) error {
|
||||
func (dbm *DBManager) CreateUser(user models.User) error {
|
||||
log.Info("[query] Creating user: ", user.Username)
|
||||
_, err := User(db, user)
|
||||
_, err := dbm.User(user)
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
log.Warn("[query] User already exists: ", user.Username)
|
||||
return errors.New("User already exists")
|
||||
@@ -23,20 +23,20 @@ func CreateUser(db *gorm.DB, user models.User) error {
|
||||
return err
|
||||
}
|
||||
user.Password = string(hashedPassword)
|
||||
return db.Create(&user).Error
|
||||
return dbm.db.Create(&user).Error
|
||||
}
|
||||
|
||||
func User (db *gorm.DB, user models.User) (models.User, error) {
|
||||
func (dbm *DBManager) User (user models.User) (models.User, error) {
|
||||
var foundUser models.User
|
||||
var count int64
|
||||
err := db.Where(&user).First(&foundUser).Count(&count).Error
|
||||
err := dbm.db.Where(&user).First(&foundUser).Count(&count).Error
|
||||
return foundUser, err
|
||||
}
|
||||
|
||||
func DeleteUser (user models.User) error {
|
||||
func (dbm *DBManager) DeleteUser (user models.User) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdatePassword (user models.User, pw string) {
|
||||
func (dbm *DBManager) UpdatePassword (user models.User, pw string) {
|
||||
|
||||
}
|
||||
@@ -5,41 +5,51 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Might not even need this
|
||||
type ServerSetting struct {
|
||||
gorm.Model
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Value string `json:"value"`
|
||||
Name string `json:"name" gorm:"not null"`
|
||||
Description string `json:"description" gorm:"not null"`
|
||||
Value string `json:"value" gorm:"not null"`
|
||||
}
|
||||
|
||||
type Device struct {
|
||||
gorm.Model
|
||||
User User `json:"user" gorm:"ForeignKey:ID"`
|
||||
DeviceName string `json:"name"`
|
||||
Type string `json:"type"` // Android, iOS, Chrome, FireFox, Edge, etc
|
||||
}
|
||||
|
||||
// TODO: ID -> UUID?
|
||||
type User struct {
|
||||
gorm.Model
|
||||
Email string `json:"email" gorm:"unique;not null"`
|
||||
Username string `json:"username" gorm:"unique;not null"`
|
||||
Email string `json:"email" gorm:"unique"`
|
||||
Username string `json:"username" gorm:"unique"`
|
||||
FirstName string `json:"first_name"`
|
||||
LastName string `json:"last_name"`
|
||||
AuthType string `json:"auth_type"`
|
||||
Password string `json:"password"`
|
||||
AuthType string `json:"auth_type" gorm:"default:Local;not null"`
|
||||
Password string `json:"-"`
|
||||
JWTSecret string `json:"-" gorm:"unique;not null"` // TODO: Auto Generate UUID
|
||||
}
|
||||
|
||||
type MediaItem struct {
|
||||
gorm.Model
|
||||
User User `json:"user" gorm:"ForeignKey:ID"`
|
||||
User User `json:"user" gorm:"ForeignKey:ID;not null"`
|
||||
EXIFDate time.Time `json:"exif_date"`
|
||||
Latitude string `json:"latitude"`
|
||||
Longitude string `json:"longitude"`
|
||||
MediaType uint `json:"media_type"`
|
||||
RelPath string `json:"rel_path"`
|
||||
MediaType string `json:"media_type" gorm:"default:Photo;not null"` // Photo, Video
|
||||
RelPath string `json:"rel_path" gorm:"not null"`
|
||||
Tags []Tag `json:"tags" gorm:"many2many:media_tags;"`
|
||||
Albums []Album `json:"albums" gorm:"many2many:media_albums;"`
|
||||
}
|
||||
|
||||
type Tag struct {
|
||||
gorm.Model
|
||||
Name string `json:"name"`
|
||||
Name string `json:"name" gorm:"not null"`
|
||||
}
|
||||
|
||||
type Album struct {
|
||||
gorm.Model
|
||||
Name string `json:"name"`
|
||||
Name string `json:"name" gorm:"not null"`
|
||||
}
|
||||
|
||||
25
internal/session/session.go
Normal file
25
internal/session/session.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type SessionManager struct {
|
||||
Mutex sync.Mutex
|
||||
}
|
||||
|
||||
func NewMgr() *SessionManager {
|
||||
return &SessionManager{}
|
||||
}
|
||||
|
||||
func (sm *SessionManager) Set(key, value string) {
|
||||
|
||||
}
|
||||
|
||||
func (sm *SessionManager) Get(key string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (sm *SessionManager) Delete(key string) {
|
||||
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package sessions
|
||||
|
||||
// import (
|
||||
// "github.com/dgrijalva/jwt-go"
|
||||
// )
|
||||
|
||||
type Manager struct {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user