Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
jinyaoMa committed Sep 15, 2022
1 parent 950ac92 commit 7dc0cbe
Show file tree
Hide file tree
Showing 22 changed files with 303 additions and 90 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ My App is a continuously updated personal service collection.

Prepare and install environment for development in Window 10/11?

- Git v2.37+, https://git-scm.com/
- Go v1.19+, https://go.dev/
- Node.js v16+, https://nodejs.org/
- PNPM v7+, https://pnpm.io/
- VS Code v1.71+ with GCC, https://code.visualstudio.com/docs/cpp/config-mingw
- WebView2 v104+, https://developer.microsoft.com/en-us/microsoft-edge/webview2/

> Setup VS Code by installing recommended extensions. To do this, enter in `@recommended` while searching for extensions.
> Run command `go env -w CGO_ENABLED=1` to prepare for _CGO_ enabled packages
> Run command `pnpm install` at project root directory to setup.
Expand Down
4 changes: 3 additions & 1 deletion backend/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ func init() {
log.Fatalf("failed to connect database: %+v\n", err)
}

db.AutoMigrate()
db.AutoMigrate(
&MyOption{},
)
}
25 changes: 25 additions & 0 deletions backend/model/my_option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package model

import "gorm.io/gorm"

type MyOption struct {
gorm.Model
Name string `gorm:"unique"` // Option name
Value string `` // Option value associated with name
}

func (mo *MyOption) Update(newValue string) *gorm.DB {
return db.Model(mo).Where(mo).Updates(MyOption{
Value: newValue,
})
}

type MyOptions []MyOption

func (mos MyOptions) Load() *gorm.DB {
return db.Find(&mos)
}

func (mos MyOptions) Save() *gorm.DB {
return db.Save(&mos)
}
15 changes: 13 additions & 2 deletions backend/web/air.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
package web

import "flag"
import (
// "context"
"flag"
// "time"
)

// for API service test purpose, testing with air
// For API service test purpose, testing with air
// Uncomment code below to run http redirector
func (w *web) Air() {
var flagAir int
flag.IntVar(&flagAir, "air", 0, "set `-air 1` to enable web.Air function")
flag.Parse()
if flagAir == 1 {
w.reset()
// go w.http.ListenAndServe()
w.https.ListenAndServeTLS("", "")

// ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
// defer cancel()
// w.http.Shutdown(ctx)
// <-ctx.Done()
}
}
20 changes: 20 additions & 0 deletions backend/web/api/test/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package test

import (
"net/http"

"github.com/gin-gonic/gin"
)

// @Summary Pass
// @Description Test pass path
// @Tags Test
// @Accept json
// @Produce json
// @Success 200 {string} string "Pass Path"
// @Router /test [get]
func Test() gin.HandlerFunc {
return func(ctx *gin.Context) {
ctx.String(http.StatusOK, "Pass "+ctx.Request.URL.Path)
}
}
21 changes: 21 additions & 0 deletions backend/web/auth/login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package auth

import (
"net/http"

"github.com/gin-gonic/gin"
)

// @Summary Login
// @Description Login and get access token
// @Tags Auth
// @Accept x-www-form-urlencoded
// @Produce json
// @Param username formData string true "Username"
// @Param password formData string true "Password"
// @Router /auth/login [post]
func Login() gin.HandlerFunc {
return func(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{})
}
}
38 changes: 17 additions & 21 deletions backend/web/certs/localhost.crt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 26 additions & 26 deletions backend/web/certs/localhost.key

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions backend/web/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package web

const (
CfgPortHttp = Package + ".PortHttp"
CfgPortHttps = Package + ".PortHttps"
CfgDirCerts = Package + ".DirCerts"
)

type Config struct {
PortHttp string
PortHttps string
DirCerts string
}

func DefaultConfig() Config {
return Config{
PortHttp: ":10080",
PortHttps: ":10443",
DirCerts: "",
}
}
32 changes: 29 additions & 3 deletions backend/web/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const docTemplate = `{
"title": "{{.Title}}",
"contact": {
"name": "Github Issues",
"url": "https://github.com/jinyaoMa/my-app/issues"
"url": "https://github.com/jinyaoMa/my-app/issues",
"email": "[email protected]"
},
"license": {
"name": "MIT",
Expand All @@ -22,9 +23,34 @@ const docTemplate = `{
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {},
"paths": {
"/test": {
"get": {
"description": "Test pass path",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Test"
],
"summary": "Pass",
"responses": {
"200": {
"description": "Pass Path",
"schema": {
"type": "string"
}
}
}
}
}
},
"securityDefinitions": {
"BearerToken": {
"description": "Authorization Header should contain value started with \"Bearer \" and followed by a JSON Web Token.",
"type": "apiKey",
"name": "Authorization",
"in": "header"
Expand All @@ -37,7 +63,7 @@ var SwaggerInfo = &swag.Spec{
Version: "1.0.0",
Host: "",
BasePath: "/api",
Schemes: []string{},
Schemes: []string{"https"},
Title: "My App (backend/web/router.go)",
Description: "\"My App is a continuously updated personal service collection.\"",
InfoInstanceName: "swagger",
Expand Down
33 changes: 31 additions & 2 deletions backend/web/docs/swagger.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
{
"schemes": [
"https"
],
"swagger": "2.0",
"info": {
"description": "\"My App is a continuously updated personal service collection.\"",
"title": "My App (backend/web/router.go)",
"contact": {
"name": "Github Issues",
"url": "https://github.com/jinyaoMa/my-app/issues"
"url": "https://github.com/jinyaoMa/my-app/issues",
"email": "[email protected]"
},
"license": {
"name": "MIT",
Expand All @@ -14,9 +18,34 @@
"version": "1.0.0"
},
"basePath": "/api",
"paths": {},
"paths": {
"/test": {
"get": {
"description": "Test pass path",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Test"
],
"summary": "Pass",
"responses": {
"200": {
"description": "Pass Path",
"schema": {
"type": "string"
}
}
}
}
}
},
"securityDefinitions": {
"BearerToken": {
"description": "Authorization Header should contain value started with \"Bearer \" and followed by a JSON Web Token.",
"type": "apiKey",
"name": "Authorization",
"in": "header"
Expand Down
22 changes: 21 additions & 1 deletion backend/web/docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
basePath: /api
info:
contact:
email: [email protected]
name: Github Issues
url: https://github.com/jinyaoMa/my-app/issues
description: '"My App is a continuously updated personal service collection."'
Expand All @@ -9,9 +10,28 @@ info:
url: https://github.com/jinyaoMa/my-app/blob/main/LICENSE
title: My App (backend/web/router.go)
version: 1.0.0
paths: {}
paths:
/test:
get:
consumes:
- application/json
description: Test pass path
produces:
- application/json
responses:
"200":
description: Pass Path
schema:
type: string
summary: Pass
tags:
- Test
schemes:
- https
securityDefinitions:
BearerToken:
description: Authorization Header should contain value started with "Bearer "
and followed by a JSON Web Token.
in: header
name: Authorization
type: apiKey
Expand Down
9 changes: 9 additions & 0 deletions backend/web/middleware/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package middleware

import "github.com/gin-gonic/gin"

func Auth() gin.HandlerFunc {
return func(ctx *gin.Context) {
ctx.Next()
}
}
Loading

0 comments on commit 7dc0cbe

Please sign in to comment.