Skip to content
Snippets Groups Projects
Select Git revision
  • c74891017a33d12f1fc5b415869ba466c40fdb86
  • master default protected
  • v1.4.5
  • v1.4.4
  • v1.4.3
  • v1.4.2
  • v1.4.1
  • v1.4.0
  • v1.3.10
  • v1.3.9
  • v1.3.8
  • v1.3.7
  • v1.3.6
  • v1.3.5
  • v1.3.4
  • v1.3.3
  • v1.3.2
  • v1.3.1
  • v1.3.0
  • v1.2.9
  • v1.2.8
  • v1.2.7
22 results

api.go

Blame
  • api.go 16.20 KiB
    package main
    
    import (
    	"github.com/gin-gonic/gin"
    	log "github.com/sirupsen/logrus"
    	"io/ioutil"
    	"net/http"
    	"random.chars.jp/git/image-board/store"
    	"strconv"
    	"strings"
    )
    
    type UserPayload struct {
    	Username   string `json:"username"`
    	ID         string `json:"id"`
    	Privileged bool   `json:"privileged"`
    }
    
    type UserCreatePayload struct {
    	Username   string `json:"username"`
    	Password   string `json:"password"`
    	Privileged bool   `json:"privileged"`
    }
    
    type UserUpdatePayload struct {
    	Username string `json:"username"`
    }
    
    type TagUpdatePayload struct {
    	Type string `json:"type"`
    }
    
    type ImageUpdatePayload struct {
    	Source string `json:"source"`
    }
    
    var unauthorized = gin.H{"error": "not authorized"}
    var denied = gin.H{"error": "permission denied"}
    
    func registerAPI() {
    	router.GET("/api", func(context *gin.Context) {
    		context.JSON(http.StatusOK, store.Info{
    			Revision:       instance.Revision,
    			Compat:         instance.Compat,
    			Register:       instance.Register,
    			InitialUser:    instance.InitialUser,
    			PermissionDir:  instance.PermissionDir,
    			PermissionFile: instance.PermissionFile,
    		})
    	})
    
    	router.GET("/api/single_user", func(context *gin.Context) {
    		context.JSON(http.StatusOK, instance.SingleUser)
    	})
    
    	router.GET("/api/user", func(context *gin.Context) {
    		context.JSON(http.StatusOK, instance.Users())
    	})
    
    	router.PUT("/api/user", func(context *gin.Context) {
    		user, ok := getUser(context)
    		if !instance.Register {
    			if !ok {
    				context.JSON(http.StatusForbidden, gin.H{
    					"error": "user registration disallowed",
    				})
    				return
    			}
    
    			if !user.Privileged {