1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GOでアプリ作りてぇな[不定期更新]

Last updated at Posted at 2024-09-26

という事でアプリつくります。

以下備忘録

wails使う。Reactと分けてAPI設計。

リアルタイムコミ二ケーションアプリを作りたい

とりあえずAPIのインターフェイス雑に設計。
websocketを導入してみた。初めて使用。
ここからセキュリティやらパスやらクエリストリングを作成していく。
ほとんど全てGETメソッドになっているが後で改修予定。
go func やchanel等も適宜使用予定。

package main

import (
	"log"
	"time"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"github.com/gorilla/csrf"
	"github.com/gorilla/websocket"
)

var upgrader = websocket.Upgrader{
	ReadBufferSize:  1024,
	WriteBufferSize: 1024,
}

var client = make(map[*websocket.Conn]bool)

func contrl() {
	api := gin.Default()

	csrf.Protect([]byte(""))

	api.Use(cors.New(cors.Config{
		AllowOrigins:     []string{"https://foo.com"},
		AllowMethods:     []string{"PUT", "PATCH"},
		AllowHeaders:     []string{"Origin"},
		ExposeHeaders:    []string{"Content-Length"},
		AllowCredentials: true,
		AllowOriginFunc: func(origin string) bool {
			return origin == "https://github.com"
		},
		MaxAge: 12 * time.Hour,
	}))

	api.Use(func(c *gin.Context) {
		c.GetHeader("User-Agent")
		c.Next()
	})

	api.GET("/message-v1", func(c *gin.Context) {
		//余分な型が残る
		conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
		if err != nil {
			log.Print("")
			return
		}
		client[conn] = true
	})

	api.GET("/video-v1", func(c *gin.Context) {
		conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
		if err != nil {
			log.Print("")
			return
		}
		client[conn] = true
	})
	api.GET("/image-v1", func(c *gin.Context) {
		conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
		if err != nil {
			log.Print("")
			return
		}
		client[conn] = true
	})
	api.GET("/voice-v1", func(c *gin.Context) {
		conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
		if err != nil {
			log.Print("")
			return
		}
		client[conn] = true
	})
	api.GET("/batch-v1", func(c *gin.Context) {
		conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
		if err != nil {
			log.Print("")
			return
		}
		client[conn] = true
	})
	api.GET("/log-v1", func(c *gin.Context) {
		conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
		if err != nil {
			log.Print("")
			return
		}
		client[conn] = true
	})
	api.GET("/group-v1", func(c *gin.Context) {
		conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
		if err != nil {
			log.Print("")
			return
		}
		client[conn] = true
	})
	api.GET("/block-v1", func(c *gin.Context) {
		conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
		if err != nil {
			log.Print("")
			return
		}
		client[conn] = true
	})
	api.GET("/moral-v1", func(c *gin.Context) {
		conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
		if err != nil {
			log.Print("")
			return
		}
		client[conn] = true
	})
	api.GET("/search-v1", func(c *gin.Context) {
		conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
		if err != nil {
			log.Print("")
			return
		}
		client[conn] = true
	})
	api.GET("/bot-v1", func(c *gin.Context) {
		conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
		if err != nil {
			log.Print("")
			return
		}
		client[conn] = true
	})
	api.GET("/help-v1", func(c *gin.Context) {
		conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
		if err != nil {
			log.Print("")
			return
		}
		client[conn] = true
	})
	api.GET("/cokkiesession-v1", func(c *gin.Context) {
		conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
		if err != nil {
			log.Print("")
			return
		}
		client[conn] = true
	})
	api.DELETE("/delete-v1", func(c *gin.Context) {

	})
	api.Run(":7000")
}

大雑把にDBのORM実装。
結構使いやすいかも。
dsn部分はどのデータベース使うか決めてないので、適当。
ファイルも切り分けてselect ,insert,update,deleteに分ける。
javaだとddl-autoしてるとテーブル情報更新時にエラー出るので、ライブラリは要確認。

package main

import (
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
)

type user struct {
	gorm.Model
	id         int
	name       string
	age        int
	blockpoint int16
}

type group struct {
	gorm.Model
	g_name   string
	g_number int
}

type video struct {
	gorm.Model
	vd_name   string
	vd_length int
	vd_time   int
}

type image struct {
	gorm.Model
	i_name  string
	i_hsize int
	i_wsize int
}

type voice struct {
	gorm.Model
	v_naem      string
	v_vibration int
	v_frequency int
	v_time      int
}

type logs struct {
	gorm.Model
}

type help struct {
	gorm.Model
	category string
	Fqestion string
	explain  string
}

type moral struct {
	gorm.Model
	criterion string
	level     int
}

type bot struct {
	gorm.Model
}

func orm(in string, count int) {

	//db setup
	dsn := "host=localhost "
	db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("faild db access")
	}

	//table create
	db.AutoMigrate(&user{})
	db.AutoMigrate(&video{})
	db.AutoMigrate(&image{})
	db.AutoMigrate(&voice{})
	db.AutoMigrate(&logs{})
	db.AutoMigrate(&group{})
	db.AutoMigrate(&help{})
	db.AutoMigrate(&moral{})
	db.AutoMigrate(&bot{})

	//query
	for i := 0; i < 100; i++ {
		db.Create(user{id: i, name: "アホ", age: i * 10, blockpoint: int16(i * 100)})
	}

}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?