Skip to content

Instantly share code, notes, and snippets.

View xieyuschen's full-sized avatar
☺️
Keep Loving, Keep Learning :)

Griffin xieyuschen

☺️
Keep Loving, Keep Learning :)
View GitHub Profile
@xieyuschen
xieyuschen / gist:02b9a8857caee6020a3315efa2238f28
Created May 24, 2024 09:38
Mockgen Generate Code for GRPC Interface
Due to the method of `UnimplementedUserServiceServer` interface isn't exported, we need to run
the shell here to manipulate the generated file again:
```shell
sed -i '' '/type MockUserServiceServer struct {/,/}/ {
/type MockUserServiceServer struct {/!b
a\
\tholepb.UnimplementedUserServiceServer
}' proto/mockedpb/api_grpc.pb.go
```
@xieyuschen
xieyuschen / print_processes.go
Created January 15, 2024 02:12
Print All Processes with A Tree format
package main
import (
"bytes"
"fmt"
"github.com/mitchellh/go-ps"
"io"
"os"
)
@xieyuschen
xieyuschen / discard_io.md
Created June 16, 2023 02:52
IO.Discard vs /Dev/Null file in Golang

IO.Discard Versus Writing /Dev/Null

Both of them could write down nothing, however, there is still a effieciency difference. Look at the bench:

package base

import (
	"io"
	"os"
	"testing"
@xieyuschen
xieyuschen / difference_return_lift.md
Created June 6, 2023 11:31
Haskell: difference between return/pure and lift

Haskell: difference between return/pure and lift

In the following code snippet, return/pure cannot work as it constructs StateT s IO (IO ()), not StateT s IO ().

return/pure will construct the value based on the currect monad in context, which is StateT s IO. lift will upgrade the current monad(IO) to the new monad through the transformer.

printState :: Show s => StateT s IO ()
printState = do
 state <- get
@xieyuschen
xieyuschen / why_monad_transformer.md
Created June 6, 2023 10:53
Haskell: why we need a monad transoformer?

Haskell: why we need a monad transoformer?

When learning the haskell monad transformer(would call it monadt in the left content), a problem in my mind is why monadT? Because from a simple example view, we could make a pale of monad together to do what we want we want to do.

Technially, it's correct, the monadT is just a wrapper. For example, those two cases are equal.

ioOp :: IO String
ioOp = getLine

maybeOp :: Maybe String -> Maybe String
@xieyuschen
xieyuschen / ls_print.hs
Created April 28, 2023 04:30
execute ls and print the results by Haskell
{-# LANGUAGE BlockArguments #-}
import System.Process
import Data.Text.IO (hGetLine)
import GHC.IO.Handle (isEOF, hGetLine)
import GHC.IO.Handle.Types (Handle)
import Data.Type.Equality (outer)
main :: IO ()
main = do
(_, out , _, _) <- createProcess (proc "ls" []){ std_out = CreatePipe }
@xieyuschen
xieyuschen / incrementally_update.md
Created January 23, 2021 09:49
Incrementally Update Struct Golang

Incrementally Update Struct Golang

增量删除是很重要的,比如说在update的方法里面会出现这样的情况。

type demoStruct struct {
	Replaced string
	Remain   string
}
func main() {
	oldInfo := demoStruct{Replaced: "You can see me no more", Remain: "I still here"}
	fmt.Println(oldInfo.Replaced," ",oldInfo.Remain)
@xieyuschen
xieyuschen / download_file.md
Last active March 20, 2024 15:19
Gin: download file with an simple api

Gin: download file with an simple api

func DownloadLicense(ctx *gin.Context) {
	content:="Download file here happliy"
	fileName := "hello.txt"
	ctx.Header("Content-Disposition", "attachment; filename="+fileName)
	ctx.Header("Content-Type", "application/text/plain")
	ctx.Header("Accept-Length", fmt.Sprintf("%d", len(content)))
	ctx.Writer.Write([]byte(content))
	ctx.JSON(http.StatusOK, gin.H{
@xieyuschen
xieyuschen / Nginx_cors.md
Last active December 28, 2020 13:54
How nginx deal with a CORS request

Nginx处理跨域请求

在过去处理跨域问题上,只要在nginx中加入如下内容,就可以解决跨域的问题:

add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;

那么根据上篇文章,使用一个OPTIONS的方法就可以获取到一些跨域的内容, 然后浏览器经过预处理之后,就可以完成跨域的操作。这个经过实践应该来说是没有问题的,但写这个贴子希望可以稍微深入一点了解这个请求的内容是如何被nginx加上去的。

@xieyuschen
xieyuschen / encounter_cors.md
Last active December 28, 2020 13:32
Cannot make a request to certain api for CORS

Cross Origin Resource Share

之前遇到的问题

之前的跨域问题很意外,hmm,我对这个东西还没有什么概念。情况是这样的,首先我写了几个api,然后监听8080端口。使用nginx进行代理,将对80访问的内容和对432访问的内容全部转发到127.0.0.1:8080去。然后前端在发请求的时候就遇到了跨域的问题。 解决方案:在nginx里面加一个同意跨域的设置 Access-Control-Allow-Origin:*。即可解决。

简单的提几个问题出来:

  • 跨域请求被阻塞发生在什么时候? 是nginx完成的阻塞,还是程序本身的阻塞

回答 阻塞由浏览器来完成,即正常发送请求,后端正常给予响应,但响应结果被浏览器拦截