-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
101 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,114 @@ | ||
package main | ||
|
||
//import "net/http" | ||
//import "io" | ||
//import "log" | ||
|
||
//import "github.com/sunshineplan/imgconv" | ||
|
||
import ( | ||
"fmt" | ||
_ "io" | ||
"log" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
"github.com/gin-contrib/sessions" | ||
"github.com/gin-contrib/sessions/cookie" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
const userKey = "user" | ||
|
||
var secret = []byte(os.Getenv("SECRET")) | ||
|
||
func main() { | ||
router := gin.Default() | ||
r := engine() | ||
r.Use(gin.Logger()) | ||
if err := engine().Run(":8080"); err != nil { | ||
log.Fatal("unable to start:", err) | ||
} | ||
} | ||
|
||
func engine() *gin.Engine { | ||
r := gin.New() | ||
|
||
r.Use(sessions.Sessions("session", cookie.NewStore(secret))) | ||
|
||
r.GET("/img", getServeImage) | ||
r.POST("/login", login) | ||
r.GET("/logout", logout) | ||
|
||
admin := r.Group("/admin") | ||
admin.Use(AuthRequired) | ||
{ | ||
admin.POST("/upload", postImage) | ||
} | ||
|
||
return r | ||
} | ||
|
||
func getServeImage(c *gin.Context) { | ||
|
||
} | ||
|
||
func postImage(c *gin.Context) { | ||
// Single file | ||
file, _ := c.FormFile("file") | ||
log.Println(file.Filename) | ||
|
||
// Upload the file to specific dst. | ||
c.SaveUploadedFile(file, "./public") | ||
|
||
c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename)) | ||
} | ||
|
||
func AuthRequired(c *gin.Context) { | ||
session := sessions.Default(c) | ||
user := session.Get(userKey) | ||
if user == nil { | ||
// Abort the request with the appropriate error code | ||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"}) | ||
return | ||
} | ||
// Continue down the chain to handler etc | ||
c.Next() | ||
} | ||
|
||
func login(c *gin.Context) { | ||
session := sessions.Default(c) | ||
username := c.PostForm("username") | ||
password := c.PostForm("password") | ||
|
||
// Validate form input | ||
if strings.Trim(username, " ") == "" || strings.Trim(password, " ") == "" { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": "Parameters can't be empty"}) | ||
return | ||
} | ||
|
||
// Check for username and password match, usually from a database | ||
if username != "hello" || password != "itsme" { | ||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authentication failed"}) | ||
return | ||
} | ||
|
||
router.Run("localhost:8000") | ||
// Save the username in the session | ||
session.Set(userKey, username) // In real world usage you'd set this to the users ID | ||
if err := session.Save(); err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save session"}) | ||
return | ||
} | ||
c.JSON(http.StatusOK, gin.H{"message": "Successfully authenticated user"}) | ||
} | ||
|
||
func convertImage(c *gin.Context) { | ||
url := c.Param("url") | ||
func logout(c *gin.Context) { | ||
session := sessions.Default(c) | ||
user := session.Get(userKey) | ||
if user == nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid session token"}) | ||
return | ||
} | ||
session.Delete(userKey) | ||
if err := session.Save(); err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save session"}) | ||
return | ||
} | ||
c.JSON(http.StatusOK, gin.H{"message": "Successfully logged out"}) | ||
} |